File size: 2,350 Bytes
b0ccf04
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# Required modules
import os
from pinecone import Pinecone
from transformers import AutoModel
from langchain_core.prompts import ChatPromptTemplate
from langchain_groq import ChatGroq
from dotenv import load_dotenv
load_dotenv()


# Initialize clients, indexes, models etc.
pc_client = Pinecone(api_key=os.getenv("PINECONE_API_KEY"))
pc_index = pc_client.Index(os.getenv("PINECONE_INDEX"))
embedding_model = AutoModel.from_pretrained('jinaai/jina-embeddings-v2-base-en', trust_remote_code=True)
groq_llm=ChatGroq(
    groq_api_key=os.getenv("GROQ_API_KEY"),
    model_name="Llama3-8b-8192"
)


#context retrivel
def retrive_context(user_query:str) -> str:
    """Retrives the context for asked query from vector database



    Args:

        user_query (str): Questions asked by user to bot



    Returns:

        context (str): Question's context

    """
    
    context = ""
    try:
        embedded_query = embedding_model.encode(user_query).tolist()
    except Exception as e:
        return 500
    
    try:
        res = pc_index.query(
            vector=embedded_query,
            top_k=5,
            include_values=True,
            include_metadata = True
        )
    except Exception as e:
        return 500
    
    for match in res['matches']:
        context = context + match['metadata']['text'] + " "
        
    print(context)
    return context


# Prompt Engineering for LLM
prompt = ChatPromptTemplate.from_template(
    """

    Hello! As a RAG agent for Biskane, your task is to answer the user's question using the provided context. Please keep your responses brief and straightforward.



    <context>

    {context}

    <context>

    Question: {query}

    """
)


# Response generator
def generate_response(query:str, context:str) -> str:
    """Generates the response for asked question from given context



    Args:

        query (str): Query asked by user to bot

        context (str): Context, retrived from vector database



    Returns:

        answer (str): Generated response

    """
    try: 
        chain = prompt | groq_llm
        llm_response = chain.invoke({
            "context": context,
            "query": query
        })
        return llm_response.content
    except Exception as e:
        return 500