Skip to content

Adding AI to CowPress

The previous team’s efforts on the AI project laid the groundwork for an exciting feature in our blogging platform, CowPress – the integration of AI-driven content generation. While they added some user interface elements for generating a blog post, found in CreatePost.cshtml and EditPost.cshtml under the label “TODO AI UI”, the actual content generation functionality was not implemented. Our goal now is to bring this feature to life using a Large Language Model (LLM).

Large-Language Models

LLMs, like OpenAI’s GPT-4, are advanced AI models capable of understanding and generating human-like text. They can compose blog posts, answer questions, write code, and much more, making them an ideal tool for enhancing content creation in CowPress.

Implementation

To start, you’ll need to uncomment the existing code in CreatePost.cshtml and EditPost.cshtml. This will activate the AI content generation UI. Then, explore the existing setup by attempting to create a new post.

Creating the Resources in Azure

Since CacheCows already utilises Azure, we’ll harness Azure OpenAI to integrate the GPT-4 model, a good choice to balance of cost and performance. The older model GPT-3.5 is cheaper and faster, but less capable and won’t be as good at creating content, which is important for our use-case.

To utilise GPT-4, you must first set up the necessary Azure resources. The following steps will guide you through this process:

  1. Visit the Microsoft Azure Documentation and follow the instructions to create your Azure OpenAI resource.
  2. During setup, select “Allow all networks” for simplicity (for a production deploy, one can limit the resources to just your servers/functions/containers).
  3. Make a note of the model name, URL, and key, as these will be crucial for integrating the model into CowPress.
  4. You will also need an embedding model later on, so go ahead and create one of the type text-embedding-ada-002 and note the name you give it (e.g. “text-embedding-ada-002”).

Client Library

Azure OpenAI Client Library Install the Azure OpenAI client library as per the instructions on its GitHub page. This library facilitates interaction with the Azure OpenAI services when using C# and .NET.

I need help

Run dotnet add package Azure.AI.OpenAI --prerelease from the project directory to install the nuget package.

Understanding Chat Completion, System Prompts, and User Messages

Before diving into the implementation, let’s clarify some key concepts:

  1. Chat Completion:
    - In the context of GPT-4, a chat completion refers to the process of generating a conversation-like sequence of text based on input prompts. In our case we won’t converse with the AI model, but we will be using this interface to structure our “query”.
  2. System Prompt:
    - The system prompt is essentially the instruction or guideline you provide to the AI model. It sets the direction for the content generation. In our case, the system prompt will be an instruction to create a blog post. This prompt guides the AI to understand the nature and structure of the content desired.
  3. User Message:
    • Alongside the system prompt, the user message will in our case provide specific details or topics for the content. For example, if you want a blog post about “The Future of AI in Healthcare”, this subject will be part of your user message. The user message tailors the AI’s response to your specific content needs.

Steps for Implementing Content Generation

  1. Setting Up the OpenAI Client:
    • Create and configure the OpenAI client in your application. Consider wiring it up using dependency injection in Program.cs for better manageability.
  2. Using the OpenAI Client in GenerateContent:
    • In the GenerateContent method, initiate a Chat Completion with GPT-4.
    • Provide a well-crafted System Prompt to guide the AI in generating a blog post.
    • Include a User Message that specifies the topic or subject of the blog post.
Prompt Writing and Design

It’s crucial to carefully design your prompts for effective content generation. A well-designed prompt should be clear, concise, and specific to the context of the desired content. It should guide the AI in tone, style, and subject matter, ensuring the output aligns with your content goals. There is a lot to learn about writing an effective prompt which is also resilient to a user trying to fool the AI.

It might be too much to dive into for your first day here at CacheCows, but maybe a topic for our monthly Competence Days, here are more resources on prompt writing:

Implement the content generation

Now it’s your turn to implement usage of the client in ContentGenerator.cs to generate a blog post. You can find more information on how to use the client to do this here

I need more help on how to do this
  1. Setting Up the OpenAI Client:
    - Create and configure the OpenAI client in your application. Consider wiring it up using dependency injection in Program.cs for better manageability.
  2. Using the OpenAI Client in GenerateContent:
    - In the GenerateContent method, initiate a Chat Completion with GPT-4.
  • Provide a well-crafted System Prompt to guide the AI in generating a blog post.
  • Include a User Message that specifies the topic or subject of the blog post.

When you have an implementation that works

Then great job! This will really come in handy for our customers. Let’s proceed to learn about Embeddings.


Last update : November 20, 2023
Created : November 16, 2023