-
Notifications
You must be signed in to change notification settings - Fork 7
/
test_usage.py
188 lines (163 loc) · 6.66 KB
/
test_usage.py
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
import base64
import json
from pathlib import Path
import sys
from colorama import init, Fore, Style
sys.path.append(str(Path(__file__).parent / "src"))
from openai_unofficial import OpenAIUnofficial
init()
#------------------------ Basic Chat Completion ------------------------#
print(f"\n{Fore.CYAN}{'='*50}")
print(f"{Fore.YELLOW}Testing OpenAI Unofficial API Endpoints")
print(f"{Fore.CYAN}{'='*50}{Style.RESET_ALL}\n")
client = OpenAIUnofficial()
print(f"{Fore.GREEN}▶ Testing Basic Chat Completion{Style.RESET_ALL}")
completion = client.chat.completions.create(
messages=[{"role": "user", "content": "Say hello!"}],
model="gpt-4o-mini-2024-07-18"
)
print(f"{Fore.WHITE}Response: {completion.choices[0].message.content}{Style.RESET_ALL}\n")
#------------------------ Chat Completion with Image ------------------------#
client = OpenAIUnofficial()
print(f"{Fore.GREEN}▶ Testing Chat Completion with Image Input{Style.RESET_ALL}")
completion = client.chat.completions.create(
messages=[{
"role": "user",
"content": [
{"type": "text", "text": "What's in this image?"},
{
"type": "image_url",
"image_url": {
"url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg",
}
},
],
}],
model="gpt-4o-mini-2024-07-18"
)
print(f"{Fore.WHITE}Response: {completion.choices[0].message.content}{Style.RESET_ALL}\n")
#------------------------ Streaming Chat Completion ------------------------#
client = OpenAIUnofficial()
print(f"{Fore.GREEN}▶ Testing Streaming Chat Completion{Style.RESET_ALL}")
completion_stream = client.chat.completions.create(
messages=[{"role": "user", "content": "Write a short story in 3 sentences."}],
model="gpt-4o-mini-2024-07-18",
stream=True
)
print(f"{Fore.WHITE}Streaming response:", end='')
for chunk in completion_stream:
content = chunk.choices[0].delta.content
if content:
print(content, end='', flush=True)
print(f"{Style.RESET_ALL}\n")
#------------------------ Audio Speech Generation ------------------------#
client = OpenAIUnofficial()
print(f"{Fore.GREEN}▶ Testing Audio Speech Generation{Style.RESET_ALL}")
audio_data = client.audio.create(
input_text="Hello, this is a test message!",
model="tts-1-hd-1106",
voice="nova"
)
output_path = Path("test_audio.mp3")
output_path.write_bytes(audio_data)
print(f"{Fore.WHITE}Audio file saved: {output_path}{Style.RESET_ALL}\n")
#------------------------ Image Generation ------------------------#
client = OpenAIUnofficial()
print(f"{Fore.GREEN}▶ Testing Image Generation{Style.RESET_ALL}")
image_response = client.image.create(
prompt="A beautiful sunset over mountains",
model="dall-e-3",
size="1024x1024"
)
print(f"{Fore.WHITE}Generated Image URL: {image_response.data[0].url}{Style.RESET_ALL}\n")
#------------------------ Audio Preview Model ------------------------#
client = OpenAIUnofficial()
print(f"{Fore.GREEN}▶ Testing Audio Preview Model{Style.RESET_ALL}")
try:
completion = client.chat.completions.create(
messages=[{"role": "user", "content": "Tell me a short joke."}],
model="gpt-4o-audio-preview-2024-10-01",
modalities=["text", "audio"],
audio={"voice": "fable", "format": "wav"}
)
message = completion.choices[0].message
print(f"{Fore.WHITE}Text Response: {message.content}")
if message.audio and 'data' in message.audio:
output_path = Path("audio_preview.wav")
output_path.write_bytes(base64.b64decode(message.audio['data']))
print(f"Audio preview saved: {output_path}{Style.RESET_ALL}")
except Exception as e:
print(f"{Fore.RED}Audio preview test failed: {e}{Style.RESET_ALL}")
#------------------------ Function Calling ------------------------#
client = OpenAIUnofficial()
print(f"{Fore.GREEN}▶ Testing Function Calling{Style.RESET_ALL}")
def get_current_weather(location: str, unit: str = "celsius") -> str:
if unit == "fahrenheit":
temperature = 72
else:
temperature = 22
return f"The weather in {location} is {temperature} degrees {unit}."
tools = [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather for a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city name, e.g., London, New York"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "Temperature unit"
}
},
"required": ["location"]
}
}
}
]
messages = [
{"role": "user", "content": "What's the weather like in New York?"}
]
print(f"{Fore.WHITE}Step 1: Initial API call for function calling")
response = client.chat.completions.create(
model="gpt-4o-mini-2024-07-18",
messages=messages,
tools=tools,
tool_choice="auto"
)
assistant_message = response.choices[0].message
print(f"Assistant's Initial Response: {assistant_message.to_dict()}")
messages.append(assistant_message.to_dict())
if assistant_message.tool_calls:
tool_call = assistant_message.tool_calls[0]
function_name = tool_call.function.name
function_args = json.loads(tool_call.function.arguments)
print(f"\nFunction Called: {function_name}")
print(f"Function Arguments: {function_args}")
function_response = get_current_weather(**function_args)
print(f"Function Response: {function_response}")
messages.append({
"role": "tool",
"tool_call_id": tool_call.id,
"name": function_name,
"content": function_response
})
print("\nStep 2: Final API call with function response")
final_response = client.chat.completions.create(
model="gpt-4o-mini-2024-07-18",
messages=messages,
tools=tools
)
print(f"Final Assistant Response: {final_response.choices[0].message.content}")
else:
print(f"No function call needed. Response: {assistant_message.content}")
print(f"{Style.RESET_ALL}\n")
print(f"\n{Fore.CYAN}{'='*50}")
print(f"{Fore.YELLOW}All tests completed")
print(f"{Fore.CYAN}{'='*50}{Style.RESET_ALL}\n")