Created
December 24, 2023 01:18
-
-
Save hwchase17/aeed44d7aea20c6b2a716dff32313cb3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from langchain.vectorstores import Pinecone | |
from langchain.embeddings.openai import OpenAIEmbeddings | |
import pinecone | |
# The environment should be the one specified next to the API key | |
# in your Pinecone console | |
pinecone.init( | |
api_key="...", environment="..." | |
) | |
index = pinecone.Index("test123") | |
embeddings = OpenAIEmbeddings() | |
vectorstore = Pinecone(index, embeddings.embed_query, "text") | |
vectorstore.add_texts(["i worked at kensho"], namespace="harrison") | |
vectorstore.add_texts(["i worked at facebook"], namespace="ankush") | |
# The pinecone kwarg for namespace can be used to separate documents | |
vectorstore.as_retriever(search_kwargs={"namespace": None}).get_relevant_documents( | |
"where did i work?" | |
) | |
vectorstore.as_retriever(search_kwargs={"namespace": "harrison"}).get_relevant_documents( | |
"where did i work?" | |
) | |
from operator import itemgetter | |
from typing import Optional, Mapping, Any | |
from langchain.chat_models import ChatOpenAI | |
from langchain.embeddings import OpenAIEmbeddings | |
from langchain.prompts import ChatPromptTemplate | |
from langchain_core.output_parsers import StrOutputParser | |
from langchain_core.runnables import RunnableLambda, RunnablePassthrough, RunnableBinding | |
from langchain_core.runnables import ConfigurableField | |
template = """Answer the question based only on the following context: | |
{context} | |
Question: {question} | |
""" | |
prompt = ChatPromptTemplate.from_template(template) | |
model = ChatOpenAI() | |
retriever = vectorstore.as_retriever() | |
# Here we mark the retriever as configurable | |
# All vectorstore retrievers have `search_kwargs` as a field | |
# This is just a dictionary, with vectorstore specific fields | |
configurable_retriever = retriever.configurable_fields( | |
search_kwargs=ConfigurableField( | |
id="search_kwargs", | |
name="Search Kwargs", | |
description="The search kwargs to use", | |
) | |
) | |
chain = ( | |
{"context": configurable_retriever, "question": RunnablePassthrough()} | |
| prompt | |
| model | |
| StrOutputParser() | |
) | |
chain.invoke( | |
"where did the user work?", | |
# We can now invoke the chain with configurable options | |
# search_kwargs is the id of the configurable field | |
# The value is the search kwargs to use for Pinecone | |
config={"configurable": {"search_kwargs": {"namespace": "harrison"}}} | |
) |
Useless itemgetter import
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A comment for line 21 saying that this'll respond with "Kensho"