Skip to content

Commit

Permalink
Ollama client docs (#5605)
Browse files Browse the repository at this point in the history
<!-- Thank you for your contribution! Please review
https://microsoft.github.io/autogen/docs/Contribute before opening a
pull request. -->

<!-- Please add a reviewer to the assignee section when you create a PR.
If you don't have the access to it, we will shortly find a reviewer and
assign them to your PR. -->

## Why are these changes needed?

Adds ollama client documentation to the docs page

## Related issue number

#5604

## Checks

- [ ] I've included any doc changes needed for
https://microsoft.github.io/autogen/. See
https://microsoft.github.io/autogen/docs/Contribute#documentation to
build and test documentation locally.
- [ ] I've added tests (if relevant) corresponding to the changes
introduced in this PR.
- [ ] I've made sure all auto checks have passed.
  • Loading branch information
peterychang authored Feb 18, 2025
1 parent 4959b24 commit 2842c76
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
1 change: 1 addition & 0 deletions python/packages/autogen-core/docs/src/reference/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ python/autogen_ext.models.openai
python/autogen_ext.models.replay
python/autogen_ext.models.azure
python/autogen_ext.models.semantic_kernel
python/autogen_ext.models.ollama
python/autogen_ext.tools.code_execution
python/autogen_ext.tools.graphrag
python/autogen_ext.tools.http
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
autogen\_ext.models.ollama
==========================


.. automodule:: autogen_ext.models.ollama
:members:
:undoc-members:
:show-inheritance:
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ async def create(
response_format_value = value.model_json_schema()
else:
# response_format_value is not a Pydantic model class
# TODO: Should this be an warning/error?
response_format_value = None

# Remove 'response_format' from create_args to prevent passing it twice
Expand Down Expand Up @@ -842,15 +843,16 @@ def model_info(self) -> ModelInfo:
class OllamaChatCompletionClient(BaseOllamaChatCompletionClient, Component[BaseOllamaClientConfigurationConfigModel]):
"""Chat completion client for Ollama hosted models.
You can also use this client for Ollama-compatible ChatCompletion endpoints.
Ollama must be installed and the appropriate model pulled.
Args:
model (str): Which Ollama model to use.
host (str): Model host url.
response_format (optional, pydantic.BaseModel)
host (optional, str): Model host url.
response_format (optional, pydantic.BaseModel): The format of the response. If provided, the response will be parsed into this format as json.
model_info (optional, ModelInfo): The capabilities of the model. **Required if the model is not listed in the ollama model info.**
Note:
Only models with 200k+ downloads (as of Jan 21, 2025), + phi4, deepseek-r1 have pre-defined model infos. See `this file <https://github.com/microsoft/autogen/blob/main/python/packages/autogen-ext/src/autogen_ext/models/ollama/_model_info.py>`__ for the full list. An entry for one model encompases all parameter variants of that model.
To use this client, you must install the `ollama` extension:
Expand Down Expand Up @@ -886,7 +888,11 @@ class OllamaChatCompletionClient(BaseOllamaChatCompletionClient, Component[BaseO
client = ChatCompletionClient.load_component(config)
To output structured data, you can use the `response_format` argument:
.. code-block:: python
from autogen_ext.models.ollama import OllamaChatCompletionClient
from autogen_core.models import UserMessage
from pydantic import BaseModel
Expand All @@ -902,7 +908,8 @@ class StructuredOutput(BaseModel):
result = await ollama_client.create([UserMessage(content="Who was the first man on the moon?", source="user")]) # type: ignore
print(result)
Note: Tool usage in ollama is stricter than in its OpenAI counterparts. While OpenAI accepts a map of [str, Any], Ollama requires a map of [str, Property] where Property is a typed object containing ``type`` and ``description`` fields. Therefore, only the keys ``type`` and ``description`` will be converted from the properties blob in the tool schema.
Note:
Tool usage in ollama is stricter than in its OpenAI counterparts. While OpenAI accepts a map of [str, Any], Ollama requires a map of [str, Property] where Property is a typed object containing ``type`` and ``description`` fields. Therefore, only the keys ``type`` and ``description`` will be converted from the properties blob in the tool schema.
To view the full list of available configuration options, see the :py:class:`OllamaClientConfigurationConfigModel` class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
from typing_extensions import TypedDict


# response_format MUST be a pydantic.BaseModel type or None
# TODO: check if we can extend response_format to support json and/or dict
class CreateArguments(TypedDict, total=False):
model: str
host: Optional[str]
response_format: Any


class BaseOllamaClientConfiguration(CreateArguments, total=False):
Expand All @@ -20,9 +23,11 @@ class BaseOllamaClientConfiguration(CreateArguments, total=False):


# Pydantic equivalents of the above TypedDicts
# response_format MUST be a pydantic.BaseModel type or None
class CreateArgumentsConfigModel(BaseModel):
model: str
host: str | None = None
response_format: Any = None


class BaseOllamaClientConfigurationConfigModel(CreateArgumentsConfigModel):
Expand Down

0 comments on commit 2842c76

Please sign in to comment.