Update utils/llm_ag.py
Browse files- utils/llm_ag.py +62 -60
utils/llm_ag.py
CHANGED
@@ -1,60 +1,62 @@
|
|
1 |
-
import requests
|
2 |
-
import os
|
3 |
-
from dotenv import load_dotenv
|
4 |
-
|
5 |
-
load_dotenv()
|
6 |
-
|
7 |
-
# Get the API key from environment variable
|
8 |
-
GROQ_API_KEY = "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"
|
9 |
-
if not GROQ_API_KEY:
|
10 |
-
raise ValueError("GROQ_API_KEY is not set in the .env file")
|
11 |
-
|
12 |
-
def intiate_convo(user_query, image_description, additional_text, model="mixtral-8x7b-32768"):
|
13 |
-
# Prepare the message payload
|
14 |
-
messages = [
|
15 |
-
{
|
16 |
-
"role": "system",
|
17 |
-
"content": """You are a AI Assistant for training. Given an image description, additional context, and a user query, respond with a
|
18 |
-
IMPORTANT: When referring to the image, subtly acknowledge it by saying "as I see here" rather than explicitly mentioning "image" or "photo."
|
19 |
-
Your tone should be natural and conversational.
|
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 |
-
return
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
#
|
55 |
-
#
|
56 |
-
#
|
57 |
-
|
58 |
-
#
|
59 |
-
|
60 |
-
#
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import os
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
|
5 |
+
load_dotenv()
|
6 |
+
|
7 |
+
# Get the API key from environment variable
|
8 |
+
GROQ_API_KEY = "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"
|
9 |
+
if not GROQ_API_KEY:
|
10 |
+
raise ValueError("GROQ_API_KEY is not set in the .env file")
|
11 |
+
|
12 |
+
def intiate_convo(user_query, image_description, additional_text, model="mixtral-8x7b-32768"):
|
13 |
+
# Prepare the message payload
|
14 |
+
messages = [
|
15 |
+
{
|
16 |
+
"role": "system",
|
17 |
+
"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.
|
18 |
+
IMPORTANT: When referring to the image, subtly acknowledge it by saying "as I see here" rather than explicitly mentioning "image" or "photo."
|
19 |
+
Your tone should be natural and conversational.relevant to the query, using both the image description and the additional context as reference points.
|
20 |
+
Provide only the answer.
|
21 |
+
"""
|
22 |
+
},
|
23 |
+
{
|
24 |
+
"role": "user",
|
25 |
+
"content": f"Image description: {image_description}. Additional context: {additional_text}. User query: {user_query}. Provide a detaile response like an ai assistant."
|
26 |
+
}
|
27 |
+
]
|
28 |
+
|
29 |
+
# Make the API request
|
30 |
+
response = requests.post(
|
31 |
+
"https://api.groq.com/openai/v1/chat/completions",
|
32 |
+
json={
|
33 |
+
"model": model,
|
34 |
+
"messages": messages,
|
35 |
+
"max_tokens": 32768,
|
36 |
+
"stop": None,
|
37 |
+
"stream": False
|
38 |
+
},
|
39 |
+
headers={
|
40 |
+
"Authorization": f"Bearer {GROQ_API_KEY}",
|
41 |
+
"Content-Type": "application/json"
|
42 |
+
},
|
43 |
+
timeout=60
|
44 |
+
)
|
45 |
+
|
46 |
+
# Process the response
|
47 |
+
if response.status_code == 200:
|
48 |
+
result = response.json()
|
49 |
+
answer = result["choices"][0]["message"]["content"]
|
50 |
+
return answer
|
51 |
+
else:
|
52 |
+
return f"Error from LLM API: {response.status_code} - {response.text}"
|
53 |
+
|
54 |
+
# # Example usage
|
55 |
+
# # Define the inputs
|
56 |
+
# user_query = "Can you tell me more about the person in this description?"
|
57 |
+
# 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."""
|
58 |
+
# 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."""
|
59 |
+
|
60 |
+
# # Get the LLM response
|
61 |
+
# response = intiate_convo(user_query, image_description, additional_text)
|
62 |
+
# print(response)
|