Skip to content

Commit e9ed418

Browse files
committed
refactor: use non-streaming chat API
Signed-off-by: leo <longshuang@msn.cn>
1 parent 86c481d commit e9ed418

File tree

3 files changed

+27
-159
lines changed

3 files changed

+27
-159
lines changed

src/AI/Service.cs

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -42,51 +42,38 @@ public async Task GenerateCommitMessage(string repo, string changeList, Action<s
4242

4343
do
4444
{
45+
ChatCompletion completion = await chatClient.CompleteChatAsync(messages, options, cancellation);
4546
var inProgress = false;
46-
var updates = chatClient.CompleteChatStreamingAsync(messages, options).WithCancellation(cancellation);
47-
var toolCalls = new ToolCallsBuilder();
48-
var contentBuilder = new StringBuilder();
4947

50-
await foreach (var update in updates)
48+
switch (completion.FinishReason)
5149
{
52-
foreach (var contentPart in update.ContentUpdate)
53-
contentBuilder.Append(contentPart.Text);
50+
case ChatFinishReason.Stop:
51+
onUpdate?.Invoke(string.Empty);
52+
onUpdate?.Invoke("[Assistant]:");
53+
if (completion.Content.Count > 0)
54+
onUpdate?.Invoke(completion.Content[0].Text);
55+
else
56+
onUpdate?.Invoke("[No content was generated.]");
57+
break;
58+
case ChatFinishReason.Length:
59+
throw new Exception("The response was cut off because it reached the maximum length. Consider increasing the max tokens limit.");
60+
case ChatFinishReason.ToolCalls:
61+
{
62+
messages.Add(new AssistantChatMessage(completion));
5463

55-
foreach (var toolCall in update.ToolCallUpdates)
56-
toolCalls.Append(toolCall);
57-
58-
switch (update.FinishReason)
59-
{
60-
case ChatFinishReason.Stop:
61-
onUpdate?.Invoke(string.Empty);
62-
onUpdate?.Invoke("[Assistant]:");
63-
onUpdate?.Invoke(contentBuilder.ToString());
64-
break;
65-
case ChatFinishReason.Length:
66-
throw new Exception("The response was cut off because it reached the maximum length. Consider increasing the max tokens limit.");
67-
case ChatFinishReason.ToolCalls:
64+
foreach (var call in completion.ToolCalls)
6865
{
69-
var calls = toolCalls.Build();
70-
var assistantMessage = new AssistantChatMessage(calls);
71-
if (contentBuilder.Length > 0)
72-
assistantMessage.Content.Add(ChatMessageContentPart.CreateTextPart(contentBuilder.ToString()));
73-
messages.Add(assistantMessage);
74-
75-
foreach (var call in calls)
76-
{
77-
var result = await ChatTools.Process(call, onUpdate);
78-
messages.Add(result);
79-
}
80-
81-
inProgress = true;
82-
break;
66+
var result = await ChatTools.Process(call, onUpdate);
67+
messages.Add(result);
8368
}
84-
case ChatFinishReason.ContentFilter:
85-
throw new Exception("Ommitted content due to a content filter flag");
86-
default:
87-
break;
88-
}
8969

70+
inProgress = true;
71+
break;
72+
}
73+
case ChatFinishReason.ContentFilter:
74+
throw new Exception("Ommitted content due to a content filter flag");
75+
default:
76+
break;
9077
}
9178

9279
if (!inProgress)

src/AI/ToolCallsBuilder.cs

Lines changed: 0 additions & 119 deletions
This file was deleted.

src/SourceGit.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@
5353
<PackageReference Include="Avalonia.Fonts.Inter" Version="11.3.12" />
5454
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.3.12" />
5555
<PackageReference Include="Avalonia.Diagnostics" Version="11.3.12" Condition="'$(Configuration)' == 'Debug'" />
56-
<PackageReference Include="Azure.AI.OpenAI" Version="2.8.0-beta.1" />
56+
<PackageReference Include="Azure.AI.OpenAI" Version="2.9.0-beta.1" />
5757
<PackageReference Include="BitMiracle.LibTiff.NET" Version="2.4.660" />
5858
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
5959
<PackageReference Include="LiveChartsCore.SkiaSharpView.Avalonia" Version="2.0.0-rc6.1" />
60-
<PackageReference Include="OpenAI" Version="2.8.0" />
60+
<PackageReference Include="OpenAI" Version="2.9.1" />
6161
<PackageReference Include="Pfim" Version="0.11.4" />
6262

6363
<ProjectReference Include="../depends/AvaloniaEdit/src/AvaloniaEdit.TextMate/AvaloniaEdit.TextMate.csproj" />

0 commit comments

Comments
 (0)