|
import requests |
|
import os |
|
from dotenv import load_dotenv |
|
|
|
load_dotenv() |
|
|
|
|
|
GROQ_API_KEY = os.getenv("GROQ_API_KEY") |
|
if not GROQ_API_KEY: |
|
raise ValueError("GROQ_API_KEY is not set in the .env file") |
|
|
|
def intiate_convo(user_query, image_description, additional_text, model="mixtral-8x7b-32768"): |
|
|
|
messages = [ |
|
{ |
|
"role": "system", |
|
"content": """You are a AI Assistant for training. Given an image description, additional context, and a user query, respond with a PRECISE answer WITH THE HELP OF ADDITIONAL CONTEXT,be polite. |
|
IMPORTANT: When referring to the image, subtly acknowledge it by saying "as I see here" rather than explicitly mentioning "image" or "photo." |
|
Your tone should be natural and conversational.relevant to the query, using both the image description and the additional context as reference points. |
|
Provide only the answer in a well formated markdown string. |
|
""" |
|
}, |
|
{ |
|
"role": "user", |
|
"content": f"Image description: {image_description}. Additional context: {additional_text}. User query: {user_query}. Provide a detaile response like an ai assistant." |
|
} |
|
] |
|
|
|
|
|
response = requests.post( |
|
"https://api.groq.com/openai/v1/chat/completions", |
|
json={ |
|
"model": model, |
|
"messages": messages, |
|
"max_tokens": 32768, |
|
"stop": None, |
|
"stream": False |
|
}, |
|
headers={ |
|
"Authorization": f"Bearer {GROQ_API_KEY}", |
|
"Content-Type": "application/json" |
|
}, |
|
timeout=60 |
|
) |
|
|
|
|
|
if response.status_code == 200: |
|
result = response.json() |
|
answer = result["choices"][0]["message"]["content"] |
|
return answer |
|
else: |
|
return f"Error from LLM API: {response.status_code} - {response.text}" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|