File size: 2,137 Bytes
7a6d0f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from dotenv import load_dotenv
from langchain_pinecone import PineconeVectorStore
from pinecone import Pinecone
from langchain_together import TogetherEmbeddings
from langchain_mistralai import ChatMistralAI
import cohere
from langchain.retrievers.contextual_compression import ContextualCompressionRetriever
from langchain_cohere import CohereRerank


load_dotenv()
PINECONE_API_KEY=os.getenv("PINECONE_API_KEY")
MISTRAL_API_KEY=os.getenv("MISTRAL_API_KEY")
TOGETHER_API=os.getenv("TOGETHER_API")
COHERE_API_KEY=os.getenv("COHERE_API_KEY")

embeddings = TogetherEmbeddings(
    model="togethercomputer/m2-bert-80M-8k-retrieval",
    api_key=TOGETHER_API
)

model=ChatMistralAI(
    model="mistral-large-latest",
    api_key=MISTRAL_API_KEY
)
pc = Pinecone(api_key=PINECONE_API_KEY)

index_name = "together" 

index = pc.Index(index_name)

vector_store = PineconeVectorStore(index=index, embedding=embeddings)
retriever = vector_store.as_retriever()

compressor = CohereRerank(model="rerank-v3.5")
compression_retriever = ContextualCompressionRetriever(
    base_compressor=compressor, base_retriever=retriever
)

def retrieve(query):
    compressed_docs = compression_retriever.invoke(query)
    
    return compressed_docs[0]

def llm(query, history):
    query=model.invoke(f"You are a helpful AI assistant. Translate the following query to urdu if the query is in urdu then return the same query. Query: {query}. Remember just translate the query and nothing else.").content
    results=retrieve(query)
    prompt=f""""
    You are a helpful AI Assistant and your job is to help user find relevant shayari.
    The query is {query}
    The matching result after semantic search is {results.page_content}. You may exclude interpretation given in the results. Just respond from the context.
    """

    try:
        response=model.invoke(prompt).content
        history = history or []
        history.append(("You", query))
        history.append(("Assistant", results.page_content))
        return history, history
    except Exception as e:
        return history, history + [("Bot", f"Error in API request: {str(e)}")]