langchain/templates/rag-pinecone-rerank/rag_pinecone_rerank/chain.py

65 lines
2.1 KiB
Python
Raw Normal View History

2023-10-27 02:44:30 +00:00
import os
2023-10-26 22:29:10 +00:00
from langchain.chat_models import ChatOpenAI
from langchain.embeddings import OpenAIEmbeddings
2023-10-27 02:44:30 +00:00
from langchain.prompts import ChatPromptTemplate
2023-10-26 22:29:10 +00:00
from langchain.retrievers import ContextualCompressionRetriever
from langchain.retrievers.document_compressors import CohereRerank
2023-10-27 02:44:30 +00:00
from langchain.schema.output_parser import StrOutputParser
from langchain.schema.runnable import RunnableParallel, RunnablePassthrough
from langchain.vectorstores import Pinecone
2023-10-26 22:29:10 +00:00
if os.environ.get("PINECONE_API_KEY", None) is None:
raise Exception("Missing `PINECONE_API_KEY` environment variable.")
if os.environ.get("PINECONE_ENVIRONMENT", None) is None:
raise Exception("Missing `PINECONE_ENVIRONMENT` environment variable.")
PINECONE_INDEX_NAME = os.environ.get("PINECONE_INDEX", "langchain-test")
### Ingest code - you may need to run this the first time
# Load
# from langchain.document_loaders import WebBaseLoader
# loader = WebBaseLoader("https://lilianweng.github.io/posts/2023-06-23-agent/")
# data = loader.load()
# # Split
# from langchain.text_splitter import RecursiveCharacterTextSplitter
# text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=0)
# all_splits = text_splitter.split_documents(data)
# # Add to vectorDB
# vectorstore = Pinecone.from_documents(
# documents=all_splits, embedding=OpenAIEmbeddings(), index_name=PINECONE_INDEX_NAME
# )
# retriever = vectorstore.as_retriever()
vectorstore = Pinecone.from_existing_index(PINECONE_INDEX_NAME, OpenAIEmbeddings())
# Get k=10 docs
2023-10-27 02:44:30 +00:00
retriever = vectorstore.as_retriever(search_kwargs={"k": 10})
2023-10-26 22:29:10 +00:00
# Re-rank
compressor = CohereRerank()
compression_retriever = ContextualCompressionRetriever(
base_compressor=compressor, base_retriever=retriever
)
# RAG prompt
template = """Answer the question based only on the following context:
{context}
Question: {question}
"""
prompt = ChatPromptTemplate.from_template(template)
# RAG
model = ChatOpenAI()
chain = (
2023-10-27 02:44:30 +00:00
RunnableParallel(
{"context": compression_retriever, "question": RunnablePassthrough()}
)
2023-10-26 22:29:10 +00:00
| prompt
| model
| StrOutputParser()
)