Repository navigation
Expand file tree
/
Copy pathmock_server.py
More file actions
226 lines (192 loc) · 6.5 KB
/
Copy pathmock_server.py
File metadata and controls
226 lines (192 loc) · 6.5 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
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
"""
模拟比赛平台服务器(mock_server.py)
用于在没有真实比赛平台的情况下测试选手程序。
使用方法:
1. 在终端1运行此服务器
2. 在终端2运行 main.py(设置 PLATFORM_URL=http://localhost:8002)
3. 观察日志输出
启动命令:
python mock_server.py
"""
from fastapi import FastAPI, HTTPException
import uvicorn
app = FastAPI()
# 测试任务列表(包含overview信息)
tasks = [
{
"task_id": "test_001",
"target_sla": "Gold",
"target_reward": 100,
"overview": {
"task_id": "test_001",
"target_sla": "Gold",
"target_reward": 100
},
"messages": [
{
"id": "msg_1",
"eval_request_type": "generate_until",
"prompt": "请介绍一下人工智能的未来发展趋势。",
"eval_gen_kwargs": {
"max_gen_toks": 100,
"temperature": 0.7,
"until": ["\n\n", "。"]
}
},
{
"id": "msg_2",
"eval_request_type": "loglikelihood",
"prompt": "中国的首都是",
"eval_continuation": "北京"
},
{
"id": "msg_3",
"eval_request_type": "loglikelihood_rolling",
"prompt": "今天天气很好,适合出门散步。"
}
]
},
{
"task_id": "test_002",
"target_sla": "Gold",
"target_reward": 150,
"overview": {
"task_id": "test_002",
"target_sla": "Gold",
"target_reward": 150
},
"messages": [
{
"id": "msg_1",
"eval_request_type": "generate_until",
"prompt": "解释一下什么是机器学习:",
"eval_gen_kwargs": {
"max_gen_toks": 150,
"temperature": 0.5,
"until": ["\n\n"]
}
},
{
"id": "msg_2",
"eval_request_type": "loglikelihood",
"prompt": "太阳从",
"eval_continuation": "东方升起"
}
]
},
{
"task_id": "test_003",
"target_sla": "Silver",
"target_reward": 80,
"overview": {
"task_id": "test_003",
"target_sla": "Silver",
"target_reward": 80
},
"messages": [
{
"id": "msg_1",
"eval_request_type": "generate_until",
"prompt": "1+1等于几?",
"eval_gen_kwargs": {
"max_gen_toks": 20,
"temperature": 0.0,
"until": ["\n"]
}
},
{
"id": "msg_2",
"eval_request_type": "loglikelihood",
"prompt": "水的化学式是",
"eval_continuation": "H2O"
},
{
"id": "msg_3",
"eval_request_type": "loglikelihood_rolling",
"prompt": "自然科学是研究自然现象和规律的学科。"
}
]
}
]
task_index = 0
@app.post("/register")
async def register(data: dict):
"""注册队伍"""
print(f"[平台] 收到注册请求: name={data.get('name')}, token={data.get('token')[:8]}...")
return {"status": "ok"}
@app.post("/query")
async def query(data: dict):
"""查询任务"""
global task_index
print(f"[平台] 收到查询请求: token={data.get('token', '')[:8]}...")
if task_index >= len(tasks):
print("[平台] 所有任务已发放完毕,返回404")
raise HTTPException(status_code=404, detail="No more tasks")
task = tasks[task_index]
print(f"[平台] 返回任务: {task['task_id']}, reward={task['target_reward']}")
return {
"task_id": task["task_id"],
"target_sla": task["target_sla"],
"target_reward": task["target_reward"]
}
@app.post("/ask")
async def ask(data: dict):
"""接受任务"""
global task_index
print(f"[平台] 收到ask请求: task_id={data.get('task_id')}, sla={data.get('sla')}")
if task_index >= len(tasks):
return {"status": "closed"}
task = tasks[task_index]
task_id = data.get("task_id")
if task_id != task["task_id"]:
print(f"[平台] Task ID不匹配")
return {"status": "rejected", "reason": "Task ID mismatch"}
result = {
"status": "accepted",
"task": {
"overview": task["overview"],
"messages": task["messages"]
}
}
print(f"[平台] ✅ 任务 {task['task_id']} 已接受")
task_index += 1
return result
@app.post("/submit")
async def submit(data: dict):
"""提交结果"""
user = data.get("user", {})
msg_data = data.get("msg", {})
messages = msg_data.get("messages", [])
print(f"[平台] 收到提交请求:")
print(f" - 队伍: {user.get('name')}")
print(f" - messages数量: {len(messages)}")
for i, msg in enumerate(messages):
eval_type = msg.get("eval_request_type", "unknown")
response = msg.get("response")
accuracy = msg.get("accuracy")
if eval_type == "generate_until":
print(f" [{i+1}] generate_until: response长度={len(response) if response else 0}")
elif eval_type in ["loglikelihood", "loglikelihood_rolling"]:
print(f" [{i+1}] {eval_type}: accuracy={accuracy:.4f if accuracy else 'None'}")
return {"status": "ok", "message": "Submission received"}
@app.get("/")
async def root():
"""平台状态"""
return {
"status": "running",
"total_tasks": len(tasks),
"tasks_sent": task_index,
"tasks_remaining": len(tasks) - task_index
}
@app.get("/health")
async def health():
"""健康检查"""
return {"status": "healthy"}
if __name__ == "__main__":
print("=" * 60)
print(" 模拟比赛平台服务器")
print("=" * 60)
print(f" 共 {len(tasks)} 个测试任务")
print(" 监听地址: http://0.0.0.0:8002")
print("=" * 60)
uvicorn.run(app, host="0.0.0.0", port=8002)