-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_llmproviders_service.sh
executable file
·350 lines (296 loc) · 10.9 KB
/
test_llmproviders_service.sh
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#!/bin/bash
BASE_URL="http://localhost:8000"
TOTAL_TESTS=0
PASSED_TESTS=0
# Check and print environment variables
check_env_var() {
local var_name=$1
local var_value=${!var_name}
if [ -z "$var_value" ]; then
echo "$var_name is not set"
else
echo "$var_name: ${var_value:0:5}..."
fi
}
echo "Checking environment variables:"
check_env_var "AMBER_FLUENT_OPENAI_API_KEY"
check_env_var "AMBER_FLUENT_ANTHROPIC_KEY_01"
check_env_var "AMBER_FLUENT_COHERE_API_KEY_01"
# Function to make API requests
make_request() {
local method=$1
local endpoint=$2
local data=$3
local token=$4
local expected_status=$5
TOTAL_TESTS=$((TOTAL_TESTS + 1))
headers=(-H "Accept: application/json")
if [ -n "$data" ]; then
headers+=(-H "Content-Type: application/json")
fi
if [ -n "$token" ]; then
headers+=(-H "Authorization: Bearer $token")
fi
echo -e "\n==== Request details ===="
echo "Method: $method"
echo "Endpoint: $endpoint"
echo "Data: $data"
echo "Token: $token"
echo "Expected status: $expected_status"
if [ -n "$data" ]; then
response=$(curl -v -s -w "\n%{http_code}" -X $method "${headers[@]}" -d "$data" "$BASE_URL$endpoint" 2>&1)
else
response=$(curl -v -s -w "\n%{http_code}" -X $method "${headers[@]}" "$BASE_URL$endpoint" 2>&1)
fi
echo -e "\n==== Full Response ===="
echo "$response"
status_code=$(echo "$response" | tail -n 1)
body=$(echo "$response" | sed '$d' | sed -n '/^{/,/^}/p' | tr -d '\n')
echo -e "\n==== Parsed Response ===="
echo "Status code: $status_code"
echo "Body: $body"
if [ "$status_code" = "$expected_status" ]; then
PASSED_TESTS=$((PASSED_TESTS + 1))
print_result "PASS" "$method" "$endpoint" "$expected_status" "$status_code"
else
print_result "FAIL" "$method" "$endpoint" "$expected_status" "$status_code"
fi
echo "$body"
}
# Function to print test results
print_result() {
local result=$1
local method=$2
local endpoint=$3
local expected=$4
local actual=$5
if [ "$result" = "PASS" ]; then
echo -e "\e[32m✓ PASS\e[0m $method $endpoint (Expected: $expected, Got: $actual)\n"
else
echo -e "\e[31m✗ FAIL\e[0m $method $endpoint (Expected: $expected, Got: $actual)\n"
fi
}
# Function to validate LLM provider fields
validate_llm_provider() {
local provider_data=$1
local name=$2
local provider_type=$3
local api_endpoint=$4
local model=$5
echo "Validating LLM provider fields:"
if [[ $provider_data == *"\"name\":\"$name\""* ]]; then
echo "✓ Name is correct"
PASSED_TESTS=$((PASSED_TESTS + 1))
else
echo "✗ Name is incorrect"
fi
TOTAL_TESTS=$((TOTAL_TESTS + 1))
if [[ $provider_data == *"\"provider_type\":\"$provider_type\""* ]]; then
echo "✓ Provider type is correct"
PASSED_TESTS=$((PASSED_TESTS + 1))
else
echo "✗ Provider type is incorrect"
fi
TOTAL_TESTS=$((TOTAL_TESTS + 1))
if [[ $provider_data == *"\"api_endpoint\":\"$api_endpoint\""* ]]; then
echo "✓ API endpoint is correct"
PASSED_TESTS=$((PASSED_TESTS + 1))
else
echo "✗ API endpoint is incorrect"
fi
TOTAL_TESTS=$((TOTAL_TESTS + 1))
if [[ $provider_data == *"\"supported_modalities\":[\"text\"]"* ]]; then
echo "✓ Supported modalities are correct"
PASSED_TESTS=$((PASSED_TESTS + 1))
else
echo "✗ Supported modalities are incorrect"
fi
TOTAL_TESTS=$((TOTAL_TESTS + 1))
if [[ $provider_data == *"\"model\":\"$model\""* ]]; then
echo "✓ Model is correct"
PASSED_TESTS=$((PASSED_TESTS + 1))
else
echo "✗ Model is incorrect"
fi
TOTAL_TESTS=$((TOTAL_TESTS + 1))
if [[ $provider_data == *"\"max_tokens\":150"* ]]; then
echo "✓ Max tokens is correct"
PASSED_TESTS=$((PASSED_TESTS + 1))
else
echo "✗ Max tokens is incorrect"
fi
TOTAL_TESTS=$((TOTAL_TESTS + 1))
}
# Generate a unique username
TIMESTAMP=$(date +%s)
USERNAME="llmuser_${TIMESTAMP}"
# Create a new user
echo "Creating a new user"
user_response=$(make_request POST "/users" '{"username": "'"$USERNAME"'", "email": "'"[email protected]"'", "password": "testpass123"}' "" "201")
user_id=$(echo "$user_response" | grep -o '"id":"[^"]*' | cut -d'"' -f4 | head -n 1)
echo "User ID: $user_id"
if [ -z "$user_id" ]; then
echo "Failed to create user. Exiting."
exit 1
fi
# Login with the new user
echo "Logging in with the new user"
login_response=$(make_request POST "/users/login" '{"username": "'"$USERNAME"'", "password": "testpass123"}' "" "200")
token=$(echo "$login_response" | grep -o '"token":"[^"]*' | cut -d'"' -f4 | head -n 1)
echo "Token: $token"
if [ -z "$token" ]; then
echo "Failed to login. Exiting."
exit 1
fi
# Function to test a specific provider
test_provider() {
local name=$1
local api_key
local provider_type
local model
case $name in
"OpenAI")
api_key="$AMBER_FLUENT_OPENAI_API_KEY"
provider_type="openai"
model="gpt-3.5-turbo"
;;
"Anthropic")
api_key="$AMBER_FLUENT_ANTHROPIC_KEY_01"
provider_type="anthropic"
model="claude-2"
;;
"Cohere")
api_key="$AMBER_FLUENT_COHERE_API_KEY_01"
provider_type="cohere"
model="command"
;;
*)
echo "Unknown provider $name. Skipping."
return
;;
esac
if [ -z "$api_key" ]; then
echo "API key for $name is not set. Skipping."
return
fi
echo "Testing $name provider"
# Create a new API key
echo "Creating a new API key for $name"
api_key_response=$(make_request POST "/api_keys" '{
"key_value": "'"$api_key"'",
"description": "Test '"$name"' API Key"
}' "$token" "201")
api_key_id=$(echo "$api_key_response" | grep -o '"id":"[^"]*' | cut -d'"' -f4 | head -n 1)
echo "$name API Key ID: $api_key_id"
if [ -z "$api_key_id" ]; then
echo "Failed to create $name API key. Skipping further tests."
return
fi
# Create a new LLM provider
echo "Creating a new LLM provider for $name"
provider_response=$(make_request POST "/llm/providers" '{
"user_id": "'"$user_id"'",
"name": "Test'"$name"'Provider",
"provider_type": "'"$provider_type"'",
"api_endpoint": "https://api.'"$provider_type"'.com/v1",
"supported_modalities": ["text"],
"configuration": {
"model": "'"$model"'",
"max_tokens": 150
}
}' "$token" "201")
provider_id=$(echo "$provider_response" | grep -o '"id":"[^"]*' | cut -d'"' -f4 | head -n 1)
echo "$name Provider ID: $provider_id"
if [ -z "$provider_id" ]; then
echo "Failed to create $name LLM provider. Skipping further tests."
return
fi
# Validate the created LLM provider
validate_llm_provider "$provider_response" "Test${name}Provider" "$provider_type" "https://api.${provider_type}.com/v1" "$model"
# Get the created LLM provider
echo "Getting the created $name LLM provider"
get_provider_response=$(make_request GET "/llm/providers/$provider_id" "" "$token" "200")
# Validate the retrieved LLM provider
validate_llm_provider "$get_provider_response" "Test${name}Provider" "$provider_type" "https://api.${provider_type}.com/v1" "$model"
# Test LLM chat
echo "Testing $name LLM chat"
chat_response=$(make_request POST "/llm/chat" '{
"provider_id": "'"$provider_id"'",
"messages": [
{"role": "user", "content": "Hello, how are you?"}
]
}' "$token" "200")
# Create a user LLM config
echo "Creating a user LLM config for $name"
config_response=$(make_request POST "/chat/user-llm-configs" '{
"user_id": "'"$user_id"'",
"provider_id": "'"$provider_id"'",
"api_key_id": "'"$api_key_id"'"
}' "$token" "201")
config_id=$(echo "$config_response" | grep -o '"id":"[^"]*' | cut -d'"' -f4 | head -n 1)
echo "$name Config ID: $config_id"
if [ -z "$config_id" ]; then
echo "Failed to create $name user LLM config. Skipping further tests."
return
fi
# Get the created user LLM config
echo "Getting the created $name user LLM config"
make_request GET "/chat/user-llm-configs/$config_id" "" "$token" "200"
# Delete the user LLM config
echo "Deleting the $name user LLM config"
make_request DELETE "/chat/user-llm-configs/$config_id" "" "$token" "204"
# Verify the user LLM config was deleted
echo "Verifying the $name user LLM config was deleted"
make_request GET "/chat/user-llm-configs/$config_id" "" "$token" "404"
# Delete the LLM provider
echo "Deleting the $name LLM provider"
make_request DELETE "/llm/providers/$provider_id" "" "$token" "204"
# Verify the LLM provider was deleted
echo "Verifying the $name LLM provider was deleted"
make_request GET "/llm/providers/$provider_id" "" "$token" "404"
# Add a small delay before deleting the API key
sleep 2
# Delete the API key
echo "Deleting the $name API key"
delete_response=$(make_request DELETE "/api_keys/$api_key_id" "" "$token" "204")
delete_status=$(echo "$delete_response" | tail -n 1)
if [ "$delete_status" != "204" ]; then
echo "Failed to delete $name API key. Status: $delete_status"
echo "Response: $delete_response"
echo "API Key ID: $api_key_id"
echo "Attempting to retrieve the API key to check its status..."
make_request GET "/api_keys/$api_key_id" "" "$token" "404"
else
# Verify the API key was deleted
echo "Verifying the $name API key was deleted"
make_request GET "/api_keys/$api_key_id" "" "$token" "404"
fi
}
# Test each provider
test_provider "OpenAI"
test_provider "Anthropic"
test_provider "Cohere"
# Get all LLM providers
echo "Getting all LLM providers"
make_request GET "/llm/providers" "" "$token" "200"
# Test creating a user LLM config with an invalid provider ID (error case)
echo "Testing creation of user LLM config with invalid provider ID"
make_request POST "/chat/user-llm-configs" '{
"user_id": "'"$user_id"'",
"provider_id": "00000000-0000-0000-0000-000000000000",
"api_key_id": "00000000-0000-0000-0000-000000000000"
}' "$token" "400"
# Test getting a non-existent user LLM config (error case)
echo "Testing retrieval of non-existent user LLM config"
make_request GET "/chat/user-llm-configs/00000000-0000-0000-0000-000000000000" "" "$token" "404"
# Print test summary
echo "Total tests: $TOTAL_TESTS"
echo "Passed tests: $PASSED_TESTS"
echo "Failed tests: $((TOTAL_TESTS - PASSED_TESTS))"
if [ $PASSED_TESTS -eq $TOTAL_TESTS ]; then
echo -e "\e[32mAll tests passed!\e[0m"
exit 0
else
echo -e "\e[31mSome tests failed.\e[0m"
exit 1
fi