web-app-temp / web_app /backend /app /answers_model.py
leothesouthafrican's picture
hf fix new branch
e54b26e
import pinecone
from dotenv import load_dotenv, find_dotenv
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.memory import ConversationBufferMemory
from langchain.vectorstores import Pinecone
from langchain.chat_models import ChatOpenAI
from langchain.chains import RetrievalQA
from langchain import PromptTemplate
# Load environment variables
load_dotenv(find_dotenv())
# Setting up embeddings
embedder = OpenAIEmbeddings()
# Setting up the vectorstore
pinecone.init(environment="us-west4-gcp")
index = pinecone.Index('seai-website')
#Set up the retriever
vectorstore = Pinecone(index, embedder.embed_query, 'text')
# Setting up the llm
llm = ChatOpenAI(temperature=0.0)
# Setting up the prompt template
template = """
Use the following context (delimited by <ctx></ctx>) and the chat history (delimited by <hs></hs>) to answer the question:
------
<ctx>
{context}
</ctx>
------
<hs>
{history}
</hs>
------
{question}
Answer:
"""
prompt = PromptTemplate(
input_variables=["history", "context", "question"],
template=template,
)
# Setting up the memory
memory = ConversationBufferMemory(memory_key="history", input_key="question")
# Setting up the QA chain
qa = RetrievalQA.from_chain_type(
llm=ChatOpenAI(),
chain_type='stuff',
retriever=vectorstore.as_retriever(),
verbose=True,
chain_type_kwargs={
"verbose": True,
"prompt": prompt,
"memory": memory
}
)
def generate_question_response(query: str) -> str:
return qa.run(query)
if __name__ == "__main__":
# Sample query
test_query = "What is the capital of France?"
# Generate a response using the model
response = generate_question_response(test_query)
# Print the query and its response
print(f"Query: {test_query}")
print(f"Response: {response}")