-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreasoning_example.py
117 lines (110 loc) · 3.93 KB
/
reasoning_example.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
from openai import OpenAI
from typing import List, Dict, Any
import json
import os
client = OpenAI(
api_key=os.getenv("OPENAI_API_KEY"),
organization=os.getenv("OPENAI_ORG_ID"), # 可选 OpenAI API 中的组织 ID
timeout=30.0 # 默认超时时间
)
def demonstrate_cot_reasoning(problem: str = None) -> Dict[str, Any]:
"""
演示链式思维(Chain of Thought)推理
通过引导模型一步步思考来解决复杂问题
"""
if problem is None:
problem = "小明买了3个苹果,每个苹果5元。小红买了2个梨,每个梨4元。他们一共花了多少钱?"
try:
response = client.chat.completions.create(
model="gpt-4", # 使用GPT-4以获得更好的推理能力
messages=[
{"role": "system", "content": """解题时请遵循以下步骤:
1. 分析问题中的已知条件
2. 列出解题步骤
3. 逐步计算
4. 得出最终结果
请在每个步骤前标明步骤编号。"""},
{"role": "user", "content": problem}
],
temperature=0.2, # 降低随机性,保持逻辑性
max_tokens=500,
top_p=0.9,
)
return {
"success": True,
"reasoning": response.choices[0].message.content
}
except Exception as e:
return {
"success": False,
"error": str(e)
}
def solve_complex_task(task: str = None) -> Dict[str, Any]:
"""
演示系统设计推理
通过分解复杂任务并逐步解决
"""
if task is None:
task = """设计一个在线书店系统的主要功能和架构"""
try:
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": """你是一个专业的系统设计专家。
分析问题时请遵循以下框架:
1. 需求分析:列出核心功能需求
2. 系统架构:描述主要组件
3. 数据模型:设计核心数据结构
4. 接口设计:定义关键API
5. 技术选型:推荐适合的技术栈
6. 扩展性考虑:分析可能的扩展点
请在每个部分前标明编号,并详细展开说明。"""},
{"role": "user", "content": task}
],
temperature=0.3,
max_tokens=1000,
top_p=0.9,
)
return {
"success": True,
"design": response.choices[0].message.content
}
except Exception as e:
return {
"success": False,
"error": str(e)
}
def demonstrate_step_by_step_coding(problem: str = None) -> Dict[str, Any]:
"""
演示编程问题的步骤分解和解决
通过引导模型逐步思考和编写代码
"""
if problem is None:
problem = "编写一个函数,找出数组中的第二大的数"
try:
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": """你是一个专业的编程教练。
解决编程问题时请遵循以下步骤:
1. 问题分析:理解问题要求
2. 思路说明:描述解决方案
3. 代码实现:编写示例代码
4. 复杂度分析:分析时间和空间复杂度
5. 测试用例:提供测试样例
请在每个步骤前标明编号。"""},
{"role": "user", "content": problem}
],
temperature=0.2,
max_tokens=800,
top_p=0.9,
)
return {
"success": True,
"solution": response.choices[0].message.content
}
except Exception as e:
return {
"success": False,
"error": str(e)
}