pydantic_ai.models.function
由本地函数控制的模型。
FunctionModel
类似于 TestModel
,但允许对模型的行为进行更精细的控制。
其主要用例是进行比 TestModel
更高级的单元测试。
这是一个最小的示例
function_model_usage.py
from pydantic_ai import Agent
from pydantic_ai.messages import ModelMessage, ModelResponse, TextPart
from pydantic_ai.models.function import FunctionModel, AgentInfo
my_agent = Agent('openai:gpt-4o')
async def model_function(
messages: list[ModelMessage], info: AgentInfo
) -> ModelResponse:
print(messages)
"""
[
ModelRequest(
parts=[
UserPromptPart(
content='Testing my agent...',
timestamp=datetime.datetime(...),
part_kind='user-prompt',
)
],
kind='request',
)
]
"""
print(info)
"""
AgentInfo(
function_tools=[], allow_text_result=True, result_tools=[], model_settings=None
)
"""
return ModelResponse(parts=[TextPart('hello world')])
async def test_my_agent():
"""Unit test for my_agent, to be run by pytest."""
with my_agent.override(model=FunctionModel(model_function)):
result = await my_agent.run('Testing my agent...')
assert result.data == 'hello world'
请参阅 使用 FunctionModel
进行单元测试 以获取详细文档。
FunctionModel dataclass
基类: Model
由本地函数控制的模型。
除了 __init__
之外,所有方法都是私有的或与基类的方法匹配。
源代码位于 pydantic_ai_slim/pydantic_ai/models/function.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
|
__init__
__init__(
function: FunctionDef, *, model_name: str | None = None
) -> None
__init__(
*,
stream_function: StreamFunctionDef,
model_name: str | None = None
) -> None
__init__(
function: FunctionDef,
*,
stream_function: StreamFunctionDef,
model_name: str | None = None
) -> None
__init__(
function: FunctionDef | None = None,
*,
stream_function: StreamFunctionDef | None = None,
model_name: str | None = None
)
初始化 FunctionModel
。
必须提供 function
或 stream_function
之一,允许同时提供两者。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
function
|
FunctionDef | None
|
用于非流式请求的函数调用。 |
None
|
stream_function
|
StreamFunctionDef | None
|
用于流式请求的函数调用。 |
None
|
model_name
|
str | None
|
模型的名称。如果未提供,则会从函数名称生成名称。 |
None
|
源代码位于 pydantic_ai_slim/pydantic_ai/models/function.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
|
AgentInfo dataclass
关于代理的信息。
这作为第二个参数传递给 FunctionModel
中使用的函数。
源代码位于 pydantic_ai_slim/pydantic_ai/models/function.py
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
|
function_tools instance-attribute
function_tools: list[ToolDefinition]
此代理上可用的函数工具。
这些工具是通过 tool
和 tool_plain
装饰器注册的。
DeltaToolCall dataclass
工具调用的增量更改。
用于描述流式结构化响应时的块。
源代码位于 pydantic_ai_slim/pydantic_ai/models/function.py
169 170 171 172 173 174 175 176 177 178 179 180 181 |
|
DeltaToolCalls module-attribute
DeltaToolCalls: TypeAlias = dict[int, DeltaToolCall]
工具调用 ID 到增量更改的映射。
FunctionDef module-attribute
FunctionDef: TypeAlias = Callable[
[list[ModelMessage], AgentInfo],
Union[ModelResponse, Awaitable[ModelResponse]],
]
用于生成非流式响应的函数。
StreamFunctionDef module-attribute
StreamFunctionDef: TypeAlias = Callable[
[list[ModelMessage], AgentInfo],
AsyncIterator[Union[str, DeltaToolCalls]],
]
用于生成流式响应的函数。
虽然这被定义为具有 AsyncIterator[Union[str, DeltaToolCalls]]
的返回类型,但实际上应该将其视为 Union[AsyncIterator[str], AsyncIterator[DeltaToolCalls]
,
例如,你需要生成所有文本或所有 DeltaToolCalls
,而不是将它们混合。
FunctionStreamedResponse dataclass
基类: StreamedResponse
FunctionModel
的 StreamedResponse
实现。
源代码位于 pydantic_ai_slim/pydantic_ai/models/function.py
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
|