File size: 1,632 Bytes
6e39c64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from langchain_groq import ChatGroq
from langchain_core.messages import HumanMessage
from dotenv import load_dotenv
import os

class Generator:
    def __init__(self,temperature:float=0.14):
        groq_key = os.getenv("GROQ_API_KEY")
        print("GROQ KEY FOUND:", bool(groq_key))
        self.llm=ChatGroq(
            api_key='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',
            model="llama-3.3-70b-versatile",
            temperature=temperature
            )

    def build_prompt(self,query:str,context:str,chat_history:str):
        '''

        Build Prompt With Context + Question'''

        prompt=f'''

      You are intelligent Assistant

Use the document context and conversation history only to answer the user's question.



Rules:

1. Prefer the document context for document-related questions.

2. Use chat history for conversation-related questions like:

   - "what was my last question?"

   - "what did you answer before?"

3. If the answer is not available in either the context or the chat history, say:

   "I don't know based on the given context."

      Conversation History:

      {chat_history}



      context:

      {context}



      Current question:

      {query}



       If the answer is not in the context,say:

       "I Dont Know Based On The Given Context"





'''
        return prompt
    
    def generate(self,query:str,context:str,chat_history:str=""):
        '''Generate Answer Using Llm'''

        prompt=self.build_prompt(query,context,chat_history)

        response=self.llm.invoke(prompt)

        return response.content