veadk-python 0.2.5__py3-none-any.whl → 0.2.7__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.
Potentially problematic release.
This version of veadk-python might be problematic. Click here for more details.
- veadk/agent.py +29 -22
- veadk/agent_builder.py +94 -0
- veadk/auth/__init__.py +13 -0
- veadk/auth/base_auth.py +22 -0
- veadk/auth/veauth/__init__.py +13 -0
- veadk/auth/veauth/apmplus_veauth.py +65 -0
- veadk/auth/veauth/ark_veauth.py +77 -0
- veadk/auth/veauth/base_veauth.py +50 -0
- veadk/auth/veauth/cozeloop_veauth.py +13 -0
- veadk/auth/veauth/prompt_pilot_veauth.py +60 -0
- veadk/auth/veauth/vesearch_veauth.py +62 -0
- veadk/cli/cli.py +2 -0
- veadk/cli/cli_deploy.py +5 -2
- veadk/cli/cli_init.py +25 -6
- veadk/cli/cli_pipeline.py +220 -0
- veadk/cli/cli_prompt.py +4 -4
- veadk/config.py +45 -81
- veadk/configs/__init__.py +13 -0
- veadk/configs/database_configs.py +83 -0
- veadk/configs/model_configs.py +42 -0
- veadk/configs/tool_configs.py +42 -0
- veadk/configs/tracing_configs.py +110 -0
- veadk/consts.py +32 -1
- veadk/database/database_adapter.py +256 -3
- veadk/database/kv/redis_database.py +47 -0
- veadk/database/local_database.py +23 -4
- veadk/database/relational/mysql_database.py +58 -0
- veadk/database/vector/opensearch_vector_database.py +6 -3
- veadk/database/viking/viking_database.py +272 -36
- veadk/integrations/ve_code_pipeline/__init__.py +13 -0
- veadk/integrations/ve_code_pipeline/ve_code_pipeline.py +431 -0
- veadk/integrations/ve_cozeloop/__init__.py +13 -0
- veadk/integrations/ve_cozeloop/ve_cozeloop.py +96 -0
- veadk/integrations/ve_cr/__init__.py +13 -0
- veadk/integrations/ve_cr/ve_cr.py +220 -0
- veadk/integrations/ve_faas/template/cookiecutter.json +3 -2
- veadk/integrations/ve_faas/template/{{cookiecutter.local_dir_name}}/deploy.py +2 -2
- veadk/integrations/ve_faas/template/{{cookiecutter.local_dir_name}}/src/agent.py +1 -1
- veadk/integrations/ve_faas/template/{{cookiecutter.local_dir_name}}/src/app.py +24 -1
- veadk/integrations/ve_faas/template/{{cookiecutter.local_dir_name}}/src/requirements.txt +3 -1
- veadk/integrations/ve_faas/template/{{cookiecutter.local_dir_name}}/src/run.sh +1 -12
- veadk/integrations/ve_faas/ve_faas.py +352 -35
- veadk/integrations/ve_faas/web_template/cookiecutter.json +17 -0
- veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/__init__.py +13 -0
- veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/clean.py +23 -0
- veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/config.yaml.example +2 -0
- veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/deploy.py +41 -0
- veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/Dockerfile +23 -0
- veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/app.py +123 -0
- veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/init_db.py +46 -0
- veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/models.py +36 -0
- veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/requirements.txt +4 -0
- veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/run.sh +21 -0
- veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/static/css/style.css +368 -0
- veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/static/js/admin.js +0 -0
- veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/templates/admin/dashboard.html +21 -0
- veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/templates/admin/edit_post.html +24 -0
- veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/templates/admin/login.html +21 -0
- veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/templates/admin/posts.html +53 -0
- veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/templates/base.html +45 -0
- veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/templates/index.html +29 -0
- veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/templates/post.html +14 -0
- veadk/integrations/ve_prompt_pilot/ve_prompt_pilot.py +6 -3
- veadk/integrations/ve_tls/__init__.py +13 -0
- veadk/integrations/ve_tls/utils.py +117 -0
- veadk/integrations/ve_tls/ve_tls.py +208 -0
- veadk/integrations/ve_tos/ve_tos.py +128 -73
- veadk/knowledgebase/knowledgebase.py +116 -20
- veadk/memory/long_term_memory.py +20 -21
- veadk/memory/short_term_memory_processor.py +9 -4
- veadk/runner.py +213 -223
- veadk/tools/builtin_tools/vesearch.py +2 -2
- veadk/tools/builtin_tools/video_generate.py +27 -20
- veadk/tracing/telemetry/attributes/extractors/common_attributes_extractors.py +5 -0
- veadk/tracing/telemetry/attributes/extractors/llm_attributes_extractors.py +253 -129
- veadk/tracing/telemetry/attributes/extractors/types.py +15 -4
- veadk/tracing/telemetry/exporters/apmplus_exporter.py +158 -12
- veadk/tracing/telemetry/exporters/cozeloop_exporter.py +4 -9
- veadk/tracing/telemetry/exporters/tls_exporter.py +4 -10
- veadk/tracing/telemetry/opentelemetry_tracer.py +11 -5
- veadk/tracing/telemetry/telemetry.py +23 -5
- veadk/utils/logger.py +1 -1
- veadk/utils/misc.py +48 -0
- veadk/utils/volcengine_sign.py +6 -2
- veadk/version.py +1 -1
- {veadk_python-0.2.5.dist-info → veadk_python-0.2.7.dist-info}/METADATA +2 -1
- veadk_python-0.2.7.dist-info/RECORD +172 -0
- veadk_python-0.2.5.dist-info/RECORD +0 -127
- /veadk/integrations/ve_faas/template/{{cookiecutter.local_dir_name}}/src/{{{ cookiecutter.app_name|replace('-', '_') }} → {{ cookiecutter.app_name }}}/__init__.py +0 -0
- /veadk/integrations/ve_faas/template/{{cookiecutter.local_dir_name}}/src/{{{ cookiecutter.app_name|replace('-', '_') }} → {{ cookiecutter.app_name }}}/agent.py +0 -0
- {veadk_python-0.2.5.dist-info → veadk_python-0.2.7.dist-info}/WHEEL +0 -0
- {veadk_python-0.2.5.dist-info → veadk_python-0.2.7.dist-info}/entry_points.txt +0 -0
- {veadk_python-0.2.5.dist-info → veadk_python-0.2.7.dist-info}/licenses/LICENSE +0 -0
- {veadk_python-0.2.5.dist-info → veadk_python-0.2.7.dist-info}/top_level.txt +0 -0
veadk/runner.py
CHANGED
|
@@ -11,32 +11,33 @@
|
|
|
11
11
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
12
|
# See the License for the specific language governing permissions and
|
|
13
13
|
# limitations under the License.
|
|
14
|
+
|
|
14
15
|
import asyncio
|
|
16
|
+
import functools
|
|
17
|
+
from types import MethodType
|
|
15
18
|
from typing import Union
|
|
16
19
|
|
|
20
|
+
from google import genai
|
|
17
21
|
from google.adk.agents import RunConfig
|
|
22
|
+
from google.adk.agents.base_agent import BaseAgent
|
|
18
23
|
from google.adk.agents.invocation_context import LlmCallsLimitExceededError
|
|
19
|
-
from google.adk.agents.run_config import StreamingMode
|
|
20
|
-
from google.adk.plugins.base_plugin import BasePlugin
|
|
21
24
|
from google.adk.runners import Runner as ADKRunner
|
|
22
25
|
from google.genai import types
|
|
23
26
|
from google.genai.types import Blob
|
|
24
27
|
|
|
25
|
-
from veadk.a2a.remote_ve_agent import RemoteVeAgent
|
|
26
28
|
from veadk.agent import Agent
|
|
27
29
|
from veadk.agents.loop_agent import LoopAgent
|
|
28
30
|
from veadk.agents.parallel_agent import ParallelAgent
|
|
29
31
|
from veadk.agents.sequential_agent import SequentialAgent
|
|
32
|
+
from veadk.config import getenv
|
|
30
33
|
from veadk.evaluation import EvalSetRecorder
|
|
31
34
|
from veadk.memory.short_term_memory import ShortTermMemory
|
|
32
35
|
from veadk.types import MediaMessage
|
|
33
36
|
from veadk.utils.logger import get_logger
|
|
34
|
-
from veadk.utils.misc import read_png_to_bytes
|
|
35
|
-
from veadk.integrations.ve_tos.ve_tos import VeTOS
|
|
37
|
+
from veadk.utils.misc import formatted_timestamp, read_png_to_bytes
|
|
36
38
|
|
|
37
39
|
logger = get_logger(__name__)
|
|
38
40
|
|
|
39
|
-
|
|
40
41
|
RunnerMessage = Union[
|
|
41
42
|
str, # single turn text-based prompt
|
|
42
43
|
list[str], # multiple turn text-based prompt
|
|
@@ -45,111 +46,227 @@ RunnerMessage = Union[
|
|
|
45
46
|
list[MediaMessage | str], # multiple turn prompt with media and text-based prompt
|
|
46
47
|
]
|
|
47
48
|
|
|
48
|
-
VeAgent = Union[Agent, RemoteVeAgent, SequentialAgent, ParallelAgent, LoopAgent]
|
|
49
49
|
|
|
50
|
+
def pre_run_process(self, process_func, new_message, user_id, session_id):
|
|
51
|
+
if new_message.parts:
|
|
52
|
+
for part in new_message.parts:
|
|
53
|
+
if (
|
|
54
|
+
part.inline_data
|
|
55
|
+
and part.inline_data.mime_type == "image/png"
|
|
56
|
+
and self.upload_inline_data_to_tos
|
|
57
|
+
):
|
|
58
|
+
process_func(
|
|
59
|
+
part,
|
|
60
|
+
self.app_name,
|
|
61
|
+
user_id,
|
|
62
|
+
session_id,
|
|
63
|
+
)
|
|
64
|
+
return
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def post_run_process(self):
|
|
68
|
+
return
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def intercept_new_message(process_func):
|
|
72
|
+
def decorator(func):
|
|
73
|
+
@functools.wraps(func)
|
|
74
|
+
async def wrapper(
|
|
75
|
+
self,
|
|
76
|
+
*,
|
|
77
|
+
user_id: str,
|
|
78
|
+
session_id: str,
|
|
79
|
+
new_message: types.Content,
|
|
80
|
+
**kwargs,
|
|
81
|
+
):
|
|
82
|
+
pre_run_process(self, process_func, new_message, user_id, session_id)
|
|
83
|
+
|
|
84
|
+
async for event in func(
|
|
85
|
+
user_id=user_id,
|
|
86
|
+
session_id=session_id,
|
|
87
|
+
new_message=new_message,
|
|
88
|
+
**kwargs,
|
|
89
|
+
):
|
|
90
|
+
yield event
|
|
91
|
+
|
|
92
|
+
post_run_process(self)
|
|
93
|
+
|
|
94
|
+
return wrapper
|
|
95
|
+
|
|
96
|
+
return decorator
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def _convert_messages(
|
|
100
|
+
messages: RunnerMessage,
|
|
101
|
+
app_name: str,
|
|
102
|
+
user_id: str,
|
|
103
|
+
session_id: str,
|
|
104
|
+
) -> list:
|
|
105
|
+
"""Convert VeADK formatted messages to Google ADK formatted messages."""
|
|
106
|
+
if isinstance(messages, str):
|
|
107
|
+
_messages = [types.Content(role="user", parts=[types.Part(text=messages)])]
|
|
108
|
+
elif isinstance(messages, MediaMessage):
|
|
109
|
+
assert messages.media.endswith(".png"), (
|
|
110
|
+
"The MediaMessage only supports PNG format file for now."
|
|
111
|
+
)
|
|
112
|
+
_messages = [
|
|
113
|
+
types.Content(
|
|
114
|
+
role="user",
|
|
115
|
+
parts=[
|
|
116
|
+
types.Part(text=messages.text),
|
|
117
|
+
types.Part(
|
|
118
|
+
inline_data=Blob(
|
|
119
|
+
display_name=messages.media,
|
|
120
|
+
data=read_png_to_bytes(messages.media),
|
|
121
|
+
mime_type="image/png",
|
|
122
|
+
)
|
|
123
|
+
),
|
|
124
|
+
],
|
|
125
|
+
)
|
|
126
|
+
]
|
|
127
|
+
elif isinstance(messages, list):
|
|
128
|
+
converted_messages = []
|
|
129
|
+
for message in messages:
|
|
130
|
+
converted_messages.extend(
|
|
131
|
+
_convert_messages(message, app_name, user_id, session_id)
|
|
132
|
+
)
|
|
133
|
+
_messages = converted_messages
|
|
134
|
+
else:
|
|
135
|
+
raise ValueError(f"Unknown message type: {type(messages)}")
|
|
136
|
+
|
|
137
|
+
return _messages
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def _upload_image_to_tos(
|
|
141
|
+
part: genai.types.Part, app_name: str, user_id: str, session_id: str
|
|
142
|
+
) -> None:
|
|
143
|
+
try:
|
|
144
|
+
if part.inline_data and part.inline_data.display_name and part.inline_data.data:
|
|
145
|
+
from veadk.integrations.ve_tos.ve_tos import VeTOS
|
|
146
|
+
|
|
147
|
+
ve_tos = VeTOS()
|
|
148
|
+
|
|
149
|
+
object_key, tos_url = ve_tos.build_tos_url(
|
|
150
|
+
user_id=user_id,
|
|
151
|
+
app_name=app_name,
|
|
152
|
+
session_id=session_id,
|
|
153
|
+
data_path=part.inline_data.display_name,
|
|
154
|
+
)
|
|
155
|
+
|
|
156
|
+
upload_task = ve_tos.upload(object_key, part.inline_data.data)
|
|
157
|
+
|
|
158
|
+
if upload_task is not None:
|
|
159
|
+
asyncio.create_task(upload_task)
|
|
50
160
|
|
|
51
|
-
|
|
161
|
+
part.inline_data.display_name = tos_url
|
|
162
|
+
except Exception as e:
|
|
163
|
+
logger.error(f"Upload to TOS failed: {e}")
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
class Runner(ADKRunner):
|
|
52
167
|
def __init__(
|
|
53
168
|
self,
|
|
54
|
-
agent:
|
|
169
|
+
agent: BaseAgent | Agent,
|
|
55
170
|
short_term_memory: ShortTermMemory | None = None,
|
|
56
|
-
plugins: list[BasePlugin] | None = None,
|
|
57
171
|
app_name: str = "veadk_default_app",
|
|
58
172
|
user_id: str = "veadk_default_user",
|
|
59
|
-
|
|
60
|
-
|
|
173
|
+
upload_inline_data_to_tos: bool = False,
|
|
174
|
+
*args,
|
|
175
|
+
**kwargs,
|
|
176
|
+
) -> None:
|
|
61
177
|
self.user_id = user_id
|
|
178
|
+
self.long_term_memory = None
|
|
179
|
+
self.short_term_memory = short_term_memory
|
|
180
|
+
self.upload_inline_data_to_tos = upload_inline_data_to_tos
|
|
62
181
|
|
|
63
|
-
|
|
182
|
+
session_service = kwargs.pop("session_service", None)
|
|
183
|
+
memory_service = kwargs.pop("memory_service", None)
|
|
64
184
|
|
|
65
|
-
if
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
else:
|
|
71
|
-
self.short_term_memory = short_term_memory
|
|
72
|
-
|
|
73
|
-
self.session_service = self.short_term_memory.session_service
|
|
185
|
+
if session_service:
|
|
186
|
+
if short_term_memory:
|
|
187
|
+
logger.warning(
|
|
188
|
+
"Short term memory is enabled, but session service is also provided. We will use session service from runner argument."
|
|
189
|
+
)
|
|
74
190
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
191
|
+
if not session_service:
|
|
192
|
+
if short_term_memory:
|
|
193
|
+
session_service = short_term_memory.session_service
|
|
194
|
+
logger.debug(
|
|
195
|
+
f"Use session service {session_service} from short term memory."
|
|
196
|
+
)
|
|
197
|
+
else:
|
|
198
|
+
logger.warning(
|
|
199
|
+
"No short term memory or session service provided, use an in-memory one instead."
|
|
200
|
+
)
|
|
201
|
+
short_term_memory = ShortTermMemory()
|
|
202
|
+
self.short_term_memory = short_term_memory
|
|
203
|
+
session_service = short_term_memory.session_service
|
|
204
|
+
|
|
205
|
+
if memory_service:
|
|
206
|
+
if hasattr(agent, "long_term_memory") and agent.long_term_memory: # type: ignore
|
|
207
|
+
self.long_term_memory = agent.long_term_memory # type: ignore
|
|
208
|
+
logger.warning(
|
|
209
|
+
"Long term memory in agent is enabled, but memory service is also provided. We will use memory service from runner argument."
|
|
210
|
+
)
|
|
80
211
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
212
|
+
if not memory_service:
|
|
213
|
+
if hasattr(agent, "long_term_memory") and agent.long_term_memory: # type: ignore
|
|
214
|
+
self.long_term_memory = agent.long_term_memory # type: ignore
|
|
215
|
+
memory_service = agent.long_term_memory # type: ignore
|
|
216
|
+
else:
|
|
217
|
+
logger.info("No long term memory provided.")
|
|
218
|
+
|
|
219
|
+
super().__init__(
|
|
220
|
+
agent=agent,
|
|
221
|
+
session_service=session_service,
|
|
222
|
+
memory_service=memory_service,
|
|
223
|
+
app_name=app_name,
|
|
224
|
+
*args,
|
|
225
|
+
**kwargs,
|
|
87
226
|
)
|
|
88
227
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
elif isinstance(messages, MediaMessage):
|
|
93
|
-
assert messages.media.endswith(".png"), (
|
|
94
|
-
"The MediaMessage only supports PNG format file for now."
|
|
95
|
-
)
|
|
96
|
-
data = read_png_to_bytes(messages.media)
|
|
228
|
+
self.run_async = MethodType(
|
|
229
|
+
intercept_new_message(_upload_image_to_tos)(self.run_async), self
|
|
230
|
+
)
|
|
97
231
|
|
|
98
|
-
|
|
99
|
-
object_key, tos_url = ve_tos.build_tos_url(
|
|
100
|
-
self.user_id, self.app_name, session_id, messages.media
|
|
101
|
-
)
|
|
102
|
-
try:
|
|
103
|
-
asyncio.create_task(ve_tos.upload(object_key, data))
|
|
104
|
-
except Exception as e:
|
|
105
|
-
logger.error(f"Upload to TOS failed: {e}")
|
|
106
|
-
tos_url = None
|
|
107
|
-
|
|
108
|
-
messages = [
|
|
109
|
-
types.Content(
|
|
110
|
-
role="user",
|
|
111
|
-
parts=[
|
|
112
|
-
types.Part(text=messages.text),
|
|
113
|
-
types.Part(
|
|
114
|
-
inline_data=Blob(
|
|
115
|
-
display_name=tos_url,
|
|
116
|
-
data=data,
|
|
117
|
-
mime_type="image/png",
|
|
118
|
-
)
|
|
119
|
-
),
|
|
120
|
-
],
|
|
121
|
-
)
|
|
122
|
-
]
|
|
123
|
-
elif isinstance(messages, list):
|
|
124
|
-
converted_messages = []
|
|
125
|
-
for message in messages:
|
|
126
|
-
converted_messages.extend(self._convert_messages(message, session_id))
|
|
127
|
-
messages = converted_messages
|
|
128
|
-
else:
|
|
129
|
-
raise ValueError(f"Unknown message type: {type(messages)}")
|
|
130
|
-
|
|
131
|
-
return messages
|
|
132
|
-
|
|
133
|
-
async def _run(
|
|
232
|
+
async def run(
|
|
134
233
|
self,
|
|
135
|
-
|
|
136
|
-
|
|
234
|
+
messages: RunnerMessage,
|
|
235
|
+
user_id: str = "",
|
|
236
|
+
session_id: str = f"tmp-session-{formatted_timestamp()}",
|
|
137
237
|
run_config: RunConfig | None = None,
|
|
138
|
-
|
|
238
|
+
save_tracing_data: bool = False,
|
|
239
|
+
upload_inline_data_to_tos: bool = False,
|
|
139
240
|
):
|
|
140
|
-
|
|
241
|
+
if upload_inline_data_to_tos:
|
|
242
|
+
_upload_inline_data_to_tos = self.upload_inline_data_to_tos
|
|
243
|
+
self.upload_inline_data_to_tos = upload_inline_data_to_tos
|
|
244
|
+
|
|
245
|
+
if not run_config:
|
|
246
|
+
run_config = RunConfig(
|
|
247
|
+
# streaming_mode=stream_mode,
|
|
248
|
+
max_llm_calls=int(getenv("MODEL_AGENT_MAX_LLM_CALLS", 100)),
|
|
249
|
+
)
|
|
250
|
+
logger.info(f"Run config: {run_config}")
|
|
141
251
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
252
|
+
user_id = user_id or self.user_id
|
|
253
|
+
|
|
254
|
+
converted_messages: list = _convert_messages(
|
|
255
|
+
messages, self.app_name, user_id, session_id
|
|
256
|
+
)
|
|
257
|
+
|
|
258
|
+
if self.short_term_memory:
|
|
259
|
+
await self.short_term_memory.create_session(
|
|
260
|
+
app_name=self.app_name, user_id=self.user_id, session_id=session_id
|
|
261
|
+
)
|
|
147
262
|
|
|
148
|
-
|
|
149
|
-
|
|
263
|
+
final_output = ""
|
|
264
|
+
for converted_message in converted_messages:
|
|
265
|
+
try:
|
|
266
|
+
async for event in self.run_async(
|
|
150
267
|
user_id=self.user_id,
|
|
151
268
|
session_id=session_id,
|
|
152
|
-
new_message=
|
|
269
|
+
new_message=converted_message,
|
|
153
270
|
run_config=run_config,
|
|
154
271
|
):
|
|
155
272
|
if event.get_function_calls():
|
|
@@ -161,41 +278,10 @@ class Runner:
|
|
|
161
278
|
and event.content.parts[0].text is not None
|
|
162
279
|
and len(event.content.parts[0].text.strip()) > 0
|
|
163
280
|
):
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
if stream:
|
|
169
|
-
print(chunk, end="", flush=True)
|
|
170
|
-
final_output += chunk
|
|
171
|
-
if stream:
|
|
172
|
-
print() # end with a new line
|
|
173
|
-
except LlmCallsLimitExceededError as e:
|
|
174
|
-
logger.warning(f"Max number of llm calls limit exceeded: {e}")
|
|
175
|
-
|
|
176
|
-
return final_output
|
|
177
|
-
|
|
178
|
-
async def run(
|
|
179
|
-
self,
|
|
180
|
-
messages: RunnerMessage,
|
|
181
|
-
session_id: str,
|
|
182
|
-
stream: bool = False,
|
|
183
|
-
run_config: RunConfig | None = None,
|
|
184
|
-
save_tracing_data: bool = False,
|
|
185
|
-
):
|
|
186
|
-
converted_messages: list = self._convert_messages(messages, session_id)
|
|
187
|
-
|
|
188
|
-
await self.short_term_memory.create_session(
|
|
189
|
-
app_name=self.app_name, user_id=self.user_id, session_id=session_id
|
|
190
|
-
)
|
|
191
|
-
|
|
192
|
-
logger.info("Begin to process user messages.")
|
|
193
|
-
|
|
194
|
-
final_output = ""
|
|
195
|
-
for converted_message in converted_messages:
|
|
196
|
-
final_output = await self._run(
|
|
197
|
-
session_id, converted_message, run_config, stream
|
|
198
|
-
)
|
|
281
|
+
final_output += event.content.parts[0].text
|
|
282
|
+
except LlmCallsLimitExceededError as e:
|
|
283
|
+
logger.warning(f"Max number of llm calls limit exceeded: {e}")
|
|
284
|
+
final_output = ""
|
|
199
285
|
|
|
200
286
|
# try to save tracing file
|
|
201
287
|
if save_tracing_data:
|
|
@@ -203,6 +289,9 @@ class Runner:
|
|
|
203
289
|
|
|
204
290
|
self._print_trace_id()
|
|
205
291
|
|
|
292
|
+
if upload_inline_data_to_tos:
|
|
293
|
+
self.upload_inline_data_to_tos = _upload_inline_data_to_tos # type: ignore
|
|
294
|
+
|
|
206
295
|
return final_output
|
|
207
296
|
|
|
208
297
|
def get_trace_id(self) -> str:
|
|
@@ -225,47 +314,6 @@ class Runner:
|
|
|
225
314
|
logger.warning(f"Get tracer id failed as {e}")
|
|
226
315
|
return "<unknown_trace_id>"
|
|
227
316
|
|
|
228
|
-
async def run_with_raw_message(
|
|
229
|
-
self,
|
|
230
|
-
message: types.Content,
|
|
231
|
-
session_id: str,
|
|
232
|
-
run_config: RunConfig | None = None,
|
|
233
|
-
):
|
|
234
|
-
run_config = RunConfig() if not run_config else run_config
|
|
235
|
-
|
|
236
|
-
await self.short_term_memory.create_session(
|
|
237
|
-
app_name=self.app_name, user_id=self.user_id, session_id=session_id
|
|
238
|
-
)
|
|
239
|
-
|
|
240
|
-
try:
|
|
241
|
-
|
|
242
|
-
async def event_generator():
|
|
243
|
-
async for event in self.runner.run_async(
|
|
244
|
-
user_id=self.user_id,
|
|
245
|
-
session_id=session_id,
|
|
246
|
-
new_message=message,
|
|
247
|
-
run_config=run_config,
|
|
248
|
-
):
|
|
249
|
-
if event.get_function_calls():
|
|
250
|
-
for function_call in event.get_function_calls():
|
|
251
|
-
logger.debug(f"Function call: {function_call}")
|
|
252
|
-
elif (
|
|
253
|
-
event.content is not None
|
|
254
|
-
and event.content.parts
|
|
255
|
-
and event.content.parts[0].text is not None
|
|
256
|
-
and len(event.content.parts[0].text.strip()) > 0
|
|
257
|
-
):
|
|
258
|
-
yield event.content.parts[0].text
|
|
259
|
-
|
|
260
|
-
final_output = ""
|
|
261
|
-
|
|
262
|
-
async for chunk in event_generator():
|
|
263
|
-
final_output += chunk
|
|
264
|
-
except LlmCallsLimitExceededError as e:
|
|
265
|
-
logger.warning(f"Max number of llm calls limit exceeded: {e}")
|
|
266
|
-
|
|
267
|
-
return final_output
|
|
268
|
-
|
|
269
317
|
def _print_trace_id(self) -> None:
|
|
270
318
|
if not isinstance(self.agent, Agent):
|
|
271
319
|
logger.warning(
|
|
@@ -336,61 +384,3 @@ class Runner:
|
|
|
336
384
|
|
|
337
385
|
await self.long_term_memory.add_session_to_memory(session)
|
|
338
386
|
logger.info(f"Add session `{session.id}` to long term memory.")
|
|
339
|
-
|
|
340
|
-
# [deprecated] we will not host a chat-service in VeADK, so the following two methods are deprecated
|
|
341
|
-
|
|
342
|
-
# async def run_with_final_event(
|
|
343
|
-
# self,
|
|
344
|
-
# messages: RunnerMessage,
|
|
345
|
-
# session_id: str,
|
|
346
|
-
# ):
|
|
347
|
-
# """non-streaming run with final event"""
|
|
348
|
-
# messages: list = self._convert_messages(messages)
|
|
349
|
-
|
|
350
|
-
# await self.short_term_memory.create_session(
|
|
351
|
-
# app_name=self.app_name, user_id=self.user_id, session_id=session_id
|
|
352
|
-
# )
|
|
353
|
-
|
|
354
|
-
# logger.info("Begin to process user messages.")
|
|
355
|
-
|
|
356
|
-
# final_event = ""
|
|
357
|
-
# async for event in self.runner.run_async(
|
|
358
|
-
# user_id=self.user_id, session_id=session_id, new_message=messages[0]
|
|
359
|
-
# ):
|
|
360
|
-
# if event.get_function_calls():
|
|
361
|
-
# for function_call in event.get_function_calls():
|
|
362
|
-
# logger.debug(f"Function call: {function_call}")
|
|
363
|
-
# elif (
|
|
364
|
-
# not event.partial
|
|
365
|
-
# and event.content.parts[0].text is not None
|
|
366
|
-
# and len(event.content.parts[0].text.strip()) > 0
|
|
367
|
-
# ):
|
|
368
|
-
# final_event = event.model_dump_json(exclude_none=True, by_alias=True)
|
|
369
|
-
|
|
370
|
-
# return final_event
|
|
371
|
-
|
|
372
|
-
# async def run_sse(
|
|
373
|
-
# self,
|
|
374
|
-
# session_id: str,
|
|
375
|
-
# prompt: str,
|
|
376
|
-
# ):
|
|
377
|
-
# message = types.Content(role="user", parts=[types.Part(text=prompt)])
|
|
378
|
-
|
|
379
|
-
# await self.short_term_memory.create_session(
|
|
380
|
-
# app_name=self.app_name, user_id=self.user_id, session_id=session_id
|
|
381
|
-
# )
|
|
382
|
-
|
|
383
|
-
# logger.info("Begin to process user messages under SSE method.")
|
|
384
|
-
|
|
385
|
-
# async for event in self.runner.run_async(
|
|
386
|
-
# user_id=self.user_id,
|
|
387
|
-
# session_id=session_id,
|
|
388
|
-
# new_message=message,
|
|
389
|
-
# run_config=RunConfig(streaming_mode=StreamingMode.SSE),
|
|
390
|
-
# ):
|
|
391
|
-
# # Format as SSE data
|
|
392
|
-
# sse_event = event.model_dump_json(exclude_none=True, by_alias=True)
|
|
393
|
-
# if event.get_function_calls():
|
|
394
|
-
# for function_call in event.get_function_calls():
|
|
395
|
-
# logger.debug(f"SSE function call event: {sse_event}")
|
|
396
|
-
# yield f"data: {sse_event}\n\n"
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
|
|
15
15
|
import requests
|
|
16
16
|
|
|
17
|
-
from veadk.config import getenv
|
|
17
|
+
from veadk.config import getenv, settings
|
|
18
18
|
|
|
19
19
|
|
|
20
20
|
def vesearch(query: str) -> str:
|
|
@@ -26,7 +26,7 @@ def vesearch(query: str) -> str:
|
|
|
26
26
|
Returns:
|
|
27
27
|
Summarized search results.
|
|
28
28
|
"""
|
|
29
|
-
api_key =
|
|
29
|
+
api_key = settings.tool.vesearch.api_key
|
|
30
30
|
bot_id = str(getenv("TOOL_VESEARCH_ENDPOINT"))
|
|
31
31
|
|
|
32
32
|
if api_key == "":
|
|
@@ -12,18 +12,22 @@
|
|
|
12
12
|
# See the License for the specific language governing permissions and
|
|
13
13
|
# limitations under the License.
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
from google.adk.tools import ToolContext
|
|
17
|
-
from volcenginesdkarkruntime import Ark
|
|
18
|
-
from veadk.config import getenv
|
|
15
|
+
import json
|
|
19
16
|
import time
|
|
20
17
|
import traceback
|
|
21
|
-
import
|
|
22
|
-
|
|
18
|
+
from typing import Dict, cast
|
|
19
|
+
|
|
20
|
+
from google.adk.tools import ToolContext
|
|
23
21
|
from opentelemetry import trace
|
|
24
22
|
from opentelemetry.trace import Span
|
|
23
|
+
from volcenginesdkarkruntime import Ark
|
|
24
|
+
from volcenginesdkarkruntime.types.content_generation.create_task_content_param import (
|
|
25
|
+
CreateTaskContentParam,
|
|
26
|
+
)
|
|
25
27
|
|
|
28
|
+
from veadk.config import getenv
|
|
26
29
|
from veadk.utils.logger import get_logger
|
|
30
|
+
from veadk.version import VERSION
|
|
27
31
|
|
|
28
32
|
logger = get_logger(__name__)
|
|
29
33
|
|
|
@@ -47,13 +51,16 @@ async def generate(prompt, first_frame_image=None, last_frame_image=None):
|
|
|
47
51
|
logger.debug("first frame generation")
|
|
48
52
|
response = client.content_generation.tasks.create(
|
|
49
53
|
model=getenv("MODEL_VIDEO_NAME"),
|
|
50
|
-
content=
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
"type": "
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
54
|
+
content=cast(
|
|
55
|
+
list[CreateTaskContentParam], # avoid IDE warning
|
|
56
|
+
[
|
|
57
|
+
{"type": "text", "text": prompt},
|
|
58
|
+
{
|
|
59
|
+
"type": "image_url",
|
|
60
|
+
"image_url": {"url": first_frame_image},
|
|
61
|
+
},
|
|
62
|
+
],
|
|
63
|
+
),
|
|
57
64
|
)
|
|
58
65
|
else:
|
|
59
66
|
logger.debug("last frame generation")
|
|
@@ -277,13 +284,13 @@ async def video_generate(params: list, tool_context: ToolContext) -> Dict:
|
|
|
277
284
|
def add_span_attributes(
|
|
278
285
|
span: Span,
|
|
279
286
|
tool_context: ToolContext,
|
|
280
|
-
input_part: dict = None,
|
|
281
|
-
output_part: dict = None,
|
|
282
|
-
input_tokens: int = None,
|
|
283
|
-
output_tokens: int = None,
|
|
284
|
-
total_tokens: int = None,
|
|
285
|
-
request_model: str = None,
|
|
286
|
-
response_model: str = None,
|
|
287
|
+
input_part: dict | None = None,
|
|
288
|
+
output_part: dict | None = None,
|
|
289
|
+
input_tokens: int | None = None,
|
|
290
|
+
output_tokens: int | None = None,
|
|
291
|
+
total_tokens: int | None = None,
|
|
292
|
+
request_model: str | None = None,
|
|
293
|
+
response_model: str | None = None,
|
|
287
294
|
):
|
|
288
295
|
try:
|
|
289
296
|
# common attributes
|
|
@@ -49,6 +49,10 @@ def common_cozeloop_report_source(**kwargs) -> str:
|
|
|
49
49
|
return "veadk"
|
|
50
50
|
|
|
51
51
|
|
|
52
|
+
def common_cozeloop_call_type(**kwargs) -> str:
|
|
53
|
+
return kwargs.get("call_type")
|
|
54
|
+
|
|
55
|
+
|
|
52
56
|
def llm_openinference_instrumentation_veadk(**kwargs) -> str:
|
|
53
57
|
return VERSION
|
|
54
58
|
|
|
@@ -68,4 +72,5 @@ COMMON_ATTRIBUTES = {
|
|
|
68
72
|
"user.id": common_gen_ai_user_id, # CozeLoop / TLS required
|
|
69
73
|
"session.id": common_gen_ai_session_id, # CozeLoop / TLS required
|
|
70
74
|
"cozeloop.report.source": common_cozeloop_report_source, # CozeLoop required
|
|
75
|
+
"cozeloop.call_type": common_cozeloop_call_type, # CozeLoop required
|
|
71
76
|
}
|