Skip to content

Commit

Permalink
Merge pull request #65 from cubenlp/rex/async_resp
Browse files Browse the repository at this point in the history
compat to the steam response
  • Loading branch information
RexWzh committed Nov 18, 2023
2 parents 37d5255 + 18d7500 commit 8efe428
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
python-version: ["3.8", "3.9"]
python-version: ["3.9"]
os: [ubuntu-latest]
include:
- python-version: "3.8"
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,5 @@ ENV/
.idea/

requests
responses
responses
.DS_Store
6 changes: 3 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ You can contribute in many ways:

### Report Bugs

Report bugs at https://github.com/cubenlp/chatapi_toolkit/issues.
Report bugs at https://github.com/cubenlp/chattool/issues.

If you are reporting a bug, please include:

Expand All @@ -32,7 +32,7 @@ articles, and such.

### Submit Feedback

The best way to send feedback is to file an issue at https://github.com/cubenlp/chatapi_toolkit/issues.
The best way to send feedback is to file an issue at https://github.com/cubenlp/chattool/issues.

If you are proposing a feature:

Expand Down Expand Up @@ -93,7 +93,7 @@ Before you submit a pull request, check that it meets these guidelines:

1. The pull request should include tests.
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.
3. The pull request should work for Python 3.8 and 3.9, and for PyPy. Check https://github.com/cubenlp/chatapi_toolkit/actions and make sure that the tests pass for all supported Python versions.
3. The pull request should work for Python 3.8 and 3.9, and for PyPy. Check https://github.com/cubenlp/chattool/actions and make sure that the tests pass for all supported Python versions.

