Spaces:
Sleeping
Sleeping
from langchain_openai import ChatOpenAI | |
from langchain_core.output_parsers import StrOutputParser | |
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder | |
from langchain.chains import create_history_aware_retriever, create_retrieval_chain | |
from langchain.chains.combine_documents import create_stuff_documents_chain | |
from typing import List | |
from typing_extensions import List, TypedDict | |
from langchain_core.documents import Document | |
import os | |
from pinecone_utilis import vectorstore | |
from dotenv import load_dotenv | |
load_dotenv() | |
OPENAI_API_KEY=os.getenv("OPENAI_API_KEY") | |
retriever = vectorstore.as_retriever(search_kwargs={"k": 2}) | |
output_parser = StrOutputParser() | |
contextualize_q_system_prompt = ( | |
"Given a chat history and the latest user question " | |
"which might reference context in the chat history, " | |
"formulate a standalone question which can be understood " | |
"without the chat history. Do NOT answer the question, " | |
"just reformulate it if needed and otherwise return it as is." | |
) | |
contextualize_q_prompt = ChatPromptTemplate.from_messages([ | |
("system", contextualize_q_system_prompt), | |
MessagesPlaceholder("chat_history"), | |
("human", "{input}"), | |
]) | |
qa_prompt = ChatPromptTemplate.from_messages([ | |
("system", "You are a helpful AI assistant. Use the following context to answer the user's question."), | |
("system", "Context: {context}"), | |
MessagesPlaceholder(variable_name="chat_history"), | |
("human", "{input}") | |
]) | |
class State(TypedDict): | |
messages: List[BaseMessage] | |