@@ -14,6 +14,7 @@ LoongSuite 扩展为 OpenTelemetry GenAI Util 包提供了额外的 Generative A
1414- **execute_tool **: 工具执行操作
1515- **retrieve **: 文档检索操作(向量数据库查询)
1616- **rerank **: 文档重排序操作
17+ - **memory **: 记忆操作,支持记忆的增删改查等操作
1718
1819这些扩展操作遵循 OpenTelemetry GenAI 语义约定,并与基础的 LLM 操作保持一致的使用体验。
1920
@@ -37,13 +38,22 @@ LoongSuite 扩展为 OpenTelemetry GenAI Util 包提供了额外的 Generative A
3738- ``EVENT_ONLY ``: 仅在 event 中捕获消息内容(结构化格式)
3839- ``SPAN_AND_EVENT ``: 同时在 span 和 event 中捕获消息内容
3940
41+ 事件发出控制
42+ ~~~~~~~~~~~~
43+
44+ 设置环境变量 ``OTEL_INSTRUMENTATION_GENAI_EMIT_EVENT `` 来控制是否发出事件:
45+
46+ - ``true ``: 启用事件发出(当内容捕获模式为 ``EVENT_ONLY `` 或 ``SPAN_AND_EVENT `` 时)
47+ - ``false ``: 禁用事件发出(默认)
48+
4049示例配置
4150~~~~~~~~
4251
4352::
4453
4554 export OTEL_SEMCONV_STABILITY_OPT_IN=gen_ai_latest_experimental
4655 export OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=SPAN_AND_EVENT
56+ export OTEL_INSTRUMENTATION_GENAI_EMIT_EVENT=true
4757
4858
4959支持的操作
@@ -91,7 +101,7 @@ Token 使用:
91101
92102**事件支持: **
93103
94- 当配置为 ``EVENT_ONLY `` 或 ``SPAN_AND_EVENT `` 模式且提供 LoggerProvider 时,会发出 ``gen_ai.client.agent.invoke.operation.details `` 事件,包含结构化的消息内容。
104+ 当 ``OTEL_INSTRUMENTATION_GENAI_EMIT_EVENT `` 设置为 ``true `` 且提供 LoggerProvider 时,会发出 ``gen_ai.client.agent.invoke.operation.details `` 事件,包含结构化的消息内容(受内容捕获模式控制) 。
95105
96106**使用示例: **
97107
@@ -302,6 +312,125 @@ Token 使用:
302312 invocation.rerank_output_documents = [...]
303313
304314
315+ 7. 记忆操作 (memory)
316+ ~~~~~~~~~~~~~~~~~~~~
317+
318+ 用于跟踪 AI Agent 的记忆操作,支持记忆的增删改查、搜索和历史查询等功能。
319+
320+ **支持的操作类型: **
321+
322+ - ``add ``: 添加记忆记录
323+ - ``search ``: 搜索记忆记录
324+ - ``update ``: 更新记忆记录
325+ - ``batch_update ``: 批量更新记忆记录
326+ - ``get ``: 获取特定记忆记录
327+ - ``get_all ``: 获取所有记忆记录
328+ - ``history ``: 获取记忆历史
329+ - ``delete ``: 删除记忆记录
330+ - ``batch_delete ``: 批量删除记忆记录
331+ - ``delete_all ``: 删除所有记忆记录
332+
333+ **支持的属性: **
334+
335+ 基础属性:
336+ - ``gen_ai.operation.name ``: 操作名称,固定为 "memory_operation"
337+ - ``gen_ai.memory.operation ``: 记忆操作类型(必需)
338+
339+ 标识符(条件必需):
340+ - ``gen_ai.memory.user_id ``: 用户标识符
341+ - ``gen_ai.memory.agent_id ``: Agent 标识符
342+ - ``gen_ai.memory.run_id ``: 运行标识符
343+ - ``gen_ai.memory.app_id ``: 应用标识符(用于托管平台)
344+ - ``gen_ai.memory.id ``: 记忆 ID(用于 get、update、delete 操作)
345+
346+ 操作参数(可选):
347+ - ``gen_ai.memory.limit ``: 返回结果数量限制
348+ - ``gen_ai.memory.page ``: 分页页码
349+ - ``gen_ai.memory.page_size ``: 分页大小
350+ - ``gen_ai.memory.top_k ``: 返回 Top K 结果数量(用于托管 API)
351+ - ``gen_ai.memory.memory_type ``: 记忆类型(如 "procedural_memory")
352+ - ``gen_ai.memory.threshold ``: 相似度阈值(用于搜索操作)
353+ - ``gen_ai.memory.rerank ``: 是否启用重排序
354+
355+ 记忆内容(受内容捕获模式控制):
356+ - ``gen_ai.memory.input.messages ``: 原始记忆内容
357+ - ``gen_ai.memory.output.messages ``: 查询结果
358+
359+ 服务器信息:
360+ - ``server.address ``: 服务器地址
361+ - ``server.port ``: 服务器端口
362+
363+ **事件支持: **
364+
365+ 当配置为 ``EVENT_ONLY `` 或 ``SPAN_AND_EVENT `` 模式且提供 LoggerProvider 时,会发出 ``gen_ai.memory.operation.details `` 事件,包含结构化的记忆内容。
366+
367+ **使用示例: **
368+
369+ ::
370+
371+ from opentelemetry.util.genai._extended_memory import MemoryInvocation
372+
373+ # 添加记忆
374+ invocation = MemoryInvocation(operation="add")
375+ with handler.memory(invocation) as invocation:
376+ invocation.user_id = "user_123"
377+ invocation.agent_id = "agent_456"
378+ invocation.run_id = "run_789"
379+ invocation.input_messages = "用户喜欢苹果"
380+ invocation.server_address = "api.mem0.ai"
381+ invocation.server_port = 443
382+
383+ # 搜索记忆
384+ invocation = MemoryInvocation(operation="search")
385+ with handler.memory(invocation) as invocation:
386+ invocation.user_id = "user_123"
387+ invocation.agent_id = "agent_456"
388+ invocation.limit = 10
389+ invocation.threshold = 0.7
390+ invocation.rerank = True
391+ invocation.top_k = 5
392+
393+ # 执行搜索...
394+ invocation.output_messages = [
395+ {"memory_id": "mem1", "content": "用户喜欢苹果", "score": 0.95},
396+ {"memory_id": "mem2", "content": "用户喜欢橙子", "score": 0.88}
397+ ]
398+
399+ # 更新记忆
400+ invocation = MemoryInvocation(operation="update")
401+ with handler.memory(invocation) as invocation:
402+ invocation.memory_id = "mem_abc123"
403+ invocation.user_id = "user_123"
404+ invocation.input_messages = "更新后的记忆内容"
405+
406+ # 获取记忆
407+ invocation = MemoryInvocation(operation="get")
408+ with handler.memory(invocation) as invocation:
409+ invocation.memory_id = "mem_xyz789"
410+ invocation.user_id = "user_123"
411+ invocation.agent_id = "agent_456"
412+
413+ # 获取所有记忆(带分页)
414+ invocation = MemoryInvocation(operation="get_all")
415+ with handler.memory(invocation) as invocation:
416+ invocation.user_id = "user_123"
417+ invocation.page = 1
418+ invocation.page_size = 100
419+
420+ # 获取记忆历史
421+ invocation = MemoryInvocation(operation="history")
422+ with handler.memory(invocation) as invocation:
423+ invocation.user_id = "user_123"
424+ invocation.agent_id = "agent_456"
425+ invocation.run_id = "run_789"
426+
427+ # 删除记忆
428+ invocation = MemoryInvocation(operation="delete")
429+ with handler.memory(invocation) as invocation:
430+ invocation.memory_id = "mem_to_delete"
431+ invocation.user_id = "user_123"
432+
433+
305434错误处理
306435--------
307436
@@ -456,6 +585,31 @@ Token 使用:
456585
457586 rerank_inv.rerank_output_documents = [...]
458587
588+ # 记忆操作
589+ from opentelemetry.util.genai._extended_memory import MemoryInvocation
590+
591+ # 添加记忆
592+ memory_inv = MemoryInvocation(operation="add")
593+ with handler.memory(memory_inv) as memory_inv:
594+ memory_inv.user_id = "user_123"
595+ memory_inv.agent_id = "ShoppingAssistant"
596+ memory_inv.input_messages = "用户偏好:喜欢轻薄型笔记本电脑"
597+
598+ # 执行添加记忆...
599+
600+ # 搜索记忆
601+ search_inv = MemoryInvocation(operation="search")
602+ with handler.memory(search_inv) as search_inv:
603+ search_inv.user_id = "user_123"
604+ search_inv.agent_id = "ShoppingAssistant"
605+ search_inv.limit = 5
606+ search_inv.threshold = 0.7
607+
608+ # 执行搜索...
609+ search_inv.output_messages = [
610+ {"memory_id": "mem1", "content": "用户偏好:喜欢轻薄型笔记本电脑", "score": 0.92}
611+ ]
612+
459613
460614设计文档
461615--------
0 commit comments