Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: BaseChatModel modify chat message before passing to run_manager #19939

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 8 additions & 8 deletions libs/core/langchain_core/language_models/chat_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,12 @@ def stream(
generation: Optional[ChatGenerationChunk] = None
try:
for chunk in self._stream(messages, stop=stop, **kwargs):
run_manager.on_llm_new_token(
cast(str, chunk.message.content), chunk=chunk
)
if chunk.message.id is None:
chunk.message.id = f"run-{run_manager.run_id}"
chunk.message.response_metadata = _gen_info_and_msg_metadata(chunk)
run_manager.on_llm_new_token(
cast(str, chunk.message.content), chunk=chunk
)
yield chunk.message
if generation is None:
generation = chunk
Expand Down Expand Up @@ -293,12 +293,12 @@ async def astream(
stop=stop,
**kwargs,
):
await run_manager.on_llm_new_token(
cast(str, chunk.message.content), chunk=chunk
)
if chunk.message.id is None:
chunk.message.id = f"run-{run_manager.run_id}"
chunk.message.response_metadata = _gen_info_and_msg_metadata(chunk)
await run_manager.on_llm_new_token(
cast(str, chunk.message.content), chunk=chunk
)
yield chunk.message
if generation is None:
generation = chunk
Expand Down Expand Up @@ -610,13 +610,13 @@ def _generate_with_cache(
):
chunks: List[ChatGenerationChunk] = []
for chunk in self._stream(messages, stop=stop, **kwargs):
chunk.message.response_metadata = _gen_info_and_msg_metadata(chunk)
if run_manager:
if chunk.message.id is None:
chunk.message.id = f"run-{run_manager.run_id}"
run_manager.on_llm_new_token(
cast(str, chunk.message.content), chunk=chunk
)
chunk.message.response_metadata = _gen_info_and_msg_metadata(chunk)
chunks.append(chunk)
result = generate_from_stream(iter(chunks))
else:
Expand Down Expand Up @@ -691,13 +691,13 @@ async def _agenerate_with_cache(
):
chunks: List[ChatGenerationChunk] = []
async for chunk in self._astream(messages, stop=stop, **kwargs):
chunk.message.response_metadata = _gen_info_and_msg_metadata(chunk)
if run_manager:
if chunk.message.id is None:
chunk.message.id = f"run-{run_manager.run_id}"
await run_manager.on_llm_new_token(
cast(str, chunk.message.content), chunk=chunk
)
chunk.message.response_metadata = _gen_info_and_msg_metadata(chunk)
chunks.append(chunk)
result = generate_from_stream(iter(chunks))
else:
Expand Down