veadk-python 0.2.13__py3-none-any.whl → 0.2.15__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.

@@ -12,419 +12,12 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
- import base64
16
- import json
17
- import mimetypes
18
- import traceback
19
- from typing import Dict
20
-
21
- from google.adk.tools import ToolContext
22
- from google.genai.types import Blob, Part
23
- from opentelemetry import trace
24
- from opentelemetry.trace import Span
25
- from volcenginesdkarkruntime import Ark
26
- from volcenginesdkarkruntime.types.images.images import SequentialImageGenerationOptions
27
-
28
- from veadk.config import getenv, settings
29
- from veadk.consts import (
30
- DEFAULT_IMAGE_GENERATE_MODEL_NAME,
31
- DEFAULT_IMAGE_GENERATE_MODEL_API_BASE,
32
- )
15
+ from veadk.tools.builtin_tools.image_generate import image_generate # noqa: F401
33
16
  from veadk.utils.logger import get_logger
34
- from veadk.utils.misc import formatted_timestamp, read_file_to_bytes
35
- from veadk.version import VERSION
36
- import asyncio
37
- import concurrent.futures
38
- import contextvars
39
-
40
17
 
41
18
  logger = get_logger(__name__)
42
19
 
43
- client = Ark(
44
- api_key=getenv(
45
- "MODEL_IMAGE_API_KEY", getenv("MODEL_AGENT_API_KEY", settings.model.api_key)
46
- ),
47
- base_url=getenv("MODEL_IMAGE_API_BASE", DEFAULT_IMAGE_GENERATE_MODEL_API_BASE),
48
- )
49
-
50
- executor = concurrent.futures.ThreadPoolExecutor(max_workers=8)
51
- tracer = trace.get_tracer("gcp.vertex.agent")
52
-
53
-
54
- def _build_input_parts(item: dict, task_type: str, image_field):
55
- input_part = {"role": "user"}
56
- input_part["parts.0.type"] = "text"
57
- input_part["parts.0.text"] = json.dumps(item, ensure_ascii=False)
58
-
59
- if image_field:
60
- if task_type.startswith("single"):
61
- assert isinstance(image_field, str), (
62
- f"single_* task_type image must be str, got {type(image_field)}"
63
- )
64
- input_part["parts.1.type"] = "image_url"
65
- input_part["parts.1.image_url.name"] = "origin_image"
66
- input_part["parts.1.image_url.url"] = image_field
67
- elif task_type.startswith("multi"):
68
- assert isinstance(image_field, list), (
69
- f"multi_* task_type image must be list, got {type(image_field)}"
70
- )
71
- assert len(image_field) <= 10, (
72
- f"multi_* task_type image list length must be <= 10, got {len(image_field)}"
73
- )
74
- for i, image_url in enumerate(image_field):
75
- idx = i + 1
76
- input_part[f"parts.{idx}.type"] = "image_url"
77
- input_part[f"parts.{idx}.image_url.name"] = f"origin_image_{i}"
78
- input_part[f"parts.{idx}.image_url.url"] = image_url
79
-
80
- return input_part
81
-
82
-
83
- def handle_single_task_sync(
84
- idx: int, item: dict, tool_context
85
- ) -> tuple[list[dict], list[str]]:
86
- logger.debug(f"handle_single_task_sync item {idx}: {item}")
87
- success_list: list[dict] = []
88
- error_list: list[str] = []
89
- total_tokens = 0
90
- output_tokens = 0
91
- output_part = {"message.role": "model"}
92
-
93
- task_type = item.get("task_type", "text_to_single")
94
- prompt = item.get("prompt", "")
95
- response_format = item.get("response_format", None)
96
- size = item.get("size", None)
97
- watermark = item.get("watermark", None)
98
- image_field = item.get("image", None)
99
- sequential_image_generation = item.get("sequential_image_generation", None)
100
- max_images = item.get("max_images", None)
101
-
102
- input_part = _build_input_parts(item, task_type, image_field)
103
-
104
- inputs = {"prompt": prompt}
105
- if size:
106
- inputs["size"] = size
107
- if response_format:
108
- inputs["response_format"] = response_format
109
- if watermark is not None:
110
- inputs["watermark"] = watermark
111
- if sequential_image_generation:
112
- inputs["sequential_image_generation"] = sequential_image_generation
113
-
114
- with tracer.start_as_current_span(f"call_llm_task_{idx}") as span:
115
- try:
116
- if (
117
- sequential_image_generation
118
- and sequential_image_generation == "auto"
119
- and max_images
120
- ):
121
- response = client.images.generate(
122
- model=getenv("MODEL_IMAGE_NAME", DEFAULT_IMAGE_GENERATE_MODEL_NAME),
123
- **inputs,
124
- sequential_image_generation_options=SequentialImageGenerationOptions(
125
- max_images=max_images
126
- ),
127
- )
128
- else:
129
- response = client.images.generate(
130
- model=getenv("MODEL_IMAGE_NAME", DEFAULT_IMAGE_GENERATE_MODEL_NAME),
131
- **inputs,
132
- )
133
-
134
- if not response.error:
135
- logger.debug(f"task {idx} Image generate response: {response}")
136
-
137
- total_tokens += getattr(response.usage, "total_tokens", 0) or 0
138
- output_tokens += getattr(response.usage, "output_tokens", 0) or 0
139
-
140
- for i, image_data in enumerate(response.data):
141
- image_name = f"task_{idx}_image_{i}"
142
- if "error" in image_data:
143
- logger.error(f"Image {image_name} error: {image_data.error}")
144
- error_list.append(image_name)
145
- continue
146
-
147
- if getattr(image_data, "url", None):
148
- image_url = image_data.url
149
- else:
150
- b64 = getattr(image_data, "b64_json", None)
151
- if not b64:
152
- logger.error(
153
- f"Image {image_name} missing data (no url/b64)"
154
- )
155
- error_list.append(image_name)
156
- continue
157
- image_bytes = base64.b64decode(b64)
158
- image_url = _upload_image_to_tos(
159
- image_bytes=image_bytes, object_key=f"{image_name}.png"
160
- )
161
- if not image_url:
162
- logger.error(f"Upload image to TOS failed: {image_name}")
163
- error_list.append(image_name)
164
- continue
165
- logger.debug(f"Image saved as ADK artifact: {image_name}")
166
-
167
- tool_context.state[f"{image_name}_url"] = image_url
168
- output_part[f"message.parts.{i}.type"] = "image_url"
169
- output_part[f"message.parts.{i}.image_url.name"] = image_name
170
- output_part[f"message.parts.{i}.image_url.url"] = image_url
171
- logger.debug(
172
- f"Image {image_name} generated successfully: {image_url}"
173
- )
174
- success_list.append({image_name: image_url})
175
- else:
176
- logger.error(
177
- f"Task {idx} No images returned by model: {response.error}"
178
- )
179
- error_list.append(f"task_{idx}")
180
-
181
- except Exception as e:
182
- logger.error(f"Error in task {idx}: {e}")
183
- traceback.print_exc()
184
- error_list.append(f"task_{idx}")
185
20
 
