Replies: 3 comments 1 reply
-
Could you provide a bit more context about what session ID is? Who generates it (client or server) and which part of the chain needs to use it? |
Beta Was this translation helpful? Give feedback.
-
I use the demo here where session_id from :
request body just like this(copy from langserve swagger):
The session_id is generated by the client and passed to me through above URL call. In order to perform subsequent operations on MySQL database or other actions, I need to retrieve this session_id within the chain of callbacks. However, I am currently unsure how to obtain this session_id or the entire configuration at the time of the request. Could you please provide guidance on how to access it? |
Beta Was this translation helpful? Give feedback.
-
Could you provide more context about the goal of the code? Why is session_id need to be accessed from a callback handler? Callbacks do not accept config right now in their methods, so you can't do it with standard callbacks, but you can create custom code (sharing a snippet below). Here's an example for chat with persistence -- I hope maybe it'll help and remove the need for using a callback handler? In this example, there's a configurable runnable that allows configuring # Server code
from typing import Optional, Any
from fastapi import FastAPI
from langchain_core.runnables import RunnablePassthrough, ConfigurableField
from langchain_core.runnables import RunnableSerializable, RunnableConfig
from langchain_core.runnables.utils import Output, Input
from langserve import add_routes
app = FastAPI()
class ConstantRunnable(RunnableSerializable):
"""Runnable that always returns a constant value"""
value: float
def invoke(self, input: Any, config: Optional[RunnableConfig] = None) -> float:
return self.value
customizable_runnable = ConstantRunnable(value=1.0).configurable_fields(
value=ConfigurableField(
id="value",
name="Value",
description="The value to return",
)
)
class RunnableWithCustomLogic(RunnablePassthrough):
def ainvoke(
self, input: Input, config: Optional[RunnableConfig] = None, **kwargs: Any
) -> Output:
from langchain_core.runnables.config import ensure_config
config = ensure_config(config)
raise ValueError(config["configurable"])
return super().invoke(input, config, **kwargs)
server_runnable = customizable_runnable | RunnableWithCustomLogic()
add_routes(
app,
server_runnable,
path="/add_one",
config_keys=["configurable"],
)
# Client code
response = await client.post(
"/add_one/invoke",
json={"input": 1, "config": {"configurable": {"value": 8}}},
) |
Beta Was this translation helpful? Give feedback.
-
add_route code
chain code
my question
how to get configurable.session_id in my chain code?
I spent a long time trying to understand langserve, but still don't know what to do. Please advise.
Beta Was this translation helpful? Give feedback.
All reactions