|
|
import os |
|
|
from pathlib import Path |
|
|
from groq import Groq |
|
|
from google import genai |
|
|
from dotenv import load_dotenv |
|
|
|
|
|
def test_groq_connection(): |
|
|
""" |
|
|
Loads the Groq API key from a .env file and tests the endpoint |
|
|
with a simple streaming query. |
|
|
""" |
|
|
|
|
|
|
|
|
|
|
|
script_dir = Path(__file__).parent |
|
|
project_root = script_dir.parent |
|
|
dotenv_path = project_root / "configs" / ".env" |
|
|
load_dotenv(dotenv_path=dotenv_path) |
|
|
|
|
|
api_key = os.getenv("GROQ_API_KEY") |
|
|
if not api_key: |
|
|
print(f"π΄ Error: GROQ_API_KEY not found.") |
|
|
print(f"Please ensure it is set in your {dotenv_path} file.") |
|
|
return |
|
|
|
|
|
print("β
Groq API key loaded successfully.") |
|
|
|
|
|
try: |
|
|
|
|
|
client = Groq() |
|
|
print("π€ Initialized Groq client. Sending a test query...") |
|
|
|
|
|
|
|
|
completion = client.chat.completions.create( |
|
|
model="llama-3.1-8b-instant", |
|
|
messages=[ |
|
|
{ |
|
|
"role": "user", |
|
|
"content": "Explain why low-latency is important for LLMs in one short sentence." |
|
|
} |
|
|
], |
|
|
temperature=0.7, |
|
|
max_tokens=1024, |
|
|
top_p=1, |
|
|
stream=True, |
|
|
stop=None, |
|
|
) |
|
|
|
|
|
|
|
|
print("\nπ Groq API Response:") |
|
|
print("-" * 20) |
|
|
for chunk in completion: |
|
|
print(chunk.choices[0].delta.content or "", end="") |
|
|
print("\n" + "-" * 20) |
|
|
print("\nβ
Test successful! The Groq endpoint is working.") |
|
|
|
|
|
except Exception as e: |
|
|
print(f"π΄ An error occurred during the Groq API call: {e}") |
|
|
|
|
|
def test_gemini_connection(): |
|
|
""" |
|
|
Loads the Google Gemini API key from a .env file and tests the endpoint |
|
|
using the genai.Client pattern. |
|
|
""" |
|
|
|
|
|
script_dir = Path(__file__).parent |
|
|
project_root = script_dir.parent |
|
|
dotenv_path = project_root / "configs" / ".env" |
|
|
load_dotenv(dotenv_path=dotenv_path) |
|
|
|
|
|
api_key = os.getenv("GOOGLE_API_KEY") |
|
|
if not api_key: |
|
|
print(f"π΄ Error: GOOGLE_API_KEY not found.") |
|
|
print(f"Please ensure it is set in your {dotenv_path} file.") |
|
|
return |
|
|
|
|
|
print("β
Google API key loaded successfully.") |
|
|
|
|
|
try: |
|
|
|
|
|
client = genai.Client(api_key=api_key) |
|
|
print("π€ Initialized Gemini client. Sending a test query...") |
|
|
|
|
|
|
|
|
response = client.models.generate_content( |
|
|
model="gemini-2.5-flash", |
|
|
contents="Explain the importance of APIs in one short sentence." |
|
|
) |
|
|
|
|
|
|
|
|
print("\nπ Gemini API Response:") |
|
|
print("-" * 20) |
|
|
print(response.text) |
|
|
print("-" * 20) |
|
|
print("\nβ
Test successful! The Gemini endpoint is working.") |
|
|
|
|
|
except Exception as e: |
|
|
print(f"π΄ An error occurred during the Gemini API call: {e}") |
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
print("--- Running Groq API Connection Test ---") |
|
|
test_groq_connection() |
|
|
print("\n" + "="*40 + "\n") |
|
|
print("--- Running Gemini API Connection Test ---") |
|
|
test_gemini_connection() |