feat: 更新场景 B 的工具调用流程,支持代码执行和语言选择
This commit is contained in:
+45
-70
@@ -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 = {
|
const demoB: PromptEnvelope = {
|
||||||
version: '1.0',
|
version: '1.0',
|
||||||
@@ -145,64 +145,48 @@ const demoB: PromptEnvelope = {
|
|||||||
kind: 'static_var',
|
kind: 'static_var',
|
||||||
name: 'current_date',
|
name: 'current_date',
|
||||||
value: '2026年6月7日',
|
value: '2026年6月7日',
|
||||||
description: '当前日期,SQL 查询中用于计算相对日期',
|
description: '当前日期',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
kind: 'static_var',
|
kind: 'static_var',
|
||||||
name: 'knowledge_cutoff',
|
name: 'language',
|
||||||
value: '2026年1月',
|
value: '中文(简体)',
|
||||||
description: '模型训练数据的截止时间',
|
description: '模型回复的首选语言',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
kind: 'system_prompt',
|
kind: 'system_prompt',
|
||||||
content: `当前日期:{{current_date}}。知识截止:{{knowledge_cutoff}}。
|
content: `当前日期:{{current_date}},回复语言:{{language}}。
|
||||||
|
|
||||||
你是一个数据分析助手,可以使用 Python 工具进行数据查询和可视化。`,
|
你是一个编程助手,可以帮助用户编写、调试和优化代码。你可以在安全沙箱中执行代码,也可以查阅技术文档。`,
|
||||||
collapsed: true,
|
collapsed: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
kind: 'tool_overview',
|
kind: 'tool_overview',
|
||||||
items: [
|
items: [
|
||||||
{
|
{
|
||||||
name: 'run_query',
|
name: 'execute_code',
|
||||||
description: '执行 SQL 查询',
|
description: '在安全沙箱中执行代码并返回输出',
|
||||||
parameters: 'sql: string',
|
parameters: 'language: string, code: string',
|
||||||
schema: {
|
schema: {
|
||||||
type: 'object',
|
type: 'object',
|
||||||
properties: {
|
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',
|
name: 'lookup_api',
|
||||||
description: '生成图表',
|
description: '查阅技术文档和 API 参考',
|
||||||
parameters: 'type: string, data: object, output_format?: string',
|
parameters: 'query: string, source: string',
|
||||||
schema: {
|
schema: {
|
||||||
type: 'object',
|
type: 'object',
|
||||||
properties: {
|
properties: {
|
||||||
type: { type: 'string', enum: ['line', 'bar', 'scatter'], description: '图表类型' },
|
query: { type: 'string', description: 'API 或函数名称' },
|
||||||
title: { type: 'string', description: '图表标题' },
|
source: { type: 'string', enum: ['python', 'mdn', 'react', 'node'], 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: '输出格式' },
|
|
||||||
},
|
},
|
||||||
required: ['type', 'data', 'output_format'],
|
required: ['query', 'source'],
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
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'],
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
@@ -217,7 +201,7 @@ const demoB: PromptEnvelope = {
|
|||||||
segments: [
|
segments: [
|
||||||
{
|
{
|
||||||
kind: 'text',
|
kind: 'text',
|
||||||
content: '帮我看看过去一周的用户注册数据,按天统计,用折线图展示趋势。',
|
content: '帮我写一个 Python 函数,统计文本中每个单词的出现频率,然后运行测试一下。',
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
timestamp: now - 180000,
|
timestamp: now - 180000,
|
||||||
@@ -228,17 +212,26 @@ const demoB: PromptEnvelope = {
|
|||||||
segments: [
|
segments: [
|
||||||
{
|
{
|
||||||
kind: 'text',
|
kind: 'text',
|
||||||
content: '好的,我先查询数据库获取注册数据。',
|
content: '好的,我来写一个词频统计函数并用示例文本测试:',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
kind: 'tool_call_request',
|
kind: 'tool_call_request',
|
||||||
toolName: 'run_query',
|
toolName: 'execute_code',
|
||||||
arguments: {
|
arguments: {
|
||||||
sql: `SELECT DATE(created_at) as date, COUNT(*) as count
|
language: 'python',
|
||||||
FROM users
|
code: `def word_frequency(text):
|
||||||
WHERE created_at >= DATE('now', '-7 days')
|
"""统计文本中每个单词的出现频率,按频率降序排列"""
|
||||||
GROUP BY DATE(created_at)
|
import re
|
||||||
ORDER BY date`,
|
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,
|
collapsed: false,
|
||||||
},
|
},
|
||||||
@@ -251,17 +244,11 @@ ORDER BY date`,
|
|||||||
segments: [
|
segments: [
|
||||||
{
|
{
|
||||||
kind: 'tool_call_result',
|
kind: 'tool_call_result',
|
||||||
toolName: 'run_query',
|
toolName: 'execute_code',
|
||||||
success: true,
|
success: true,
|
||||||
result: `[
|
result: `python: 3
|
||||||
{"date": "2026-06-01", "count": 42},
|
world: 2
|
||||||
{"date": "2026-06-02", "count": 38},
|
hello: 2`,
|
||||||
{"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}
|
|
||||||
]`,
|
|
||||||
collapsed: true,
|
collapsed: true,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
@@ -273,25 +260,13 @@ ORDER BY date`,
|
|||||||
segments: [
|
segments: [
|
||||||
{
|
{
|
||||||
kind: 'text',
|
kind: 'text',
|
||||||
content: '查询成功。现在生成折线图——',
|
content: '运行成功!其实用 `collections.Counter` 可以让代码更简洁。让我查一下 Python 官方文档确认 Counter 的 API 细节——',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
kind: 'tool_call_request',
|
kind: 'tool_call_request',
|
||||||
toolName: 'plot_chart',
|
toolName: 'lookup_api',
|
||||||
arguments: {
|
arguments: {
|
||||||
type: 'line',
|
query: 'collections.Counter most_common',
|
||||||
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 },
|
|
||||||
],
|
|
||||||
},
|
},
|
||||||
collapsed: false,
|
collapsed: false,
|
||||||
},
|
},
|
||||||
@@ -304,9 +279,9 @@ ORDER BY date`,
|
|||||||
segments: [
|
segments: [
|
||||||
{
|
{
|
||||||
kind: 'tool_call_result',
|
kind: 'tool_call_result',
|
||||||
toolName: 'plot_chart',
|
toolName: 'lookup_api',
|
||||||
success: false,
|
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,
|
collapsed: false,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
|||||||
Reference in New Issue
Block a user