Skip to content

Commit ff8917e

Browse files
authored
[Fix] Response Streaming Not Working as Expected
2 parents 8f7c99a + b75e11d commit ff8917e

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

docs/guides/function_calling.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def get_current_temperature(location: str, unit: str) -> dict:
111111
name2function = {"get_current_temperature": get_current_temperature}
112112
func = name2function[function_call["name"]]
113113
args = json.loads(function_call["arguments"])
114-
res = get_current_temperature(location=args["location"], unit=args["unit"])
114+
res = func(location=args["location"], unit=args["unit"])
115115
```
116116

117117
将模型上一轮的响应以及函数的响应加入到对话上下文信息中,再次传递给模型。如果函数的响应不是JSON格式的字符串,需要先对其进行编码。

erniebot/client.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,9 @@ def request(
147147
if stream != got_stream:
148148
logger.error("Unexpected response: %s", resp)
149149
raise RuntimeError(
150-
"Streaming response is expected, but got non-streaming response. This may indicate a bug in erniebot."
151-
)
150+
f"A {'streamed' if stream else 'non-streamed'} response was expected, "
151+
f"but got a {'streamed' if got_stream else 'non-streamed'} response. "
152+
"This may indicate a bug in erniebot.")
152153
return resp
153154

154155
@overload
@@ -226,8 +227,9 @@ async def arequest(
226227
if stream != got_stream:
227228
logger.error("Unexpected response: %s", resp)
228229
raise RuntimeError(
229-
"Streaming response is expected, but got non-streaming response. This may indicate a bug in erniebot."
230-
)
230+
f"A {'streamed' if stream else 'non-streamed'} response was expected, "
231+
f"but got a {'streamed' if got_stream else 'non-streamed'} response. "
232+
"This may indicate a bug in erniebot.")
231233
except Exception as e:
232234
await ctx.__aexit__(None, None, None)
233235
raise e
@@ -300,7 +302,6 @@ def request_raw(
300302
raise errors.ConnectionError(
301303
f"Error communicating with server: {e}") from e
302304

303-
logger.debug("API response body: %r", result.content)
304305
logger.debug("API response headers: %r", result.headers)
305306

306307
return result
@@ -340,7 +341,6 @@ async def arequest_raw(
340341
raise errors.ConnectionError(
341342
f"Error communicating with server: {e}") from e
342343

343-
logger.debug("API response body: %r", result.content)
344344
logger.debug("API response headers: %r", result.headers)
345345

346346
return result
@@ -406,10 +406,11 @@ def _validate_headers(
406406
def _parse_line(self, line: bytes) -> Optional[str]:
407407
if line:
408408
if line.startswith(b"data: "):
409+
# Data-only messages
409410
line = line[len(b"data: "):]
410411
return line.decode('utf-8')
411412
else:
412-
# Filter out empty lines
413+
# Filter out other lines
413414
return None
414415
return None
415416

0 commit comments

Comments
 (0)