autogen_ext.models.ollama#

pydantic 模型 BaseOllamaClientConfigurationConfigModel[源码]#

基类: CreateArgumentsConfigModel

显示 JSON 模式
{
   "title": "BaseOllamaClientConfigurationConfigModel",
   "type": "object",
   "properties": {
      "model": {
         "title": "Model",
         "type": "string"
      },
      "host": {
         "anyOf": [
            {
               "type": "string"
            },
            {
               "type": "null"
            }
         ],
         "default": null,
         "title": "Host"
      },
      "response_format": {
         "default": null,
         "title": "Response Format"
      },
      "follow_redirects": {
         "default": true,
         "title": "Follow Redirects",
         "type": "boolean"
      },
      "timeout": {
         "default": null,
         "title": "Timeout"
      },
      "headers": {
         "anyOf": [
            {
               "additionalProperties": {
                  "type": "string"
               },
               "type": "object"
            },
            {
               "type": "null"
            }
         ],
         "default": null,
         "title": "Headers"
      },
      "model_capabilities": {
         "anyOf": [
            {
               "$ref": "#/$defs/ModelCapabilities"
            },
            {
               "type": "null"
            }
         ],
         "default": null
      },
      "model_info": {
         "anyOf": [
            {
               "$ref": "#/$defs/ModelInfo"
            },
            {
               "type": "null"
            }
         ],
         "default": null
      },
      "options": {
         "anyOf": [
            {
               "type": "object"
            },
            {
               "$ref": "#/$defs/Options"
            },
            {
               "type": "null"
            }
         ],
         "default": null,
         "title": "Options"
      }
   },
   "$defs": {
      "ModelCapabilities": {
         "deprecated": true,
         "properties": {
            "vision": {
               "title": "Vision",
               "type": "boolean"
            },
            "function_calling": {
               "title": "Function Calling",
               "type": "boolean"
            },
            "json_output": {
               "title": "Json Output",
               "type": "boolean"
            }
         },
         "required": [
            "vision",
            "function_calling",
            "json_output"
         ],
         "title": "ModelCapabilities",
         "type": "object"
      },
      "ModelInfo": {
         "description": "ModelInfo is a dictionary that contains information about a model's properties.\nIt is expected to be used in the model_info property of a model client.\n\nWe are expecting this to grow over time as we add more features.",
         "properties": {
            "vision": {
               "title": "Vision",
               "type": "boolean"
            },
            "function_calling": {
               "title": "Function Calling",
               "type": "boolean"
            },
            "json_output": {
               "title": "Json Output",
               "type": "boolean"
            },
            "family": {
               "anyOf": [
                  {
                     "enum": [
                        "gpt-41",
                        "gpt-45",
                        "gpt-4o",
                        "o1",
                        "o3",
                        "o4",
                        "gpt-4",
                        "gpt-35",
                        "r1",
                        "gemini-1.5-flash",
                        "gemini-1.5-pro",
                        "gemini-2.0-flash",
                        "gemini-2.5-pro",
                        "claude-3-haiku",
                        "claude-3-sonnet",
                        "claude-3-opus",
                        "claude-3-5-haiku",
                        "claude-3-5-sonnet",
                        "claude-3-7-sonnet",
                        "unknown"
                     ],
                     "type": "string"
                  },
                  {
                     "type": "string"
                  }
               ],
               "title": "Family"
            },
            "structured_output": {
               "title": "Structured Output",
               "type": "boolean"
            },
            "multiple_system_messages": {
               "anyOf": [
                  {
                     "type": "boolean"
                  },
                  {
                     "type": "null"
                  }
               ],
               "title": "Multiple System Messages"
            }
         },
         "required": [
            "vision",
            "function_calling",
            "json_output",
            "family",
            "structured_output"
         ],
         "title": "ModelInfo",
         "type": "object"
      },
      "Options": {
         "properties": {
            "numa": {
               "anyOf": [
                  {
                     "type": "boolean"
                  },
                  {
                     "type": "null"
                  }
               ],
               "default": null,
               "title": "Numa"
            },
            "num_ctx": {
               "anyOf": [
                  {
                     "type": "integer"
                  },
                  {
                     "type": "null"
                  }
               ],
               "default": null,
               "title": "Num Ctx"
            },
            "num_batch": {
               "anyOf": [
                  {
                     "type": "integer"
                  },
                  {
                     "type": "null"
                  }
               ],
               "default": null,
               "title": "Num Batch"
            },
            "num_gpu": {
               "anyOf": [
                  {
                     "type": "integer"
                  },
                  {
                     "type": "null"
                  }
               ],
               "default": null,
               "title": "Num Gpu"
            },
            "main_gpu": {
               "anyOf": [
                  {
                     "type": "integer"
                  },
                  {
                     "type": "null"
                  }
               ],
               "default": null,
               "title": "Main Gpu"
            },
            "low_vram": {
               "anyOf": [
                  {
                     "type": "boolean"
                  },
                  {
                     "type": "null"
                  }
               ],
               "default": null,
               "title": "Low Vram"
            },
            "f16_kv": {
               "anyOf": [
                  {
                     "type": "boolean"
                  },
                  {
                     "type": "null"
                  }
               ],
               "default": null,
               "title": "F16 Kv"
            },
            "logits_all": {
               "anyOf": [
                  {
                     "type": "boolean"
                  },
                  {
                     "type": "null"
                  }
               ],
               "default": null,
               "title": "Logits All"
            },
            "vocab_only": {
               "anyOf": [
                  {
                     "type": "boolean"
                  },
                  {
                     "type": "null"
                  }
               ],
               "default": null,
               "title": "Vocab Only"
            },
            "use_mmap": {
               "anyOf": [
                  {
                     "type": "boolean"
                  },
                  {
                     "type": "null"
                  }
               ],
               "default": null,
               "title": "Use Mmap"
            },
            "use_mlock": {
               "anyOf": [
                  {
                     "type": "boolean"
                  },
                  {
                     "type": "null"
                  }
               ],
               "default": null,
               "title": "Use Mlock"
            },
            "embedding_only": {
               "anyOf": [
                  {
                     "type": "boolean"
                  },
                  {
                     "type": "null"
                  }
               ],
               "default": null,
               "title": "Embedding Only"
            },
            "num_thread": {
               "anyOf": [
                  {
                     "type": "integer"
                  },
                  {
                     "type": "null"
                  }
               ],
               "default": null,
               "title": "Num Thread"
            },
            "num_keep": {
               "anyOf": [
                  {
                     "type": "integer"
                  },
                  {
                     "type": "null"
                  }
               ],
               "default": null,
               "title": "Num Keep"
            },
            "seed": {
               "anyOf": [
                  {
                     "type": "integer"
                  },
                  {
                     "type": "null"
                  }
               ],
               "default": null,
               "title": "Seed"
            },
            "num_predict": {
               "anyOf": [
                  {
                     "type": "integer"
                  },
                  {
                     "type": "null"
                  }
               ],
               "default": null,
               "title": "Num Predict"
            },
            "top_k": {
               "anyOf": [
                  {
                     "type": "integer"
                  },
                  {
                     "type": "null"
                  }
               ],
               "default": null,
               "title": "Top K"
            },
            "top_p": {
               "anyOf": [
                  {
                     "type": "number"
                  },
                  {
                     "type": "null"
                  }
               ],
               "default": null,
               "title": "Top P"
            },
            "tfs_z": {
               "anyOf": [
                  {
                     "type": "number"
                  },
                  {
                     "type": "null"
                  }
               ],
               "default": null,
               "title": "Tfs Z"
            },
            "typical_p": {
               "anyOf": [
                  {
                     "type": "number"
                  },
                  {
                     "type": "null"
                  }
               ],
               "default": null,
               "title": "Typical P"
            },
            "repeat_last_n": {
               "anyOf": [
                  {
                     "type": "integer"
                  },
                  {
                     "type": "null"
                  }
               ],
               "default": null,
               "title": "Repeat Last N"
            },
            "temperature": {
               "anyOf": [
                  {
                     "type": "number"
                  },
                  {
                     "type": "null"
                  }
               ],
               "default": null,
               "title": "Temperature"
            },
            "repeat_penalty": {
               "anyOf": [
                  {
                     "type": "number"
                  },
                  {
                     "type": "null"
                  }
               ],
               "default": null,
               "title": "Repeat Penalty"
            },
            "presence_penalty": {
               "anyOf": [
                  {
                     "type": "number"
                  },
                  {
                     "type": "null"
                  }
               ],
               "default": null,
               "title": "Presence Penalty"
            },
            "frequency_penalty": {
               "anyOf": [
                  {
                     "type": "number"
                  },
                  {
                     "type": "null"
                  }
               ],
               "default": null,
               "title": "Frequency Penalty"
            },
            "mirostat": {
               "anyOf": [
                  {
                     "type": "integer"
                  },
                  {
                     "type": "null"
                  }
               ],
               "default": null,
               "title": "Mirostat"
            },
            "mirostat_tau": {
               "anyOf": [
                  {
                     "type": "number"
                  },
                  {
                     "type": "null"
                  }
               ],
               "default": null,
               "title": "Mirostat Tau"
            },
            "mirostat_eta": {
               "anyOf": [
                  {
                     "type": "number"
                  },
                  {
                     "type": "null"
                  }
               ],
               "default": null,
               "title": "Mirostat Eta"
            },
            "penalize_newline": {
               "anyOf": [
                  {
                     "type": "boolean"
                  },
                  {
                     "type": "null"
                  }
               ],
               "default": null,
               "title": "Penalize Newline"
            },
            "stop": {
               "anyOf": [
                  {
                     "items": {
                        "type": "string"
                     },
                     "type": "array"
                  },
                  {
                     "type": "null"
                  }
               ],
               "default": null,
               "title": "Stop"
            }
         },
         "title": "Options",
         "type": "object"
      }
   },
   "required": [
      "model"
   ]
}

