OpenAI
Introduction
The Devprime platform accelerates software developer productivity by offering a complete software architecture design, components with intelligent behaviors, accelerators for code deployment, and updates with new features.
In this article, we will discuss the use of the Extensions Adapter present in the Devprime architecture, which allows you to add additional behaviors through external components based on NuGet technology. This provides the possibility to expand the functionalities of the Devprime platform and adhere to software architecture standards, improving maintainability, reusability, and testability.
During this article, we will use the nuget component Azure.AI.OpenAI that will incorporate access to the Microsoft Azure OpenAI and OpenAI API.
Cheklist and preperation of the initial environment:
- Open an account on Devprime Platform and purchase a Developer/Enterprise) license.
- Install an updated version of .NET (Linux, macOS, and Windows)
- Install and/or update Visual Studio Code and/or Visual Studio 2023 Community / Professional / Enterprise.
- Install and/or update docker (For Windows, use WSL2).
- Initialize the MongoDB and RabbitMQ containers in docker and add the queue ‘orderevents’ in RabbitMQ.
- Install and activate the latest version of the Devprime CLI.
- Create a folder for your projects and set read and write permissions.
- See the article “Creating the first microservice” to explore getting started with Devprime.
- Create an account on Microsoft Azure OpenAI or OpenAI and obtain the API access credentials.
Ready-made example with all the steps in the article
This article includes a complete project that demonstrates the feature discussed. You can choose to download it by following the steps below or simply proceed to the next items.
- Clone the repo
git clone https://github.com/devprime/examples.git
- Enter the folder
cd examples/extensions/openai/basic
- Upgrade your Devprime license
dp stack
- Update MongoDB/RabbitMQ settings
- Update Azure OpenAI/OpenAI credentials on appsettings.json
- Locate the items in DevPrime_Custom:
ai.url /ai.credential /ai.deploymentmodel
- Run the microservice
Creating a microservice to use in the example
The first step is to create a new microservice so that we can make customizations to the Extensions Adapter, adding the external NuGet component and preparing it to interact with the OpenAI API. The name of this microservice will be set to “ms-ai”, as demonstrated in the command below.
dp new ms-ai --state mongodb --stream rabbitmq
After the creation of the new microservice, enter the “ms-ai” project folder and you will be able to view all the implementations by Visual Studio Code, as demonstrated in the article related to creation of the first microservice.
Adding a Business Rule
The business rules in the Devprime platform architecture are based on Domain-Driven Design (DDD), and to move forward, we need to add a Aggregate Root
class inside the Domain project. To make this procedure easier, we will use the command below available in the Devprime CLI.
dp add aggregate AI
Preview the new class created by Visual Studio Code.
code src/Core/Domain/Aggregates/AI/AI.cs
Modify the AI
class by adding a property called “Prompt” as shown in the example below.
|
|
Using the Devprime Platform Code Accelerators
Now that you’ve implemented a business rule, let’s use the Devprime CLI accelerator to generate the necessary code and launch the microservice based on the initial business rule you provided.
Run the following command and type A
to move forward with the implementations:
|
|
After completing this command, you will already have the basic implementations of your microservice and we will use it as a reference to move on to the next step, which involves embedding the OpenAI component in the Extension Adapter.
Adding the Intelligence extension
At this point, we will begin the procedures to enable a third-party extension provided as a NuGet component on the Devprime platform, following development patterns that ensure maintainability and decoupling.
To speed up this process, we’ll use the command below, which implements the new extension to the application service via an interface and dependency injection, and will build the required initial implementation on the adapter.
Run the command below:
dp add extensions IntelligenceService
|
|
The IntelligenceService
class and its interface IIntelligenceService
implement the integrations with the OpenAI library. On the other hand, the Extensions
class and its interface IExtensions
act as proxies, allowing the Devprime Pipeline execution context to access all the extensions available in the application through dependency injection.
Adding the reference to the OpenAI component
Add the NuGet component reference Azure.AI.OpenAI to the Extensions Adapter project file. Be sure to check the NuGet portal for the most current version and adjust the version if necessary:
Run the command below on a single line:
|
|
Now that you’ve just added a new component, take the opportunity to build the microservice to make sure
that everything is working properly with this new dependency.
Run the command:
|
|
Implementing integration with the OpenAI component
At this point, we’re going to implement the integration with the OpenAI component inside the Extensions Adapter. The first step is to modify the interface to include a method called “Conversation” and then implement the code that makes the OpenAI API call and returns the result in a format that can be transported into the context of our application.
Open the IIntelligenceService
interface in Visual Studio Code.
code src/Core/Application/Interfaces/Adapters/Extensions/IIntelligenceService.cs
Replace with the code below:
|
|
Open the IntelligenceService
class in Visual Studio Code.
code src/Adapters/Extensions/IntelligenceService/IntelligenceService.cs
Replace with the code below:
|
|
Adding environment variables for use in OpenAI
In the previous implementation we used the Dp.Settings.Default("ai.credential")
command to get the parameter of an environment variable defined in the local project in the “src/App/appsettings.json” file. Open the “DevPrime_Custom” block following the example below adding the OpenAI service URL, credential and deployment template.
Open and edit from Visual Studio Code
code src/App/appsettings.json
|
|
Implemented extension adapter call
The first point of contact with the new functionality made available by the new OpenAI Extension is the Handler, which in this context we will use CreateAIEventHandler
which has the role of intercepting a domain event and mediating the integration with the technological resources of the platform. To support Extensions, we’ve added inheritance for the EventHandlerWithExtensions
class, as well as adding the IExtensions
interface.
It is important to note that after the previous implementations we now have a new method Dp.Extensions.IntelligenceService.Conversation()
that executes the external code embedded inside the Extensions Adapter.
This new implementation will execute the call to OpenAI and return the result. To move forward, open the file and replace all the code.
Open the Handler from Visual Studio Code.
code src/Core/Application/EventHandlers/AI/CreateAIEventHandler.cs
|
|
Because of this modification in the Handler, remove the test file that is not used in this example
using the command below:
Removing file
rm src/Tests/Core/Application/AI/CreateAIEventHandlerTest.cs
Modifying the ADD Method in the Aggregate Root
Now we’ll return to the business rule in the Aggregate Root to perform a customization on the “ADD” method, replacing it with the code below to allow the “CreateAI” domain event to run and get that return. This event will be processed in the “CreateAIEventHandler” handler responsible for interacting with the Extensions Adapter."
Open the Aggregate Root class in Visual Studio Code
code src/Core/domain/aggregates/AI/AI.cs
|
|
Modifying the Interface and Application Services
Due to the implementation in the Extension Adapter, it is necessary to get the return of the Aggregate Root in Application Services. The first step is to modify the IAIService
interface as shown in the example below.
Open the Interface in Vsiual Studio Code
code src\Core\Application\Interfaces\Services\IAIService.cs
|
|
After the interface modification in the previous step, it’s time to reflect that change in Application Services. Locate the Add
method below and override it so that the application service returns the result of our Aggregate Root.
Open Application Services in Visual Studio Code.
code src\Core\Application\Services\AI\AIService.cs
|
|
Exploring the built-in microservice with OpenAI
At the end of this implementation, we can already perform the tests by running the microservice, customizing it with the OpenAI integration.
Start the microservice by running one of the scripts below:
|
|
Go to the microservice swagger portal
Post to the API via Swagger by filling out the prompt
|
|
And check out the result in the JSON below:
|
|
Follow along with the example log record automatically generated by the Observability Adapter, which details the entire internal flow, from receiving the POST to integrating with OpenAI in the Extensions Adapter.
|
|
More articles about extesions on the Devprime platform
Using SendGrid in microservices
Using Humanizer in microservices