-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Aisuko <urakiny@gmail.com>
- Loading branch information
Showing
6 changed files
with
153 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
node_modules | ||
.git | ||
volumes | ||
volumes | ||
__pycache__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"python.testing.pytestArgs": [ | ||
"example" | ||
], | ||
"python.testing.unittestEnabled": false, | ||
"python.testing.pytestEnabled": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
|
||
# Demo for how to use openai API client to invoke voyager API | ||
|
||
## Prerequisites | ||
|
||
- Python 3.10^ | ||
|
||
## Installation | ||
|
||
|
||
Make sure you execute the following command at the root of the project | ||
|
||
```bash | ||
pip3 install -r example/requirements.txt | ||
``` | ||
|
||
## Usage | ||
|
||
Before running the test, you need to start all the services by running the following command: | ||
|
||
```bash | ||
make up | ||
``` | ||
|
||
Run the command below and check the results | ||
|
||
``` | ||
ec2-user@ip-10-110-145-209:~/workspace/voyager$ make pytest | ||
================================================================================= test session starts ================================================================================= | ||
platform linux -- Python 3.10.12, pytest-8.1.1, pluggy-1.5.0 -- /usr/bin/python3 | ||
cachedir: .pytest_cache | ||
rootdir: /home/ec2-user/workspace/voyager | ||
plugins: anyio-4.4.0 | ||
collected 5 items | ||
example/test_apis.py::TestAllAPIs::test_api_key PASSED [ 20%] | ||
example/test_apis.py::TestAllAPIs::test_health PASSED [ 40%] | ||
example/test_apis.py::TestAllAPIs::test_inference_by_openai PASSED [ 60%] | ||
example/test_apis.py::TestAllAPIs::test_inference_by_request PASSED [ 80%] | ||
example/test_apis.py::TestAllAPIs::test_inference_by_request_stream PASSED [100%] | ||
================================================================================== 5 passed in 7.30s ================================================================================== | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
openai==1.35.7 | ||
pytest==8.1.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# coding=utf-8 | ||
|
||
# Copyright [2024] [SkywardAI] | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
|
||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import unittest | ||
import openai | ||
import requests | ||
import json | ||
|
||
class TestAllAPIs(unittest.TestCase): | ||
""" | ||
Test all the APIs | ||
""" | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
cls.api_key = "API_KEY" | ||
cls.base_url="http://127.0.0.1:8000" | ||
cls.base_url_openai = "http://127.0.0.1:8000/v1" | ||
cls.client= openai.OpenAI( | ||
api_key=cls.api_key, base_url=cls.base_url_openai) | ||
|
||
@classmethod | ||
def tearDownClass(cls): | ||
pass | ||
|
||
def test_health(self): | ||
res=requests.get(url=self.base_url+"/healthy") | ||
self.assertEqual(res.status_code, 200) | ||
|
||
def test_inference_by_openai(self): | ||
completion=self.client.chat.completions.create( | ||
model="", | ||
messages=[ | ||
{"role": "system", "content": "You are a helpful assistant."}, | ||
{"role": "user", "content": "What should I do today?"}, | ||
], | ||
max_tokens=16, | ||
stop=["\n### user:"], | ||
stream=False | ||
) | ||
# length of message should more than 0 | ||
self.assertTrue(len(completion.choices)>0) | ||
print(completion.choices[0].message) | ||
|
||
def test_inference_by_request(self): | ||
data = { | ||
"model": "gpt-3.5-turbo", | ||
"messages": [ | ||
{"role": "system", "content": "You are a helpful assistant."}, | ||
{"role": "user", "content": "What should I do today?"}, | ||
], | ||
"max_tokens": 16, | ||
"stop":["\n### user:"], | ||
"stream": False | ||
} | ||
res=requests.post(url=self.base_url+"/v1/chat/completions", json=data,headers={"Content-Type": "application/json", "Authorization":"Bearer no-key"},) | ||
self.assertEqual(res.status_code, 200) | ||
|
||
|
||
def test_inference_by_request_stream(self): | ||
data = { | ||
"model": "gpt-3.5-turbo", | ||
"messages": [ | ||
{"role": "system", "content": "You are a helpful assistant."}, | ||
{"role": "user", "content": "What should I do today?"}, | ||
], | ||
"max_tokens": 16, | ||
"stop":["\n### user:"], | ||
"stream": True | ||
} | ||
res=requests.post(url=self.base_url+"/v1/chat/completions", json=data,headers={"Content-Type": "application/json", "Authorization":"Bearer no-key"},) | ||
self.assertEqual(res.status_code, 200) | ||
|
||
|
||
def test_api_key(self): | ||
res=requests.get(url=self.base_url+"/v1/token/api-key") | ||
self.assertEqual(res.status_code, 200) | ||
# api key should not be empty | ||
self.assertTrue(len(res.json().get("api_key"))>0) |