-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpydantic_ai.py
230 lines (192 loc) · 7.24 KB
/
pydantic_ai.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
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
# SPDX-FileCopyrightText: Copyright (c) 2025 Cisco and/or its affiliates.
# SPDX-License-Identifier: Apache-2.0
import logging
from typing import Any, Literal, TypedDict
from openai import AsyncAzureOpenAI
from pydantic import Field, model_validator
from pydantic_ai import Agent
from pydantic_ai.messages import (
ModelMessage,
ModelRequest,
ModelResponse,
SystemPromptPart,
TextPart,
UserPromptPart,
)
from pydantic_ai.models import KnownModelName
from pydantic_ai.models.openai import OpenAIModel
from typing_extensions import Self
from agntcy_iomapper.base import (
AgentIOMapper,
AgentIOMapperConfig,
AgentIOMapperInput,
AgentIOMapperOutput,
)
logger = logging.getLogger(__name__)
class AgentIOModelArgs(TypedDict, total=False):
base_url: str
api_version: str
azure_endpoint: str
azure_ad_token: str
project: str
organization: str
class AgentModelSettings(TypedDict, total=False):
max_tokens: int
temperature: float
top_p: float
parallel_tool_calls: bool
seed: int
presence_penalty: float
frequency_penalty: float
logit_bias: dict[str, int]
class PydanticAIAgentIOMapperInput(AgentIOMapperInput):
model_settings: AgentModelSettings | None = Field(
default=None,
description="Specific arguments for LLM transformation.",
)
model: str | None = Field(
default=None,
description="Specific model out of those configured to handle request.",
)
PydanticAIAgentIOMapperOutput = AgentIOMapperOutput
class PydanticAIAgentIOMapperConfig(AgentIOMapperConfig):
models: dict[str, AgentIOModelArgs] = Field(
default={"azure:gpt-4o-mini": AgentIOModelArgs()},
description="LLM configuration to use for translation",
)
default_model: str | None = Field(
default="azure:gpt-4o-mini",
description="Default arguments to LLM completion function by configured model.",
)
default_model_settings: dict[str, AgentModelSettings] = Field(
default={"azure:gpt-4o-mini": AgentModelSettings(seed=42, temperature=0.8)},
description="LLM configuration to use for translation",
)
@model_validator(mode="after")
def _validate_obj(self) -> Self:
if self.models and self.default_model not in self.models:
raise ValueError(
f"default model {self.default_model} not present in configured models"
)
# Fill out defaults to eliminate need for checking.
for model_name in self.models.keys():
if model_name not in self.default_model_settings:
self.default_model_settings[model_name] = AgentModelSettings()
return self
SupportedModelName = (
KnownModelName
| Literal[
"azure:gpt-4o-mini",
"azure:gpt-4o",
"azure:gpt-4",
]
)
def get_supported_agent(
model_name: SupportedModelName,
model_args: dict[str, Any] = {},
**kwargs,
) -> Agent:
"""
Creates and returns an `Agent` instance for the given model.
Args:
model_name (SupportedModelName): The name of the model to be used.
If the name starts with "azure:", an `AsyncAzureOpenAI` client is used.
model_args (dict[str, Any], optional): Additional arguments for model
initialization. Defaults to an empty dictionary.
**kwargs: Additional keyword arguments passed to the `Agent` constructor.
Returns:
Agent: An instance of the `Agent` class configured with the specified model.
Notes:
- The `pydantic-ai` package does not currently pass `model_args` to the
inferred model in the constructor, but this behavior might change in
the future.
"""
if model_name.startswith("azure:"):
client = AsyncAzureOpenAI(**model_args)
model = OpenAIModel(model_name[6:], openai_client=client)
return Agent(model, **kwargs)
return Agent(model_name, **kwargs)
class PydanticAIIOAgentIOMapper(AgentIOMapper):
def __init__(
self,
config: PydanticAIAgentIOMapperConfig,
**kwargs,
):
super().__init__(config, **kwargs)
def _get_model_settings(self, input: PydanticAIAgentIOMapperInput):
if hasattr(input, "model") and input.model is not None:
model_name = input.model
else:
model_name = self.config.default_model
if model_name not in self.config.models:
raise ValueError(f"requested model {model_name} not found")
elif hasattr(input, "model_settings") and input.model_settings is not None:
model_settings = self.config.default_model_settings[model_name].copy()
model_settings.update(input.model_settings)
return model_settings
else:
return self.config.default_model_settings[model_name]
def _get_agent(
self, input: PydanticAIAgentIOMapperInput, system_prompt: str
) -> Agent:
if hasattr(input, "model") and input.model is not None:
model_name = input.model
else:
model_name = self.config.default_model
if model_name not in self.config.models:
raise ValueError(f"requested model {model_name} not found")
return get_supported_agent(
model_name,
model_args=self.config.models[model_name],
system_prompt=system_prompt,
)
def _get_prompts(
self, messages: list[dict[str, str]]
) -> tuple[str, str, list[ModelMessage]]:
system_prompt = ""
user_prompt = ""
message_history = []
for msg in messages:
role = msg.get("role", "user")
if role.lower() == "system":
system_prompt = msg.get("content", "")
message_history.append(
ModelRequest(parts=[SystemPromptPart(content=system_prompt)])
)
elif role.lower() == "user":
user_prompt = msg.get("content", "")
message_history.append(
ModelRequest(parts=[UserPromptPart(content=user_prompt)])
)
elif role.lower() == "assistant":
content = msg.get("content", "")
message_history.append(ModelResponse(parts=[TextPart(content=content)]))
return (system_prompt, user_prompt, message_history)
def _invoke(
self,
input: PydanticAIAgentIOMapperInput,
messages: list[dict[str, str]],
**kwargs,
) -> str:
system_prompt, user_prompt, message_history = self._get_prompts(messages)
agent = self._get_agent(input, system_prompt)
response = agent.run_sync(
user_prompt,
model_settings=self._get_model_settings(input),
message_history=message_history,
)
return response.data
async def _ainvoke(
self,
input: PydanticAIAgentIOMapperInput,
messages: list[dict[str, str]],
**kwargs,
) -> str:
system_prompt, user_prompt, message_history = self._get_prompts(messages)
agent = self._get_agent(input, system_prompt)
response = await agent.run(
user_prompt,
model_settings=self._get_model_settings(input),
message_history=message_history,
)
return response.data