186
- finally:
187
- add_span_attributes(
188
- span,
189
- tool_context,
190
- input_part=input_part,
191
- output_part=output_part,
192
- output_tokens=output_tokens,
193
- total_tokens=total_tokens,
194
- request_model=getenv(
195
- "MODEL_IMAGE_NAME", DEFAULT_IMAGE_GENERATE_MODEL_NAME
196
- ),
197
- response_model=getenv(
198
- "MODEL_IMAGE_NAME", DEFAULT_IMAGE_GENERATE_MODEL_NAME
199
- ),
200
- )
201
- logger.debug(
202
- f"task {idx} Image generate success_list: {success_list}\nerror_list: {error_list}"
203
- )
204
- return success_list, error_list
205
-
206
-
207
- async def image_generate(tasks: list[dict], tool_context) -> Dict:
208
- """
209
- Seedream 4.0: batch image generation via tasks.
210
- Args:
211
- tasks (list[dict]):
212
- A list of image-generation tasks. Each task is a dict.
213
- Per-task schema
214
- ---------------
215
- Required:
216
- - task_type (str):
217
- One of:
218
- * "multi_image_to_group" # 多图生组图
219
- * "single_image_to_group" # 单图生组图
220
- * "text_to_group" # 文生组图
221
- * "multi_image_to_single" # 多图生单图
222
- * "single_image_to_single" # 单图生单图
223
- * "text_to_single" # 文生单图
224
- - prompt (str)
225
- Text description of the desired image(s). 中文/English 均可。
226
- 若要指定生成图片的数量,请在prompt中添加"生成N张图片",其中N为具体的数字。
227
- Optional:
228
- - size (str)
229
- 指定生成图像的大小,有两种用法(二选一,不可混用):
230
- 方式 1:分辨率级别
231
- 可选值: "1K", "2K", "4K"
232
- 模型会结合 prompt 中的语义推断合适的宽高比、长宽。
233
- 方式 2:具体宽高值
234
- 格式: "<宽度>x<高度>",如 "2048x2048", "2384x1728"
235
- 约束:
236
- * 总像素数范围: [1024x1024, 4096x4096]
237
- * 宽高比范围: [1/16, 16]
238
- 推荐值:
239
- - 1:1 → 2048x2048
240
- - 4:3 → 2384x1728
241
- - 3:4 → 1728x2304
242
- - 16:9 → 2560x1440
243
- - 9:16 → 1440x2560
244
- - 3:2 → 2496x1664
245
- - 2:3 → 1664x2496
246
- - 21:9 → 3024x1296
247
- 默认值: "2048x2048"
248
- - response_format (str)
249
- Return format: "url" (default, URL 24h 过期) | "b64_json".
250
- - watermark (bool)
251
- Add watermark. Default: true.
252
- - image (str | list[str]) # 仅“非文生图”需要。文生图请不要提供 image
253
- Reference image(s) as URL or Base64.
254
- * 生成“单图”的任务:传入 string(exactly 1 image)。
255
- * 生成“组图”的任务:传入 array(2–10 images)。
256
- - sequential_image_generation (str)
257
- 控制是否生成“组图”。Default: "disabled".
258
- * 若要生成组图:必须设为 "auto"。
259
- - max_images (int)
260
- 仅当生成组图时生效。控制模型能生成的最多张数,范围 [1, 15], 不设置默认为15。
261
- 注意这个参数不等于生成的图片数量,而是模型最多能生成的图片数量。
262
- 在单图组图场景最多 14;多图组图场景需满足 (len(images)+max_images ≤ 15)。
263
- Model 行为说明(如何由参数推断模式)
264
- ---------------------------------
265
- 1) 文生单图: 不提供 image 且 (S 未设置或 S="disabled") → 1 张图。
266
- 2) 文生组图: 不提供 image 且 S="auto" → 组图,数量由 max_images 控制。
267
- 3) 单图生单图: image=string 且 (S 未设置或 S="disabled") → 1 张图。
268
- 4) 单图生组图: image=string 且 S="auto" → 组图,数量 ≤14。
269
- 5) 多图生单图: image=array (2–10) 且 (S 未设置或 S="disabled") → 1 张图。
270
- 6) 多图生组图: image=array (2–10) 且 S="auto" → 组图,需满足总数 ≤15。
271
- 返回结果
272
- --------
273
- Dict with generation summary.
274
- Example:
275
- {
276
- "status": "success",
277
- "success_list": [
278
- {"image_name": "url"}
279
- ],
280
- "error_list": ["image_name"]
281
- }
282
- Notes:
283
- - 组图任务必须 sequential_image_generation="auto"。
284
- - 如果想要指定生成组图的数量,请在prompt里添加数量说明,例如:"生成3张图片"。
285
- - size 推荐使用 2048x2048 或表格里的标准比例,确保生成质量。
286
- """
287
- logger.debug(
288
- f"Using model: {getenv('MODEL_IMAGE_NAME', DEFAULT_IMAGE_GENERATE_MODEL_NAME)}"
289
- )
290
- success_list: list[dict] = []
291
- error_list: list[str] = []
292
- logger.debug(f"image_generate tasks: {tasks}")
293
- with tracer.start_as_current_span("image_generate"):
294
- base_ctx = contextvars.copy_context()
295
-
296
- def make_task(idx, item):
297
- ctx = base_ctx.copy()
298
- return lambda: ctx.run(handle_single_task_sync, idx, item, tool_context)
299
-
300
- loop = asyncio.get_event_loop()
301
- futures = [
302
- loop.run_in_executor(executor, make_task(idx, item))
303
- for idx, item in enumerate(tasks)
304
- ]
305
-
306
- results = await asyncio.gather(*futures, return_exceptions=True)
307
-
308
- for res in results:
309
- if isinstance(res, Exception):
310
- logger.error(f"Task raised exception: {res}")
311
- error_list.append("unknown_task_exception")
312
- continue
313
- s, e = res
314
- success_list.extend(s)
315
- error_list.extend(e)
316
-
317
- if not success_list:
318
- logger.debug(
319
- f"image_generate success_list: {success_list}\nerror_list: {error_list}"
320
- )
321
- return {
322
- "status": "error",
323
- "success_list": success_list,
324
- "error_list": error_list,
325
- }
326
- app_name = tool_context._invocation_context.app_name
327
- user_id = tool_context._invocation_context.user_id
328
- session_id = tool_context._invocation_context.session.id
329
- artifact_service = tool_context._invocation_context.artifact_service
330
-
331
- if artifact_service:
332
- for image in success_list:
333
- for _, image_tos_url in image.items():
334
- filename = f"artifact_{formatted_timestamp()}"
335
- await artifact_service.save_artifact(
336
- app_name=app_name,
337
- user_id=user_id,
338
- session_id=session_id,
339
- filename=filename,
340
- artifact=Part(
341
- inline_data=Blob(
342
- display_name=filename,
343
- data=read_file_to_bytes(image_tos_url),
344
- mime_type=mimetypes.guess_type(image_tos_url)[0],
345
- )
346
- ),
347
- )
348
-
349
- logger.debug(
350
- f"image_generate success_list: {success_list}\nerror_list: {error_list}"
351
- )
352
- return {"status": "success", "success_list": success_list, "error_list": error_list}
353
-
354
-
355
- def add_span_attributes(
356
- span: Span,
357
- tool_context: ToolContext,
358
- input_part: dict = None,
359
- output_part: dict = None,
360
- input_tokens: int = None,
361
- output_tokens: int = None,
362
- total_tokens: int = None,
363
- request_model: str = None,
364
- response_model: str = None,
365
- ):
366
- try:
367
- # common attributes
368
- app_name = tool_context._invocation_context.app_name
369
- user_id = tool_context._invocation_context.user_id
370
- agent_name = tool_context.agent_name
371
- session_id = tool_context._invocation_context.session.id
372
- span.set_attribute("gen_ai.agent.name", agent_name)
373
- span.set_attribute("openinference.instrumentation.veadk", VERSION)
374
- span.set_attribute("gen_ai.app.name", app_name)
375
- span.set_attribute("gen_ai.user.id", user_id)
376
- span.set_attribute("gen_ai.session.id", session_id)
377
- span.set_attribute("agent_name", agent_name)
378
- span.set_attribute("agent.name", agent_name)
379
- span.set_attribute("app_name", app_name)
380
- span.set_attribute("app.name", app_name)
381
- span.set_attribute("user.id", user_id)
382
- span.set_attribute("session.id", session_id)
383
- span.set_attribute("cozeloop.report.source", "veadk")
384
-
385
- # llm attributes
386
- span.set_attribute("gen_ai.system", "openai")
387
- span.set_attribute("gen_ai.operation.name", "chat")
388
- if request_model:
389
- span.set_attribute("gen_ai.request.model", request_model)
390
- if response_model:
391
- span.set_attribute("gen_ai.response.model", response_model)
392
- if total_tokens:
393
- span.set_attribute("gen_ai.usage.total_tokens", total_tokens)
394
- if output_tokens:
395
- span.set_attribute("gen_ai.usage.output_tokens", output_tokens)
396
- if input_tokens:
397
- span.set_attribute("gen_ai.usage.input_tokens", input_tokens)
398
- if input_part:
399
- span.add_event("gen_ai.user.message", input_part)
400
- if output_part:
401
- span.add_event("gen_ai.choice", output_part)
402
-
403
- except Exception:
404
- traceback.print_exc()
405
-
406
-
407
- def _upload_image_to_tos(image_bytes: bytes, object_key: str) -> None:
408
- try:
409
- import os
410
- from datetime import datetime
411
-
412
- from veadk.integrations.ve_tos.ve_tos import VeTOS
413
-
414
- timestamp: str = datetime.now().strftime("%Y%m%d%H%M%S%f")[:-3]
415
- object_key = f"{timestamp}-{object_key}"
416
- bucket_name = os.getenv("DATABASE_TOS_BUCKET")
417
- ve_tos = VeTOS()
418
-
419
- tos_url = ve_tos.build_tos_signed_url(
420
- object_key=object_key, bucket_name=bucket_name
421
- )
422
-
423
- ve_tos.upload_bytes(
424
- data=image_bytes, object_key=object_key, bucket_name=bucket_name
425
- )
426
-
427
- return tos_url
428
- except Exception as e:
429
- logger.error(f"Upload to TOS failed: {e}")
430
- return None
21
+ logger.warning(
22
+ "The 'generate_image' tool is deprecated and will be removed in future versions. Use `image_generate` tool from `veadk.tools.builtin_tools.image_generate` instead."
23
+ )