Skip to content

Commit f293ae0

Browse files
committed
Add demo for invoking API
Signed-off-by: Aisuko <[email protected]>
1 parent b92afdf commit f293ae0

File tree

6 files changed

+153
-2
lines changed

6 files changed

+153
-2
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
.git
3-
volumes
3+
volumes
4+
__pycache__

.vscode/settings.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"python.testing.pytestArgs": [
3+
"example"
4+
],
5+
"python.testing.unittestEnabled": false,
6+
"python.testing.pytestEnabled": true
7+
}

Makefile

+8-1
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,11 @@ dev: env model-prepare
7575
# stop
7676
.PHONY: stop
7777
stop:
78-
docker compose stop
78+
docker compose stop
79+
80+
#########################################################################################
81+
# testing
82+
83+
.PHONY: pytest
84+
pytest:
85+
@python3 -m pytest -v

example/README.md

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
# Demo for how to use openai API client to invoke voyager API
3+
4+
## Prerequisites
5+
6+
- Python 3.10^
7+
8+
## Installation
9+
10+
11+
Make sure you execute the following command at the root of the project
12+
13+
```bash
14+
pip3 install -r example/requirements.txt
15+
```
16+
17+
## Usage
18+
19+
Before running the test, you need to start all the services by running the following command:
20+
21+
```bash
22+
make up
23+
```
24+
25+
Run the command below and check the results
26+
27+
```
28+
ec2-user@ip-10-110-145-209:~/workspace/voyager$ make pytest
29+
================================================================================= test session starts =================================================================================
30+
platform linux -- Python 3.10.12, pytest-8.1.1, pluggy-1.5.0 -- /usr/bin/python3
31+
cachedir: .pytest_cache
32+
rootdir: /home/ec2-user/workspace/voyager
33+
plugins: anyio-4.4.0
34+
collected 5 items
35+
36+
example/test_apis.py::TestAllAPIs::test_api_key PASSED [ 20%]
37+
example/test_apis.py::TestAllAPIs::test_health PASSED [ 40%]
38+
example/test_apis.py::TestAllAPIs::test_inference_by_openai PASSED [ 60%]
39+
example/test_apis.py::TestAllAPIs::test_inference_by_request PASSED [ 80%]
40+
example/test_apis.py::TestAllAPIs::test_inference_by_request_stream PASSED [100%]
41+
42+
================================================================================== 5 passed in 7.30s ==================================================================================
43+
```

example/requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
openai==1.35.7
2+
pytest==8.1.1

example/test_apis.py

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# coding=utf-8
2+
3+
# Copyright [2024] [SkywardAI]
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
import unittest
17+
import openai
18+
import requests
19+
import json
20+
21+
class TestAllAPIs(unittest.TestCase):
22+
"""
23+
Test all the APIs
24+
"""
25+
26+
@classmethod
27+
def setUpClass(cls):
28+
cls.api_key = "API_KEY"
29+
cls.base_url="http://127.0.0.1:8000"
30+
cls.base_url_openai = "http://127.0.0.1:8000/v1"
31+
cls.client= openai.OpenAI(
32+
api_key=cls.api_key, base_url=cls.base_url_openai)
33+
34+
@classmethod
35+
def tearDownClass(cls):
36+
pass
37+
38+
def test_health(self):
39+
res=requests.get(url=self.base_url+"/healthy")
40+
self.assertEqual(res.status_code, 200)
41+
42+
def test_inference_by_openai(self):
43+
completion=self.client.chat.completions.create(
44+
model="",
45+
messages=[
46+
{"role": "system", "content": "You are a helpful assistant."},
47+
{"role": "user", "content": "What should I do today?"},
48+
],
49+
max_tokens=16,
50+
stop=["\n### user:"],
51+
stream=False
52+
)
53+
# length of message should more than 0
54+
self.assertTrue(len(completion.choices)>0)
55+
print(completion.choices[0].message)
56+
57+
def test_inference_by_request(self):
58+
data = {
59+
"model": "gpt-3.5-turbo",
60+
"messages": [
61+
{"role": "system", "content": "You are a helpful assistant."},
62+
{"role": "user", "content": "What should I do today?"},
63+
],
64+
"max_tokens": 16,
65+
"stop":["\n### user:"],
66+
"stream": False
67+
}
68+
res=requests.post(url=self.base_url+"/v1/chat/completions", json=data,headers={"Content-Type": "application/json", "Authorization":"Bearer no-key"},)
69+
self.assertEqual(res.status_code, 200)
70+
71+
72+
def test_inference_by_request_stream(self):
73+
data = {
74+
"model": "gpt-3.5-turbo",
75+
"messages": [
76+
{"role": "system", "content": "You are a helpful assistant."},
77+
{"role": "user", "content": "What should I do today?"},
78+
],
79+
"max_tokens": 16,
80+
"stop":["\n### user:"],
81+
"stream": True
82+
}
83+
res=requests.post(url=self.base_url+"/v1/chat/completions", json=data,headers={"Content-Type": "application/json", "Authorization":"Bearer no-key"},)
84+
self.assertEqual(res.status_code, 200)
85+
86+
87+
def test_api_key(self):
88+
res=requests.get(url=self.base_url+"/v1/token/api-key")
89+
self.assertEqual(res.status_code, 200)
90+
# api key should not be empty
91+
self.assertTrue(len(res.json().get("api_key"))>0)

0 commit comments

Comments
 (0)