[x] I added a very descriptive title to this issue.
[X] I searched the LangChain documentation with the integrated search.
[X] I used the GitHub search to find a similar question and didn't find it.
[X] I am sure that this is a bug in LangChain rather than my code.
[X] The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).
Example Code
def tools_chain(model_output):
tool_map = {tool.name: tool for tool in tools}
namekey = model_output.get("name")
if not namekey or namekey not in tool_map:
return model_output
chosen_tool = tool_map[namekey]
return itemgetter("arguments") | chosen_tool
class OpenAIChainFactory:
@classmethod
def get_chat_chain(
cls,
model: str,
sysmsg: str,
sessionid: str,
indexname: str,
history_token_limit=4096,
):
"""
Returns a chat chain runnable that can be used for conversational AI chat interactions.
Args:
model (str): The name of the OpenAI model to use for chat.
sysmsg (str): The system message to provide context for the conversation.
sessionid (str): The session ID for the chat conversation.
indexname (str): The name of the index to use for retrieving relevant documents.
history_token_limit (int, optional): The maximum number of tokens to store in the chat history. Defaults to 4096.
Returns:
RunnableWithMessageHistory: A chat chain runnable with message history.
"""
model = model or "gpt-4-turbo-preview"
prompt = get_conversation_with_context_prompt(sysmsg)
retriever = get_pgvector_retriever(indexname=indexname)
_tools_prompt = get_tools_prompt(tools)
_tools_chain = (
_tools_prompt
| ChatOpenAI(model=model, temperature=0.3)
| JsonOutputParser()
| tools_chain
| StdOutputRunnable()
)
llmchain = (
RunnableParallel(
{
"tools_output": _tools_chain,
"context": CONDENSE_QUESTION_PROMPT
| ChatOpenAI(model=model, temperature=0.3)
| StrOutputParser()
| retriever
| RunnableUtils.docs_to_string(),
"question": itemgetter("question"),
"history": itemgetter("history"),
}
)
| prompt
| ChatOpenAI(model=model, temperature=0.3)
)
return RunnableWithMessageHistory(
llmchain,
lambda session_id: RedisChatMessageHistory(
sessionid,
url=os.environ["REDIS_URL"],
max_token_limit=history_token_limit,
),
input_messages_key="question",
history_messages_key="history",
verbose=True,
)
async def test_chat_chain():
chain = OpenAIChainFactory.get_chat_chain(
"gpt-3.5-turbo", "You are an interesting teacher,", "test", "test_index"
)
fresp = await chain.ainvoke(
input={"question": "When is 1+1 equal to 3"},
config={"configurable": {"session_id": "test"}},
)
print(fresp)
if __name__ == "__main__":
from langchain.globals import set_verbose
set_verbose(True)
asyncio.run(test_chat_chain())
Error Message and Stack Trace (if applicable)
Traceback (most recent call last):
File "/Volumes/ExtDISK/github/teamsgpt/teamsgpt/teamsbot.py", line 138, in on_message_activity
await self.on_openai_chat_stream(turn_context)
File "/Volumes/ExtDISK/github/teamsgpt/teamsgpt/openai_handler.py", line 57, in on_openai_chat_stream
async for r in lchain.astream(
File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 4583, in astream
async for item in self.bound.astream(
File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 4583, in astream
async for item in self.bound.astream(
File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 2769, in astream
async for chunk in self.atransform(input_aiter(), config, **kwargs):
File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 2752, in atransform
async for chunk in self._atransform_stream_with_config(
File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 1849, in _atransform_stream_with_config
chunk: Output = await asyncio.create_task( # type: ignore[call-arg]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 2722, in _atransform
async for output in final_pipeline:
File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 4619, in atransform
async for item in self.bound.atransform(
File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 2752, in atransform
async for chunk in self._atransform_stream_with_config(
File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 1849, in _atransform_stream_with_config
chunk: Output = await asyncio.create_task( # type: ignore[call-arg]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 2722, in _atransform
async for output in final_pipeline:
File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 1182, in atransform
async for ichunk in input:
File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 1182, in atransform
async for ichunk in input:
File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 3184, in atransform
async for chunk in self._atransform_stream_with_config(
File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 1849, in _atransform_stream_with_config
chunk: Output = await asyncio.create_task( # type: ignore[call-arg]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 3171, in _atransform
chunk = AddableDict({step_name: task.result()})
^^^^^^^^^^^^^
File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 3154, in get_next_chunk
return await py_anext(generator)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 2752, in atransform
async for chunk in self._atransform_stream_with_config(
File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 1849, in _atransform_stream_with_config
chunk: Output = await asyncio.create_task( # type: ignore[call-arg]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 2722, in _atransform
async for output in final_pipeline:
File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 1182, in atransform
async for ichunk in input:
File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 4049, in atransform
async for output in self._atransform_stream_with_config(
File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 1849, in _atransform_stream_with_config
chunk: Output = await asyncio.create_task( # type: ignore[call-arg]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/wangjuntao/Library/Caches/pypoetry/virtualenvs/teamsgpt-DQ8gji6d-py3.11/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 4019, in _atransform
cast(Callable, afunc), cast(Input, final), config, run_manager, **kwargs
Description
I tried to use a chain to implement the agent, but I got an error, which I initially judged to be an execution error of _tools_chain, which is the function code added later.
But this error doesn't always occur, sometimes it works fine
Checked other resources
Example Code
Error Message and Stack Trace (if applicable)
Description
I tried to use a chain to implement the agent, but I got an error, which I initially judged to be an execution error of _tools_chain, which is the function code added later.
But this error doesn't always occur, sometimes it works fine
System Info