File size: 2,693 Bytes
03d82bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import requests
import os
from dotenv import load_dotenv

load_dotenv()

# Get the API key from environment variable
GROQ_API_KEY = "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"
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 detailed long answer with steps, ,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. Keep it detailed , engaging, and relevant to the query, using both the image description and the additional context as reference points."""
        },
        {
            "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)