multidoc_chat / groq_api.py
akashshahade's picture
Upload 7 files
6363d82 verified
raw
history blame contribute delete
915 Bytes
import os
import requests
from dotenv import load_dotenv
load_dotenv()
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
MODEL_NAME = "llama3-8b-chat" # Update model as needed
API_URL = "https://api.groq.com/v1/chat/completions"
def ask_groq(user_query, context_text):
"""Send user query and extracted text to Groq API"""
headers = {"Authorization": f"Bearer {GROQ_API_KEY}", "Content-Type": "application/json"}
payload = {
"model": MODEL_NAME,
"messages": [
{"role": "system", "content": "You are an AI that answers questions based on the given document."},
{"role": "user", "content": f"Document: {context_text[:4000]}\n\nQuestion: {user_query}"}
]
}
response = requests.post(API_URL, json=payload, headers=headers)
return response.json().get("choices", [{}])[0].get("message", {}).get("content", "No response")