|
| 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