File size: 1,604 Bytes
a46269a
 
 
 
 
 
 
411adbd
 
 
0dd3f9b
 
411adbd
 
 
0dd3f9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
411adbd
 
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
import os
from groq import Groq

groq_token = os.getenv("GROQ_TOKEN")

groq_client = Groq(
    api_key = groq_token
)



def GenerateAnswer(query, top_documents, prompt_model, timeout_seconds: int = 30):
    """
    Generates an answer using an AI model based on the top retrieved documents.
    """
    try:
        # Convert each document (if it's a list) into a string before joining
        documents = "\n\n".join([" ".join(doc) if isinstance(doc, list) else str(doc) for doc in top_documents["document"]])

        # Construct the prompt
        prompt = f"""
        You are an AI assistant tasked with answering a question based on the information provided in the given documents. Your response should be accurate, concise, and directly address the question using only the information from the documents. If the documents do not contain sufficient information to answer the question, state that clearly.

        Documents:
        {documents}

        Question: {query}
        Answer:
        """

        # Call Groq API (Llama 3.3-70B)
        completion = groq_client.chat.completions.create(
            model=prompt_model,
            messages=[{"role": "user", "content": prompt}],
            temperature=0.7,
            max_tokens=2048,
            top_p=1,
            timeout=timeout_seconds 
        )

        # Extract and print the response
        response_text = completion.choices[0].message.content
        print("\nGenerated Response:\n", response_text)

    except Exception as e:
        print(f"Error generating answer: {e}")
        return None

    return response_text