From 8f84d466d600e25298187c410e66110ce20f97cc Mon Sep 17 00:00:00 2001 From: elhambadri2411 Date: Mon, 28 Oct 2024 15:14:48 -0400 Subject: [PATCH 1/5] bug: chatollama now streams chunks even when tools are bound --- .../ollama/langchain_ollama/chat_models.py | 31 ++++++++++++------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/libs/partners/ollama/langchain_ollama/chat_models.py b/libs/partners/ollama/langchain_ollama/chat_models.py index f0db82b835ff1..98eec782d8084 100644 --- a/libs/partners/ollama/langchain_ollama/chat_models.py +++ b/libs/partners/ollama/langchain_ollama/chat_models.py @@ -401,7 +401,8 @@ def _convert_messages_to_ollama_messages( role = "tool" tool_call_id = message.tool_call_id else: - raise ValueError("Received unsupported message type for Ollama.") + raise ValueError( + "Received unsupported message type for Ollama.") content = "" images = [] @@ -514,10 +515,10 @@ def _create_chat_stream( params["options"]["stop"] = stop if "tools" in kwargs: - yield self._client.chat( + yield from self._client.chat( model=params["model"], messages=ollama_messages, - stream=False, + stream=True, options=Options(**params["options"]), keep_alive=params["keep_alive"], format=params["format"], @@ -558,7 +559,8 @@ def _chat_stream_with_aggregation( tool_calls=_get_tool_calls_from_response(stream_resp), ), generation_info=( - dict(stream_resp) if stream_resp.get("done") is True else None + dict(stream_resp) if stream_resp.get( + "done") is True else None ), ) if final_chunk is None: @@ -601,7 +603,8 @@ async def _achat_stream_with_aggregation( tool_calls=_get_tool_calls_from_response(stream_resp), ), generation_info=( - dict(stream_resp) if stream_resp.get("done") is True else None + dict(stream_resp) if stream_resp.get( + "done") is True else None ), ) if final_chunk is None: @@ -648,8 +651,10 @@ def _generate( chat_generation = ChatGeneration( message=AIMessage( content=final_chunk.text, - usage_metadata=cast(AIMessageChunk, final_chunk.message).usage_metadata, - tool_calls=cast(AIMessageChunk, final_chunk.message).tool_calls, + usage_metadata=cast( + AIMessageChunk, final_chunk.message).usage_metadata, + tool_calls=cast( + AIMessageChunk, final_chunk.message).tool_calls, ), generation_info=generation_info, ) @@ -678,7 +683,8 @@ def _stream( tool_calls=_get_tool_calls_from_response(stream_resp), ), generation_info=( - dict(stream_resp) if stream_resp.get("done") is True else None + dict(stream_resp) if stream_resp.get( + "done") is True else None ), ) if run_manager: @@ -711,7 +717,8 @@ async def _astream( tool_calls=_get_tool_calls_from_response(stream_resp), ), generation_info=( - dict(stream_resp) if stream_resp.get("done") is True else None + dict(stream_resp) if stream_resp.get( + "done") is True else None ), ) if run_manager: @@ -735,8 +742,10 @@ async def _agenerate( chat_generation = ChatGeneration( message=AIMessage( content=final_chunk.text, - usage_metadata=cast(AIMessageChunk, final_chunk.message).usage_metadata, - tool_calls=cast(AIMessageChunk, final_chunk.message).tool_calls, + usage_metadata=cast( + AIMessageChunk, final_chunk.message).usage_metadata, + tool_calls=cast( + AIMessageChunk, final_chunk.message).tool_calls, ), generation_info=generation_info, ) From e3396fbf4185a1f86919c11c9e14de0be8d8ebca Mon Sep 17 00:00:00 2001 From: elhambadri2411 Date: Mon, 28 Oct 2024 15:18:49 -0400 Subject: [PATCH 2/5] chore: formatting --- .../ollama/langchain_ollama/chat_models.py | 27 +++++++------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/libs/partners/ollama/langchain_ollama/chat_models.py b/libs/partners/ollama/langchain_ollama/chat_models.py index 98eec782d8084..0d7697763025b 100644 --- a/libs/partners/ollama/langchain_ollama/chat_models.py +++ b/libs/partners/ollama/langchain_ollama/chat_models.py @@ -401,8 +401,7 @@ def _convert_messages_to_ollama_messages( role = "tool" tool_call_id = message.tool_call_id else: - raise ValueError( - "Received unsupported message type for Ollama.") + raise ValueError("Received unsupported message type for Ollama.") content = "" images = [] @@ -559,8 +558,7 @@ def _chat_stream_with_aggregation( tool_calls=_get_tool_calls_from_response(stream_resp), ), generation_info=( - dict(stream_resp) if stream_resp.get( - "done") is True else None + dict(stream_resp) if stream_resp.get("done") is True else None ), ) if final_chunk is None: @@ -603,8 +601,7 @@ async def _achat_stream_with_aggregation( tool_calls=_get_tool_calls_from_response(stream_resp), ), generation_info=( - dict(stream_resp) if stream_resp.get( - "done") is True else None + dict(stream_resp) if stream_resp.get("done") is True else None ), ) if final_chunk is None: @@ -651,10 +648,8 @@ def _generate( chat_generation = ChatGeneration( message=AIMessage( content=final_chunk.text, - usage_metadata=cast( - AIMessageChunk, final_chunk.message).usage_metadata, - tool_calls=cast( - AIMessageChunk, final_chunk.message).tool_calls, + usage_metadata=cast(AIMessageChunk, final_chunk.message).usage_metadata, + tool_calls=cast(AIMessageChunk, final_chunk.message).tool_calls, ), generation_info=generation_info, ) @@ -683,8 +678,7 @@ def _stream( tool_calls=_get_tool_calls_from_response(stream_resp), ), generation_info=( - dict(stream_resp) if stream_resp.get( - "done") is True else None + dict(stream_resp) if stream_resp.get("done") is True else None ), ) if run_manager: @@ -717,8 +711,7 @@ async def _astream( tool_calls=_get_tool_calls_from_response(stream_resp), ), generation_info=( - dict(stream_resp) if stream_resp.get( - "done") is True else None + dict(stream_resp) if stream_resp.get("done") is True else None ), ) if run_manager: @@ -742,10 +735,8 @@ async def _agenerate( chat_generation = ChatGeneration( message=AIMessage( content=final_chunk.text, - usage_metadata=cast( - AIMessageChunk, final_chunk.message).usage_metadata, - tool_calls=cast( - AIMessageChunk, final_chunk.message).tool_calls, + usage_metadata=cast(AIMessageChunk, final_chunk.message).usage_metadata, + tool_calls=cast(AIMessageChunk, final_chunk.message).tool_calls, ), generation_info=generation_info, ) From db2d4c4802c55aebfb3f90f83b76a48c2efd0402 Mon Sep 17 00:00:00 2001 From: elhambadri2411 Date: Fri, 1 Nov 2024 18:55:00 -0400 Subject: [PATCH 3/5] bug: fixed integration test --- .../ollama/langchain_ollama/chat_models.py | 36 ++++++++++++------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/libs/partners/ollama/langchain_ollama/chat_models.py b/libs/partners/ollama/langchain_ollama/chat_models.py index 0d7697763025b..b90bb5d393935 100644 --- a/libs/partners/ollama/langchain_ollama/chat_models.py +++ b/libs/partners/ollama/langchain_ollama/chat_models.py @@ -476,15 +476,16 @@ async def _acreate_chat_stream( params["options"]["stop"] = stop if "tools" in kwargs: - yield await self._async_client.chat( + async for part in await self._async_client.chat( model=params["model"], messages=ollama_messages, - stream=False, + stream=True, options=Options(**params["options"]), keep_alive=params["keep_alive"], format=params["format"], tools=kwargs["tools"], - ) # type:ignore + ): # type:ignore + yield part else: async for part in await self._async_client.chat( model=params["model"], @@ -514,15 +515,26 @@ def _create_chat_stream( params["options"]["stop"] = stop if "tools" in kwargs: - yield from self._client.chat( - model=params["model"], - messages=ollama_messages, - stream=True, - options=Options(**params["options"]), - keep_alive=params["keep_alive"], - format=params["format"], - tools=kwargs["tools"], - ) + if len(kwargs["tools"]) == 0: + yield from self._client.chat( + model=params["model"], + messages=ollama_messages, + stream=True, + options=Options(**params["options"]), + keep_alive=params["keep_alive"], + format=params["format"], + tools=kwargs["tools"], + ) + else: + yield self._client.chat( + model=params["model"], + messages=ollama_messages, + stream=False, + options=Options(**params["options"]), + keep_alive=params["keep_alive"], + format=params["format"], + tools=kwargs["tools"], + ) else: yield from self._client.chat( model=params["model"], From 3e0f8cae9d98bd1c779b05e7c5c7d51164873792 Mon Sep 17 00:00:00 2001 From: 4meyDam1e Date: Thu, 14 Nov 2024 16:50:24 -0500 Subject: [PATCH 4/5] Made _create_chat_stream modifications concise --- .../ollama/langchain_ollama/chat_models.py | 50 ++++++++----------- 1 file changed, 20 insertions(+), 30 deletions(-) diff --git a/libs/partners/ollama/langchain_ollama/chat_models.py b/libs/partners/ollama/langchain_ollama/chat_models.py index e26a5bfcfa901..7bc272172fd24 100644 --- a/libs/partners/ollama/langchain_ollama/chat_models.py +++ b/libs/partners/ollama/langchain_ollama/chat_models.py @@ -327,7 +327,7 @@ class Multiply(BaseModel): """Base url the model is hosted under.""" client_kwargs: Optional[dict] = {} - """Additional kwargs to pass to the httpx Client. + """Additional kwargs to pass to the httpx Client. For a full list of the params, see [this link](https://pydoc.dev/httpx/latest/httpx.Client.html) """ @@ -514,36 +514,26 @@ def _create_chat_stream( params[key] = kwargs[key] params["options"]["stop"] = stop - if "tools" in kwargs: - if len(kwargs["tools"]) == 0: - yield from self._client.chat( - model=params["model"], - messages=ollama_messages, - stream=True, - options=Options(**params["options"]), - keep_alive=params["keep_alive"], - format=params["format"], - tools=kwargs["tools"], - ) - else: - yield self._client.chat( - model=params["model"], - messages=ollama_messages, - stream=False, - options=Options(**params["options"]), - keep_alive=params["keep_alive"], - format=params["format"], - tools=kwargs["tools"], - ) + + tools = kwargs.get("tools", None) + stream = tools is None or len(tools) == 0 + + chat_params = { + "model": params["model"], + "messages": ollama_messages, + "stream": stream, + "options": Options(**params["options"]), + "keep_alive": params["keep_alive"], + "format": params["format"], + } + + if tools is not None: + chat_params["tools"] = tools + + if stream: + yield from self._client.chat(**chat_params) else: - yield from self._client.chat( - model=params["model"], - messages=ollama_messages, - stream=True, - options=Options(**params["options"]), - keep_alive=params["keep_alive"], - format=params["format"], - ) + yield self._client.chat(**chat_params) def _chat_stream_with_aggregation( self, From 39e1d7a3be28237f5f6c02e3c26611fa879cff6e Mon Sep 17 00:00:00 2001 From: Chester Curme Date: Fri, 15 Nov 2024 11:20:20 -0500 Subject: [PATCH 5/5] update async tool calling --- .../ollama/langchain_ollama/chat_models.py | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/libs/partners/ollama/langchain_ollama/chat_models.py b/libs/partners/ollama/langchain_ollama/chat_models.py index 7bc272172fd24..d6ece0d66a854 100644 --- a/libs/partners/ollama/langchain_ollama/chat_models.py +++ b/libs/partners/ollama/langchain_ollama/chat_models.py @@ -475,27 +475,27 @@ async def _acreate_chat_stream( params[key] = kwargs[key] params["options"]["stop"] = stop - if "tools" in kwargs: - async for part in await self._async_client.chat( - model=params["model"], - messages=ollama_messages, - stream=True, - options=Options(**params["options"]), - keep_alive=params["keep_alive"], - format=params["format"], - tools=kwargs["tools"], - ): # type:ignore + + tools = kwargs.get("tools", None) + stream = tools is None or len(tools) == 0 + + chat_params = { + "model": params["model"], + "messages": ollama_messages, + "stream": stream, + "options": Options(**params["options"]), + "keep_alive": params["keep_alive"], + "format": params["format"], + } + + if tools is not None: + chat_params["tools"] = tools + + if stream: + async for part in await self._async_client.chat(**chat_params): yield part else: - async for part in await self._async_client.chat( - model=params["model"], - messages=ollama_messages, - stream=True, - options=Options(**params["options"]), - keep_alive=params["keep_alive"], - format=params["format"], - ): # type:ignore - yield part + yield await self._async_client.chat(**chat_params) def _create_chat_stream( self,