Skip to content

Commit

Permalink
Add demo for invoking API
Browse files Browse the repository at this point in the history
Signed-off-by: Aisuko <urakiny@gmail.com>
  • Loading branch information
Aisuko committed Aug 1, 2024
1 parent b92afdf commit f293ae0
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
.git
volumes
volumes
__pycache__
7 changes: 7 additions & 0 deletions .vscode/settings.json
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
}
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,11 @@ dev: env model-prepare
# stop
.PHONY: stop
stop:
docker compose stop
docker compose stop

#########################################################################################
# testing

.PHONY: pytest
pytest:
@python3 -m pytest -v
43 changes: 43 additions & 0 deletions example/README.md
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 ==================================================================================
```
2 changes: 2 additions & 0 deletions example/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
openai==1.35.7
pytest==8.1.1
91 changes: 91 additions & 0 deletions example/test_apis.py
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)

0 comments on commit f293ae0

Please sign in to comment.