A library for easy integration with Groq API
Project description
PocketGroq
PocketGroq provides a simpler interface to interact with the Groq API, aiding in rapid development by abstracting complex API calls into simple functions.
Installation
- Clone the repository or download the source code:
git clone https://github.com/jgravelle/pocketgroq.git
cd pocketgroq
- Install the required packages using the
requirements.txt
file:
pip install -r requirements.txt
This will install the following dependencies:
- groq>=0.8.0
- python-dotenv>=0.19.1
- pytest>=7.3.1 (for development)
- pytest-asyncio>=0.21.0 (for development)
- requests>=2.32.3
Note: If you're not planning to contribute to the development of PocketGroq, you can omit the pytest packages by creating a new requirements file without those lines.
Basic Usage
Initializing GroqProvider
from pocketgroq import GroqProvider
# Initialize the GroqProvider
groq = GroqProvider()
Simple Text Generation
response = groq.generate("Tell me a joke about programming.")
print(response)
Tool Usage Example: String Reverser
PocketGroq allows you to define tools (functions) that the model can use during the conversation:
from typing import Dict
def reverse_string(input_string: str) -> Dict[str, str]:
""" Reverse the given string """
return {"reversed_string": input_string[::-1]}
# Define the tool
tools = [
{
"type": "function",
"function": {
"name": "reverse_string",
"description": "Reverse the given string",
"parameters": {
"type": "object",
"properties": {
"input_string": {
"type": "string",
"description": "The string to be reversed, e.g., 'hello'",
}
},
"required": ["input_string"],
},
"implementation": reverse_string
}
}
]
# Generate a response using the tool
response = groq.generate("Please reverse the string 'hello world'", tools=tools)
print("Response:", response)
Retrieving Available Models
You can retrieve all available models:
models = groq.get_available_models()
print("Available Models:", models)
Overriding the Default Model
Override the default model by passing the model
parameter to the generate
method:
# Use a specific model (ensure it's available in your Groq account)
selected_model = 'llama3-groq-8b-8192-tool-use-preview'
response = groq.generate("Please reverse the string 'hello world'", model=selected_model, tools=tools)
print("Response with Selected Model:", response)
Advanced Usage
Streaming Responses
For long responses, you can use streaming:
for chunk in groq.generate("Write a short story about AI", stream=True):
print(chunk, end='', flush=True)
Asynchronous Generation
For asynchronous operations:
import asyncio
async def main():
response = await groq.generate("Explain quantum computing", async_mode=True)
print(response)
asyncio.run(main())
JSON Mode
To get responses in JSON format:
response = groq.generate("List 3 programming languages and their main uses", json_mode=True)
print(response)
Configuration
PocketGroq uses environment variables for configuration. Set GROQ_API_KEY
in your environment or in a .env
file in your project root.
Error Handling
PocketGroq raises custom exceptions:
GroqAPIKeyMissingError
: Raised when the Groq API key is missing.GroqAPIError
: Raised when there's an error with the Groq API.
Handle these exceptions in your code for robust error management.
Contributing
Feel free to open issues or submit pull requests on the GitHub repository if you encounter any problems or have feature suggestions.
License
This project is licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pocketgroq-0.1.5.tar.gz
.
File metadata
- Download URL: pocketgroq-0.1.5.tar.gz
- Upload date:
- Size: 6.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6bf7683afa4a716bb02e3a8896f79ea61fbad64ec1ab1e4c88b1aa27ca7e2aa5 |
|
MD5 | ff46f9008cfd17cd85af6dc4e80de5bd |
|
BLAKE2b-256 | 9721363bbf95d087b2d22a2fe015a51697d2e733bfdf0efea18ecd98aa645084 |
File details
Details for the file pocketgroq-0.1.5-py3-none-any.whl
.
File metadata
- Download URL: pocketgroq-0.1.5-py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5bf7904b9f3be5639600bee4e571ec1e8a237c8f53f90449c1e4b71dd7c0c059 |
|
MD5 | de3e705e81974d930e951553cf17d095 |
|
BLAKE2b-256 | 6797dd81ed34b4b111263f68f6365c4f9efd468ab7eb2704154bac93982595ee |