-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathtestcalls.py
More file actions
97 lines (85 loc) · 3.02 KB
/
Copy pathtestcalls.py
File metadata and controls
97 lines (85 loc) · 3.02 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
import requests
import json
base_url = "http://localhost:1337/v1"
conversation_id = None
def test_list_models():
response = requests.get(f"{base_url}/models")
assert response.status_code == 200
data = response.json()
assert "data" in data
assert len(data["data"]) > 0
print("List Models:", data)
def test_chat_completion():
global conversation_id
headers = {"Content-Type": "application/json"}
payload = {
"model": "keyless-gpt-4o-mini",
"messages": [
{"role": "user", "content": "Hello, how are you?"}
],
"stream": True
}
response = requests.post(
f"{base_url}/chat/completions",
json=payload,
headers=headers,
stream=True
)
assert response.status_code == 200
print("Chat Completion:")
try:
for line in response.iter_lines():
if line:
decoded_line = line.decode("utf-8")
if decoded_line.startswith("data: "):
data = decoded_line[6:]
if data != "[DONE]":
try:
response_data = json.loads(data)
print(f"Received chunk: {response_data}")
if "id" in response_data and not conversation_id:
conversation_id = response_data["id"]
print(f"Conversation ID captured: {conversation_id}")
except json.JSONDecodeError as e:
print(f"Failed to parse JSON: {data}")
continue
except Exception as e:
print(f"Error during streaming: {str(e)}")
def test_non_streaming_chat_completion():
global conversation_id
headers = {"Content-Type": "application/json"}
payload = {
"model": "keyless-gpt-4o-mini",
"messages": [
{"role": "user", "content": "Hello, how are you?"}
],
"stream": False
}
response = requests.post(
f"{base_url}/chat/completions",
json=payload,
headers=headers
)
assert response.status_code == 200
data = response.json()
print("Non-streaming Chat Completion:", data)
if "id" in data:
conversation_id = data["id"]
print(f"Conversation ID captured: {conversation_id}")
def test_end_conversation(conversation_id):
response = requests.delete(f"{base_url}/conversations/{conversation_id}")
assert response.status_code == 200
data = response.json()
assert "message" in data
print("End Conversation:", data)
if __name__ == "__main__":
test_list_models()
print("\nTesting streaming completion:")
test_chat_completion()
print("\nTesting non-streaming completion:")
test_non_streaming_chat_completion()
if conversation_id:
print("\nTesting conversation deletion:")
test_end_conversation(conversation_id)
else:
print("No valid conversation ID found to end the conversation.")