Skip to content

Instantly share code, notes, and snippets.

@iht
Last active November 24, 2023 15:02
Show Gist options
  • Save iht/d5bedff9ee86ed5223cfa2ad06080ca0 to your computer and use it in GitHub Desktop.
Save iht/d5bedff9ee86ed5223cfa2ad06080ca0 to your computer and use it in GitHub Desktop.
Passing info from one chain to the next -- this does not work
from operator import itemgetter
from langchain.chat_models import ChatVertexAI
from langchain.prompts import PromptTemplate
from langchain.schema import StrOutputParser
from langchain.schema.vectorstore import VectorStoreRetriever
## Chain1
# The input is {"input": "Some question written by a user"}
some_model1 = ChatVertexAI(model_name="codechat-bison", max_output_tokens=2048)
some_prompt1 = PromptTemplate(input_variables=["question", "context"], prompt="...")
question = {"question": itemgetter('input')} # we rename to question
# Get just one doc
retriever: VectorStoreRetriever = vector_store.as_retriever(search_kwargs={'k': 1})
docs = {"context": RunnableLamnda(lambda x: retriever.get_documents(x["input"])[0])}
output_chain1 = question | docs | some_prompt | some_model1 | StrOutputParser()
## Chain 2
some_model2 = ChatVertexAI(max_output_tokens=2048)
some_prompt2 = PromptTemplate(input_variables=["question", "context", "output"], prompt="...")
# ERROR: the input does not the "input" key anymore, and both question and docs will fail
chain2 = question | docs | {"output": output_chain1} | some_prompt2 | some_model2 | StrOutputParser()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment