mirror of
https://github.com/hwchase17/langchain
synced 2024-11-18 09:25:54 +00:00
community[patch]: Invoke callback prior to yielding token (#17348)
**Description:** Invoke callback prior to yielding token in stream method for Ollama. **Issue:** [Callback for on_llm_new_token should be invoked before the token is yielded by the model #16913](https://github.com/langchain-ai/langchain/issues/16913) Co-authored-by: Robby <h0rv@users.noreply.github.com>
This commit is contained in:
parent
ab025507bc
commit
e135dc70c3
@ -313,12 +313,12 @@ class ChatOllama(BaseChatModel, _OllamaCommon):
|
|||||||
for stream_resp in self._create_chat_stream(messages, stop, **kwargs):
|
for stream_resp in self._create_chat_stream(messages, stop, **kwargs):
|
||||||
if stream_resp:
|
if stream_resp:
|
||||||
chunk = _chat_stream_response_to_chat_generation_chunk(stream_resp)
|
chunk = _chat_stream_response_to_chat_generation_chunk(stream_resp)
|
||||||
yield chunk
|
|
||||||
if run_manager:
|
if run_manager:
|
||||||
run_manager.on_llm_new_token(
|
run_manager.on_llm_new_token(
|
||||||
chunk.text,
|
chunk.text,
|
||||||
verbose=self.verbose,
|
verbose=self.verbose,
|
||||||
)
|
)
|
||||||
|
yield chunk
|
||||||
except OllamaEndpointNotFoundError:
|
except OllamaEndpointNotFoundError:
|
||||||
yield from self._legacy_stream(messages, stop, **kwargs)
|
yield from self._legacy_stream(messages, stop, **kwargs)
|
||||||
|
|
||||||
@ -332,12 +332,12 @@ class ChatOllama(BaseChatModel, _OllamaCommon):
|
|||||||
async for stream_resp in self._acreate_chat_stream(messages, stop, **kwargs):
|
async for stream_resp in self._acreate_chat_stream(messages, stop, **kwargs):
|
||||||
if stream_resp:
|
if stream_resp:
|
||||||
chunk = _chat_stream_response_to_chat_generation_chunk(stream_resp)
|
chunk = _chat_stream_response_to_chat_generation_chunk(stream_resp)
|
||||||
yield chunk
|
|
||||||
if run_manager:
|
if run_manager:
|
||||||
await run_manager.on_llm_new_token(
|
await run_manager.on_llm_new_token(
|
||||||
chunk.text,
|
chunk.text,
|
||||||
verbose=self.verbose,
|
verbose=self.verbose,
|
||||||
)
|
)
|
||||||
|
yield chunk
|
||||||
|
|
||||||
@deprecated("0.0.3", alternative="_stream")
|
@deprecated("0.0.3", alternative="_stream")
|
||||||
def _legacy_stream(
|
def _legacy_stream(
|
||||||
@ -351,9 +351,9 @@ class ChatOllama(BaseChatModel, _OllamaCommon):
|
|||||||
for stream_resp in self._create_generate_stream(prompt, stop, **kwargs):
|
for stream_resp in self._create_generate_stream(prompt, stop, **kwargs):
|
||||||
if stream_resp:
|
if stream_resp:
|
||||||
chunk = _stream_response_to_chat_generation_chunk(stream_resp)
|
chunk = _stream_response_to_chat_generation_chunk(stream_resp)
|
||||||
yield chunk
|
|
||||||
if run_manager:
|
if run_manager:
|
||||||
run_manager.on_llm_new_token(
|
run_manager.on_llm_new_token(
|
||||||
chunk.text,
|
chunk.text,
|
||||||
verbose=self.verbose,
|
verbose=self.verbose,
|
||||||
)
|
)
|
||||||
|
yield chunk
|
||||||
|
Loading…
Reference in New Issue
Block a user