forked from Jasonliu-0/cursorweb2api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_context_memory.py
More file actions
151 lines (129 loc) · 4.56 KB
/
test_context_memory.py
File metadata and controls
151 lines (129 loc) · 4.56 KB
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
#!/usr/bin/env python3
"""
测试上下文记忆功能
验证 API 是否能记住之前的对话内容
"""
import requests
import json
import time
# API 配置
API_URL = "http://localhost:8000/v1/chat/completions"
API_KEY = "sk-yoursk"
def test_conversation():
"""测试多轮对话"""
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json",
"User-Agent": "TestClient/1.0" # 固定的 User-Agent 以保持会话
}
print("=" * 50)
print("测试上下文记忆功能")
print("=" * 50)
# 第一轮对话:问一个数学题
print("\n第一轮对话:")
data1 = {
"model": "gpt-5",
"messages": [{"role": "user", "content": "100+50等于多少?"}],
"stream": False
}
response1 = requests.post(API_URL, headers=headers, json=data1)
if response1.status_code == 200:
result1 = response1.json()
answer1 = result1['choices'][0]['message']['content']
print(f"用户: 100+50等于多少?")
print(f"AI: {answer1}")
else:
print(f"错误: {response1.status_code}")
return
time.sleep(1) # 短暂延迟
# 第二轮对话:引用之前的内容
print("\n第二轮对话(引用上下文):")
data2 = {
"model": "gpt-5",
"messages": [{"role": "user", "content": "这个结果再乘以2是多少?"}],
"stream": False
}
response2 = requests.post(API_URL, headers=headers, json=data2)
if response2.status_code == 200:
result2 = response2.json()
answer2 = result2['choices'][0]['message']['content']
print(f"用户: 这个结果再乘以2是多少?")
print(f"AI: {answer2}")
else:
print(f"错误: {response2.status_code}")
return
time.sleep(1)
# 第三轮对话:继续引用
print("\n第三轮对话(继续引用):")
data3 = {
"model": "gpt-5",
"messages": [{"role": "user", "content": "刚才的计算都对吗?"}],
"stream": False
}
response3 = requests.post(API_URL, headers=headers, json=data3)
if response3.status_code == 200:
result3 = response3.json()
answer3 = result3['choices'][0]['message']['content']
print(f"用户: 刚才的计算都对吗?")
print(f"AI: {answer3}")
else:
print(f"错误: {response3.status_code}")
return
print("\n" + "=" * 50)
print("测试完成!")
print("如果 AI 能够理解'这个结果'和'刚才'的引用,")
print("说明上下文记忆功能正常工作。")
print("=" * 50)
def test_different_sessions():
"""测试不同会话的隔离性"""
print("\n" + "=" * 50)
print("测试会话隔离性")
print("=" * 50)
# 会话1
headers1 = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json",
"User-Agent": "TestClient1"
}
data = {
"model": "gpt-5",
"messages": [{"role": "user", "content": "记住数字888"}],
"stream": False
}
print("\n会话1:")
response1 = requests.post(API_URL, headers=headers1, json=data)
if response1.status_code == 200:
result1 = response1.json()
print(f"用户: 记住数字888")
print(f"AI: {result1['choices'][0]['message']['content']}")
# 会话2(不同的 User-Agent)
headers2 = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json",
"User-Agent": "TestClient2"
}
print("\n会话2(不同客户端):")
data2 = {
"model": "gpt-5",
"messages": [{"role": "user", "content": "我刚才说的数字是什么?"}],
"stream": False
}
response2 = requests.post(API_URL, headers=headers2, json=data2)
if response2.status_code == 200:
result2 = response2.json()
print(f"用户: 我刚才说的数字是什么?")
print(f"AI: {result2['choices'][0]['message']['content']}")
print("(预期:AI 不应该知道888,因为是不同的会话)")
if __name__ == "__main__":
print("开始测试上下文记忆功能...")
print("注意:需要先启动本地服务器")
print("python api/index.py")
print()
try:
test_conversation()
test_different_sessions()
except requests.exceptions.ConnectionError:
print("\n错误:无法连接到 API 服务器")
print("请先运行: python api/index.py")
except Exception as e:
print(f"\n发生错误: {e}")