Skip to content

Commit 7758e57

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

File tree

4 files changed

+30
-161
lines changed

4 files changed

+30
-161
lines changed

THIRD-PARTY-LICENSES.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ The project uses the following third-party libraries or assets
3636
### OpenAI .NET SDK
3737

3838
- **Source**: https://github.com/openai/openai-dotnet
39-
- **Version**: 2.8.0
39+
- **Version**: 2.9.1
4040
- **License**: MIT License
4141
- **License Link**: https://github.com/openai/openai-dotnet/blob/main/LICENSE
4242

4343
### Azure.AI.OpenAI
4444

4545
- **Source**: https://github.com/Azure/azure-sdk-for-net
46-
- **Version**: 2.8.0-beta.1
46+
- **Version**: 2.9.0-beta.1
4747
- **License**: MIT License
4848
- **License Link**: https://github.com/Azure/azure-sdk-for-net/blob/main/LICENSE.txt
4949

src/AI/Service.cs

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public async Task GenerateCommitMessage(string repo, string changeList, Action<s
3333
userMessageBuilder
3434
.AppendLine("Generate a commit message (follow the rule of conventional commit message) for given git repository.")
3535
.AppendLine("- Read all given changed files before generating. Do not skip any one file.")
36+
.AppendLine("- Output the conventional commit message (with detail changes in list) directly. Do not explain your output nor introduce your answer.")
3637
.AppendLine(string.IsNullOrEmpty(_ai.AdditionalPrompt) ? string.Empty : _ai.AdditionalPrompt)
3738
.Append("Reposiory path: ").AppendLine(repo.Quoted())
3839
.AppendLine("Changed files: ")
@@ -42,51 +43,38 @@ public async Task GenerateCommitMessage(string repo, string changeList, Action<s
4243

4344
do
4445
{
46+
ChatCompletion completion = await chatClient.CompleteChatAsync(messages, options, cancellation);
4547
var inProgress = false;
46-
var updates = chatClient.CompleteChatStreamingAsync(messages, options).WithCancellation(cancellation);
47-
var toolCalls = new ToolCallsBuilder();
48-
var contentBuilder = new StringBuilder();
4948

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

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:
65+
foreach (var call in completion.ToolCalls)
6866
{
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;
67+
var result = await ChatTools.Process(call, onUpdate);
68+
messages.Add(result);
8369
}
84-
case ChatFinishReason.ContentFilter:
85-
throw new Exception("Ommitted content due to a content filter flag");
86-
default:
87-
break;
88-
}
8970

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

9280
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)