Skip to content

Commit 341d1cd

Browse files
authored
Merge pull request #26 from RexWzh/master
Add debug tool
2 parents 0692bcd + dfa7444 commit 341d1cd

9 files changed

+199
-6
lines changed

CONTRIBUTING.md

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# Contributing
2+
3+
Contributions are welcome, and they are greatly appreciated! Every little bit helps, and credit will always be given.
4+
5+
You can contribute in many ways:
6+
7+
## Types of Contributions
8+
9+
### Report Bugs
10+
11+
Report bugs at https://github.com/cubenlp/openai_api_call/issues.
12+
13+
If you are reporting a bug, please include:
14+
15+
- Your operating system name and version.
16+
- Any details about your local setup that might be helpful in troubleshooting.
17+
- Detailed steps to reproduce the bug.
18+
19+
### Fix Bugs
20+
21+
Look through the GitHub issues for bugs. Anything tagged with "bug" and "help wanted" is open to whoever wants to implement it.
22+
23+
### Implement Features
24+
25+
Look through the GitHub issues for features. Anything tagged with "enhancement" and "help wanted" is open to whoever wants to implement it.
26+
27+
### Write Documentation
28+
29+
Openai API call could always use more documentation, whether as part of the
30+
official Openai API call docs, in docstrings, or even on the web in blog posts,
31+
articles, and such.
32+
33+
### Submit Feedback
34+
35+
The best way to send feedback is to file an issue at https://github.com/cubenlp/openai_api_call/issues.
36+
37+
If you are proposing a feature:
38+
39+
- Explain in detail how it would work.
40+
- Keep the scope as narrow as possible, to make it easier to implement.
41+
- Remember that this is a volunteer-driven project, and that contributions are welcome :)
42+
43+
## Get Started!
44+
45+
Ready to contribute? Here's how to set up `openai_api_call` for local development.
46+
47+
1. Fork the `openai_api_call` repo on GitHub.
48+
2. Clone your fork locally:
49+
50+
```shell
51+
$ git clone [email protected]:your_name_here/openapi_api_call.git
52+
```
53+
54+
3. Install your local copy into a virtualenv. Assuming you have virtualenvwrapper installed, this is how you set up your fork for local development:
55+
56+
```shell
57+
$ mkvirtualenv openai_api_call
58+
$ cd openai_api_call/
59+
$ python setup.py develop
60+
```
61+
62+
4. Create a branch for local development:
63+
64+
```shell
65+
$ git checkout -b name-of-your-bugfix-or-feature
66+
```
67+
68+
Now you can make your changes locally.
69+
70+
5. When you're done making changes, check that your changes pass flake8 and the tests, including testing other Python versions with tox:
71+
72+
```shell
73+
$ flake8 openai_api_call tests
74+
$ python setup.py test or pytest
75+
$ tox
76+
```
77+
78+
To get flake8 and tox, just pip install them into your virtualenv.
79+
80+
6. Commit your changes and push your branch to GitHub:
81+
82+
```shell
83+
$ git add .
84+
$ git commit -m "Your detailed description of your changes."
85+
$ git push origin name-of-your-bugfix-or-feature
86+
```
87+
88+
7. Submit a pull request through the GitHub website.
89+
90+
## Pull Request Guidelines
91+
92+
Before you submit a pull request, check that it meets these guidelines:
93+
94+
1. The pull request should include tests.
95+
2. If the pull request adds functionality, the docs should be updated. Put your new functionality into a function with a docstring, and add the feature to the list in README.rst.
96+
3. The pull request should work for Python 3.8 and 3.9, and for PyPy. Check https://github.com/cubenlp/openai_api_call/actions and make sure that the tests pass for all supported Python versions.
97+
98+
## Tips
99+
100+
To run a subset of tests:
101+
102+
```shell
103+
$ pytest tests.test_openai_api_call
104+
```
105+
106+
## Deploying
107+
108+
A reminder for the maintainers on how to deploy. Make sure all your changes are committed (including an entry in HISTORY.rst). Then run:
109+
110+
```shell
111+
$ bump2version patch # possible: major / minor / patch
112+
$ git push
113+
$ git push --tags
114+
```
115+
116+
Travis will then deploy to PyPI if tests pass.
117+
118+
---
119+
120+
Thank you for reading and considering contributing to `openai_api_call`!

