Skip to content

Commit

Permalink
ollama[patch]: support ollama 0.4 (#28364)
Browse files Browse the repository at this point in the history
v0.4 of the Python SDK is already installed via the lock file in CI, but
our current implementation is not compatible with it.

This also addresses an issue introduced in
#28299. @RyanMagnuson
would you mind explaining the motivation for that change? From what I
can tell the Ollama SDK [does not support
kwargs](https://github.com/ollama/ollama-python/blob/6c44bb272976b9dc8d24d3a3fd913fd39b83394b/ollama/_client.py#L286).
Previously, unsupported kwargs were ignored, but they currently raise
`TypeError`.

Some of LangChain's standard test suite expects `tool_choice` to be
supported, so here we catch it in `bind_tools` so it is ignored and not
passed through to the client.
  • Loading branch information
ccurme authored Nov 26, 2024
1 parent e9c1655 commit 74d9d2c
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions libs/partners/ollama/langchain_ollama/chat_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ def _get_tool_calls_from_response(
"""Get tool calls from ollama response."""
tool_calls = []
if "message" in response:
if "tool_calls" in response["message"]:
for tc in response["message"]["tool_calls"]:
if raw_tool_calls := response["message"].get("tool_calls"):
for tc in raw_tool_calls:
tool_calls.append(
tool_call(
id=str(uuid4()),
Expand Down Expand Up @@ -728,6 +728,8 @@ def _llm_type(self) -> str:
def bind_tools(
self,
tools: Sequence[Union[Dict[str, Any], Type, Callable, BaseTool]],
*,
tool_choice: Optional[Union[dict, str, Literal["auto", "any"], bool]] = None,
**kwargs: Any,
) -> Runnable[LanguageModelInput, BaseMessage]:
"""Bind tool-like objects to this chat model.
Expand All @@ -738,6 +740,8 @@ def bind_tools(
tools: A list of tool definitions to bind to this chat model.
Supports any tool definition handled by
:meth:`langchain_core.utils.function_calling.convert_to_openai_tool`.
tool_choice: If provided, which tool for model to call. **This parameter
is currently ignored as it is not supported by Ollama.**
kwargs: Any additional parameters are passed directly to
``self.bind(**kwargs)``.
""" # noqa: E501
Expand Down

0 comments on commit 74d9d2c

Please sign in to comment.