Skip to content

Commit

Permalink
compat to the steam response
Browse files Browse the repository at this point in the history
  • Loading branch information
RexWzh committed Nov 18, 2023
1 parent 37d5255 commit 344b0b8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
Binary file added .DS_Store
Binary file not shown.
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: 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

0 comments on commit 344b0b8

Please sign in to comment.