core[patch]: Pass sync run manager for sync stream fallback in astream (#19280)

This PR patches the fallback in chat models and language models to pass
in the appropriate version of the run manager (sync vs. async)
This commit is contained in:
Eugene Yurtsev
2024-03-19 12:32:33 -04:00
committed by GitHub
parent d314acb2d5
commit 4b3dd34544
4 changed files with 77 additions and 22 deletions

View File

@@ -312,6 +312,68 @@ async def test_event_stream_with_lambdas_from_lambda() -> None:
]
async def test_astream_events_from_model() -> None:
"""Test the output of a model."""
infinite_cycle = cycle(
[AIMessage(content="hello world!"), AIMessage(content="goodbye world!")]
)
# When streaming GenericFakeChatModel breaks AIMessage into chunks based on spaces
model = (
GenericFakeChatModel(messages=infinite_cycle)
.with_config(
{
"metadata": {"a": "b"},
"tags": ["my_model"],
"run_name": "my_model",
}
)
.bind(stop="<stop_token>")
)
events = await _collect_events(model.astream_events("hello", version="v1"))
assert events == [
{
"data": {"input": "hello"},
"event": "on_chat_model_start",
"metadata": {"a": "b"},
"name": "my_model",
"run_id": "",
"tags": ["my_model"],
},
{
"data": {"chunk": AIMessageChunk(content="hello")},
"event": "on_chat_model_stream",
"metadata": {"a": "b"},
"name": "my_model",
"run_id": "",
"tags": ["my_model"],
},
{
"data": {"chunk": AIMessageChunk(content=" ")},
"event": "on_chat_model_stream",
"metadata": {"a": "b"},
"name": "my_model",
"run_id": "",
"tags": ["my_model"],
},
{
"data": {"chunk": AIMessageChunk(content="world!")},
"event": "on_chat_model_stream",
"metadata": {"a": "b"},
"name": "my_model",
"run_id": "",
"tags": ["my_model"],
},
{
"data": {"output": AIMessageChunk(content="hello world!")},
"event": "on_chat_model_end",
"metadata": {"a": "b"},
"name": "my_model",
"run_id": "",
"tags": ["my_model"],
},
]
async def test_event_stream_with_simple_chain() -> None:
"""Test as event stream."""
template = ChatPromptTemplate.from_messages(