{"id":21917504,"url":"https://github.com/clusterm/chatgptlib","last_synced_at":"2025-04-19T09:50:56.689Z","repository":{"id":176399842,"uuid":"657108516","full_name":"ClusterM/chatgptlib","owner":"ClusterM","description":"Just another OpenAI client library (chat completion only) with functions support","archived":false,"fork":false,"pushed_at":"2024-06-27T12:00:15.000Z","size":978,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-16T02:06:10.847Z","etag":null,"topics":["gpt","gpt-3","gpt-4","openai","openai-api"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ClusterM.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["ClusterM"],"custom":["https://www.buymeacoffee.com/cluster","https://boosty.to/cluster"]}},"created_at":"2023-06-22T10:37:15.000Z","updated_at":"2024-08-30T12:50:42.000Z","dependencies_parsed_at":"2024-03-31T16:43:44.524Z","dependency_job_id":null,"html_url":"https://github.com/ClusterM/chatgptlib","commit_stats":null,"previous_names":["clusterm/chatgptlib"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ClusterM%2Fchatgptlib","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ClusterM%2Fchatgptlib/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ClusterM%2Fchatgptlib/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ClusterM%2Fchatgptlib/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ClusterM","download_url":"https://codeload.github.com/ClusterM/chatgptlib/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249667410,"owners_count":21308232,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["gpt","gpt-3","gpt-4","openai","openai-api"],"created_at":"2024-11-28T19:33:22.758Z","updated_at":"2025-04-19T09:50:56.663Z","avatar_url":"https://github.com/ClusterM.png","language":"C#","funding_links":["https://github.com/sponsors/ClusterM","https://www.buymeacoffee.com/cluster","https://boosty.to/cluster"],"categories":[],"sub_categories":[],"readme":"# ChatGptLib\nJust another OpenAI client library (chat completion only) with new functions feature support. For .NET 7.\n\nFull documentation: https://clusterm.github.io/chatgptlib/\n\n## Simple usage example\n```C#\ninternal class Program\n{\n    const string OpenAIKey = \"\u003cinsert your OpenAI API key here\u003e\";\n    const string RequestText = \"How much o'clock? Also, can you say the current date? And day of the week.\";\n    \n    static async Task Main(string[] args)\n    {\n        var messages = new List\u003cChatMessage\u003e()\n        {\n            new ChatMessage() { Role = ChatMessage.ChatMessageRole.User, Content = RequestText }\n        };\n        var functions = new Dictionary\u003cstring, GptFunction\u003e\n        {                \n            [\"get_time\"] = new GptFunction()\n            {\n                Description = \"Returns current time in ISO 8601 format\",\n                Function = (args) =\u003e Task.FromResult(DateTime.Now.ToString(\"o\")),\n                Parameters = new JsonObjectSchema()\n            }\n        };\n\n        var gptApi = new ChatGptClient(OpenAIKey);\n        while (true)\n        {\n            var request = new ChatRequest()\n            {\n                Model = \"gpt-4-0613\",\n                Messages = messages,\n                Functions = new List\u003cChatFunction\u003e(functions.Select(kv =\u003e new ChatFunction() { Name = kv.Key, Description = kv.Value.Description, Parameters = kv.Value.Parameters })),\n                Temperature = 0.5\n            };\n            // Create empty response\n            var completionResult = new ChatResponse();\n            var completionResultStream = gptApi.RequestStreamAsync(request);\n            // Process stream data\n            await foreach (var p in completionResultStream)\n            {\n                // You can just use + operator to combine stream chunks\n                completionResult = completionResult! + p;\n                // Skip if it's function call\n                if (completionResult?.Choices?.First()?.Message?.FunctionCall != null)\n                    continue;\n                // Print partial data\n                if (p.Choices?.FirstOrDefault()?.Delta != null)\n                    Console.Write(p.Choices.First().Delta!.Content);\n            }\n            if (completionResult?.Choices?.First()?.Message?.FunctionCall == null)\n                Console.WriteLine();\n            else\n                Console.WriteLine($\"Function call: {completionResult?.Choices?.First()?.Message?.FunctionCall}\");\n            // Append message to the chat history\n            var msg = completionResult!.Choices.FirstOrDefault()?.Message!;\n            messages.Add(msg);\n            if (msg.FunctionCall?.Name != null)\n            {\n                // It's function call\n                if (!functions.TryGetValue(msg.FunctionCall.Name, out GptFunction? function))\n                    throw new NotImplementedException($\"Unknown function: {msg.FunctionCall.Name}\");\n                // calling the fuctions\n                var argsDoc = JsonDocument.Parse(msg.FunctionCall.Arguments!);\n                var functionResult = await function!.Function(argsDoc.RootElement);\n                Console.WriteLine($\"Function call result: {functionResult}\");\n\n                // Need to add function result to the chat history\n                var functionResultMessage = new ChatMessage(ChatMessageRole.Function, functionResult, msg.FunctionCall.Name);\n                messages.Add(functionResultMessage);\n            }\n            else\n            {\n                // Text answer received, done.\n                break;\n            }\n        }\n    }\n\n    class GptFunction\n    {\n        public delegate Task\u003cstring\u003e GptFunctionMethod(JsonElement args);\n\n        class GptFunction\n        {\n            public delegate Task\u003cstring\u003e GptFunctionMethod(JsonElement args);\n\n            public required string Description { get; init; }\n            public required GptFunctionMethod Function { get; init; }\n            public required IJsonSchema Parameters { get; init; }\n        }    \n    }\n}\n```\n\nAlso, check [GhatGptTests](https://github.com/ClusterM/chatgptlib/tree/master/GhatGptTests) directory, there is YouTube search demo.\n\n## Download on NuGet\n`dotnet add package ChatGptLib`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fclusterm%2Fchatgptlib","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fclusterm%2Fchatgptlib","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fclusterm%2Fchatgptlib/lists"}