CONTRIBUTORS.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Contributors List
2+
3+
- Rex (Main Developer)
4+
- GitHub: [Rex's GitHub Profile](https://github.com/RexWzh)
5+
6+
- Zhouhao (Code Tester)
7+
- GitHub: [Zhouhao's GitHub Profile](https://github.com/zh-i9)
8+
9+
- Shangqing (Code Tester)
10+
- GitHub: [Shangqing's GitHub Profile](https://github.com/Qing25)
11+
12+
- Binxuan (Code Tester)
13+
- GitHub: [Binxuan's GitHub Profile](https://github.com/LuaMarke)
14+
15+
Thank you all for your contributions!
16+
17+
License: MIT

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,5 @@ This package is licensed under the MIT license. See the LICENSE file for more de
155155
## update log
156156

157157
- Since version `0.2.0`, `Chat` type is used to handle data
158-
- Since version `0.3.0`, you can use different API Key to send requests.
158+
- Since version `0.3.0`, you can use different API Key to send requests.
159+
- Since version `0.4.0`, this package is mantained by [cubenlp](https://github.com/cubenlp).

README_zh_CN.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,5 @@ chat.show_usage_status()
154154
- 版本 `0.2.0` 改用 `Chat` 类型作为中心交互对象
155155
- 版本 `0.3.0` 开始不依赖模块 `openai.py` ,而是直接使用 `requests` 发送请求
156156
- 支持对每个 `Chat` 使用不同 API 密钥
157-
- 支持使用代理链接
157+
- 支持使用代理链接
158+
- 版本 `0.4.0` 开始,工具维护转至 [CubeNLP](https://github.com/cubenlp) 组织账号

openai_api_call/__init__.py

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
__author__ = """Rex Wang"""
44
__email__ = '[email protected]'
5-
__version__ = '0.4.0'
5+
__version__ = '0.4.1'
66

77
import os
88
from .chattool import Chat, Resp, chat_completion, usage_status
99
from .proxy import proxy_on, proxy_off, proxy_status
10+
import requests
11+
from . import request
12+
1013

1114
# read API key from the environment variable
1215
if os.environ.get('OPENAI_API_KEY') is not None:
@@ -33,3 +36,50 @@ def default_prompt(msg:str):
3336
List[Dict]: default prompt message
3437
"""
3538
return [{"role": "user", "content": msg},]
39+
40+
def show_base_url():
41+
"""Show the base url of the API call"""
42+
print(f"Base url:\t{request.base_url}")
43+
44+
def debug_log( net_url:str="https://www.baidu.com"
45+
, timeout:int=5
46+
, test_usage:bool=True
47+
, test_response:bool=True):
48+
# 1. Test whether the network is available
49+
try:
50+
requests.get(net_url, timeout=timeout)
51+
except:
52+
print("Warning: Network is not available.")
53+
return False
54+
55+
print("Your network is available.")
56+
57+
# 2. Check the API key
58+
print("\nPlease verify the API key:")
59+
show_apikey()
60+
61+
# 3. Check the proxy status
62+
print("\nYour proxy status:")
63+
proxy_status()
64+
print("Note that, you don't need to set proxy if your `base_url` has done it!")
65+
66+
# 4. Base url
67+
print("\nCheck your base url:")
68+
show_base_url()
69+
if request.url is not None:
70+
print("Warning: the `url` parameter is deprecated, please use `base_url` instead.")
71+
72+
# 5. Get usage status
73+
if test_usage:
74+
print("\nThe usage status of your API key:")
75+
Chat().show_usage_status(recent=3)
76+
77+
# 6. Test hello world
78+
if test_response:
79+
print("\nTest hello world:")
80+
chat = Chat("hello world")
81+
chat.getresponse(max_requests=3)
82+
chat.print_log()
83+
84+
print("\nDebug is finished.")
85+
return True

openai_api_call/chattool.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def save(self, path:str, mode:str='a', end:str='\n'):
192192
assert mode in ['a', 'w'], "mode should be 'a' or 'w'"
193193
data = self.chat_log
194194
with open(path, mode, encoding='utf-8') as f:
195-
f.write(json.dumps(data) + end)
195+
f.write(json.dumps(data, ensure_ascii=False) + end)
196196
return True
197197

198198
def print_log(self, sep: Union[str, None]=None):

openai_api_call/request.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def chat_completion(api_key:str, messages:List[Dict], model:str, **options) -> D
8181

8282
chat_url = normalize_url(chat_url)
8383
# get response
84-
response = requests.post(chat_url, headers=headers, data=json.dumps(payload))
84+
response = requests.post(chat_url, headers=headers, data=json.dumps(payload, ensure_ascii=False))
8585
if response.status_code != 200:
8686
raise Exception(response.text)
8787
return response.json()

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
with open('README.md') as readme_file:
88
readme = readme_file.read()
99

10-
VERSION = '0.4.0'
10+
VERSION = '0.4.1'
1111

1212
requirements = ['Click>=7.0', 'requests>=2.20']
1313

tests/test_openai_api_call.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pytest
66
from click.testing import CliRunner
77
from openai_api_call import cli
8+
from openai_api_call import debug_log
89

910

1011
@pytest.fixture
@@ -32,3 +33,6 @@ def test_command_line_interface():
3233
help_result = runner.invoke(cli.main, ['--help'])
3334
assert help_result.exit_code == 0
3435
assert '--help Show this message and exit.' in help_result.output
36+
37+
def test_debug_log():
38+
assert debug_log(test_response=False, test_usage=False)

0 commit comments

Comments
 (0)