## Tips

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
# ChatAPI Toolkit
[![PyPI version](https://img.shields.io/pypi/v/chattool.svg)](https://pypi.python.org/pypi/chattool)
[![Tests](https://github.com/cubenlp/chatapi_toolkit/actions/workflows/test.yml/badge.svg)](https://github.com/cubenlp/chatapi_toolkit/actions/workflows/test.yml/)
[![Tests](https://github.com/cubenlp/chattool/actions/workflows/test.yml/badge.svg)](https://github.com/cubenlp/chattool/actions/workflows/test.yml/)
[![Documentation Status](https://img.shields.io/badge/docs-github_pages-blue.svg)](https://chattool.cubenlp.com)
[![Coverage](https://codecov.io/gh/cubenlp/chatapi_toolkit/branch/master/graph/badge.svg)](https://codecov.io/gh/cubenlp/chatapi_toolkit)
[![Coverage](https://codecov.io/gh/cubenlp/chattool/branch/master/graph/badge.svg)](https://codecov.io/gh/cubenlp/chattool)

<!--
[![Updates](https://pyup.io/repos/github/cubenlp/chattool/shield.svg)](https://pyup.io/repos/github/cubenlp/chattool/)
Expand Down
4 changes: 2 additions & 2 deletions README_zh_CN.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# ChatAPI Toolkit
[![PyPI version](https://img.shields.io/pypi/v/chattool.svg)](https://pypi.python.org/pypi/chattool)
[![Tests](https://github.com/cubenlp/chatapi_toolkit/actions/workflows/test.yml/badge.svg)](https://github.com/cubenlp/chatapi_toolkit/actions/workflows/test.yml/)
[![Tests](https://github.com/cubenlp/chattool/actions/workflows/test.yml/badge.svg)](https://github.com/cubenlp/chattool/actions/workflows/test.yml/)
[![Documentation Status](https://img.shields.io/badge/docs-github_pages-blue.svg)](https://apicall.wzhecnu.cn)
[![Coverage](https://codecov.io/gh/cubenlp/chatapi_toolkit/branch/master/graph/badge.svg)](https://codecov.io/gh/cubenlp/chatapi_toolkit)
[![Coverage](https://codecov.io/gh/cubenlp/chattool/branch/master/graph/badge.svg)](https://codecov.io/gh/cubenlp/chattool)

<!--
[![Updates](https://pyup.io/repos/github/cubenlp/chattool/shield.svg)](https://pyup.io/repos/github/cubenlp/chattool/)
Expand Down
2 changes: 1 addition & 1 deletion chattool/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

__author__ = """Rex Wang"""
__email__ = '1073853456@qq.com'
__version__ = '2.4.2'
__version__ = '2.5.0'

import os, sys, requests
from .chattool import Chat, Resp
Expand Down
9 changes: 8 additions & 1 deletion chattool/chattool.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ async def async_stream_responses(self, timeout:int=0, textonly:bool=False):
Args:
timeout (int, optional): timeout for the API call. Defaults to 0(no timeout).
textonly (bool, optional): whether to only return the text. Defaults to True.
textonly (bool, optional): whether to only return the text. Defaults to False.
Returns:
str: response text
Expand All @@ -242,11 +242,18 @@ async def async_stream_responses(self, timeout:int=0, textonly:bool=False):
while True:
line = await response.content.readline()
if not line: break
# strip the prefix of `data: {...}`
strline = line.decode().lstrip('data:').strip()
# skip empty line
if not strline: continue
# read the json string
line = json.loads(strline)
# wrap the response
resp = Resp(line)
# stop if the response is finished
if resp.finish_reason == 'stop': break
# deal with the message
if 'content' not in resp.delta: continue
if textonly:
yield resp.delta_content
else:
Expand Down
7 changes: 5 additions & 2 deletions chattool/checkpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def load_chats( checkpoint:str
Args:
checkpoint (str): path to the checkpoint file
withid (bool, optional): whether the checkpoint file contains chatid. Defaults to False.
withid (bool, optional): Deprecated. It is not needed anymore. Defaults to False.
Returns:
list: chats
Expand All @@ -27,14 +27,17 @@ def load_chats( checkpoint:str
# get the chatlogs
logs = [json.loads(txt) for txt in txts]
## chatlogs with chatid
if withid:
if 'chatid' in logs[0]:
chat_size, chatlogs = 1, [None]
for log in logs:
idx = log['chatid']
if idx >= chat_size: # extend chatlogs
chatlogs.extend([None] * (idx - chat_size + 1))
chat_size = idx + 1
chatlogs[idx] = log['chatlog']
# check if there are missing chatlogs
if None in chatlogs:
warnings.warn(f"checkpoint file {checkpoint} has unfinished chats")
else: ## logs without chatid
chatlogs = logs
# return Chat class
Expand Down
7 changes: 6 additions & 1 deletion chattool/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,15 @@ def function_call(self):
"""Function call"""
return self.message['function_call']

@property
def delta(self):
"""Delta"""
return self.response['choices'][0]['delta']

@property
def delta_content(self):
"""Content of stream response"""
return self.response['choices'][0]['delta']['content']
return self.delta['content']

@property
def object(self):
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
with open('README.md') as readme_file:
readme = readme_file.read()

VERSION = '2.4.2'
VERSION = '2.5.0'

requirements = [
'Click>=7.0', 'requests>=2.20', "responses>=0.23",
Expand Down Expand Up @@ -43,7 +43,7 @@
packages=find_packages(include=['chattool', 'chattool.*']),
test_suite='tests',
tests_require=test_requirements,
url='https://github.com/cubenlp/chatapi_toolkit',
url='https://github.com/cubenlp/chattool',
version=VERSION,
zip_safe=False,
)
15 changes: 8 additions & 7 deletions tests/test_finetune.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ def test_finetune():
# get job list
jobs = ft.list_jobs()
jobs = ft.list_jobs(limit=1)
jobid = jobs[0]['id']
# TODO: create job/cancel job/delete job
# retrieve job
ft.retrieve_job(jobid)
# list events
ft.list_events(jobid)
ft.list_events(jobid, limit=20)
if len(jobs):
jobid = jobs[0]['id']
# TODO: create job/cancel job/delete job
# retrieve job
ft.retrieve_job(jobid)
# list events
ft.list_events(jobid)
ft.list_events(jobid, limit=20)
# default repl
print(ft)
# get model list
Expand Down

0 comments on commit 8efe428

Please sign in to comment.