-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTalktoPDF.py
35 lines (28 loc) · 1.44 KB
/
TalktoPDF.py
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
from langchain_pinecone import PineconeVectorStore
from langchain_openai import ChatOpenAI
from langchain_openai import OpenAIEmbeddings
from langchain_core.prompts import ChatPromptTemplate
from langchain.chains.combine_documents import create_stuff_documents_chain
from pinecone import Pinecone
import os
OPENAI_KEY = os.getenv('OPENAI_API_KEY')
PINECONE_API_KEY = os.getenv('PINECONE_API_KEY')
# Initialize OpenAI Embeddings
# embeddings = OpenAIEmbeddings(openai_api_key=OPENAI_KEY, model = "text-embedding-ada-002")
embeddings = OpenAIEmbeddings(openai_api_key=OPENAI_KEY, model = "text-embedding-3-small")
# Initialize OpenAI Chat Model
llm = ChatOpenAI(temperature=0.6, openai_api_key=OPENAI_KEY, model="gpt-4o-mini")
# Reinitialize Pinecone and load the vector store
Pinecone = Pinecone(api_key=PINECONE_API_KEY)
index_name = 'langchain2'
vectorstore = PineconeVectorStore(index_name=index_name, embedding=embeddings)
# Query the vector store
#query = "What was the revenue from operations for quarter ending 30.09.2024?"
query = "What dividends per unitholder have been distributed for the last few quarters? "
results = vectorstore.similarity_search(query, k=20)
#print(results)
# Use the retrieved context to generate an answer
prompt = ChatPromptTemplate.from_template("Based on this {context}, provide the answer to {question}")
chain = create_stuff_documents_chain(llm, prompt)
result = chain.invoke({"context": results, "question": query})
print(result)