-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun.py
428 lines (368 loc) · 16.3 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
from chat_example import basic_chat, creative_chat, stream_chat
from function_calling import chat_with_function
from image_generation import generate_image, create_image_variation
from speech_to_text import transcribe_audio
from text_to_speech import generate_speech
from error_handling import safe_api_call
from embeddings_example import demonstrate_embeddings, search_documents
from moderation_example import demonstrate_moderation, filter_safe_content
from reasoning_example import demonstrate_cot_reasoning, solve_complex_task, demonstrate_step_by_step_coding
from vision_example import (
analyze_image,
analyze_multiple_images,
analyze_image_with_specific_focus
)
from structured_output_example import (
demonstrate_complex_extraction,
batch_process_texts,
custom_schema_extraction
)
from predicted_outputs_example import (
demonstrate_temperature_effects,
demonstrate_top_p_effects,
demonstrate_penalties,
demonstrate_reproducibility,
analyze_output_consistency,
demonstrate_code_refactoring,
demonstrate_prediction_streaming,
demonstrate_content_completion
)
from rag_example import RAGSystem
from tools.file_search_example import FileSearchAssistant
from meta_prompts_example import demonstrate_meta_prompts
from tools.code_interpreter_demo import demo_code_interpreter
import argparse
import json
import os
async def demo_all_features():
try:
print("=== 1. 基础聊天演示 ===")
chat_response = basic_chat()
print(f"基础聊天响应: {chat_response}\n")
print("=== 2. 创意聊天演示 ===")
creative_response = creative_chat()
print(f"创意聊天响应: {creative_response}\n")
print("=== 3. 流式聊天演示 ===")
print("开始流式输出诗歌:")
await stream_chat()
print("\n")
print("=== 4. 函数调用演示 ===")
weather_response = chat_with_function()
print(f"天气查询结果: {weather_response}\n")
print("=== 5. 图像生成演示 ===")
image_url = generate_image()
print(f"生成的图片URL: {image_url}\n")
# 假设我们已经有了一张熊猫图片
if os.path.exists("panda.png"):
variation_url = create_image_variation()
print(f"图片变体URL: {variation_url}\n")
print("=== 6. 语音转文字演示 ===")
# 假设我们有语音文件
if os.path.exists("speech.mp3"):
transcript = transcribe_audio()
print(f"转录结果: {transcript}\n")
print("=== 7. 文字转语音演示 ===")
generate_speech()
print("语音文件已生成: output.mp3\n")
print("=== 8. 错误处理演示 ===")
safe_response = safe_api_call()
print(f"安全调用响应: {safe_response}\n")
print("=== 9. 文本嵌入演示 ===")
# 基础嵌入演示
embedding_results = demonstrate_embeddings()
print(f"基础嵌入维度: {embedding_results['basic_embedding_length']}")
print(f"批量嵌入维度: {embedding_results['batch_embedding_length']}")
print(f"文本相似度: {embedding_results['text_similarity']:.4f}\n")
# 文档搜索示例
documents = [
"机器学习是人工智能的一个子领域",
"深度学习是机器学习的一种方法",
"自然语言处理是人工智能的重要应用",
"计算机视觉主要处理图像和视频数据"
]
query = "什么是机器学习"
best_match_index = search_documents(query, documents)
print(f"搜索查询: {query}")
print(f"最相关的文档: {documents[best_match_index]}\n")
print("=== 10. 内容审核演示 ===")
# 基础审核演示
moderation_results = demonstrate_moderation()
if moderation_results["success"]:
print("\n=== 内容审核结果 ===")
for result in moderation_results["results"]:
print(f"\n文本: {result['文本']}")
print(f"违规: {'是' if result['违规'] else '否'}")
if result['违规']:
print("违规类别:")
for category, is_flagged in result['违规类别'].items():
if is_flagged:
score = result['违规分数'][category]
print(f"- {category}: {score:.3f}")
# 内容过滤示例
test_texts = [
"今天天气真好",
"我要杀了你",
"让我们一起学习",
"毒品在哪里买"
]
safe_texts = filter_safe_content(test_texts)
print("\n=== 安全内容过滤结果 ===")
print("原始文本:", test_texts)
print("安全文本:", safe_texts)
print("=== 11. 推理能力演示 ===")
# Chain of Thought 演示
print("\n=== 链式思维解题示例 ===")
math_result = demonstrate_cot_reasoning()
if math_result["success"]:
print(math_result["reasoning"])
# 系统设计推理演示
print("\n=== 系统设计推理示例 ===")
design_result = solve_complex_task()
if design_result["success"]:
print(design_result["design"])
# 编程问题解决演示
print("\n=== 编程问题解决示例 ===")
coding_result = demonstrate_step_by_step_coding()
if coding_result["success"]:
print(coding_result["solution"])
print("=== 12. 视觉分析演示 ===")
# 基础图片分析
print("\n=== 单图分析示例 ===")
image_path = "panda.png"
if os.path.exists(image_path):
result = analyze_image(
image_path,
prompt="这张图片展示了什么?",
detail_level="high"
)
if result["success"]:
print(result["analysis"])
# 多图比较分析
print("\n=== 多图比较示例 ===")
image_paths = ["panda1.png", "panda2.png"]
if all(os.path.exists(path) for path in image_paths):
result = analyze_multiple_images(
image_paths,
prompt="请比较这两张图片的主要区别"
)
if result["success"]:
print(result["comparison"])
# 特定焦点分析
print("\n=== 特定焦点分析示例 ===")
if os.path.exists(image_path):
result = analyze_image_with_specific_focus(
image_path,
focus_areas=["颜色搭配", "构图技巧", "情感表达"]
)
if result["success"]:
print(result["focused_analysis"])
print("=== 13. 结构化输出演示 ===")
# 基础信息提取
print("\n=== 基础信息提取示例 ===")
text = """
李四是一名28岁的医生,住在上海市浦东新区阳光路456号。
他喜欢打篮球和游泳,邮箱是[email protected]。
"""
result = demonstrate_complex_extraction(text)
if result["success"]:
print(json.dumps(result["data"], indent=2, ensure_ascii=False))
# 批量处理
print("\n=== 批量处理示例 ===")
texts = [
"王五,42岁,居住地:广州市天河区月亮街789号,爱好:钓鱼、摄影",
"赵六,31岁,居住地:深市南山区星光路321号,爱好:烹饪、旅行"
]
results = batch_process_texts(texts)
for idx, result in enumerate(results, 1):
if result["success"]:
print(f"\n文本 {idx} 的提取结果:")
print(json.dumps(result["data"], indent=2, ensure_ascii=False))
# 自定义Schema提取
print("\n=== 自定义Schema提取示例 ===")
custom_schema = {
"type": "object",
"properties": {
"产品名称": {"type": "string"},
"价格": {"type": "number"},
"规格": {
"type": "object",
"properties": {
"尺寸": {"type": "string"},
"重量": {"type": "string"},
"颜色": {"type": "array", "items": {"type": "string"}}
}
}
}
}
product_text = """
新款智能手机X100,售价4999元,
尺寸:158.3x73.2x8.2mm,重量189g,
有暗夜黑、极光蓝、珍珠白三种颜色可选。
"""
custom_result = custom_schema_extraction(product_text, custom_schema)
if custom_result["success"]:
print(json.dumps(custom_result["data"], indent=2, ensure_ascii=False))
print("=== 14. 预测输出控制演示 ===")
# Temperature效果演示
print("\n=== Temperature效果示例 ===")
prompt = "给我讲一个关于太空探险的短故事"
temp_results = demonstrate_temperature_effects(prompt)
for temp, outputs in temp_results.items():
print(f"\nTemperature = {temp}:")
for idx, output in enumerate(outputs, 1):
print(f"样本 {idx}:\n{output[:100]}...")
# 分析输出一致性
consistency = analyze_output_consistency(outputs)
print(f"一致性分析: 平均相似度 = {consistency['mean_similarity']:.2f}")
# Top-p效果演示
print("\n=== Top-p效果示例 ===")
top_p_results = demonstrate_top_p_effects(prompt)
for p, outputs in top_p_results.items():
print(f"\nTop-p = {p}:")
for idx, output in enumerate(outputs, 1):
print(f"样本 {idx}:\n{output[:100]}...")
# 惩罚效果演示
print("\n=== 惩罚效果示例 ===")
repetitive_prompt = "列出5个编程语言的优点"
penalty_results = demonstrate_penalties(repetitive_prompt, True)
for penalty_type, output in penalty_results.items():
print(f"\n{penalty_type}:\n{output[:200]}...")
# 可重复性演示
print("\n=== 可重复性示例 ===")
seed_results = demonstrate_reproducibility(prompt)
print("\n使用固定种子:")
for idx, output in enumerate(seed_results["with_seed"], 1):
print(f"生成 {idx}:\n{output[:100]}...")
print("\n不使用种子:")
for idx, output in enumerate(seed_results["without_seed"], 1):
print(f"生成 {idx}:\n{output[:100]}...")
print("\n=== Prediction参数演示 ===")
# 代码重构示例
print("\n1. 代码重构示例")
original_code = """
class User {
firstName: string = "";
lastName: string = "";
username: string = "";
}
""".strip()
modifications = [
"将username替换为email",
"添加一个age属性",
"添加一个getFullName方法"
]
refactor_results = await demonstrate_code_refactoring(
original_code,
modifications
)
if refactor_results["success"]:
for result in refactor_results["results"]:
print(f"\n修改要求: {result['modification']}")
if result["result"]["success"]:
print("修改后的代码:")
print(result["result"]["content"])
# 流式输出示例
print("\n2. 流式输出示例")
refactor_prompt = "将username属性替换为email属性"
await demonstrate_prediction_streaming(
original_code,
refactor_prompt
)
# 内容补全示例
print("\n3. 内容补全示例")
partial_content = "OpenAI是一家"
expected_completion = "OpenAI是一家专注于人工智能研究的公司"
completion_result = await demonstrate_content_completion(
partial_content,
expected_completion
)
if completion_result["success"]:
print(f"原始内容: {completion_result['original']}")
print(f"补全结果: {completion_result['completion']}")
print(f"预期结果: {completion_result['expected']}")
print("=== 15. RAG系统演示 ===")
# 初始化RAG系统
rag = RAGSystem()
# 添加示例文档
documents = [
{
"title": "Python简介",
"content": "Python是一种高级编程语言,以其简洁的语法和丰富的库而闻名。它支持多种编程范式,包括面向对象、命令式和函数式编程。"
},
{
"title": "机器学习基础",
"content": "机器学习是人工智能的一个子领域,它使计算机系统能够通过经验自动改进。常见的机器学习方法包括监督学习、无监督学习和强化学习。"
},
{
"title": "深度学习简介",
"content": "深度学习是机器学习的一个分支,使用多层神经网络进行特征学习和模式识别。它在图像识别、自然语言处理等领域取得了突破性进展。"
}
]
for doc in documents:
rag.add_document(doc["title"], doc["content"])
# 保存知识库
rag.save_knowledge_base("output/knowledge_base.json")
# 测试查询
queries = [
"什么是Python?",
"机器学习和深度学习有什么关系?",
"量子计算机是什么?" # 知识库中没有的信息
]
for query in queries:
print(f"\n问题: {query}")
result = await rag.generate_answer(query)
if result["success"]:
print("\n回答:", result["answer"])
print("\n相关文档:")
for doc in result["relevant_docs"]:
print(f"- {doc['document']['title']} (相似度: {doc['similarity']:.3f})")
else:
print(f"生成回答时出错: {result['error']}")
print("\n=== 16. 文件搜索助手演示 ===")
assistant = FileSearchAssistant()
# 设置助手
print("正在设置文件搜索助手...")
setup_result = await assistant.setup_assistant()
if not setup_result["success"]:
raise Exception(f"设置助手失败: {setup_result['error']}")
# 上传测试文件
print("\n正在上传测试文件...")
test_files = [
"tools/docs/python_guide.txt",
"tools/docs/machine_learning.pdf",
"tools/docs/api_documentation.md"
]
for file_path in test_files:
result = await assistant.upload_file(file_path)
if result["success"]:
print(f"成功上传文件: {file_path}")
else:
print(f"上传文件失败 {file_path}: {result['error']}")
# 测试搜索查询
test_queries = [
"Python中如何处理异常?请给出示例代码",
"机器习的主要类有哪些?每种类型的特点是什么?",
"API认证有哪些主要方法?请详细说明每种方法的特点"
]
print("\n开始测试文件搜索...")
for query in test_queries:
print(f"\n问题: {query}")
result = await assistant.search_in_files(query)
if result["success"]:
print("\n回答:", result["response"])
else:
print(f"搜索失败: {result['error']}")
# 清理资源
print("\n清理资源...")
await assistant.cleanup()
print("文件搜索演示完成")
print("\n=== 17. 元提示生成演示 ===")
await demonstrate_meta_prompts()
print("\n=== 18. 代码解释器演示 ===")
interpreter_result = demo_code_interpreter()
if interpreter_result:
print(f"代码解释器助手ID: {interpreter_result['assistant_id']}")
print(f"对话线程ID: {interpreter_result['thread_id']}")
print("代码解释器演示完成!")
except Exception as e:
print(f"发生错误: {str(e)}")