This repository has been archived by the owner on Sep 19, 2024. It is now read-only.
forked from geekan/MetaGPT
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update zhipu api due to new model and api; repair extra invalid gener…
…ate output; update its unittest
- Loading branch information
Showing
17 changed files
with
157 additions
and
215 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,31 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# @Desc : async_sse_client to make keep the use of Event to access response | ||
# refs to `https://github.com/zhipuai/zhipuai-sdk-python/blob/main/zhipuai/utils/sse_client.py` | ||
# refs to `zhipuai/core/_sse_client.py` | ||
|
||
from zhipuai.utils.sse_client import _FIELD_SEPARATOR, Event, SSEClient | ||
import json | ||
from typing import Any, Iterator | ||
|
||
|
||
class AsyncSSEClient(SSEClient): | ||
async def _aread(self): | ||
data = b"" | ||
async for chunk in self._event_source: | ||
for line in chunk.splitlines(True): | ||
data += line | ||
if data.endswith((b"\r\r", b"\n\n", b"\r\n\r\n")): | ||
yield data | ||
data = b"" | ||
if data: | ||
yield data | ||
|
||
async def async_events(self): | ||
async for chunk in self._aread(): | ||
event = Event() | ||
# Split before decoding so splitlines() only uses \r and \n | ||
for line in chunk.splitlines(): | ||
# Decode the line. | ||
line = line.decode(self._char_enc) | ||
|
||
# Lines starting with a separator are comments and are to be | ||
# ignored. | ||
if not line.strip() or line.startswith(_FIELD_SEPARATOR): | ||
continue | ||
|
||
data = line.split(_FIELD_SEPARATOR, 1) | ||
field = data[0] | ||
|
||
# Ignore unknown fields. | ||
if field not in event.__dict__: | ||
self._logger.debug("Saw invalid field %s while parsing " "Server Side Event", field) | ||
continue | ||
|
||
if len(data) > 1: | ||
# From the spec: | ||
# "If value starts with a single U+0020 SPACE character, | ||
# remove it from value." | ||
if data[1].startswith(" "): | ||
value = data[1][1:] | ||
else: | ||
value = data[1] | ||
else: | ||
# If no value is present after the separator, | ||
# assume an empty value. | ||
value = "" | ||
class AsyncSSEClient(object): | ||
def __init__(self, event_source: Iterator[Any]): | ||
self._event_source = event_source | ||
|
||
# The data field may come over multiple lines and their values | ||
# are concatenated with each other. | ||
if field == "data": | ||
event.__dict__[field] += value + "\n" | ||
else: | ||
event.__dict__[field] = value | ||
|
||
# Events with no data are not dispatched. | ||
if not event.data: | ||
continue | ||
|
||
# If the data field ends with a newline, remove it. | ||
if event.data.endswith("\n"): | ||
event.data = event.data[0:-1] | ||
|
||
# Empty event names default to 'message' | ||
event.event = event.event or "message" | ||
|
||
# Dispatch the event | ||
self._logger.debug("Dispatching %s...", event) | ||
yield event | ||
async def stream(self) -> dict: | ||
if isinstance(self._event_source, bytes): | ||
raise RuntimeError( | ||
f"Request failed, msg: {self._event_source.decode('utf-8')}, please ref to `https://open.bigmodel.cn/dev/api#error-code-v3`" | ||
) | ||
async for chunk in self._event_source: | ||
line = chunk.decode("utf-8") | ||
if line.startswith(":") or not line: | ||
return | ||
|
||
field, _p, value = line.partition(":") | ||
if value.startswith(" "): | ||
value = value[1:] | ||
if field == "data": | ||
if value.startswith("[DONE]"): | ||
break | ||
data = json.loads(value) | ||
yield data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.