-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: Added FunctionCalling example.
- Loading branch information
Showing
11 changed files
with
337 additions
and
362 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
```csharp | ||
using var api = GetAuthenticatedClient(); | ||
|
||
List<ChatCompletionRequestMessage> messages = [ | ||
"What's the weather like today?", | ||
]; | ||
|
||
var service = new FunctionCallingService(); | ||
IList<ChatCompletionTool> tools = service.AsTools(); | ||
|
||
bool requiresAction; | ||
|
||
do | ||
{ | ||
requiresAction = false; | ||
CreateChatCompletionResponse chatCompletion = await api.Chat.CreateChatCompletionAsync( | ||
messages, | ||
model: CreateChatCompletionRequestModel.Gpt4o, | ||
tools: tools); | ||
|
||
switch (chatCompletion.Choices[0].FinishReason) | ||
{ | ||
case CreateChatCompletionResponseChoiceFinishReason.Stop: | ||
{ | ||
// Add the assistant message to the conversation history. | ||
messages.Add(chatCompletion.Choices[0].Message.AsRequestMessage()); | ||
break; | ||
} | ||
|
||
case CreateChatCompletionResponseChoiceFinishReason.ToolCalls: | ||
{ | ||
// First, add the assistant message with tool calls to the conversation history. | ||
messages.Add(chatCompletion.Choices[0].Message.AsRequestMessage()); | ||
|
||
// Then, add a new tool message for each tool call that is resolved. | ||
foreach (ChatCompletionMessageToolCall toolCall in chatCompletion.Choices[0].Message.ToolCalls ?? []) | ||
{ | ||
var json = await service.CallAsync( | ||
functionName: toolCall.Function.Name, | ||
argumentsAsJson: toolCall.Function.Arguments); | ||
messages.Add(json.AsToolMessage(toolCall.Id)); | ||
} | ||
|
||
requiresAction = true; | ||
break; | ||
} | ||
|
||
case CreateChatCompletionResponseChoiceFinishReason.Length: | ||
throw new NotImplementedException("Incomplete model output due to MaxTokens parameter or token limit exceeded."); | ||
|
||
case CreateChatCompletionResponseChoiceFinishReason.ContentFilter: | ||
throw new NotImplementedException("Omitted content due to a content filter flag."); | ||
|
||
case CreateChatCompletionResponseChoiceFinishReason.FunctionCall: | ||
throw new NotImplementedException("Deprecated in favor of tool calls."); | ||
|
||
default: | ||
throw new NotImplementedException(chatCompletion.Choices[0].FinishReason.ToString()); | ||
} | ||
} while (requiresAction); | ||
|
||
foreach (ChatCompletionRequestMessage requestMessage in messages) | ||
{ | ||
if (requestMessage.System is { } systemMessage) | ||
{ | ||
Console.WriteLine($"[SYSTEM]:"); | ||
Console.WriteLine($"{systemMessage.Content.Value1}"); | ||
Console.WriteLine(); | ||
break; | ||
} | ||
else if (requestMessage.User is { } userMessage) | ||
{ | ||
Console.WriteLine($"[USER]:"); | ||
Console.WriteLine($"{userMessage.Content.Value1}"); | ||
Console.WriteLine(); | ||
} | ||
else if (requestMessage.Assistant is { Content: not null } assistantMessage) | ||
{ | ||
Console.WriteLine($"[ASSISTANT]:"); | ||
Console.WriteLine($"{assistantMessage.Content?.Value1}"); | ||
Console.WriteLine(); | ||
} | ||
else if (requestMessage.Tool is { } toolMessage) | ||
{ | ||
// Do not print any tool messages; let the assistant summarize the tool results instead. | ||
break; | ||
|
||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
```csharp | ||
public enum WeatherUnit | ||
{ | ||
Celsius, | ||
Fahrenheit, | ||
} | ||
|
||
[OpenAiTools(Strict = true)] | ||
public interface IFunctionCallingService | ||
{ | ||
[Description("Get the user's current location")] | ||
public Task<string> GetCurrentLocation( | ||
CancellationToken cancellationToken = default); | ||
|
||
[Description("Get the current weather in a given location")] | ||
public Task<string> GetCurrentWeatherAsync( | ||
[Description("The city and state, e.g. Boston, MA")] | ||
string location, | ||
[Description("The temperature unit to use. Infer this from the specified location.")] | ||
WeatherUnit unit = WeatherUnit.Celsius, | ||
CancellationToken cancellationToken = default); | ||
} | ||
|
||
public class FunctionCallingService : IFunctionCallingService | ||
{ | ||
public Task<string> GetCurrentLocation( | ||
CancellationToken cancellationToken = default) | ||
{ | ||
// Call the location API here. | ||
return Task.FromResult("San Francisco"); | ||
} | ||
|
||
public Task<string> GetCurrentWeatherAsync( | ||
string location, | ||
WeatherUnit unit = WeatherUnit.Celsius, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
// Call the weather API here. | ||
return Task.FromResult($"31 {unit:G}"); | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
src/tests/OpenAI.IntegrationTests/Examples/Examples.Chat.FunctionCalling.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
namespace OpenAI.IntegrationTests.Examples; | ||
|
||
public partial class Examples | ||
{ | ||
[Test] | ||
[Explicit] | ||
public async Task FunctionCalling() | ||
{ | ||
using var api = GetAuthenticatedClient(); | ||
|
||
List<ChatCompletionRequestMessage> messages = [ | ||
"What's the weather like today?", | ||
]; | ||
|
||
var service = new FunctionCallingService(); | ||
IList<ChatCompletionTool> tools = service.AsTools(); | ||
|
||
bool requiresAction; | ||
|
||
do | ||
{ | ||
requiresAction = false; | ||
CreateChatCompletionResponse chatCompletion = await api.Chat.CreateChatCompletionAsync( | ||
messages, | ||
model: CreateChatCompletionRequestModel.Gpt4o, | ||
tools: tools); | ||
|
||
switch (chatCompletion.Choices[0].FinishReason) | ||
{ | ||
case CreateChatCompletionResponseChoiceFinishReason.Stop: | ||
{ | ||
// Add the assistant message to the conversation history. | ||
messages.Add(chatCompletion.Choices[0].Message.AsRequestMessage()); | ||
break; | ||
} | ||
|
||
case CreateChatCompletionResponseChoiceFinishReason.ToolCalls: | ||
{ | ||
// First, add the assistant message with tool calls to the conversation history. | ||
messages.Add(chatCompletion.Choices[0].Message.AsRequestMessage()); | ||
|
||
// Then, add a new tool message for each tool call that is resolved. | ||
foreach (ChatCompletionMessageToolCall toolCall in chatCompletion.Choices[0].Message.ToolCalls ?? []) | ||
{ | ||
var json = await service.CallAsync( | ||
functionName: toolCall.Function.Name, | ||
argumentsAsJson: toolCall.Function.Arguments); | ||
messages.Add(json.AsToolMessage(toolCall.Id)); | ||
} | ||
|
||
requiresAction = true; | ||
break; | ||
} | ||
|
||
case CreateChatCompletionResponseChoiceFinishReason.Length: | ||
throw new NotImplementedException("Incomplete model output due to MaxTokens parameter or token limit exceeded."); | ||
|
||
case CreateChatCompletionResponseChoiceFinishReason.ContentFilter: | ||
throw new NotImplementedException("Omitted content due to a content filter flag."); | ||
|
||
case CreateChatCompletionResponseChoiceFinishReason.FunctionCall: | ||
throw new NotImplementedException("Deprecated in favor of tool calls."); | ||
|
||
default: | ||
throw new NotImplementedException(chatCompletion.Choices[0].FinishReason.ToString()); | ||
} | ||
} while (requiresAction); | ||
|
||
foreach (ChatCompletionRequestMessage requestMessage in messages) | ||
{ | ||
if (requestMessage.System is { } systemMessage) | ||
{ | ||
Console.WriteLine($"[SYSTEM]:"); | ||
Console.WriteLine($"{systemMessage.Content.Value1}"); | ||
Console.WriteLine(); | ||
break; | ||
} | ||
else if (requestMessage.User is { } userMessage) | ||
{ | ||
Console.WriteLine($"[USER]:"); | ||
Console.WriteLine($"{userMessage.Content.Value1}"); | ||
Console.WriteLine(); | ||
} | ||
else if (requestMessage.Assistant is { Content: not null } assistantMessage) | ||
{ | ||
Console.WriteLine($"[ASSISTANT]:"); | ||
Console.WriteLine($"{assistantMessage.Content?.Value1}"); | ||
Console.WriteLine(); | ||
} | ||
else if (requestMessage.Tool is { } toolMessage) | ||
{ | ||
// Do not print any tool messages; let the assistant summarize the tool results instead. | ||
break; | ||
|
||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.