-
Checked other resources
Commit to Help
Example Codefrom langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnableConfig
from langserve import add_routes
from fastapi import FastAPI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnableLambda
from langchain_openai import ChatOpenAI
import json
llm = ChatOpenAI()
def parse_or_fix(text: str, config: RunnableConfig):
fixing_chain = (
ChatPromptTemplate.from_template(
"Fix the following text:\n\n\n{input}\n\nError: {error}"
" Don't narrate, just respond with the fixed data."
)
| llm
| StrOutputParser()
)
for _ in range(3):
try:
return json.loads(text)
except Exception as e:
text = fixing_chain.invoke({"input": text, "error": e}, config)
return "Failed to parse"
app = FastAPI()
add_routes(app, RunnableLambda(parse_or_fix) , path='/parseorfixchain') DescriptionI am trying to create a web server with langserve I am exposing a chain as shown using add_routes method. I want to pass a runnable config and want to do the equivalent of RunnableLambda(parse_or_fix).invoke("{foo: bar}", {"tags": ["my-tag"], "configurable": {"session_id":"" , "some_nore_config": ""}} I want the route to show correct documentation on the docs page in langserve and also to pass this config inside my custom function. System InfoSystem Information
Package Information
Optional packages not installed
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 13 replies
-
Hey there, @tyadav7! I'm here to help you with any bugs, questions, or contributions you might have. Let's tackle this together while we wait for a human maintainer. To pass a
By following these steps, you can pass a |
Beta Was this translation helpful? Give feedback.
-
@dosu { here is a snippet of the data that I have received inside my parse_or_fix function |
Beta Was this translation helpful? Give feedback.
@dosu
After a lot of debugging I have finally figure out a way to work with it
So the add_routes takes an additional parameter called CONFIG_KEYS when passed it maps the body to the schema provided.
But this still wont map the additional configurable fields needed to be set for the runnable function.
So I created a separate class which extends the RunnableLambda and overrides the method config_specs this override is needed so that the configurable parameters could be map so that FastAPI could then resolve this schema later when this lambda is called.