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 using the code from the sample section of the RunnableWithMessageHistory. it works perfectly if I invoke the chain manually by calling in python code directly. It DOES NOT work when calling through the langserve API. hopefully I am not mistaken but i believe the dictionary parsing in langserve probably not able to recognize the dict properly. Attach is the error I am encountering also.
with the samples from the RunnableWithMessageHistory file
fromtypingimportListfromlangchain_openai.chat_modelsimportChatOpenAIfromlangchain_core.chat_historyimportBaseChatMessageHistoryfromlangchain_core.messagesimportBaseMessage, AIMessagefromlangchain_core.promptsimportChatPromptTemplate, MessagesPlaceholderfromlangchain_core.pydantic_v1importBaseModel, Fieldfromlangchain_core.runnables.historyimportRunnableWithMessageHistorydefexample_history(settings):
classInMemoryHistory(BaseChatMessageHistory, BaseModel):
messages: List[BaseMessage] =Field(default_factory=list)
defadd_messages(self, messages: List[BaseMessage]) ->None:
self.messages.extend(messages)
defclear(self) ->None:
self.messages= []
# Here we use a global variable to store the chat message history.# This will make it easier to inspect it to see the underlying results.store= {}
defget_by_session_id(session_id: str) ->BaseChatMessageHistory:
ifsession_idnotinstore:
store[session_id] =InMemoryHistory()
returnstore[session_id]
history=get_by_session_id("1")
history.add_message(AIMessage(content="hello"))
# print(store) # noqa: T201prompt=ChatPromptTemplate.from_messages([
("system", "You're an assistant who's good at {ability}"),
MessagesPlaceholder(variable_name="history"),
("human", "{question}"),
])
model=ChatOpenAI(temperature=0, openai_api_key=settings.OPENAI_API_KEY)
chain=prompt|modelchain_with_history=RunnableWithMessageHistory(
chain,
# Uses the get_by_session_id function defined in the example# above.get_by_session_id,
input_messages_key="question",
history_messages_key="history",
)
returnchain_with_history
However, it will fail if call through the api through langserve.
ne 65, in __call__
await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
File "/Users/harris/code/meow/.venv/lib/python3.10/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app
raise exc
File "/Users/harris/code/meow/.venv/lib/python3.10/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
await app(scope, receive, sender)
File "/Users/harris/code/meow/.venv/lib/python3.10/site-packages/starlette/routing.py", line 756, in __call__
await self.middleware_stack(scope, receive, send)
File "/Users/harris/code/meow/.venv/lib/python3.10/site-packages/starlette/routing.py", line 776, in app
await route.handle(scope, receive, send)
File "/Users/harris/code/meow/.venv/lib/python3.10/site-packages/starlette/routing.py", line 297, in handle
await self.app(scope, receive, send)
File "/Users/harris/code/meow/.venv/lib/python3.10/site-packages/starlette/routing.py", line 77, in app
await wrap_app_handling_exceptions(app, request)(scope, receive, send)
File "/Users/harris/code/meow/.venv/lib/python3.10/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app
raise exc
File "/Users/harris/code/meow/.venv/lib/python3.10/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
await app(scope, receive, sender)
File "/Users/harris/code/meow/.venv/lib/python3.10/site-packages/starlette/routing.py", line 72, in app
response = await func(request)
File "/Users/harris/code/meow/.venv/lib/python3.10/site-packages/fastapi/routing.py", line 278, in app
raw_response = await run_endpoint_function(
File "/Users/harris/code/meow/.venv/lib/python3.10/site-packages/fastapi/routing.py", line 191, in run_endpoint_function
return await dependant.call(**values)
File "/Users/harris/code/meow/.venv/lib/python3.10/site-packages/langserve/server.py", line 530, in invoke
return await api_handler.invoke(request)
File "/Users/harris/code/meow/.venv/lib/python3.10/site-packages/langserve/api_handler.py", line 833, in invoke
output = await invoke_coro
File "/Users/harris/code/meow/.venv/lib/python3.10/site-packages/langchain_core/runnables/base.py", line 4529, in ainvoke
return await self.bound.ainvoke(
File "/Users/harris/code/meow/.venv/lib/python3.10/site-packages/langchain_core/runnables/base.py", line 4529, in ainvoke
return await self.bound.ainvoke(
File "/Users/harris/code/meow/.venv/lib/python3.10/site-packages/langchain_core/runnables/base.py", line 2536, in ainvoke
input = await step.ainvoke(
File "/Users/harris/code/meow/.venv/lib/python3.10/site-packages/langchain_core/runnables/base.py", line 4529, in ainvoke
return await self.bound.ainvoke(
File "/Users/harris/code/meow/.venv/lib/python3.10/site-packages/langchain_core/runnables/base.py", line 2536, in ainvoke
input = await step.ainvoke(
File "/Users/harris/code/meow/.venv/lib/python3.10/site-packages/langchain_core/prompts/base.py", line 143, in ainvoke
return await self._acall_with_config(
File "/Users/harris/code/meow/.venv/lib/python3.10/site-packages/langchain_core/runnables/base.py", line 1677, in _acall_with_config
output = await coro
File "/Users/harris/code/meow/.venv/lib/python3.10/site-packages/langchain_core/prompts/base.py", line 117, in _aformat_prompt_with_error_handling
_inner_input = self._validate_input(inner_input)
File "/Users/harris/code/meow/.venv/lib/python3.10/site-packages/langchain_core/prompts/base.py", line 103, in _validate_input
raise KeyError(
KeyError: "Input to ChatPromptTemplate is missing variables {'ability'}. Expected: ['ability', 'history', 'question'] Received: ['question', 'history']"
The text was updated successfully, but these errors were encountered:
one thing I notice is the input in BasePromptTemplate::invoke would get parsed properly with ability while BasePromptTemplate::ainvoke would not include it.
looking for the solution!!! Anyone able to manage session id based chat history(each user specific) or pipeline state(langgraph) if we deploy through langserve
I am using the code from the sample section of the
RunnableWithMessageHistory
. it works perfectly if I invoke the chain manually by calling in python code directly. It DOES NOT work when calling through the langserve API. hopefully I am not mistaken but i believe the dictionary parsing in langserve probably not able to recognize the dict properly. Attach is the error I am encountering also.This works:
with the samples from the
RunnableWithMessageHistory
fileHowever, it will fail if call through the api through langserve.
This produces the following errors:
The text was updated successfully, but these errors were encountered: