You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to use the per_req_config modifier to pass in a user_id as well as an openai_api_key to my chain
It looks like this:
classPerUserQuery(RunnableSerializable):
""" A custom runnable that returns a list of documents for the given user. The runnable is configurable by the user, and the search results are filtered by the user ID. """user_id: Optional[str]
openai_api_key: Optional[str]
vectorstore: Optional[VectorStore]
classConfig:
# Allow arbitrary types since VectorStore is an abstract interface# and not a pydantic modelarbitrary_types_allowed=Truedefget_rag_chain(self, query):
self.vectorstore=get_vectorstore(self.openai_api_key)
retriever=self.vectorstore.similarity_search(
query, k=5, filter={"user_id": {"$eq": self.user_id}}
)
defformat_docs(docs):
return"\n\n".join(doc.page_contentfordocindocs)
return {
"context": format_docs(retriever),
"question": RunnablePassthrough(),
}
definvoke(
self, input: str, config: Optional[RunnableConfig] =None, **kwargs
) ->List[Document]:
retriever=self.get_rag_chain(query=input)
returnretriever
This works with the chain:
per_user_retriever=PerUserQuery(
user_id=None, # Placeholder ID that will be replaced by the per_req_config_modifieropenai_api_key=None,
vectorstore=None,
).configurable_fields(
user_id=ConfigurableField(
id="user_id",
name="User ID",
description="The user ID to use for the retriever",
),
openai_api_key=ConfigurableField(
id="openai_api_key",
name="OpenAI API Key",
description="The OpenAI API key to use for the retriever",
),
vectorstore=ConfigurableField(
id="vectorstore",
name="Vectorstore",
description="The vectorstore to use for the retriever",
),
)
prompt=hub.pull("rlm/rag-prompt")
llm=get_llm(assume_api_key)
chain=per_user_retriever|prompt|llm|StrOutputParser()
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I am trying to use the per_req_config modifier to pass in a user_id as well as an openai_api_key to my chain
It looks like this:
This works with the chain:
However I am unable to extend the chain like so:
hoping to use it as:
Instead I get the error:
Any help with this would be appreciated
Beta Was this translation helpful? Give feedback.
All reactions