-
Notifications
You must be signed in to change notification settings - Fork 18
[Playground] Refactoring OpenAIApiClient
#163
#314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
justinyoo
merged 14 commits into
aliencube:main
from
5jisoo:feature/163-refactor-openaiapiclient
Oct 6, 2024
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
20efedb
Refactor: OpenAIApiClient endpoint
5jisoo 449de1f
Fix: Uri endpoint
5jisoo 337f2a5
Fix: inject AzureOpenAIClient
5jisoo 98f897e
Chore: delete apikey
5jisoo 76fb03c
Fix: inject OpenAIApiClientOptions
5jisoo edce183
Merge branch 'main' into feature/163-refactor-openaiapiclient
5jisoo 468e7f8
Feat: Options.UserPrompt값 설정
5jisoo ca37946
Fix: ClientOptions Uri로 변경
5jisoo d5b1c06
Fix: add HttpClient
5jisoo ef9e10b
Add app settings to resolve service discovery
justinyoo 94df151
Merge pull request #1 from justinyoo/feature/163-refactor-openaiapicl…
5jisoo 77a8ce6
Merge branch 'main' into feature/163-refactor-openaiapiclient
5jisoo 37739ad
Fix: MaxOutputTokenCount로 변경
5jisoo 70287e1
Merge branch 'main' into feature/163-refactor-openaiapiclient
5jisoo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 58 additions & 51 deletions
109
src/AzureOpenAIProxy.PlaygroundApp/Clients/OpenAIApiClient.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,58 @@ | ||
using System.ClientModel; | ||
|
||
using Azure.AI.OpenAI; | ||
|
||
using OpenAI.Chat; | ||
|
||
namespace AzureOpenAIProxy.PlaygroundApp.Clients; | ||
|
||
/// <summary> | ||
/// This provides interfaces to the <see cref="OpenAIApiClient"/> class. | ||
/// </summary> | ||
public interface IOpenAIApiClient | ||
{ | ||
/// <summary> | ||
/// Send a chat completion request to the OpenAI API. | ||
/// </summary> | ||
/// <param name="clientOptions"><see cref="OpenAIApiClientOptions"/> instance.</param> | ||
/// <returns>Returns the chat completion result.</returns> | ||
Task<string> CompleteChatAsync(OpenAIApiClientOptions clientOptions); | ||
} | ||
|
||
/// <summary> | ||
/// This represents the OpenAI API client entity. | ||
/// </summary> | ||
public class OpenAIApiClient : IOpenAIApiClient | ||
{ | ||
/// <inheritdoc /> | ||
public async Task<string> CompleteChatAsync(OpenAIApiClientOptions clientOptions) | ||
{ | ||
var endpoint = new Uri($"{clientOptions.Endpoint!.TrimEnd('/')}/api"); | ||
var credential = new ApiKeyCredential(clientOptions.ApiKey!); | ||
var openai = new AzureOpenAIClient(endpoint, credential); | ||
var chat = openai.GetChatClient(clientOptions.DeploymentName); | ||
|
||
var messages = new List<ChatMessage>() | ||
{ | ||
new SystemChatMessage(clientOptions.SystemPrompt), | ||
new UserChatMessage(clientOptions.UserPrompt), | ||
}; | ||
var options = new ChatCompletionOptions | ||
{ | ||
MaxOutputTokenCount = clientOptions.MaxOutputTokenCount, | ||
Temperature = clientOptions.Temperature, | ||
}; | ||
|
||
var result = await chat.CompleteChatAsync(messages, options).ConfigureAwait(false); | ||
var response = result.Value.Content.First().Text; | ||
|
||
return response; | ||
} | ||
} | ||
using System.ClientModel; | ||
|
||
using Azure.AI.OpenAI; | ||
|
||
using AzureOpenAIProxy.PlaygroundApp.Configurations; | ||
|
||
using OpenAI.Chat; | ||
|
||
namespace AzureOpenAIProxy.PlaygroundApp.Clients; | ||
|
||
/// <summary> | ||
/// This provides interfaces to the <see cref="OpenAIApiClient"/> class. | ||
/// </summary> | ||
public interface IOpenAIApiClient | ||
{ | ||
/// <summary> | ||
/// Send a chat completion request to the OpenAI API. | ||
/// </summary> | ||
/// <param name="clientOptions"><see cref="OpenAIApiClientOptions"/> instance.</param> | ||
/// <returns>Returns the chat completion result.</returns> | ||
Task<string> CompleteChatAsync(OpenAIApiClientOptions clientOptions); | ||
} | ||
|
||
/// <summary> | ||
/// This represents the OpenAI API client entity. | ||
/// </summary> | ||
public class OpenAIApiClient(ServiceNamesSettings names, ServicesSettings settings) : IOpenAIApiClient | ||
{ | ||
private readonly ServiceNamesSettings _names = names ?? throw new ArgumentNullException(nameof(names)); | ||
private readonly ServicesSettings _settings = settings ?? throw new ArgumentNullException(nameof(settings)); | ||
|
||
/// <inheritdoc /> | ||
public async Task<string> CompleteChatAsync(OpenAIApiClientOptions clientOptions) | ||
{ | ||
var service = settings[this._names.Backend!]; | ||
var endpoint = service.Https.FirstOrDefault() ?? service.Http.First(); | ||
Check warning on line 36 in src/AzureOpenAIProxy.PlaygroundApp/Clients/OpenAIApiClient.cs
|
||
|
||
clientOptions.Endpoint = new Uri($"{endpoint!.TrimEnd('/')}/api"); | ||
|
||
var credential = new ApiKeyCredential(clientOptions.ApiKey!); | ||
var openai = new AzureOpenAIClient(clientOptions.Endpoint, credential); | ||
var chat = openai.GetChatClient(clientOptions.DeploymentName); | ||
|
||
var messages = new List<ChatMessage>() | ||
{ | ||
new SystemChatMessage(clientOptions.SystemPrompt), new UserChatMessage(clientOptions.UserPrompt), | ||
}; | ||
var options = new ChatCompletionOptions | ||
{ | ||
MaxOutputTokenCount = clientOptions.MaxTokens, Temperature = clientOptions.Temperature, | ||
}; | ||
|
||
var result = await chat.CompleteChatAsync(messages, options).ConfigureAwait(false); | ||
var response = result.Value.Content.First().Text; | ||
|
||
return response; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/AzureOpenAIProxy.PlaygroundApp/Configurations/ServicesSettings.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
namespace AzureOpenAIProxy.PlaygroundApp.Configurations; | ||
|
||
/// <summary> | ||
/// This represents the app settings entity for services. | ||
/// </summary> | ||
public class ServicesSettings : Dictionary<string, ServiceSettings> | ||
{ | ||
/// <summary> | ||
/// Gets the name of the configuration settings. | ||
/// </summary> | ||
public const string Name = "services"; | ||
} | ||
|
||
/// <summary> | ||
/// This represents the service settings entity. | ||
/// </summary> | ||
public class ServiceSettings | ||
{ | ||
/// <summary> | ||
/// Gets or sets the HTTP endpoints. | ||
/// </summary> | ||
public List<string>? Http { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the HTTPS endpoints. | ||
/// </summary> | ||
public List<string>? Https { get; set; } | ||
} | ||
|
||
/// <summary> | ||
/// This represents the app settings entity for service names. | ||
/// </summary> | ||
public class ServiceNamesSettings | ||
{ | ||
/// <summary> | ||
/// Gets the name of the configuration settings. | ||
/// </summary> | ||
public const string Name = "ServiceNames"; | ||
|
||
/// <summary> | ||
/// Gets or sets the backend service name. | ||
/// </summary> | ||
public string? Backend { get; set; } | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.