-
-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from wieslawsoltes/FunctionCalling
Function calling
- Loading branch information
Showing
22 changed files
with
622 additions
and
9 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
19 changes: 19 additions & 0 deletions
19
samples/ChatGPT.CLI.FunctionCalling/ChatGPT.CLI.FunctionCalling.csproj
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,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<RuntimeIdentifiers>win-x64;linux-x64;linux-arm64;osx-x64;osx-arm64</RuntimeIdentifiers> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<TrimMode>full</TrimMode> | ||
<IsPackable>False</IsPackable> | ||
<RootNamespace>ChatGPT.CLI</RootNamespace> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)'=='Release'"> | ||
<PublishAot>True</PublishAot> | ||
<TrimmerSingleWarn>false</TrimmerSingleWarn> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\ChatGPT.Core\ChatGPT.Core.csproj" /> | ||
</ItemGroup> | ||
</Project> |
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,113 @@ | ||
using ChatGPT; | ||
using ChatGPT.ViewModels.Chat; | ||
|
||
Defaults.ConfigureDefaultServices(); | ||
|
||
var directions = | ||
""" | ||
You are a helpful assistant. | ||
Write answers in plain text. | ||
Do not use markdown. | ||
Only use the functions you have been provided with. | ||
"""; | ||
|
||
if (args.Length == 1) | ||
{ | ||
directions = args[0]; | ||
} | ||
|
||
using var cts = new CancellationTokenSource(); | ||
|
||
var functions = GetFunctions(); | ||
|
||
var chat = new ChatViewModel(new ChatSettingsViewModel | ||
{ | ||
MaxTokens = 2000, | ||
Model = "gpt-3.5-turbo-0613", | ||
Functions = functions, | ||
FunctionCall = "auto" | ||
// Force function call by setting FunctionCall property. | ||
// FunctionCall = new { name = "GetCurrentWeather" } | ||
}); | ||
|
||
// Enable to debug json requests and responses. | ||
// chat.Debug = true; | ||
|
||
chat.AddSystemMessage(directions); | ||
|
||
while (true) | ||
{ | ||
Console.Write("> "); | ||
|
||
var input = Console.ReadLine(); | ||
if (string.IsNullOrWhiteSpace(input) || input == Environment.NewLine) | ||
{ | ||
continue; | ||
} | ||
|
||
try | ||
{ | ||
chat.AddUserMessage(input); | ||
var result = await chat.SendAsync(chat.CreateChatMessages(), cts.Token); | ||
|
||
chat.AddAssistantMessage(result?.Message); | ||
|
||
if (result?.Message is { }) | ||
{ | ||
Console.WriteLine(result.Message); | ||
} | ||
|
||
if (result?.FunctionCall is { } functionCall) | ||
{ | ||
if (functionCall.Name == "GetCurrentWeather" && functionCall.Arguments is { }) | ||
{ | ||
functionCall.Arguments.TryGetValue("location", out var location); | ||
functionCall.Arguments.TryGetValue("unit", out var unit); | ||
var functionCallResult = GetCurrentWeather(location, unit ?? "celsius"); | ||
chat.AddFunctionMessage(functionCallResult, functionCall.Name); | ||
|
||
Console.WriteLine(functionCallResult); | ||
} | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine("Error: " + ex.Message); | ||
} | ||
} | ||
|
||
string GetCurrentWeather(string? location, string? unit) | ||
{ | ||
Console.WriteLine($"Weather for {location} [{unit}]."); | ||
return "Cloudy."; | ||
} | ||
|
||
object GetFunctions() | ||
{ | ||
return new[] | ||
{ | ||
new | ||
{ | ||
name = "GetCurrentWeather", | ||
description = "Get the current weather in a given location", | ||
parameters = new | ||
{ | ||
type = "object", | ||
properties = new | ||
{ | ||
location = new | ||
{ | ||
type = "string", | ||
description = "The city and state, e.g. San Francisco, CA" | ||
}, | ||
unit = new | ||
{ | ||
type = "string", | ||
@enum = new[] {"celsius", "fahrenheit"} | ||
}, | ||
}, | ||
required = new[] {"location"} | ||
}, | ||
} | ||
}; | ||
} |
35 changes: 35 additions & 0 deletions
35
src/ChatGPT.Core/ViewModels/Chat/ChatFunctionCallViewModel.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,35 @@ | ||
using System.Text.Json.Serialization; | ||
using CommunityToolkit.Mvvm.ComponentModel; | ||
|
||
namespace ChatGPT.ViewModels.Chat; | ||
|
||
public class ChatFunctionCallViewModel : ObservableObject | ||
{ | ||
private string? _name; | ||
|
||
[JsonConstructor] | ||
public ChatFunctionCallViewModel() | ||
{ | ||
} | ||
|
||
public ChatFunctionCallViewModel(string name) | ||
: this() | ||
{ | ||
_name = name; | ||
} | ||
|
||
[JsonPropertyName("name")] | ||
public string? Name | ||
{ | ||
get => _name; | ||
set => SetProperty(ref _name, value); | ||
} | ||
|
||
public ChatFunctionCallViewModel Copy() | ||
{ | ||
return new ChatFunctionCallViewModel | ||
{ | ||
Name = _name, | ||
}; | ||
} | ||
} |
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,63 @@ | ||
using System.Text.Json.Serialization; | ||
using CommunityToolkit.Mvvm.ComponentModel; | ||
|
||
namespace ChatGPT.ViewModels.Chat; | ||
|
||
public class ChatFunctionViewModel : ObservableObject | ||
{ | ||
private string? _name; | ||
private string? _description; | ||
private object? _parameters; | ||
|
||
[JsonConstructor] | ||
public ChatFunctionViewModel() | ||
{ | ||
} | ||
|
||
public ChatFunctionViewModel(string name, string description) | ||
: this() | ||
{ | ||
_name = name; | ||
_description = description; | ||
} | ||
|
||
public ChatFunctionViewModel(string name, string description, object parameters) | ||
: this() | ||
{ | ||
_name = name; | ||
_description = description; | ||
_parameters = parameters; | ||
} | ||
|
||
[JsonPropertyName("name")] | ||
public string? Name | ||
{ | ||
get => _name; | ||
set => SetProperty(ref _name, value); | ||
} | ||
|
||
[JsonPropertyName("description")] | ||
public string? Description | ||
{ | ||
get => _description; | ||
set => SetProperty(ref _description, value); | ||
} | ||
|
||
[JsonPropertyName("parameters")] | ||
public object? Parameters | ||
{ | ||
get => _parameters; | ||
set => SetProperty(ref _parameters, value); | ||
} | ||
|
||
public ChatFunctionViewModel Copy() | ||
{ | ||
return new ChatFunctionViewModel | ||
{ | ||
Name = _name, | ||
Description = _description, | ||
// TODO: Copy Parameters if type is reference. | ||
Parameters = _parameters | ||
}; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/ChatGPT.Core/ViewModels/Chat/ChatMessageFunctionCallViewModel.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,52 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text.Json.Serialization; | ||
using CommunityToolkit.Mvvm.ComponentModel; | ||
|
||
namespace ChatGPT.ViewModels.Chat; | ||
|
||
public class ChatMessageFunctionCallViewModel : ObservableObject | ||
{ | ||
private string? _name; | ||
private Dictionary<string, string>? _arguments; | ||
|
||
[JsonConstructor] | ||
public ChatMessageFunctionCallViewModel() | ||
{ | ||
} | ||
|
||
public ChatMessageFunctionCallViewModel(string role, Dictionary<string, string> arguments) | ||
: this() | ||
{ | ||
_name = role; | ||
_arguments = arguments; | ||
} | ||
|
||
[JsonPropertyName("name")] | ||
public string? Name | ||
{ | ||
get => _name; | ||
set => SetProperty(ref _name, value); | ||
} | ||
|
||
[JsonPropertyName("arguments")] | ||
public Dictionary<string, string>? Arguments | ||
{ | ||
get => _arguments; | ||
set => SetProperty(ref _arguments, value); | ||
} | ||
|
||
public ChatMessageFunctionCallViewModel Copy() | ||
{ | ||
var functionCall = new ChatMessageFunctionCallViewModel | ||
{ | ||
Name = _name, | ||
// TODO: Copy entry Value if it's reference value. | ||
Arguments = _arguments?.ToDictionary( | ||
e => e.Key, | ||
e => e.Value) | ||
}; | ||
|
||
return functionCall; | ||
} | ||
} |
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
Oops, something went wrong.