Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/clusterm/chatgptlib
Just another OpenAI client library (chat completion only) with functions support
https://github.com/clusterm/chatgptlib
gpt gpt-3 gpt-4 openai openai-api
Last synced: 25 days ago
JSON representation
Just another OpenAI client library (chat completion only) with functions support
- Host: GitHub
- URL: https://github.com/clusterm/chatgptlib
- Owner: ClusterM
- License: gpl-3.0
- Created: 2023-06-22T10:37:15.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-06-27T12:00:15.000Z (6 months ago)
- Last Synced: 2024-11-24T19:48:10.716Z (29 days ago)
- Topics: gpt, gpt-3, gpt-4, openai, openai-api
- Language: C#
- Homepage:
- Size: 955 KB
- Stars: 5
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# 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
```C#
internal class Program
{
const string OpenAIKey = "";
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()
{
new ChatMessage() { Role = ChatMessage.ChatMessageRole.User, Content = RequestText }
};
var functions = new Dictionary
{
["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(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 GptFunctionMethod(JsonElement args);class GptFunction
{
public delegate Task GptFunctionMethod(JsonElement args);public required string Description { get; init; }
public required GptFunctionMethod Function { get; init; }
public required IJsonSchema Parameters { get; init; }
}
}
}
```Also, check [GhatGptTests](https://github.com/ClusterM/chatgptlib/tree/master/GhatGptTests) directory, there is YouTube search demo.
## Download on NuGet
`dotnet add package ChatGptLib`