-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.py
211 lines (182 loc) · 7.24 KB
/
base.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
# Copyright AGNTCY Contributors (https://github.com/agntcy)
# SPDX-License-Identifier: Apache-2.0
import json
import logging
import re
from abc import ABC, abstractmethod
from typing import ClassVar, Optional
import jsonschema
from jinja2 import Environment
from jinja2.sandbox import SandboxedEnvironment
from agntcy_iomapper.base.models import (
AgentIOMapperInput,
AgentIOMapperOutput,
BaseIOMapperConfig,
)
logger = logging.getLogger(__name__)
class BaseIOMapper(ABC):
"""Abstract base class for interfacing with io mapper.
All io mappers wrappers inherited from BaseIOMapper.
"""
_json_search_pattern: ClassVar[re.Pattern] = re.compile(
r"```json\n(.*?)\n```", re.DOTALL
)
def __init__(
self,
config: Optional[BaseIOMapperConfig] = None,
jinja_env: Optional[Environment] = None,
jinja_env_async: Optional[Environment] = None,
):
if config is None:
config = BaseIOMapperConfig()
if jinja_env is not None and jinja_env.is_async:
raise ValueError("Async Jinja env passed to jinja_env argument")
elif jinja_env_async is not None and not jinja_env_async.is_async:
raise ValueError("Sync Jinja env passed to jinja_env_async argument")
self.jinja_env = jinja_env
self.prompt_template = None
self.user_template = None
self.jinja_env_async = jinja_env_async
self.prompt_template_async = None
self.user_template_async = None
self.config = config
# Delay init until sync or async functions called.
def _check_jinja_env(self, enable_async: bool):
if enable_async:
# Delay load of env until needed
if self.jinja_env_async is None:
# Default is sandboxed, no loader
self.jinja_env_async = SandboxedEnvironment(
loader=None,
enable_async=True,
autoescape=False,
)
if self.prompt_template_async is None:
self.prompt_template_async = self.jinja_env_async.from_string(
self.config.system_prompt_template
)
if self.user_template_async is None:
self.user_template_async = self.jinja_env_async.from_string(
self.config.message_template
)
else:
if self.jinja_env is None:
self.jinja_env = SandboxedEnvironment(
loader=None,
enable_async=False,
autoescape=False,
)
if self.prompt_template is None:
self.prompt_template = self.jinja_env.from_string(
self.config.system_prompt_template
)
if self.user_template is None:
self.user_template = self.jinja_env.from_string(
self.config.message_template
)
def _get_render_env(self, input: AgentIOMapperInput) -> dict[str, str]:
return {
"input": input.input,
"output": input.output,
"data": input.data,
}
def _get_output(
self, input: AgentIOMapperInput, outputs: str
) -> AgentIOMapperOutput:
if input.output.json_schema is None:
# If there is no schema, quote the chars for JSON.
return AgentIOMapperOutput.model_validate_json(
f'{{"data": {json.dumps(outputs)} }}'
)
logger.debug(f"{outputs}")
# Check if data is returned in JSON markdown text
matches = self._json_search_pattern.findall(outputs)
if matches:
outputs = matches[-1]
return AgentIOMapperOutput.model_validate_json(f'{{"data": {outputs} }}')
def _validate_input(self, input: AgentIOMapperInput) -> None:
if self.config.validate_json_input and input.input.json_schema is not None:
jsonschema.validate(
instance=input.data,
schema=input.input.json_schema.model_dump(
exclude_none=True, mode="json"
),
)
def _validate_output(
self, input: AgentIOMapperInput, output: AgentIOMapperOutput
) -> None:
if self.config.validate_json_output and input.output.json_schema is not None:
output_schema = input.output.json_schema.model_dump(
exclude_none=True, mode="json"
)
logging.debug(f"Checking output schema: {output_schema}")
jsonschema.validate(
instance=output.data,
schema=output_schema,
)
def _invoke(self, input: AgentIOMapperInput, **kwargs) -> AgentIOMapperOutput:
self._validate_input(input)
self._check_jinja_env(False)
render_env = self._get_render_env(input)
system_prompt = self.prompt_template.render(render_env)
if input.message_template is not None:
logging.info(f"User template supplied on input: {input.message_template}")
user_template = self.jinja_env.from_string(input.message_template)
else:
user_template = self.user_template
user_prompt = user_template.render(render_env)
outputs = self.invoke(
input,
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt},
],
**kwargs,
)
logging.debug(f"The LLM returned: {outputs}")
output = self._get_output(input, outputs)
self._validate_output(input, output)
return output
async def _ainvoke(
self, input: AgentIOMapperInput, **kwargs
) -> AgentIOMapperOutput:
self._validate_input(input)
self._check_jinja_env(True)
render_env = self._get_render_env(input)
system_prompt = await self.prompt_template_async.render_async(render_env)
if input.message_template is not None:
logging.info(f"User template supplied on input: {input.message_template}")
user_template_async = self.jinja_env_async.from_string(
input.message_template
)
else:
user_template_async = self.user_template_async
user_prompt = await user_template_async.render_async(render_env)
outputs = await self.ainvoke(
input,
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt},
],
**kwargs,
)
logging.debug(f"The LLM returned: {outputs}")
output = self._get_output(input, outputs)
self._validate_output(input, output)
return output
@abstractmethod
def invoke(
self, input: AgentIOMapperInput, messages: list[dict[str, str]], **kwargs
) -> str:
"""Invoke internal model to process messages.
Args:
messages: the messages to send to the LLM
"""
@abstractmethod
async def ainvoke(
self, input: AgentIOMapperInput, messages: list[dict[str, str]], **kwargs
) -> str:
"""Async invoke internal model to process messages.
Args:
messages: the messages to send to the LLM
"""