Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2
DevExtremeAI is a library with full and complete implementation of all OpenAI's APIs.
License
AndreaPic/DevextremeAI
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
DevExtremeAI is a library with full and complete implementation of all OpenAI's APIs.This library is fully compliant to openAI specs and also implement openAI error response.It's very easy to use with asp.net core and has full support to dependency injection (with a single line of code as asp.net standard pattern).It's also easy to use in libraries without dependency injection (see samples below).
Please note that this isunofficial OpenAPI library (It's not mantained by OpenAI Company).
| Build | Status | Current Version |
|---|---|---|
| CI | N/A | |
| Packages |
To use this library you need OpenAI api key (and optionally organization id).To obtain the api key you have to register your account to openai.com.After registered go to your OpenAI Account and search 'View API keys', in this page you can create your apikey.From your account page you can find the Settings page where is placed your organization ID.
You can use this library vianuget package DevExtremeAI
ImportantNote that this library support dot net IConfiguration and dependency injection so it can read apikey and organization from them instead of hard coding in source code. (Please don't hard code apikey and organization id in source code and don't push them to git or any source repository).
This library fully adhere to OpenAI specs and its object model is the same of OpenAI (with dotnet peculiarities).This library also implement OpenAI error codes that aren't documented in OpenAI's APIs Reference.
Installnuget package DevExtremeAIIn Program.cs add this using:
usingDevExtremeAI.AspNet;
This using allow you to use the asp.net service extension.With the webapplication builder now you can use theAddDevExtremeAI() method that register all that you need with dependency injection.
varbuilder=WebApplication.CreateBuilder(args);// Add services to the container.builder.Services.AddDevExtremeAI();
ThisAddDevExtremeAI() overload looks for the apikey in appsettings.json or appsettings.Development.json so you can avoid to hardcode them in source code. I suggest you to use GitHub Action Secrets.
varbuilder=WebApplication.CreateBuilder(args);// Add services to the container.builder.Services.AddDevExtremeAI<DevExtremeAI.Settings.CurrentEnvironmentData>();
In this way api key and organization id are readed from environment variables named OPENAI_ORGANIZATION and OPENAI_API_KEY.
varbuilder=WebApplication.CreateBuilder(args);// Add services to the container.builder.Services.AddDevExtremeAI<MyEnvironmentReader>();
In the above example you can use the overloadAddDevExtremeAI<TEnvironment> that require an object type that implement theDevExtremeAI.Settings.IAIEnvironment interface so you can read apikey or organization id from where you want. (Your implementation ofIAIEnvironment will be used in singleton way).
Finally you can use the overloadAddDevExtremeAI(string apiKey, string? organization) and pass apikey and organization id values readed from where you want (please read them from where you want but don't hardcode in any source code).
That's all! From now you can use OpenAI in your asp.net project via Dependency Injection.
Now you can declare the constructor of your controller & friends withDevExtremeAI.OpenAIClient.IOpenAIAPIClient argument like this:
privateDevExtremeAI.OpenAIClient.IOpenAIAPIClient_openAIApiClient;publicTestAIController(DevExtremeAI.OpenAIClient.IOpenAIAPIClient openAIApiClient){_openAIApiClient=openAIApiClient;}
an example of use of IOpenAIAPIClient in controller or apicontroller could be:
// GET api/<TestAIController>/5[HttpGet("{id}")]publicasyncTask<string>Get(intid){varchat=newCreateChatCompletionRequest();chat.Model="gpt-3.5-turbo-1106";stringprompt=$"Is the number{id} even or odd?";chat.AddMessage(newChatCompletionUserContentMessage(){Content=prompt});varresponse=await_openAIApiClient.CreateChatCompletionAsync(chat);return$"{prompt} ->{response?.OpenAIResponse?.Choices[0]?.Message?.Content}";}
NoteYou can find the complete documentation of api and DTO in intellisense, examples below orOpenAI official API Reference because are the same.
If you use outside asp.net core or without HostBuilder or Dependency Injection like in Console Application or dotnet library you can use Factory methods.
In this scenario you need the same packagenuget package DevExtremeAI
After installing this package you can use the DevExtremeAI Library.
In Program.cs ad this using:
usingDevExtremeAI.OpenAIClient;
Now you you can use theOpenAIClientFactory
Inside your Library or Main method of the console application you can create an instace ofIOpenAIAPIClient like in this example:
internalclassProgram{staticasyncTaskMain(string[]args){varopenAIClient=OpenAIClientFactory.CreateInstance();//create an instance o IOpenAIAPIClientCreateChatCompletionRequestcreateCompletionRequest=newCreateChatCompletionRequest();createCompletionRequest.Model="gpt-3.5-turbo-1106";createCompletionRequest.Messages.Add(newChatCompletionRoleStringContentMessage(){Role=ChatCompletionMessageRoleEnum.User,Content="Hello!",});varresponse=awaitopenAIClient.CreateChatCompletionAsync(createCompletionRequest);Console.WriteLine(response.OpenAIResponse.Choices[0].Message.Content);}}
The factory method (CreateInstance) in above example use the overload without arguments, this overload look for apikeyvalue and organization ind in appsettings.json or appsettings.Development.json.The appsettings key names must be OPENAI_API_KEY and OPENAI_ORGANIZATION.
varopenAIClient=OpenAIClientFactory.CreateInstance<CurrentEnvironmentData>();//create an instance o IOpenAIAPIClient
varopenAIClient=OpenAIClientFactory.CreateInstance(newCurrentEnvironmentData());//create an instance o IOpenAIAPIClient
In this way api key and organization id are readed from environment variables named OPENAI_ORGANIZATION and OPENAI_API_KEY.
varopenAIClient=OpenAIClientFactory.CreateInstance<MyEnvironmentValueReader>();//create an instance o IOpenAIAPIClient
You can use the overload that require an instance ofDevExtremeAI.Settings.IAIEnvironment implemented by yourself so you can read apikey value and organization id from where you want.
Also you can use the overload that reqire apikey value and organization id as arguments (but pleas don't hardcode them in source code).
However don't hardcode apikey and organization id in any file (source code or appsettings files), don't push into source repository the appsettings.Development.json and use GitHub Action Secrets.
Every methods ofDevExtremeAI.OpenAIClient.IOpenAIAPIClient are the same of OpenAI, so you can use the
official OpenAI API Reference.Request DTO objects are described also with standard .net documentation so you can use intellisese.Every methods ofIOpenAIAPIClient are present in the xUnit integration tests therefore you can look at them there (DevExtremeAILibTest directory).
Every response is ofDevExtremeAI.OpenAIDTO.ResponseDTO<T> type.This type has three properties:
ErrorResponsethat contains the error details returned by OpenAI API in case of problem.HasErrorthat is true if an error happened otherwise is false.OpenAIResponsethat is the same of the OpenAI response.- Every DTO has the standard .net documentation so you can find documentation in intellisense and because are the same of OpenAI you can find documentation in theofficial OpenAI API Reference also you can look at the integration tests in DevExtremeAILibTest directory.
This release support openai api and object models complaint to gpt-4 and gpt-3.5-turbo.In this openai api version some api has been deprecated and some object model has changed.You can see the updated object model in the UML schema.For chat api now you can use objects that inherits from the base class 'ChatCompletionRoleRequestMessage'.
Object ChatCompletionRoleStringContentMessage is for backward compatibility only. With this object you can set the Role property like old object model version.
for example old code like this:
CreateChatCompletionRequestcreateCompletionRequest=newCreateChatCompletionRequest();createCompletionRequest.Model="gpt-3.5-turbo";createCompletionRequest.Messages.Add(newChatCompletionRequestMessage(){Role=ChatCompletionMessageRoleEnum.User,Content="Who are you?"});
Can be update in this way:
CreateChatCompletionRequestcreateCompletionRequest=newCreateChatCompletionRequest();createCompletionRequest.Model="gpt-3.5-turbo-1106";createCompletionRequest.Messages.Add(newChatCompletionRoleStringContentMessage(){Role=ChatCompletionMessageRoleEnum.User,Content="Who are you?"});
Changes are:
- Model updated from 'gpt-3.5-turbo' to 'gpt-3.5-turbo-1106'
- 'new ChatCompletionRequestMessage()' updated with 'new ChatCompletionRoleStringContentMessage()'
To fully update to latest version update must be done in this way:
CreateChatCompletionRequestcreateCompletionRequest=newCreateChatCompletionRequest();createCompletionRequest.Model="gpt-3.5-turbo-1106";createCompletionRequest.Messages.Add(newChatCompletionUserContentMessage(){Content="Who are you?"});
Changes are:
- Model updated from 'gpt-3.5-turbo' to 'gpt-3.5-turbo-1106'
- 'new ChatCompletionRequestMessage()' updated with 'new ChatCompletionUserContentMessage()'
- Role property is not used (is intrinsic in its type)
Are covered all OpenAI API types:
- Audio
- Chat
- Embeddings
- Fine-tuning
- Files
- Images
- Models
- Moderations
- Completions
- Edits
- Fine-tunes
You can find the examples of every api in test unit project.If you want, you can contribute extending this readme file with examples ;)
Copyright (c) 2023 Andrea Piccioni
Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THESOFTWARE.
About
DevExtremeAI is a library with full and complete implementation of all OpenAI's APIs.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.