ZyphrZero commited on
Commit
db61ab8
·
1 Parent(s): d382277

Create test_anthropic.py

Browse files
Files changed (1) hide show
  1. tests/test_anthropic.py +94 -0
tests/test_anthropic.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+
3
+ import json
4
+ import requests
5
+
6
+ # 服务器配置
7
+ BASE_URL = "http://localhost:8080/v1/messages"
8
+ API_KEY = "sk-your-api-key" # 修改为你的 API key
9
+
10
+ test_data = {
11
+ "model": "GLM-4.5",
12
+ "messages": [
13
+ {
14
+ "role": "user",
15
+ "content": "你好,这是一个测试"
16
+ }
17
+ ],
18
+ "system": [
19
+ {
20
+ "type": "text",
21
+ "text": "You are Claude Code, Anthropic's official CLI for Claude.",
22
+ "cache_control": {
23
+ "type": "ephemeral"
24
+ }
25
+ }
26
+ ],
27
+ "max_tokens": 1024,
28
+ "stream": False,
29
+ }
30
+
31
+ def test_non_stream():
32
+ """测试非流式请求"""
33
+ print("=== 测试非流式请求 ===")
34
+
35
+ try:
36
+ response = requests.post(
37
+ BASE_URL,
38
+ headers={"x-api-key": API_KEY},
39
+ json=test_data,
40
+ timeout=30.0
41
+ )
42
+
43
+ print(f"状态码: {response.status_code}")
44
+
45
+ if response.status_code == 200:
46
+ result = response.json()
47
+ print("响应成功!")
48
+ print(f"ID: {result.get('id')}")
49
+ print(f"模型: {result.get('model')}")
50
+ if result.get('content'):
51
+ print(f"内容: {result['content'][0]['text']}")
52
+ else:
53
+ print("错误响应:")
54
+ print(response.text)
55
+
56
+ except Exception as e:
57
+ print(f"请求失败: {e}")
58
+
59
+ def test_stream():
60
+ """测试流式请求"""
61
+ print("\n=== 测试流式请求 ===")
62
+
63
+ stream_data = test_data.copy()
64
+ stream_data["stream"] = True
65
+
66
+ try:
67
+ response = requests.post(
68
+ BASE_URL,
69
+ headers={"x-api-key": API_KEY},
70
+ json=stream_data,
71
+ stream=True,
72
+ timeout=30.0
73
+ )
74
+
75
+ print(f"状态码: {response.status_code}")
76
+
77
+ if response.status_code == 200:
78
+ print("流式响应内容:")
79
+ for line in response.iter_lines():
80
+ if line:
81
+ print(f" {line.decode('utf-8')}")
82
+ else:
83
+ print("错误响应:")
84
+ print(response.text)
85
+
86
+ except Exception as e:
87
+ print(f"请求失败: {e}")
88
+
89
+ if __name__ == "__main__":
90
+ try:
91
+ test_non_stream()
92
+ test_stream()
93
+ except KeyboardInterrupt:
94
+ print("\n测试已取消")