DEV Community

Cover image for Azure AOAI integration with Sitecore
karleeov
karleeov

Posted on

Azure AOAI integration with Sitecore

How to Build a Real-Time Content Generator with Azure OpenAI and XM Cloud API
Have you ever wondered how to create engaging and relevant content for your website or blog without spending hours writing and editing? If so, you might be interested in learning how to use Azure OpenAI and XM Cloud API to build a real-time content generator that can produce high-quality text from a simple prompt.

Azure OpenAI is a service that provides access to powerful language models, such as GPT-3, GPT-35-Turbo, and Embeddings, that can understand and generate natural language. XM Cloud API is a platform that enables you to manage and deliver content across multiple channels, such as web, mobile, and social media.

In this blog post, we will show you how to build an application that can integrate Azure OpenAI and XM Cloud API, so you can generate and update content on your website or blog in real time, based on the keywords or topics you provide. This way, you can keep your audience engaged and informed, without having to write everything from scratch.

Step 1: Create an Azure OpenAI Resource
The first step is to create an Azure OpenAI resource, which will allow you to access the OpenAI APIs and models. To do this, you need to sign in to the Azure portal and create a new resource group. Then, you need to search for Azure OpenAI and create a new instance. You can choose the pricing tier and region that suit your needs. Once the resource is created, you will get an endpoint and a key, which you will need later to authenticate your requests.

Step 2: Create an XM Cloud API Account
The next step is to create an XM Cloud API account, which will allow you to manage and deliver your content. To do this, you need to visit the XM Cloud API website and sign up for a free trial. You will get an email with your credentials and a link to access the XM Cloud API dashboard. There, you can create a new project and a new content type, which will define the structure and fields of your content. For example, you can create a content type called “Blog Post”, with fields such as “Title”, “Body”, “Image”, and “Tags”.

Step 3: Create an Application to Integrate Azure OpenAI and XM Cloud API
The final step is to create an application that can integrate Azure OpenAI and XM Cloud API, so you can generate and update content on your website or blog in real time. To do this, you need to use a programming language and framework of your choice, such as Python, Node.js, or .NET. You also need to install the Azure OpenAI SDK and the XM Cloud API SDK, which will make it easier to interact with the APIs.

The basic logic of the application is as follows:

The application takes a keyword or a topic as an input from the user.
The application uses the Azure OpenAI SDK to call the completions endpoint, which will generate a text based on the input, using the GPT-3 model. You can specify the parameters, such as the engine, the temperature, the max tokens, and the stop sequence, to control the output.
The application parses the output and extracts the title, the body, the image, and the tags of the generated text.
The application uses the XM Cloud API SDK to create a new content item of the type “Blog Post”, and fills the fields with the extracted data. You can also specify the parameters, such as the project, the environment, and the language, to control the delivery.
The application returns the content item ID and the URL of the generated content, which can be displayed on the website or blog.
Here is an example of the code in Python, using the Flask framework:

Python

# Import the libraries
from flask import Flask, request, jsonify
from azure.openai import OpenAIClient
from xmcloudapi import XMCloudAPIClient

# Initialize the app
app = Flask(__name__)

# Initialize the Azure OpenAI client
openai_client = OpenAIClient(
    endpoint="<your-azure-openai-endpoint>",
    key="<your-azure-openai-key>"
)

# Initialize the XM Cloud API client
xm_client = XMCloudAPIClient(
    username="<your-xm-cloud-api-username>",
    password="<your-xm-cloud-api-password>",
    project="<your-xm-cloud-api-project>",
    environment="<your-xm-cloud-api-environment>",
    language="<your-xm-cloud-api-language>"
)

# Define the route for the content generation
@app.route("/generate", methods=["POST"])
def generate():
    # Get the input from the user
    input = request.form.get("input")

    # Call the Azure OpenAI completions endpoint
    response = openai_client.completions(
        engine="gpt-3",
        prompt=input,
        temperature=0.7,
        max_tokens=500,
        stop="###"
    )

    # Parse the output and extract the data
    output = response["choices"][0]["text"]
    title = output.split("\n")[0]
    body = output.split("\n")[1]
    image = output.split("\n")[2]
    tags = output.split("\n")[3]

    # Create a new content item of the type "Blog Post"
    content = xm_client.create_content_item(
        content_type="Blog Post",
        fields={
            "Title": title,
            "Body": body,
            "Image": image,
            "Tags": tags
        }
    )

    # Return the content item ID and the URL
    return jsonify({
        "id": content["id"],
        "url": content["url"]
    })


# Run the app
if __name__ == "__main__":
    app.run()
Enter fullscreen mode Exit fullscreen mode

AI-generated code. Review and use carefully. More info on FAQ.
Conclusion
In this blog post, we have shown you how to build a real-time content generator with Azure OpenAI and XM Cloud API, so you can create and update content on your website or blog in real time, based on the keywords or topics you provide. This way, you can save time and effort, while providing your audience with engaging and relevant content.
return jsonify({
"id": content["id"],
"url": content["url"]
})

Run the app

if name == "main":
app.run()




AI-generated code. Review and use carefully. More info on FAQ.

Conclusion
In this blog post, we have shown you how to build a real-time content generator with Azure OpenAI and XM Cloud API, so you can create and update content on your website or blog in real time, based on the keywords or topics you provide. This way, you can save time and effort, while providing your audience with engaging and relevant content.


here is the video demo link
https://youtu.be/unhdDLKkxWw


Enter fullscreen mode Exit fullscreen mode

Top comments (0)