feat: 更新场景 B 的工具调用流程,支持代码执行和语言选择

This commit is contained in:
carry
2026-06-07 22:11:02 +08:00
parent bab102538d
commit 6e98170a7d
+45 -70
View File
@@ -131,7 +131,7 @@ const demoA: PromptEnvelope = {
}
// ============================================================
// Scenario B: Tool call flow (request → result)
// Scenario B: Tool call flow — code execution (success + failure)
// ============================================================
const demoB: PromptEnvelope = {
version: '1.0',
@@ -145,64 +145,48 @@ const demoB: PromptEnvelope = {
kind: 'static_var',
name: 'current_date',
value: '2026年6月7日',
description: '当前日期SQL 查询中用于计算相对日期',
description: '当前日期',
},
{
kind: 'static_var',
name: 'knowledge_cutoff',
value: '2026年1月',
description: '模型训练数据的截止时间',
name: 'language',
value: '中文(简体)',
description: '模型回复的首选语言',
},
{
kind: 'system_prompt',
content: `当前日期:{{current_date}}。知识截止:{{knowledge_cutoff}}。
content: `当前日期:{{current_date}},回复语言:{{language}}。
你是一个数据分析助手,可以使用 Python 工具进行数据查询和可视化`,
你是一个编程助手,可以帮助用户编写、调试和优化代码。你可以在安全沙箱中执行代码,也可以查阅技术文档`,
collapsed: true,
},
{
kind: 'tool_overview',
items: [
{
name: 'run_query',
description: '执行 SQL 查询',
parameters: 'sql: string',
name: 'execute_code',
description: '在安全沙箱中执行代码并返回输出',
parameters: 'language: string, code: string',
schema: {
type: 'object',
properties: {
sql: { type: 'string', description: 'SQL 查询语句' },
language: { type: 'string', enum: ['python', 'javascript', 'typescript'], description: '编程语言' },
code: { type: 'string', description: '要执行的代码' },
},
required: ['sql'],
required: ['language', 'code'],
},
},
{
name: 'plot_chart',
description: '生成图表',
parameters: 'type: string, data: object, output_format?: string',
name: 'lookup_api',
description: '查阅技术文档和 API 参考',
parameters: 'query: string, source: string',
schema: {
type: 'object',
properties: {
type: { type: 'string', enum: ['line', 'bar', 'scatter'], description: '图表类型' },
title: { type: 'string', description: '图表标题' },
x_label: { type: 'string', description: 'X 轴标签' },
y_label: { type: 'string', description: 'Y 轴标签' },
data: { type: 'array', description: '数据点数组' },
output_format: { type: 'string', enum: ['png', 'svg'], description: '输出格式' },
query: { type: 'string', description: 'API 或函数名称' },
source: { type: 'string', enum: ['python', 'mdn', 'react', 'node'], description: '文档来源(必填)' },
},
required: ['type', 'data', 'output_format'],
},
},
{
name: 'fetch_data',
description: '获取远程数据',
parameters: 'url: string, format: string',
schema: {
type: 'object',
properties: {
url: { type: 'string', description: '数据 URL' },
format: { type: 'string', enum: ['json', 'csv'], description: '数据格式' },
},
required: ['url', 'format'],
required: ['query', 'source'],
},
},
],
@@ -217,7 +201,7 @@ const demoB: PromptEnvelope = {
segments: [
{
kind: 'text',
content: '帮我看看过去一周的用户注册数据,按天统计,用折线图展示趋势。',
content: '帮我写一个 Python 函数,统计文本中每个单词的出现频率,然后运行测试一下。',
},
],
timestamp: now - 180000,
@@ -228,17 +212,26 @@ const demoB: PromptEnvelope = {
segments: [
{
kind: 'text',
content: '好的,我先查询数据库获取注册数据。',
content: '好的,我来写一个词频统计函数并用示例文本测试:',
},
{
kind: 'tool_call_request',
toolName: 'run_query',
toolName: 'execute_code',
arguments: {
sql: `SELECT DATE(created_at) as date, COUNT(*) as count
FROM users
WHERE created_at >= DATE('now', '-7 days')
GROUP BY DATE(created_at)
ORDER BY date`,
language: 'python',
code: `def word_frequency(text):
"""统计文本中每个单词的出现频率,按频率降序排列"""
import re
words = re.findall(r'\\b\\w+\\b', text.lower())
freq = {}
for w in words:
freq[w] = freq.get(w, 0) + 1
return dict(sorted(freq.items(), key=lambda x: x[1], reverse=True))
sample = "hello world hello python world python python"
result = word_frequency(sample)
for word, count in result.items():
print(f"{word}: {count}")`,
},
collapsed: false,
},
@@ -251,17 +244,11 @@ ORDER BY date`,
segments: [
{
kind: 'tool_call_result',
toolName: 'run_query',
toolName: 'execute_code',
success: true,
result: `[
{"date": "2026-06-01", "count": 42},
{"date": "2026-06-02", "count": 38},
{"date": "2026-06-03", "count": 55},
{"date": "2026-06-04", "count": 47},
{"date": "2026-06-05", "count": 61},
{"date": "2026-06-06", "count": 73},
{"date": "2026-06-07", "count": 58}
]`,
result: `python: 3
world: 2
hello: 2`,
collapsed: true,
},
],
@@ -273,25 +260,13 @@ ORDER BY date`,
segments: [
{
kind: 'text',
content: '查询成功。现在生成折线图——',
content: '运行成功!其实用 `collections.Counter` 可以让代码更简洁。让我查一下 Python 官方文档确认 Counter 的 API 细节——',
},
{
kind: 'tool_call_request',
toolName: 'plot_chart',
toolName: 'lookup_api',
arguments: {
type: 'line',
title: 'Daily User Registrations (Past 7 Days)',
x_label: 'Date',
y_label: 'New Users',
data: [
{ date: '06-01', count: 42 },
{ date: '06-02', count: 38 },
{ date: '06-03', count: 55 },
{ date: '06-04', count: 47 },
{ date: '06-05', count: 61 },
{ date: '06-06', count: 73 },
{ date: '06-07', count: 58 },
],
query: 'collections.Counter most_common',
},
collapsed: false,
},
@@ -304,9 +279,9 @@ ORDER BY date`,
segments: [
{
kind: 'tool_call_result',
toolName: 'plot_chart',
toolName: 'lookup_api',
success: false,
result: 'Error: plot_chart requires "output_format" parameter (png | svg). Please retry with format specified.',
result: 'Error: lookup_api requires "source" parameter (python | mdn | react | node). Please specify the documentation source and retry.',
collapsed: false,
},
],