Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
190 changes: 157 additions & 33 deletions examples/api/server_router_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,47 @@ def example_01b_standard_chat_triplet():
call_add_api("example_01b_standard_chat_triplet", payload)


def example_01c_sync_fast_minimal():
"""
Sync fast mode example - immediate response required.

- async_mode="sync": Blocks until processing completes
- mode="fast": Fast pipeline without LLM call
- Use case: User needs immediate confirmation
"""
payload = {
"user_id": USER_ID,
"writable_cube_ids": [MEM_CUBE_ID],
"messages": "紧急:明天上午10点开会。",
"async_mode": "sync",
"mode": "fast",
Comment on lines +151 to +152
Copy link

Copilot AI Jan 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is trailing whitespace on these lines (e.g., after the comma). This will fail the repo’s formatting/lint checks (ruff format / trailing-whitespace hook). Remove the extra spaces.

Copilot uses AI. Check for mistakes.
}
call_add_api("example_01c_sync_fast_minimal", payload)


def example_01d_sync_fine_detailed():
"""
Sync fine mode example - high-quality extraction required.

- async_mode="sync": Blocks until processing completes
- mode="fine": Fine pipeline with LLM analysis
- Use case: Important information needs deep understanding
"""
payload = {
"user_id": USER_ID,
"writable_cube_ids": [MEM_CUBE_ID],
"messages": "明年计划去杭州西湖旅游,预算5000元,喜欢安静的酒店。",
"async_mode": "sync",
"mode": "fine",
Comment on lines +169 to +170
Copy link

Copilot AI Jan 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing whitespace is present on these lines (e.g., after the comma). This will cause formatter/pre-commit checks to fail. Remove the extra spaces.

Copilot uses AI. Check for mistakes.
"custom_tags": ["travel", "planning"],
"info": {
"priority": "high",
"source_type": "important_note"
}
}
call_add_api("example_01d_sync_fine_detailed", payload)


# ===========================================================================
# 2. Tool / function-calling related examples
# ===========================================================================
Expand All @@ -144,6 +185,7 @@ def example_01b_standard_chat_triplet():
def example_02a_assistant_with_tool_calls():
"""
Assistant message containing tool_calls (function calls).
both multi_model_struct and simple_struct memreaders support this format.
Copy link

Copilot AI Jan 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docstring mentions multi_model_struct, but the repository config uses multimodal_struct (e.g., MEM_READER_BACKEND=multimodal_struct). This is likely a typo and can mislead users trying to run the example. Update the backend name and capitalize the sentence start ("Both ...").

Suggested change
both multi_model_struct and simple_struct memreaders support this format.
Both multimodal_struct and simple_struct memreaders support this format.

Copilot uses AI. Check for mistakes.

- `role = assistant`, `content = None`.
- `tool_calls` contains a list of function calls with arguments.
Expand Down Expand Up @@ -177,6 +219,7 @@ def example_02a_assistant_with_tool_calls():
def example_02b_tool_message_with_result():
"""
Tool message returning the result of a tool call.
only multi_model_struct memreader supports this format.

Comment on lines 221 to 223
Copy link

Copilot AI Jan 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docstring mentions multi_model_struct, but the supported backend appears to be multimodal_struct in the codebase. Also, capitalize the sentence start ("Only ...").

Copilot uses AI. Check for mistakes.
- `role = tool`, `content` contains the tool execution result.
- `tool_call_id` links this message to the original tool call.
Expand Down Expand Up @@ -215,52 +258,87 @@ def example_02b_tool_message_with_result():
call_add_api("example_02b_tool_message_with_result", payload)


def example_02c_tool_description_input_output():
# ===========================================================================
# 3. Multimodal messages
# ===========================================================================


