Skip to content

Commit

Permalink
groq api test files
Browse files Browse the repository at this point in the history
  • Loading branch information
JAlcocerT committed Jan 4, 2025
1 parent c368ec5 commit 752a834
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 0 deletions.
1 change: 1 addition & 0 deletions Z_Tests/GroqAPI/.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GROQ_API_KEY=your-api-key-here
53 changes: 53 additions & 0 deletions Z_Tests/GroqAPI/groq_api_query.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
###QUERIES TO API AS PER THE GROQ API, DEFAULT###
from groq import Groq

# Load environment variables from .env file
from dotenv import load_dotenv
load_dotenv() # This loads all environment variables from the .env file

client = Groq()

# Adding a sample message in the `messages` list
messages = [
{"role": "user", "content": "Hello, which model are you and who is your creator? Whats your context window? and how many parameters you have"}
]

completion = client.chat.completions.create(
model="llama-3.2-90b-vision-preview",
#model="llama3-groq-70b-8192-tool-use-preview",
messages=messages, # Now the messages list contains one message
temperature=0.5,
max_tokens=1024,
top_p=0.65,
stream=True,
stop=None,
)

for chunk in completion:
print(chunk.choices[0].delta.content or "", end="")


#######
# https://console.groq.com/playground?model=llama3-groq-70b-8192-tool-use-preview
#####

# from groq import Groq

# # Load environment variables from .env file
# from dotenv import load_dotenv
# load_dotenv() # This loads all environment variables from the .env file


# client = Groq()
# completion = client.chat.completions.create(
# model="llama3-groq-70b-8192-tool-use-preview",
# messages=[],
# temperature=0.5,
# max_tokens=1024,
# top_p=0.65,
# stream=True,
# stop=None,
# )

# for chunk in completion:
# print(chunk.choices[0].delta.content or "", end="")
75 changes: 75 additions & 0 deletions Z_Tests/GroqAPI/groq_available_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#https://console.groq.com/docs/api-reference#models

import requests
from dotenv import load_dotenv
import os
from datetime import datetime

# Load environment variables from .env file
load_dotenv()

# Get the GROQ API key from environment variables
api_key = os.getenv("GROQ_API_KEY")

# API endpoint and headers
url = "https://api.groq.com/openai/v1/models"
headers = {
"Authorization": f"Bearer {api_key}"
}

# Make the API request
response = requests.get(url, headers=headers)

# Check if the response was successful
if response.status_code == 200:
data = response.json() # Parse the JSON response

# Sort models by 'created' date in descending order (newest first)
sorted_models = sorted(data['data'], key=lambda x: x['created'], reverse=True)

# Prepare the final output with relevant details
output = []
for model in sorted_models:
model_name = model['id']
created_date = datetime.utcfromtimestamp(model['created']).strftime('%Y-%m-%d %H:%M:%S')
context_window = model['context_window']

# Collect the model details
output.append(f"Model: {model_name}\nCreated: {created_date}\nContext Window: {context_window}\n")

# Print the final output
print("\n".join(output))
else:
print("Error fetching data:", response.status_code)



# import requests
# from dotenv import load_dotenv
# import os

# # Load environment variables from .env file
# load_dotenv()

# # Get the GROQ API key from environment variables
# api_key = os.getenv("GROQ_API_KEY")

# # API endpoint and headers
# url = "https://api.groq.com/openai/v1/models"
# headers = {
# "Authorization": f"Bearer {api_key}"
# }

# # Make the API request
# response = requests.get(url, headers=headers)

# # Check if the response was successful
# if response.status_code == 200:
# data = response.json() # Parse the JSON response
# model_names = [model['id'] for model in data['data']] # Extract model names

# # Print the model names
# for model_name in model_names:
# print(model_name)
# else:
# print("Error fetching data:", response.status_code)
36 changes: 36 additions & 0 deletions Z_Tests/GroqAPI/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
```sh
pip install groq==0.13.1
#https://www.youtube.com/watch?v=WaRjHz9ErO4
```

> https://console.groq.com/keys
---

## Venv Setup


```sh
python3 -m venv TestGroqAPI_venv

#Unix
source TestGroqAPI_venv/bin/activate
#.\TestGroqAPI_venv\Scripts\activate #Windows

pip install -r requirements.txt

source .env
#export GROQ_API_KEY="your-api-key-here"
#set GROQ_API_KEY=your-api-key-here
#$env:GROQ_API_KEY="your-api-key-here"
echo $GROQ_API_KEY

python3 groq_api_query.py
```

See all **[models available](https://jalcocert.github.io/JAlcocerT/how-to-use-lite-llm/) via Groq API** with:

```sh
curl https://api.groq.com/openai/v1/models \
-H "Authorization: Bearer $GROQ_API_KEY"
```
1 change: 1 addition & 0 deletions Z_Tests/GroqAPI/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pip install groq==0.13.1 #https://pypi.org/project/groq/

0 comments on commit 752a834

Please sign in to comment.