GroqApiLibrary 1.0.0
See the version list below for details.
dotnet add package GroqApiLibrary --version 1.0.0
NuGet\Install-Package GroqApiLibrary -Version 1.0.0
<PackageReference Include="GroqApiLibrary" Version="1.0.0" />
<PackageVersion Include="GroqApiLibrary" Version="1.0.0" />
<PackageReference Include="GroqApiLibrary" />
paket add GroqApiLibrary --version 1.0.0
#r "nuget: GroqApiLibrary, 1.0.0"
#:package GroqApiLibrary@1.0.0
#addin nuget:?package=GroqApiLibrary&version=1.0.0
#tool nuget:?package=GroqApiLibrary&version=1.0.0
Groq API C# Client Library
This library provides a simple interface to interact with the Groq AI API. It allows you to send requests to the API and receive responses asynchronously through the IGroqApiClient interface.
Installation
To use this library, you'll need to copy the GroqApiClient.cs file and the IGroqApiClient interface file into your project.
Usage
- Implement the
IGroqApiClientinterface in your application. An example implementation,GroqApiClient, is provided. - Create an instance of the
GroqApiClientclass (or any class that implementsIGroqApiClient), providing your API key. - Create a
JsonObjectwith your request. The available parameters are listed in the Groq API documentation. - Receive the response, which is also a
JsonObject, and extract the response information accordingly.
Examples
Standard Chat Completion
using GroqApiLibrary;
using System;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
string apiKey = "xxxxxxxxx";
IGroqApiClient groqApi = new GroqApiClient(apiKey);
JsonObject request = new()
{
["model"] = "mixtral-8x7b-32768", // llama2-70b-chat | gemma-7b-it | llama3-70b-8192| llama3-8b-8192 also supported
["temperature"] = 0.5,
["max_tokens"] = 100,
["top_p"] = 1,
["stop"] = "TERMINATE",
["messages"] = new JsonArray
{
new JsonObject
{
["role"] = "system",
["content"] = "You are a chatbot capable of anything and everything."
},
new JsonObject
{
["role"] = "user",
["content"] = "Write a poem about GitHub."
}
}
};
JsonObject? result = await groqApi.CreateChatCompletionAsync(request);
string response = result?["choices"]?[0]?["message"]?["content"]?.ToString() ?? "No response found";
Console.WriteLine(response);
Console.ReadLine();
}
}
Streaming Chat Completion
using GroqApiLibrary;
using System;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
class Program_streaming
{
static async Task Main()
{
string apiKey = "xxxxxxxxx";
IGroqApiClient groqApi = new GroqApiClient(apiKey);
JsonObject request = new()
{
["model"] = "mixtral-8x7b-32768", // LLaMA2-70b-chat or Gemma-7b-it also supported
["temperature"] = 0.5,
["max_tokens"] = 100,
["top_p"] = 1,
["stop"] = "TERMINATE",
["messages"] = new JsonArray
{
new JsonObject
{
["role"] = "system",
["content"] = "You are a chatbot capable of anything and everything."
},
new JsonObject
{
["role"] = "user",
["content"] = "Write a poem about GitHub."
}
}
};
await foreach (JsonObject? chunk in groqApi.CreateChatCompletionStreamAsync(request))
{
string delta = chunk?["choices"]?[0]?["delta"]?["content"]?.ToString() ?? string.Empty;
Console.Write(delta);
}
Console.WriteLine();
Console.ReadLine();
}
}
Latest Updates
- The library now uses the native
System.Text.Jsoninstead ofNewtonsoft.Jsonfor JSON serialization and deserialization. - The
JsonObjectandJsonArraytypes fromSystem.Text.Json.Nodesare used instead ofJObjectandJArrayfromNewtonsoft.Json.Linq. - The
CreateChatCompletionAsyncandCreateChatCompletionStreamAsyncmethods now return nullableJsonObjectandIAsyncEnumerable<JsonObject?>respectively. - The code has been updated to handle nullable types and use null-conditional operators to avoid potential null reference exceptions.
The modifications to switch from Newtonsoft.Json to System.Text.Json should not impact existing client applications that are using this library. The API and usage of the library remain the same, and the only change is the underlying JSON library used. Existing client applications should continue to work without any modifications.
Contributing
Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request.
License
This library is licensed under the MIT License. See the LICENSE file for more information.
Special Thanks
Marcus Cazzola, who did some of the heavy lifting. And Joaquin Grech for coaxing me to kick NewtonSoft to the curb.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- Microsoft.AspNetCore.Components (>= 8.0.4)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on GroqApiLibrary:
| Package | Downloads |
|---|---|
|
AiAssistant
A library for an AI assistant that can execute commands |
GitHub repositories
This package is not used by any popular GitHub repositories.