The GroqAPI
class is designed to facilitate interaction with the Groq API for AI model requests. It provides methods to send chat completion requests and retrieve a list of available models, handling the necessary HTTP communication under the hood.
- SendChatCompletionRequest: Sends a POST request to generate chat completions based on provided messages and parameters.
- GetAvailableModels: Sends a GET request to retrieve a list of available AI models from the Groq API.
- AddMessage: Allows adding user or assistant messages to the request payload, supporting conversation context.
- CheckContextWindowUsage: Monitors the usage of the model's context window to ensure efficient token management.
To use the GroqAPI
class in your Xojo project:
- Download or clone this repository.
- Open your Xojo project.
- Drag the
GroqAPI
class file into your Xojo project navigator. - Get an API key from Groq.com at https://console.groq.com/keys
- Open your Xojo Project: Launch Xojo and open your existing project or create a new one.
- Import the GroqAPI Class: Drag the
GroqAPI
class file from your file explorer into the Xojo project navigator on the left side of the Xojo IDE. - Add to a Window: Drag the
GroqAPI
class from the project navigator onto a window in your project. This creates an instance ofGroqAPI
in that window.
- Select the GroqAPI Instance: Click on the
GroqAPI
instance in your window to select it. - Open the Inspector Panel: If the Inspector panel is not already open, you can open it by selecting
View -> Inspector
from the menu. - Set Properties: In the Inspector panel, you can set the following properties for the
GroqAPI
instance:- ApiKey: Enter your API key obtained from Groq.com.
- Model: Specify the AI model to use (e.g.,
llama-3.1-70b-versatile
). - Temperature: Set the temperature value to control response randomness.
- MaxTokens: Define the maximum number of tokens for the response.
- TopP: Adjust this to control response diversity.
- Memory: Enable or disable memory to retain previous messages.
// Create an instance of the GroqAPI class
Var groqAPI As New GroqAPI
// Set the API key for authentication
groqAPI.ApiKey = "YOUR_API_KEY"
// Set additional parameters if needed
groqAPI.Model = "llama-3.1-70b-versatile"
groqAPI.Temperature = 0.7
groqAPI.MaxTokens = 500
// Attach the event handlers to the events
AddHandler groqAPI.ResponseReceived, AddressOf HandleResponseReceived
AddHandler groqAPI.ErrorReceived, AddressOf HandleErrorReceived
// Send a chat completion request
groqAPI.AddMessage("user", "Hello, can you tell me about the weather today?")
groqAPI.SendChatCompletionRequest
// Retrieve available models
groqAPI.GetAvailableModels
// Define the event handlers
Sub HandleResponseReceived(sender As GroqAPI, ResponseType As String, AvailableData As JSONItem)
// Process the response
End Sub
Sub HandleErrorReceived(sender As GroqAPI, URL As String, HTTPStatus As Integer, content As String)
// Handle any errors
End Sub
- ApiKey: String - Your API key for authentication.
- Model: String - The AI model to use for requests.
- Temperature: Double - Controls randomness in responses.
- MaxTokens: Integer - Maximum number of tokens to generate in the response.
- TopP: Double - Controls diversity of the response.
- Memory: Boolean - Determines if previous messages should be retained.
- ResponseReceived: Triggered when a successful API response is received.
- ErrorReceived: Triggered when an error occurs during an API request.
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please submit a pull request or open an issue to discuss potential changes.