Skip to content

Commit

Permalink
fix null refrence of openai client
Browse files Browse the repository at this point in the history
  • Loading branch information
MajidRafieGeeks committed Dec 20, 2024
1 parent 8db636b commit f9bc587
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Olive.OpenAI/Olive.OpenAI/Olive.OpenAI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>2.1.190</Version>
<Version>2.1.191</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
41 changes: 18 additions & 23 deletions Olive.OpenAI/Olive.OpenAI/OpenAI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,35 @@ public OpenAI(string? key = null)
var model = Olive.Config.Get("OpenAI:Models:ChatModel").Or("gpt-4o");
var apiKey = key.Or(Olive.Config.Get("OpenAI:Key"));

if (key.IsEmpty())
if (apiKey.IsEmpty())
{
throw new ArgumentException("The api key was not provided. Please set it in the AppSettings or webConfig in the OpenAI:APIKey entry");
}
ChatClient client = new(model, apiKey);
_client = new(model, apiKey);
}

public async Task<string> GetResponse(string[] messages, string instruction = null)
{
if (messages.None())
{
throw new ArgumentException("There are no messages");
}
var chatMessages = new List<ChatMessage>();
if (instruction.HasValue())
{
chatMessages.Add(new SystemChatMessage(instruction));
}
chatMessages.AddRange(messages.Select(x => new UserChatMessage(x)));

var chatMessages = GetChatMessages(messages, instruction);
ChatCompletion completion = await this._client.CompleteChatAsync(chatMessages);
return completion.Content[0].Text;
}

public async IAsyncEnumerable<string> GetResponseStream(string[] messages, string instruction = null)
{
var chatMessages = GetChatMessages(messages, instruction);
var completionUpdates = this._client.CompleteChatStreamingAsync(chatMessages);
await foreach (var completionUpdate in completionUpdates)
{
if (completionUpdate.ContentUpdate.Count > 0)
{
var response = completionUpdate.ContentUpdate[0].Text;
yield return response;
}
}
}

List<ChatMessage> GetChatMessages(string[] messages, string instruction = null)
{
if (messages.None())
{
Expand All @@ -56,16 +60,7 @@ public async IAsyncEnumerable<string> GetResponseStream(string[] messages, strin
chatMessages.Add(new SystemChatMessage(instruction));
}
chatMessages.AddRange(messages.Select(x => new UserChatMessage(x)));

var completionUpdates = this._client.CompleteChatStreamingAsync(chatMessages);
await foreach (var completionUpdate in completionUpdates)
{
if (completionUpdate.ContentUpdate.Count > 0)
{
var response = completionUpdate.ContentUpdate[0].Text;
yield return response;
}
}
return chatMessages;
}

}
Expand Down

0 comments on commit f9bc587

Please sign in to comment.