Titan / utils /llm_ag.py
NEXAS's picture
Update utils/llm_ag.py
2b1d8e8 verified
import requests
import os
from dotenv import load_dotenv
load_dotenv()
# Get the API key from environment variable
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"):
# Prepare the message payload
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."
}
]
# Make the API request
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
)
# Process the response
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}"
# # Example usage
# # Define the inputs
# user_query = "Can you tell me more about the person in this description?"
# image_description = """The main subject of the image is a person with dark complexion, short black hair, and white-framed glasses, wearing a dark-colored shirt or jacket. They are looking directly at the camera with a subtle expression."""
# additional_text = """This individual is a software engineer specializing in AI development. They are known for their expertise in computer vision and enjoy photography as a hobby."""
# # Get the LLM response
# response = intiate_convo(user_query, image_description, additional_text)
# print(response)