ChatGptLib 1.0.0
dotnet add package ChatGptLib --version 1.0.0
NuGet\Install-Package ChatGptLib -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ChatGptLib" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ChatGptLib --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: ChatGptLib, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ChatGptLib as a Cake Addin #addin nuget:?package=ChatGptLib&version=1.0.0 // Install ChatGptLib as a Cake Tool #tool nuget:?package=ChatGptLib&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
ChatGptLib
Just another OpenAI client library (chat completion only) with new functions feature support. For .NET 7.
Full documentation: https://clusterm.github.io/chatgptlib/
Simple usage example
internal class Program
{
const string OpenAIKey = "<insert your OpenAI API key here>";
const string RequestText = "How much o'clock? Also, can you say the current date? And day of the week.";
static async Task Main(string[] args)
{
var messages = new List<ChatMessage>()
{
new ChatMessage() { Role = ChatMessage.ChatMessageRole.User, Content = RequestText }
};
var functions = new Dictionary<string, GptFunction>
{
["get_time"] = new GptFunction()
{
Description = "Returns current time in ISO 8601 format",
Function = (args) => Task.FromResult(DateTime.Now.ToString("o")),
Parameters = new JsonObjectSchema()
}
};
var gptApi = new ChatGptClient(OpenAIKey);
while (true)
{
var request = new ChatRequest()
{
Model = "gpt-4-0613",
Messages = messages,
Functions = new List<ChatFunction>(functions.Select(kv => new ChatFunction() { Name = kv.Key, Description = kv.Value.Description, Parameters = kv.Value.Parameters })),
Temperature = 0.5
};
// Create empty response
var completionResult = new ChatResponse();
var completionResultStream = gptApi.RequestStreamAsync(request);
// Process stream data
await foreach (var p in completionResultStream)
{
// You can just use + operator to combine stream chunks
completionResult = completionResult! + p;
// Skip if it's function call
if (completionResult?.Choices?.First()?.Message?.FunctionCall != null)
continue;
// Print partial data
if (p.Choices?.FirstOrDefault()?.Delta != null)
Console.Write(p.Choices.First().Delta!.Content);
}
if (completionResult?.Choices?.First()?.Message?.FunctionCall == null)
Console.WriteLine();
else
Console.WriteLine($"Function call: {completionResult?.Choices?.First()?.Message?.FunctionCall}");
// Append message to the chat history
var msg = completionResult!.Choices.FirstOrDefault()?.Message!;
messages.Add(msg);
if (msg.FunctionCall?.Name != null)
{
// It's function call
if (!functions.TryGetValue(msg.FunctionCall.Name, out GptFunction? function))
throw new NotImplementedException($"Unknown function: {msg.FunctionCall.Name}");
// calling the fuctions
var argsDoc = JsonDocument.Parse(msg.FunctionCall.Arguments!);
var functionResult = await function!.Function(argsDoc.RootElement);
Console.WriteLine($"Function call result: {functionResult}");
// Need to add function result to the chat history
var functionResultMessage = new ChatMessage(ChatMessageRole.Function, functionResult, msg.FunctionCall.Name);
messages.Add(functionResultMessage);
}
else
{
// Text answer received, done.
break;
}
}
}
class GptFunction
{
public delegate Task<string> GptFunctionMethod(JsonElement args);
public required string? Description { get; init; }
public required GptFunctionMethod? Function { get; init; }
public required IJsonSchema? Parameters { get; init; }
public GptFunction() { }
public GptFunction(string description, GptFunctionMethod function, IJsonSchema args)
{
Description = description;
Function = function;
Parameters = args;
}
}
}
Also, check GhatGptTests directory, there is YouTube search demo.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net7.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0 | 200 | 6/22/2023 |