Skip to content

Commit

Permalink
Merge pull request #59 from cubenlp/rex
Browse files Browse the repository at this point in the history
catch error in async process
  • Loading branch information
RexWzh committed Nov 1, 2023
2 parents 6424933 + 0afb612 commit 3fb50d3
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 17 deletions.
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.3.2'
__version__ = '2.3.3'

import os, sys, requests
from .chattool import Chat, Resp
Expand Down
26 changes: 12 additions & 14 deletions chattool/asynctool.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ async def async_post( session
while max_requests > 0:
try:
async with session.post(url, headers=headers, data=data, timeout=timeout) as response:
return await response.text()
resp = await response.text()
resp = Resp(json.loads(resp))
assert resp.is_valid(), resp.error_message
return resp
except Exception as e:
max_requests -= 1
ntries += 1
Expand Down Expand Up @@ -86,19 +89,14 @@ async def chat_complete(ind, locker, chatlog, chkpoint, **options):
if max_tokens is not None:
payload['max_tokens'] = max_tokens(chatlog)
data = json.dumps(payload)
response = await async_post( session=session
, sem=sem
, url=chat_url
, data=data
, headers=headers
, max_requests=max_requests
, timeinterval=timeinterval
, timeout=timeout)
if response is None:return False
resp = Resp(json.loads(response))
if not resp.is_valid():
warnings.warn(f"Invalid response: {resp.error_message}")
return 0, 0
resp = await async_post( session=session
, sem=sem
, url=chat_url
, data=data
, headers=headers
, max_requests=max_requests
, timeinterval=timeinterval
, timeout=timeout)
## saving files
chatlog.append(resp.message)
chat = Chat(chatlog)
Expand Down
2 changes: 1 addition & 1 deletion chattool/chattool.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def getresponse( self
api_key=api_key, messages=msg, model=model,
chat_url=self.chat_url, timeout=timeout, **options)
resp = Resp(response)
assert resp.is_valid(), "Invalid response with message: " + resp.error_message
assert resp.is_valid(), resp.error_message
break
except Exception as e:
max_requests -= 1
Expand Down
2 changes: 1 addition & 1 deletion 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.3.2'
VERSION = '2.3.3'

requirements = [
'Click>=7.0', 'requests>=2.20', "responses>=0.23",
Expand Down

0 comments on commit 3fb50d3

Please sign in to comment.