|
3 | 3 | #:package OpenAI@2.2.*-* |
4 | 4 | #:property PublishAot=false |
5 | 5 |
|
6 | | -using System.Net.NetworkInformation; |
7 | 6 | using OpenAI.Chat; |
8 | 7 |
|
9 | 8 | string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!; |
|
32 | 31 | ChatMessage.CreateUserMessage("what is the current local time?") |
33 | 32 | ]; |
34 | 33 |
|
35 | | -while (true) |
| 34 | +complete: |
| 35 | +ChatCompletion completion = client.CompleteChat(messages, options); |
| 36 | +messages.AddRange(ChatMessage.CreateAssistantMessage(completion)); |
| 37 | +switch(completion.FinishReason) |
36 | 38 | { |
37 | | - complete: |
38 | | - ChatCompletion completion = client.CompleteChat(messages, options); |
39 | | - messages.AddRange(ChatMessage.CreateAssistantMessage(completion)); |
40 | | - switch(completion.FinishReason) |
41 | | - { |
42 | | - case ChatFinishReason.ToolCalls: |
43 | | - Console.WriteLine($"{completion.ToolCalls.Count} tool call[s] detected."); |
44 | | - foreach (ChatToolCall toolCall in completion.ToolCalls) |
| 39 | + case ChatFinishReason.ToolCalls: |
| 40 | + Console.WriteLine($"{completion.ToolCalls.Count} tool call[s] detected."); |
| 41 | + foreach (ChatToolCall toolCall in completion.ToolCalls) |
| 42 | + { |
| 43 | + if (toolCall.FunctionName == nameof(MyTools.GetCurrentTime)) |
45 | 44 | { |
46 | | - if (toolCall.FunctionName == nameof(MyTools.GetCurrentTime)) |
47 | | - { |
48 | | - string result = MyTools.GetCurrentTime(); |
49 | | - messages.Add(ChatMessage.CreateToolMessage(toolCall.Id, result)); |
50 | | - } |
51 | | - else |
52 | | - { |
53 | | - Console.WriteLine($"Unknown tool call: {toolCall.FunctionName}"); |
54 | | - } |
| 45 | + string result = MyTools.GetCurrentTime(); |
| 46 | + messages.Add(ChatMessage.CreateToolMessage(toolCall.Id, result)); |
55 | 47 | } |
56 | | - goto complete; |
57 | | - case ChatFinishReason.Stop: |
58 | | - Console.WriteLine(completion.Content[0].Text); |
59 | | - return; |
60 | | - default: |
61 | | - Console.WriteLine("Unexpected finish reason: " + completion.FinishReason); |
62 | | - return; |
63 | | - } |
| 48 | + else |
| 49 | + { |
| 50 | + Console.WriteLine($"Unknown tool call: {toolCall.FunctionName}"); |
| 51 | + } |
| 52 | + } |
| 53 | + goto complete; |
| 54 | + case ChatFinishReason.Stop: |
| 55 | + Console.WriteLine(completion.Content[0].Text); |
| 56 | + return; |
| 57 | + default: |
| 58 | + Console.WriteLine("Unexpected finish reason: " + completion.FinishReason); |
| 59 | + return; |
64 | 60 | } |
| 61 | + |
65 | 62 | public static class MyTools |
66 | 63 | { |
67 | 64 | public static string GetCurrentTime(bool utc = false) => utc ? DateTime.UtcNow.ToString("HH:mm") : DateTime.Now.ToString("HH:mm"); |
|
0 commit comments