跳到主要内容

oai.ollama

使用Ollama的API创建一个OpenAI兼容的客户端。

示例:

llm_config={

  • "config_list" - [{

  • "api_type" - "ollama",

  • "model" - "mistral:7b-instruct-v0.3-q6_K" } ]}

    agent = autogen.AssistantAgent("my_agent", llm_config=llm_config)

    使用以下命令安装Ollama的python库:pip install --upgrade ollama

    资源:

OllamaClient

class OllamaClient()

Ollama API 的客户端。

__init__

def __init__(**kwargs)

请注意,Ollama不需要api_key或环境变量。

参数:

消息检索

def message_retrieval(response) -> List

从响应中检索并返回一个字符串列表或一个Choice.Message列表。

注意:如果返回的是一个Choice.Message列表,它目前需要包含OpenAI的ChatCompletion Message对象的字段,因为在当前代码库中,除非使用自定义代理,否则这是函数或工具调用的预期。

get_usage

@staticmethod
def get_usage(response) -> Dict

使用 RESPONSE_USAGE_KEYS 返回响应的使用摘要。

parse_params

def parse_params(params: Dict[str, Any]) -> Dict[str, Any]

从传入的参数中加载Ollama API的参数,并返回一组经过验证的参数。检查类型、范围并设置默认值。

oai_messages_to_ollama_messages

def oai_messages_to_ollama_messages(messages: list[Dict[str, Any]],
tools: list) -> list[dict[str, Any]]

将消息从OAI格式转换为Ollama的格式。 我们纠正任何特定的角色顺序和类型,并将工具转换为消息(因为Ollama无法使用工具消息)

response_to_tool_call

def response_to_tool_call(response_string: str) -> Any

尝试将响应转换为对象,旨在与函数格式 [{},{}] 对齐

is_valid_tool_call_item

def is_valid_tool_call_item(call_item: dict) -> bool

检查字典项是否至少包含'name',可选地包含'arguments',并且没有其他键以匹配工具调用的JSON