字段:
  • follow_redirects (bool)

  • headers (Mapping[str, str] | None)

  • model_capabilities (autogen_core.models._model_client.ModelCapabilities | None)

  • model_info (autogen_core.models._model_client.ModelInfo | None)

  • options (Mapping[str, Any] | ollama._types.Options | None)

  • timeout (Any)

字段 follow_redirects: bool = True#
字段 headers: Mapping[str, str] | None = None#
字段 model_capabilities: ModelCapabilities | None = None#
字段 model_info: ModelInfo | None = None#
字段 options: Mapping[str, Any] | Options | None = None#
字段 timeout: Any = None#
pydantic 模型 CreateArgumentsConfigModel[源码]#

基类: BaseModel

显示 JSON 模式
{
   "title": "CreateArgumentsConfigModel",
   "type": "object",
   "properties": {
      "model": {
         "title": "Model",
         "type": "string"
      },
      "host": {
         "anyOf": [
            {
               "type": "string"
            },
            {
               "type": "null"
            }
         ],
         "default": null,
         "title": "Host"
      },
      "response_format": {
         "default": null,
         "title": "Response Format"
      }
   },
   "required": [
      "model"
   ]
}

字段:
  • host (str | None)

  • model (str)

  • response_format (Any)

字段 host: str | None = None#
字段 model: str [必需]#
字段 response_format: Any = None#
OllamaChatCompletionClient(**kwargs: Unpack)[源码]#

基类: BaseOllamaChatCompletionClient, Component[BaseOllamaClientConfigurationConfigModel]

用于 Ollama 托管模型的聊天完成客户端。

必须安装 Ollama 并拉取相应的模型。

参数:
  • model (str) – 要使用的 Ollama 模型。

  • host (可选, str) – 模型主机 URL。

  • response_format (可选, pydantic.BaseModel) – 响应的格式。 如果提供,响应将被解析为 JSON 格式。

  • options (可选, Mapping[str, Any] | Options) – 传递给 Ollama 客户端的其他选项。

  • model_info (可选, ModelInfo) – 模型的性能。 如果模型未在 Ollama 模型信息中列出,则为必需项。

注意

只有下载量超过 20 万的模型(截至 2025 年 1 月 21 日)以及 phi4、deepseek-r1 具有预定义的模型信息。 请参阅此文件获取完整列表。 一个模型的条目涵盖该模型的所有参数变体。

要使用此客户端,您必须安装 ollama 扩展

pip install "autogen-ext[ollama]"

以下代码片段展示了如何将客户端与 Ollama 模型一起使用

from autogen_ext.models.ollama import OllamaChatCompletionClient
from autogen_core.models import UserMessage

ollama_client = OllamaChatCompletionClient(
    model="llama3",
)

result = await ollama_client.create([UserMessage(content="What is the capital of France?", source="user")])  # type: ignore
print(result)

要从配置加载客户端,可以使用 load_component 方法

from autogen_core.models import ChatCompletionClient

config = {
    "provider": "OllamaChatCompletionClient",
    "config": {"model": "llama3"},
}

client = ChatCompletionClient.load_component(config)

要输出结构化数据,可以使用 response_format 参数

from autogen_ext.models.ollama import OllamaChatCompletionClient
from autogen_core.models import UserMessage
from pydantic import BaseModel


class StructuredOutput(BaseModel):
    first_name: str
    last_name: str


ollama_client = OllamaChatCompletionClient(
    model="llama3",
    response_format=StructuredOutput,
)
result = await ollama_client.create([UserMessage(content="Who was the first man on the moon?", source="user")])  # type: ignore
print(result)

注意

Ollama 中的工具使用比 OpenAI 对应项更严格。 虽然 OpenAI 接受 [str, Any] 的映射,但 Ollama 需要 [str, Property] 的映射,其中 Property 是包含 typedescription 字段的类型化对象。 因此,只有键 typedescription 将从工具模式中的属性 blob 转换。

要查看可用配置选项的完整列表,请参阅 OllamaClientConfigurationConfigModel 类。

classmethod _from_config(config: BaseOllamaClientConfigurationConfigModel) Self[source]#

从配置对象创建一个组件的新实例。

参数:

config (T) – 配置对象。

返回值:

Self – 组件的新实例。

_to_config() BaseOllamaClientConfigurationConfigModel[source]#

转储创建与此实例配置匹配的组件的新实例所需的配置。

返回值:

T – 组件的配置。

component_config_schema#

BaseOllamaClientConfigurationConfigModel的别名

component_provider_override: ClassVar[str | None] = 'autogen_ext.models.ollama.OllamaChatCompletionClient'#

覆盖组件的提供程序字符串。 这应该用于防止内部模块名称成为模块名称的一部分。

component_type: ClassVar[ComponentType] = 'model'#

组件的逻辑类型。