-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_openai_api.py
More file actions
156 lines (126 loc) · 4.51 KB
/
test_openai_api.py
File metadata and controls
156 lines (126 loc) · 4.51 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
#!/usr/bin/env python3
"""
Test script for the OpenAI-compatible API endpoints.
This demonstrates how to use the API with both direct requests and OpenAI client library.
"""
import requests
import json
import sys
# Configuration
BASE_URL = "http://localhost:8000"
HEADERS = {"Content-Type": "application/json"}
def test_health_check():
"""Test the health check endpoint."""
print("Testing health check...")
response = requests.get(f"{BASE_URL}/health")
print(f"Status: {response.status_code}")
print(f"Response: {response.json()}")
print()
def test_list_models():
"""Test the models listing endpoint."""
print("Testing models listing...")
response = requests.get(f"{BASE_URL}/v1/models")
print(f"Status: {response.status_code}")
print(f"Response: {json.dumps(response.json(), indent=2)}")
print()
def test_chat_completion():
"""Test the chat completion endpoint."""
print("Testing chat completion...")
payload = {
"model": "mistral:latest",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello! Can you tell me a short joke?"}
],
"temperature": 0.7,
"max_tokens": 150
}
response = requests.post(
f"{BASE_URL}/v1/chat/completions",
headers=HEADERS,
json=payload
)
print(f"Status: {response.status_code}")
if response.status_code == 200:
result = response.json()
print(f"Response: {json.dumps(result, indent=2)}")
print(f"Assistant's message: {result['choices'][0]['message']['content']}")
else:
print(f"Error: {response.text}")
print()
def test_conversation():
"""Test a multi-turn conversation."""
print("Testing multi-turn conversation...")
payload = {
"model": "mistral:latest",
"messages": [
{"role": "system", "content": "You are a helpful programming assistant."},
{"role": "user", "content": "What is Python?"},
{"role": "assistant", "content": "Python is a high-level, interpreted programming language known for its simplicity and readability."},
{"role": "user", "content": "Can you give me a simple Python example?"}
],
"temperature": 0.5,
"max_tokens": 200
}
response = requests.post(
f"{BASE_URL}/v1/chat/completions",
headers=HEADERS,
json=payload
)
print(f"Status: {response.status_code}")
if response.status_code == 200:
result = response.json()
print(f"Assistant's response: {result['choices'][0]['message']['content']}")
print(f"Token usage: {result['usage']}")
else:
print(f"Error: {response.text}")
print()
def test_with_openai_client():
"""Test using the OpenAI Python client library."""
try:
from openai import OpenAI
print("Testing with OpenAI client library...")
# Create client pointing to local server
client = OpenAI(
base_url=f"{BASE_URL}/v1",
api_key="dummy-key" # Not used but required by client
)
# Test chat completion
response = client.chat.completions.create(
model="mistral:latest",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Explain what an API is in simple terms."}
],
temperature=0.7,
max_tokens=100
)
print(f"Response: {response.choices[0].message.content}")
print(f"Usage: {response.usage}")
print()
except ImportError:
print("OpenAI client library not installed. Install with: pip install openai")
print("Skipping OpenAI client test...")
print()
def main():
"""Run all tests."""
print("=" * 50)
print("OpenProject Haystack - OpenAI API Compatibility Test")
print("=" * 50)
print()
try:
test_health_check()
test_list_models()
test_chat_completion()
test_conversation()
test_with_openai_client()
print("All tests completed!")
except requests.exceptions.ConnectionError:
print("Error: Could not connect to the server.")
print("Make sure the server is running on http://localhost:8000")
sys.exit(1)
except Exception as e:
print(f"Error: {e}")
sys.exit(1)
if __name__ == "__main__":
main()