|
| 1 | +namespace OpenAI.IntegrationTests.Examples; |
| 2 | + |
| 3 | +public partial class Examples |
| 4 | +{ |
| 5 | + [Test] |
| 6 | + [Explicit] |
| 7 | + public async Task FunctionCalling() |
| 8 | + { |
| 9 | + using var api = GetAuthenticatedClient(); |
| 10 | + |
| 11 | + List<ChatCompletionRequestMessage> messages = [ |
| 12 | + "What's the weather like today?", |
| 13 | + ]; |
| 14 | + |
| 15 | + var service = new FunctionCallingService(); |
| 16 | + IList<ChatCompletionTool> tools = service.AsTools(); |
| 17 | + |
| 18 | + bool requiresAction; |
| 19 | + |
| 20 | + do |
| 21 | + { |
| 22 | + requiresAction = false; |
| 23 | + CreateChatCompletionResponse chatCompletion = await api.Chat.CreateChatCompletionAsync( |
| 24 | + messages, |
| 25 | + model: CreateChatCompletionRequestModel.Gpt4o, |
| 26 | + tools: tools); |
| 27 | + |
| 28 | + switch (chatCompletion.Choices[0].FinishReason) |
| 29 | + { |
| 30 | + case CreateChatCompletionResponseChoiceFinishReason.Stop: |
| 31 | + { |
| 32 | + // Add the assistant message to the conversation history. |
| 33 | + messages.Add(chatCompletion.Choices[0].Message.AsRequestMessage()); |
| 34 | + break; |
| 35 | + } |
| 36 | + |
| 37 | + case CreateChatCompletionResponseChoiceFinishReason.ToolCalls: |
| 38 | + { |
| 39 | + // First, add the assistant message with tool calls to the conversation history. |
| 40 | + messages.Add(chatCompletion.Choices[0].Message.AsRequestMessage()); |
| 41 | + |
| 42 | + // Then, add a new tool message for each tool call that is resolved. |
| 43 | + foreach (ChatCompletionMessageToolCall toolCall in chatCompletion.Choices[0].Message.ToolCalls ?? []) |
| 44 | + { |
| 45 | + var json = await service.CallAsync( |
| 46 | + functionName: toolCall.Function.Name, |
| 47 | + argumentsAsJson: toolCall.Function.Arguments); |
| 48 | + messages.Add(json.AsToolMessage(toolCall.Id)); |
| 49 | + } |
| 50 | + |
| 51 | + requiresAction = true; |
| 52 | + break; |
| 53 | + } |
| 54 | + |
| 55 | + case CreateChatCompletionResponseChoiceFinishReason.Length: |
| 56 | + throw new NotImplementedException("Incomplete model output due to MaxTokens parameter or token limit exceeded."); |
| 57 | + |
| 58 | + case CreateChatCompletionResponseChoiceFinishReason.ContentFilter: |
| 59 | + throw new NotImplementedException("Omitted content due to a content filter flag."); |
| 60 | + |
| 61 | + case CreateChatCompletionResponseChoiceFinishReason.FunctionCall: |
| 62 | + throw new NotImplementedException("Deprecated in favor of tool calls."); |
| 63 | + |
| 64 | + default: |
| 65 | + throw new NotImplementedException(chatCompletion.Choices[0].FinishReason.ToString()); |
| 66 | + } |
| 67 | + } while (requiresAction); |
| 68 | + |
| 69 | + foreach (ChatCompletionRequestMessage requestMessage in messages) |
| 70 | + { |
| 71 | + if (requestMessage.System is { } systemMessage) |
| 72 | + { |
| 73 | + Console.WriteLine($"[SYSTEM]:"); |
| 74 | + Console.WriteLine($"{systemMessage.Content.Value1}"); |
| 75 | + Console.WriteLine(); |
| 76 | + break; |
| 77 | + } |
| 78 | + else if (requestMessage.User is { } userMessage) |
| 79 | + { |
| 80 | + Console.WriteLine($"[USER]:"); |
| 81 | + Console.WriteLine($"{userMessage.Content.Value1}"); |
| 82 | + Console.WriteLine(); |
| 83 | + } |
| 84 | + else if (requestMessage.Assistant is { Content: not null } assistantMessage) |
| 85 | + { |
| 86 | + Console.WriteLine($"[ASSISTANT]:"); |
| 87 | + Console.WriteLine($"{assistantMessage.Content?.Value1}"); |
| 88 | + Console.WriteLine(); |
| 89 | + } |
| 90 | + else if (requestMessage.Tool is { } toolMessage) |
| 91 | + { |
| 92 | + // Do not print any tool messages; let the assistant summarize the tool results instead. |
| 93 | + break; |
| 94 | + |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments