GroqApiLibrary 1.0.1
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet tool install --global GroqApiLibrary --version 1.0.1
This package contains a .NET tool you can call from the shell/command line.
dotnet new tool-manifest # if you are setting up this repo dotnet tool install --local GroqApiLibrary --version 1.0.1
This package contains a .NET tool you can call from the shell/command line.
#tool dotnet:?package=GroqApiLibrary&version=1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
nuke :add-package GroqApiLibrary --version 1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
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 using .NET 8.
Installation
To use this library in your .NET 8 project:
- Copy the
GroqApiClient.cs
file into your project. - Ensure your project targets .NET 8 or later.
Usage
- Create an instance of the
GroqApiClient
class, providing your API key. - Create a
JsonObject
with your request parameters as documented in the Groq API documentation. - Use the client to send requests and receive responses.
Examples
Standard Chat Completion
using GroqApiLibrary;
using System.Text.Json.Nodes;
class Program
{
static async Task Main(string[] args)
{
string apiKey = "your_api_key_here";
var groqApi = new GroqApiClient(apiKey);
var request = new JsonObject
{
["model"] = "mixtral-8x7b-32768", // Other models: llama2-70b-chat, gemma-7b-it, llama3-70b-8192, llama3-8b-8192
["temperature"] = 0.5,
["max_tokens"] = 100,
["top_p"] = 1,
["stop"] = "TERMINATE",
["messages"] = new JsonArray
{
new JsonObject
{
["role"] = "system",
["content"] = "You are a helpful assistant."
},
new JsonObject
{
["role"] = "user",
["content"] = "Write a haiku about coding."
}
}
};
var result = await groqApi.CreateChatCompletionAsync(request);
var response = result?["choices"]?[0]?["message"]?["content"]?.ToString() ?? "No response found";
Console.WriteLine(response);
}
}
Streaming Chat Completion
using GroqApiLibrary;
using System.Text.Json.Nodes;
class Program
{
static async Task Main()
{
string apiKey = "your_api_key_here";
var groqApi = new GroqApiClient(apiKey);
var request = new JsonObject
{
["model"] = "mixtral-8x7b-32768",
["temperature"] = 0.5,
["max_tokens"] = 100,
["top_p"] = 1,
["stop"] = "TERMINATE",
["messages"] = new JsonArray
{
new JsonObject
{
["role"] = "system",
["content"] = "You are a helpful assistant."
},
new JsonObject
{
["role"] = "user",
["content"] = "Explain quantum computing in simple terms."
}
}
};
await foreach (var chunk in groqApi.CreateChatCompletionStreamAsync(request))
{
var delta = chunk?["choices"]?[0]?["delta"]?["content"]?.ToString() ?? string.Empty;
Console.Write(delta);
}
}
}
Features
- Built for .NET 8, taking advantage of the latest C# features.
- Uses
System.Text.Json
for efficient JSON handling. - Supports both synchronous and streaming API calls.
- Implements
IDisposable
for proper resource management. - Nullable aware, helping to prevent null reference exceptions.
Latest Updates
- Upgraded to .NET 8 compatibility.
- Removed dependency on Newtonsoft.Json, now using
System.Text.Json
. - Improved null handling with nullable reference types.
- Simplified API calls using
HttpClient.PostAsJsonAsync
. - Enhanced streaming support with
IAsyncEnumerable
.
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 for significant contributions to the library's development.
- Joaquin Grech for advocating the transition from Newtonsoft.Json to System.Text.Json.
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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
This package has no dependencies.