def example_03a_multimodal_text_and_image():
"""
Custom tool message format: tool_description, tool_input, tool_output.
Multimodal user message: text + image_url.

- This demonstrates the custom tool message format (not OpenAI standard).
- `tool_description`: describes the tool/function definition.
- `tool_input`: the input parameters for the tool call.
- `tool_output`: the result/output from the tool execution.
- These are alternative formats for representing tool interactions.
- `content` is a list of content parts.
- Each part can be text/image_url/... etc.
"""
payload = {
"user_id": USER_ID,
"writable_cube_ids": [MEM_CUBE_ID],
"messages": [
{
"role": "assistant",
"content": None,
"tool_calls": [
"role": "user",
"content": [
{
"id": "tool-call-weather-1",
"type": "function",
"function": {
"name": "get_weather",
"arguments": '{"location": "北京"}',
"type": "text",
"text": "帮我看看这张图片大概是什么内容?",
},
{
"type": "image_url",
"image_url": {
"url": "https://example.com/mountain_lake.jpg",
"detail": "high",
},
}
},
],
"chat_time": "2025-11-24T10:12:00Z",
"message_id": "assistant-with-call-1",
"chat_time": "2025-11-24T10:20:00Z",
"message_id": "mm-img-1",
}
],
"info": {"source_type": "image_analysis"},
}
call_add_api("example_02c_tool_description_input_output", payload)
call_add_api("example_03_multimodal_text_and_image", payload)
Copy link

Copilot AI Jan 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

call_add_api example name is inconsistent with the function name (example_03a_multimodal_text_and_image). This makes logs harder to correlate and breaks the otherwise consistent pattern in this file. Rename the string argument to match the function name (or rename the function back), so the printed "Example:" label is accurate.

Suggested change
call_add_api("example_03_multimodal_text_and_image", payload)
call_add_api("example_03a_multimodal_text_and_image", payload)

Copilot uses AI. Check for mistakes.


# ===========================================================================
# 3. Multimodal messages
# ===========================================================================
def example_03b_multimodal_text_and_file():
"""
Multimodal message: text + file.

- Combines text description with attached document
"""
payload = {
"user_id": USER_ID,
"writable_cube_ids": [MEM_CUBE_ID],
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "这是旅游计划的详细文档,请帮我保存。",
},
{
"type": "file",
"file": {
"file_id": "file_travel_plan_123",
"filename": "travel_plan.pdf",
},
},
],
"chat_time": "2025-11-24T10:25:00Z",
"message_id": "mm-file-1",
}
],
"info": {"source_type": "document_upload"},
}
call_add_api("example_03b_multimodal_text_and_file", payload)


def example_03_multimodal_text_and_image():
def example_03c_multimodal_image_fine_mode():
"""
Multimodal user message: text + image_url.
Multimodal message with image in fine mode.

- `content` is a list of content parts.
- Each part can be text/image_url/... etc.
- `mode="fine"`: Fine pipeline with LLM analysis for detailed understanding
- Combines text description with image_url for deep analysis
- Use case: When you need detailed image analysis and understanding
"""
payload = {
"user_id": USER_ID,
Expand All @@ -271,7 +349,7 @@ def example_03_multimodal_text_and_image():
"content": [
{
"type": "text",
"text": "帮我看看这张图片大概是什么内容?",
"text": "这是我最近去的旅游地点,请详细分析一下这个地方的特点和我可能的旅游偏好。",
},
{
"type": "image_url",
Expand All @@ -281,13 +359,55 @@ def example_03_multimodal_text_and_image():
},
},
],
"chat_time": "2025-11-24T10:20:00Z",
"message_id": "mm-img-1",
"chat_time": "2025-11-24T10:22:00Z",
"message_id": "mm-img-fine-1",
}
],
"info": {"source_type": "image_analysis"},
"mode": "fine",
"async_mode": "sync",
"custom_tags": ["travel", "image_analysis", "preference"],
"info": {"source_type": "detailed_image_analysis"},
}
call_add_api("example_03_multimodal_text_and_image", payload)
call_add_api("example_03c_multimodal_image_fine_mode", payload)


def example_03d_multimodal_image_fast_mode():
"""
Multimodal message with image in fast mode.

- `mode="fast"`: Fast pipeline for quick processing
- Combines text description with image_url for rapid extraction
- Use case: When you need quick image processing without deep analysis
"""
payload = {
"user_id": USER_ID,
"writable_cube_ids": [MEM_CUBE_ID],
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "快速保存一下这张风景照片的记录。",
},
{
"type": "image_url",
"image_url": {
"url": "https://example.com/mountain_lake.jpg",
"detail": "high",
},
},
],
"chat_time": "2025-11-24T10:23:00Z",
"message_id": "mm-img-fast-1",
}
],
"mode": "fast",
"async_mode": "async",
"custom_tags": ["travel", "photo"],
"info": {"source_type": "quick_image_save"},
}
call_add_api("example_03d_multimodal_image_fast_mode", payload)


# ===========================================================================
Expand Down Expand Up @@ -687,10 +807,14 @@ def example_07b_chat_complete():
# You can comment out some examples if you do not want to run all of them.
example_01a_string_message_minimal()
example_01b_standard_chat_triplet()
example_01c_sync_fast_minimal()
example_01d_sync_fine_detailed()
example_02a_assistant_with_tool_calls()
example_02b_tool_message_with_result()
example_02c_tool_description_input_output()
example_03_multimodal_text_and_image()
example_03a_multimodal_text_and_image()
example_03b_multimodal_text_and_file()
example_03c_multimodal_image_fine_mode()
example_03d_multimodal_image_fast_mode()
example_04a_pure_text_input_items()
example_04b_pure_file_input_by_file_id()
example_04c_pure_file_input_by_file_data()
Expand Down
Loading