Skip to content

Instantly share code, notes, and snippets.

@hwchase17
Created December 24, 2023 01:18
Show Gist options
  • Select an option

  • Save hwchase17/aeed44d7aea20c6b2a716dff32313cb3 to your computer and use it in GitHub Desktop.

Select an option

Save hwchase17/aeed44d7aea20c6b2a716dff32313cb3 to your computer and use it in GitHub Desktop.
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"}}}
)
@CyrusNuevoDia
Copy link
Copy Markdown

CyrusNuevoDia commented Dec 24, 2023

Maybe a comment on line 69 saying you can modify namespace to equal the user_id of your application?

@CyrusNuevoDia
Copy link
Copy Markdown

A comment for line 18 saying that this won't retrieve any of the texts

@CyrusNuevoDia
Copy link
Copy Markdown

A comment for line 21 saying that this'll respond with "Kensho"

@CyrusNuevoDia
Copy link
Copy Markdown

CyrusNuevoDia commented Dec 24, 2023

Useless itemgetter import

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment