agentscope-runtime 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- agentscope_runtime/__init__.py +4 -0
- agentscope_runtime/engine/__init__.py +9 -0
- agentscope_runtime/engine/agents/__init__.py +2 -0
- agentscope_runtime/engine/agents/agentscope_agent/__init__.py +6 -0
- agentscope_runtime/engine/agents/agentscope_agent/agent.py +342 -0
- agentscope_runtime/engine/agents/agentscope_agent/hooks.py +156 -0
- agentscope_runtime/engine/agents/agno_agent.py +220 -0
- agentscope_runtime/engine/agents/base_agent.py +29 -0
- agentscope_runtime/engine/agents/langgraph_agent.py +59 -0
- agentscope_runtime/engine/agents/llm_agent.py +51 -0
- agentscope_runtime/engine/deployers/__init__.py +3 -0
- agentscope_runtime/engine/deployers/adapter/__init__.py +0 -0
- agentscope_runtime/engine/deployers/adapter/a2a/__init__.py +2 -0
- agentscope_runtime/engine/deployers/adapter/a2a/a2a_adapter_utils.py +425 -0
- agentscope_runtime/engine/deployers/adapter/a2a/a2a_agent_adapter.py +69 -0
- agentscope_runtime/engine/deployers/adapter/a2a/a2a_protocol_adapter.py +60 -0
- agentscope_runtime/engine/deployers/adapter/protocol_adapter.py +24 -0
- agentscope_runtime/engine/deployers/base.py +17 -0
- agentscope_runtime/engine/deployers/local_deployer.py +586 -0
- agentscope_runtime/engine/helpers/helper.py +127 -0
- agentscope_runtime/engine/llms/__init__.py +3 -0
- agentscope_runtime/engine/llms/base_llm.py +60 -0
- agentscope_runtime/engine/llms/qwen_llm.py +47 -0
- agentscope_runtime/engine/misc/__init__.py +0 -0
- agentscope_runtime/engine/runner.py +186 -0
- agentscope_runtime/engine/schemas/__init__.py +0 -0
- agentscope_runtime/engine/schemas/agent_schemas.py +551 -0
- agentscope_runtime/engine/schemas/context.py +54 -0
- agentscope_runtime/engine/services/__init__.py +9 -0
- agentscope_runtime/engine/services/base.py +77 -0
- agentscope_runtime/engine/services/context_manager.py +129 -0
- agentscope_runtime/engine/services/environment_manager.py +50 -0
- agentscope_runtime/engine/services/manager.py +174 -0
- agentscope_runtime/engine/services/memory_service.py +270 -0
- agentscope_runtime/engine/services/sandbox_service.py +198 -0
- agentscope_runtime/engine/services/session_history_service.py +256 -0
- agentscope_runtime/engine/tracing/__init__.py +40 -0
- agentscope_runtime/engine/tracing/base.py +309 -0
- agentscope_runtime/engine/tracing/local_logging_handler.py +356 -0
- agentscope_runtime/engine/tracing/tracing_metric.py +69 -0
- agentscope_runtime/engine/tracing/wrapper.py +321 -0
- agentscope_runtime/sandbox/__init__.py +14 -0
- agentscope_runtime/sandbox/box/__init__.py +0 -0
- agentscope_runtime/sandbox/box/base/__init__.py +0 -0
- agentscope_runtime/sandbox/box/base/base_sandbox.py +37 -0
- agentscope_runtime/sandbox/box/base/box/__init__.py +0 -0
- agentscope_runtime/sandbox/box/browser/__init__.py +0 -0
- agentscope_runtime/sandbox/box/browser/box/__init__.py +0 -0
- agentscope_runtime/sandbox/box/browser/browser_sandbox.py +176 -0
- agentscope_runtime/sandbox/box/dummy/__init__.py +0 -0
- agentscope_runtime/sandbox/box/dummy/dummy_sandbox.py +26 -0
- agentscope_runtime/sandbox/box/filesystem/__init__.py +0 -0
- agentscope_runtime/sandbox/box/filesystem/box/__init__.py +0 -0
- agentscope_runtime/sandbox/box/filesystem/filesystem_sandbox.py +87 -0
- agentscope_runtime/sandbox/box/sandbox.py +115 -0
- agentscope_runtime/sandbox/box/shared/__init__.py +0 -0
- agentscope_runtime/sandbox/box/shared/app.py +44 -0
- agentscope_runtime/sandbox/box/shared/dependencies/__init__.py +5 -0
- agentscope_runtime/sandbox/box/shared/dependencies/deps.py +22 -0
- agentscope_runtime/sandbox/box/shared/routers/__init__.py +12 -0
- agentscope_runtime/sandbox/box/shared/routers/generic.py +173 -0
- agentscope_runtime/sandbox/box/shared/routers/mcp.py +207 -0
- agentscope_runtime/sandbox/box/shared/routers/mcp_utils.py +153 -0
- agentscope_runtime/sandbox/box/shared/routers/runtime_watcher.py +187 -0
- agentscope_runtime/sandbox/box/shared/routers/workspace.py +325 -0
- agentscope_runtime/sandbox/box/training_box/__init__.py +0 -0
- agentscope_runtime/sandbox/box/training_box/base.py +120 -0
- agentscope_runtime/sandbox/box/training_box/env_service.py +752 -0
- agentscope_runtime/sandbox/box/training_box/environments/__init__.py +0 -0
- agentscope_runtime/sandbox/box/training_box/environments/appworld/appworld_env.py +987 -0
- agentscope_runtime/sandbox/box/training_box/registry.py +54 -0
- agentscope_runtime/sandbox/box/training_box/src/trajectory.py +278 -0
- agentscope_runtime/sandbox/box/training_box/training_box.py +219 -0
- agentscope_runtime/sandbox/build.py +213 -0
- agentscope_runtime/sandbox/client/__init__.py +5 -0
- agentscope_runtime/sandbox/client/http_client.py +527 -0
- agentscope_runtime/sandbox/client/training_client.py +265 -0
- agentscope_runtime/sandbox/constant.py +5 -0
- agentscope_runtime/sandbox/custom/__init__.py +16 -0
- agentscope_runtime/sandbox/custom/custom_sandbox.py +40 -0
- agentscope_runtime/sandbox/custom/example.py +37 -0
- agentscope_runtime/sandbox/enums.py +68 -0
- agentscope_runtime/sandbox/manager/__init__.py +4 -0
- agentscope_runtime/sandbox/manager/collections/__init__.py +22 -0
- agentscope_runtime/sandbox/manager/collections/base_mapping.py +20 -0
- agentscope_runtime/sandbox/manager/collections/base_queue.py +25 -0
- agentscope_runtime/sandbox/manager/collections/base_set.py +25 -0
- agentscope_runtime/sandbox/manager/collections/in_memory_mapping.py +22 -0
- agentscope_runtime/sandbox/manager/collections/in_memory_queue.py +28 -0
- agentscope_runtime/sandbox/manager/collections/in_memory_set.py +27 -0
- agentscope_runtime/sandbox/manager/collections/redis_mapping.py +26 -0
- agentscope_runtime/sandbox/manager/collections/redis_queue.py +27 -0
- agentscope_runtime/sandbox/manager/collections/redis_set.py +23 -0
- agentscope_runtime/sandbox/manager/container_clients/__init__.py +8 -0
- agentscope_runtime/sandbox/manager/container_clients/base_client.py +39 -0
- agentscope_runtime/sandbox/manager/container_clients/docker_client.py +170 -0
- agentscope_runtime/sandbox/manager/sandbox_manager.py +694 -0
- agentscope_runtime/sandbox/manager/server/__init__.py +0 -0
- agentscope_runtime/sandbox/manager/server/app.py +194 -0
- agentscope_runtime/sandbox/manager/server/config.py +68 -0
- agentscope_runtime/sandbox/manager/server/models.py +17 -0
- agentscope_runtime/sandbox/manager/storage/__init__.py +10 -0
- agentscope_runtime/sandbox/manager/storage/data_storage.py +16 -0
- agentscope_runtime/sandbox/manager/storage/local_storage.py +44 -0
- agentscope_runtime/sandbox/manager/storage/oss_storage.py +89 -0
- agentscope_runtime/sandbox/manager/utils.py +78 -0
- agentscope_runtime/sandbox/mcp_server.py +192 -0
- agentscope_runtime/sandbox/model/__init__.py +12 -0
- agentscope_runtime/sandbox/model/api.py +16 -0
- agentscope_runtime/sandbox/model/container.py +72 -0
- agentscope_runtime/sandbox/model/manager_config.py +158 -0
- agentscope_runtime/sandbox/registry.py +129 -0
- agentscope_runtime/sandbox/tools/__init__.py +12 -0
- agentscope_runtime/sandbox/tools/base/__init__.py +8 -0
- agentscope_runtime/sandbox/tools/base/tool.py +52 -0
- agentscope_runtime/sandbox/tools/browser/__init__.py +57 -0
- agentscope_runtime/sandbox/tools/browser/tool.py +597 -0
- agentscope_runtime/sandbox/tools/filesystem/__init__.py +32 -0
- agentscope_runtime/sandbox/tools/filesystem/tool.py +319 -0
- agentscope_runtime/sandbox/tools/function_tool.py +321 -0
- agentscope_runtime/sandbox/tools/mcp_tool.py +191 -0
- agentscope_runtime/sandbox/tools/sandbox_tool.py +104 -0
- agentscope_runtime/sandbox/tools/tool.py +123 -0
- agentscope_runtime/sandbox/tools/utils.py +68 -0
- agentscope_runtime/version.py +2 -0
- agentscope_runtime-0.1.0.dist-info/METADATA +327 -0
- agentscope_runtime-0.1.0.dist-info/RECORD +131 -0
- agentscope_runtime-0.1.0.dist-info/WHEEL +5 -0
- agentscope_runtime-0.1.0.dist-info/entry_points.txt +4 -0
- agentscope_runtime-0.1.0.dist-info/licenses/LICENSE +202 -0
- agentscope_runtime-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
# pylint:disable=too-many-nested-blocks, too-many-branches, too-many-statements
|
|
3
|
+
import json
|
|
4
|
+
from typing import Optional, Type
|
|
5
|
+
|
|
6
|
+
from agno.agent import Agent as AgAgent
|
|
7
|
+
from agno.models.base import Model
|
|
8
|
+
from agno.run.response import (
|
|
9
|
+
RunResponseContentEvent,
|
|
10
|
+
ToolCallStartedEvent,
|
|
11
|
+
ToolCallCompletedEvent,
|
|
12
|
+
)
|
|
13
|
+
from agno.tools.function import Function
|
|
14
|
+
|
|
15
|
+
from ..agents import Agent
|
|
16
|
+
from ..schemas.context import Context
|
|
17
|
+
from ..schemas.agent_schemas import (
|
|
18
|
+
Message,
|
|
19
|
+
TextContent,
|
|
20
|
+
DataContent,
|
|
21
|
+
FunctionCall,
|
|
22
|
+
FunctionCallOutput,
|
|
23
|
+
MessageType,
|
|
24
|
+
RunStatus,
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class AgnoContextAdapter:
|
|
29
|
+
def __init__(self, context: Context, attr: dict):
|
|
30
|
+
self.context = context
|
|
31
|
+
self.attr = attr
|
|
32
|
+
|
|
33
|
+
# Adapted attribute
|
|
34
|
+
self.toolkit = None
|
|
35
|
+
self.model = None
|
|
36
|
+
self.memory = None
|
|
37
|
+
self.new_message = None
|
|
38
|
+
|
|
39
|
+
async def initialize(self):
|
|
40
|
+
self.model = await self.adapt_model()
|
|
41
|
+
self.memory = await self.adapt_memory()
|
|
42
|
+
self.new_message = await self.adapt_new_message()
|
|
43
|
+
self.toolkit = await self.adapt_tools()
|
|
44
|
+
|
|
45
|
+
async def adapt_memory(self):
|
|
46
|
+
messages = []
|
|
47
|
+
|
|
48
|
+
# Build context
|
|
49
|
+
for msg in self.context.session.messages[:-1]: # Exclude the last one
|
|
50
|
+
messages.append(AgnoContextAdapter.converter(msg))
|
|
51
|
+
|
|
52
|
+
return messages
|
|
53
|
+
|
|
54
|
+
@staticmethod
|
|
55
|
+
def converter(message: Message):
|
|
56
|
+
return dict(message)
|
|
57
|
+
|
|
58
|
+
async def adapt_new_message(self):
|
|
59
|
+
last_message = self.context.session.messages[-1]
|
|
60
|
+
return AgnoContextAdapter.converter(last_message)
|
|
61
|
+
|
|
62
|
+
async def adapt_model(self):
|
|
63
|
+
return self.attr["model"]
|
|
64
|
+
|
|
65
|
+
async def adapt_tools(self):
|
|
66
|
+
toolkit = self.attr["agent_config"].get("toolkit", [])
|
|
67
|
+
tools = self.attr["tools"]
|
|
68
|
+
|
|
69
|
+
# in case, tools is None and tools == []
|
|
70
|
+
if not tools:
|
|
71
|
+
return toolkit
|
|
72
|
+
|
|
73
|
+
if self.context.activate_tools:
|
|
74
|
+
# Only add activated tool
|
|
75
|
+
activated_tools = self.context.activate_tools
|
|
76
|
+
else:
|
|
77
|
+
from ...sandbox.tools.utils import setup_tools
|
|
78
|
+
|
|
79
|
+
activated_tools = setup_tools(
|
|
80
|
+
tools=self.attr["tools"],
|
|
81
|
+
environment_manager=self.context.environment_manager,
|
|
82
|
+
session_id=self.context.session.id,
|
|
83
|
+
user_id=self.context.session.user_id,
|
|
84
|
+
include_schemas=False,
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
for tool in activated_tools:
|
|
88
|
+
func = Function(
|
|
89
|
+
name=tool.name,
|
|
90
|
+
description=tool.schema["function"]["description"],
|
|
91
|
+
parameters=tool.schema["function"]["parameters"],
|
|
92
|
+
entrypoint=tool.__call__,
|
|
93
|
+
)
|
|
94
|
+
toolkit.append(func)
|
|
95
|
+
|
|
96
|
+
return toolkit
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
class AgnoAgent(Agent):
|
|
100
|
+
def __init__(
|
|
101
|
+
self,
|
|
102
|
+
name: str,
|
|
103
|
+
model: Model,
|
|
104
|
+
tools=None,
|
|
105
|
+
agent_config=None,
|
|
106
|
+
agent_builder: Optional[Type[AgAgent]] = AgAgent,
|
|
107
|
+
):
|
|
108
|
+
super().__init__(name=name, agent_config=agent_config)
|
|
109
|
+
|
|
110
|
+
assert isinstance(
|
|
111
|
+
model,
|
|
112
|
+
Model,
|
|
113
|
+
), "model must be a subclass of Model in Agno"
|
|
114
|
+
|
|
115
|
+
# Set default agent_builder
|
|
116
|
+
if agent_builder is None:
|
|
117
|
+
agent_builder = Agent
|
|
118
|
+
|
|
119
|
+
assert issubclass(
|
|
120
|
+
agent_builder,
|
|
121
|
+
AgAgent,
|
|
122
|
+
), "agent_builder must be a subclass of Agent in Agno"
|
|
123
|
+
|
|
124
|
+
# Replace name if not exists
|
|
125
|
+
self.agent_config["name"] = self.agent_config.get("name") or name
|
|
126
|
+
|
|
127
|
+
self._attr = {
|
|
128
|
+
"model": model,
|
|
129
|
+
"tools": tools,
|
|
130
|
+
"agent_config": self.agent_config,
|
|
131
|
+
"agent_builder": agent_builder,
|
|
132
|
+
}
|
|
133
|
+
self._agent = None
|
|
134
|
+
self.tools = tools
|
|
135
|
+
|
|
136
|
+
def copy(self) -> "AgnoAgent":
|
|
137
|
+
return AgnoAgent(**self._attr)
|
|
138
|
+
|
|
139
|
+
def build(self, as_context):
|
|
140
|
+
self._agent = self._attr["agent_builder"](
|
|
141
|
+
**self._attr["agent_config"],
|
|
142
|
+
model=as_context.model,
|
|
143
|
+
tools=as_context.toolkit,
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
return self._agent
|
|
147
|
+
|
|
148
|
+
async def run(self, context):
|
|
149
|
+
ag_context = AgnoContextAdapter(context=context, attr=self._attr)
|
|
150
|
+
await ag_context.initialize()
|
|
151
|
+
|
|
152
|
+
# We should always build a new agent since the state is manage outside
|
|
153
|
+
# the agent
|
|
154
|
+
self._agent = self.build(ag_context)
|
|
155
|
+
|
|
156
|
+
resp = await self._agent.arun(
|
|
157
|
+
ag_context.new_message,
|
|
158
|
+
messages=ag_context.memory,
|
|
159
|
+
stream=True,
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
text_message = Message(
|
|
163
|
+
type=MessageType.MESSAGE,
|
|
164
|
+
role="assistant",
|
|
165
|
+
status=RunStatus.InProgress,
|
|
166
|
+
)
|
|
167
|
+
yield text_message
|
|
168
|
+
|
|
169
|
+
text_delta_content = TextContent(delta=True)
|
|
170
|
+
is_text_delta = False
|
|
171
|
+
async for event in resp:
|
|
172
|
+
if isinstance(event, RunResponseContentEvent):
|
|
173
|
+
is_text_delta = True
|
|
174
|
+
text_delta_content.text = event.content
|
|
175
|
+
text_delta_content = text_message.add_delta_content(
|
|
176
|
+
new_content=text_delta_content,
|
|
177
|
+
)
|
|
178
|
+
yield text_delta_content
|
|
179
|
+
elif isinstance(event, ToolCallStartedEvent):
|
|
180
|
+
json_str = json.dumps(event.tool.tool_args)
|
|
181
|
+
data = DataContent(
|
|
182
|
+
data=FunctionCall(
|
|
183
|
+
call_id=event.tool.tool_call_id,
|
|
184
|
+
name=event.tool.tool_name,
|
|
185
|
+
arguments=json_str,
|
|
186
|
+
).model_dump(),
|
|
187
|
+
)
|
|
188
|
+
message = Message(
|
|
189
|
+
type=MessageType.PLUGIN_CALL,
|
|
190
|
+
role="assistant",
|
|
191
|
+
status=RunStatus.Completed,
|
|
192
|
+
content=[data],
|
|
193
|
+
)
|
|
194
|
+
yield message
|
|
195
|
+
elif isinstance(event, ToolCallCompletedEvent):
|
|
196
|
+
data = DataContent(
|
|
197
|
+
data=FunctionCallOutput(
|
|
198
|
+
call_id=event.tool.tool_call_id,
|
|
199
|
+
output=str(event.tool.result),
|
|
200
|
+
).model_dump(),
|
|
201
|
+
)
|
|
202
|
+
message = Message(
|
|
203
|
+
type=MessageType.PLUGIN_CALL_OUTPUT,
|
|
204
|
+
role="assistant",
|
|
205
|
+
status=RunStatus.Completed,
|
|
206
|
+
content=[data],
|
|
207
|
+
)
|
|
208
|
+
yield message
|
|
209
|
+
|
|
210
|
+
if is_text_delta:
|
|
211
|
+
yield text_message.content_completed(text_delta_content.index)
|
|
212
|
+
yield text_message.completed()
|
|
213
|
+
|
|
214
|
+
async def run_async(
|
|
215
|
+
self,
|
|
216
|
+
context,
|
|
217
|
+
**kwargs,
|
|
218
|
+
):
|
|
219
|
+
async for event in self.run(context):
|
|
220
|
+
yield event
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
from typing import AsyncGenerator
|
|
3
|
+
|
|
4
|
+
from ..schemas.agent_schemas import Event
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class Agent:
|
|
8
|
+
def __init__(
|
|
9
|
+
self,
|
|
10
|
+
name: str = "",
|
|
11
|
+
description: str = "",
|
|
12
|
+
before_agent_callback=None,
|
|
13
|
+
after_agent_callback=None,
|
|
14
|
+
agent_config=None,
|
|
15
|
+
**kwargs,
|
|
16
|
+
):
|
|
17
|
+
self.name = name
|
|
18
|
+
self.description = description
|
|
19
|
+
self.before_agent_callback = before_agent_callback or []
|
|
20
|
+
self.after_agent_callback = after_agent_callback or []
|
|
21
|
+
self.agent_config = agent_config or {}
|
|
22
|
+
self.kwargs = kwargs
|
|
23
|
+
|
|
24
|
+
async def run_async(
|
|
25
|
+
self,
|
|
26
|
+
context,
|
|
27
|
+
**kwargs,
|
|
28
|
+
) -> AsyncGenerator[Event, None]:
|
|
29
|
+
raise NotImplementedError("Subclasses must implement this method")
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
import json
|
|
3
|
+
|
|
4
|
+
from langgraph.graph.state import CompiledStateGraph
|
|
5
|
+
|
|
6
|
+
from .base_agent import Agent
|
|
7
|
+
from ..schemas.agent_schemas import (
|
|
8
|
+
Message,
|
|
9
|
+
TextContent,
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _state_folder(messages):
|
|
14
|
+
if len(messages) > 0:
|
|
15
|
+
return json.loads(messages[0]["content"])
|
|
16
|
+
else:
|
|
17
|
+
return []
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _state_unfolder(state):
|
|
21
|
+
state_jsons = json.dumps(state)
|
|
22
|
+
return state_jsons
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class LangGraphAgent(Agent):
|
|
26
|
+
def __init__(
|
|
27
|
+
self,
|
|
28
|
+
graph: CompiledStateGraph = None,
|
|
29
|
+
state_folder=_state_folder,
|
|
30
|
+
state_unfolder=_state_unfolder,
|
|
31
|
+
**kwargs,
|
|
32
|
+
):
|
|
33
|
+
super().__init__(**kwargs)
|
|
34
|
+
self.state_folder = state_folder
|
|
35
|
+
self.state_unfolder = state_unfolder
|
|
36
|
+
self.graph = graph
|
|
37
|
+
|
|
38
|
+
async def run_async(
|
|
39
|
+
self,
|
|
40
|
+
context,
|
|
41
|
+
**kwargs,
|
|
42
|
+
):
|
|
43
|
+
# fold the last m
|
|
44
|
+
list_messages = []
|
|
45
|
+
for m in context.session.messages:
|
|
46
|
+
dumped = m.model_dump()
|
|
47
|
+
dumped["content"] = dumped["content"][0]["text"]
|
|
48
|
+
list_messages.append(dumped)
|
|
49
|
+
|
|
50
|
+
_input = self.state_folder(list_messages)
|
|
51
|
+
|
|
52
|
+
output = await self.graph.ainvoke(_input)
|
|
53
|
+
content = self.state_unfolder(output)
|
|
54
|
+
|
|
55
|
+
message = Message(role="assistant")
|
|
56
|
+
text = TextContent(type="text", text=content)
|
|
57
|
+
message.add_content(text)
|
|
58
|
+
message.completed()
|
|
59
|
+
yield message
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
from .base_agent import Agent
|
|
4
|
+
from ..llms import BaseLLM
|
|
5
|
+
from ..schemas.agent_schemas import (
|
|
6
|
+
Message,
|
|
7
|
+
TextContent,
|
|
8
|
+
convert_to_openai_messages,
|
|
9
|
+
MessageType,
|
|
10
|
+
convert_to_openai_tools,
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class LLMAgent(Agent):
|
|
15
|
+
def __init__(
|
|
16
|
+
self,
|
|
17
|
+
model: BaseLLM,
|
|
18
|
+
**kwargs,
|
|
19
|
+
):
|
|
20
|
+
super().__init__(
|
|
21
|
+
**kwargs,
|
|
22
|
+
)
|
|
23
|
+
self.model = model
|
|
24
|
+
|
|
25
|
+
async def run_async(
|
|
26
|
+
self,
|
|
27
|
+
context,
|
|
28
|
+
**kwargs,
|
|
29
|
+
):
|
|
30
|
+
# agent request --> model request
|
|
31
|
+
openai_messages = convert_to_openai_messages(context.session.messages)
|
|
32
|
+
tools = convert_to_openai_tools(context.request.tools)
|
|
33
|
+
|
|
34
|
+
# Step 3: Create initial Message
|
|
35
|
+
message = Message(type=MessageType.MESSAGE, role="assistant")
|
|
36
|
+
yield message.in_progress()
|
|
37
|
+
|
|
38
|
+
# Step 4: LLM Content delta
|
|
39
|
+
text_delta_content = TextContent(delta=True)
|
|
40
|
+
async for chunk in self.model.chat_stream(openai_messages, tools):
|
|
41
|
+
delta = chunk.choices[0].delta
|
|
42
|
+
|
|
43
|
+
if delta.content:
|
|
44
|
+
text_delta_content.text = delta.content
|
|
45
|
+
text_delta_content = message.add_delta_content(
|
|
46
|
+
new_content=text_delta_content,
|
|
47
|
+
)
|
|
48
|
+
yield text_delta_content
|
|
49
|
+
|
|
50
|
+
message.completed()
|
|
51
|
+
yield message
|
|
File without changes
|