This commit is contained in:
Nexus 2023-11-10 23:27:28 +00:00
parent ff192c5b88
commit a6e92a44af
Signed by: nex
GPG key ID: 0FA334385D0B689F

View file

@ -90,24 +90,22 @@ except Exception as _pyttsx3_err:
async def ollama_stream_reader(response: httpx.Response) -> typing.AsyncGenerator[ async def ollama_stream_reader(response: httpx.Response) -> typing.AsyncGenerator[
dict[str, str | int | bool], None dict[str, str | int | bool], None
]: ]:
stream = response.aiter_bytes(1)
_buffer = b"" _buffer = b""
while not response.is_stream_consumed: async for char in response.aiter_bytes(1):
_buffer = b""
while not _buffer.endswith(b"}\n"): while not _buffer.endswith(b"}\n"):
async for char in stream: _buffer += char
_buffer += char print(
print( "Read {:,} bytes in chunk for a total of {:,} bytes in buffer.".format(
"Read {:,} bytes in chunk for a total of {:,} bytes in buffer.".format( len(char),
len(char), len(_buffer)
len(_buffer)
)
) )
)
_buffer = _buffer.rstrip() _buffer = _buffer.rstrip()
print("[ollama stream reader] Resolving %r" % (_buffer.decode())) print("[ollama stream reader] Resolving %r" % (_buffer.decode()))
chunk = json.loads(_buffer.decode("utf-8", "replace")) chunk = json.loads(_buffer.decode("utf-8", "replace"))
print("[ollama stream reader] %r -> %r" % (_buffer.decode(), chunk)) print("[ollama stream reader] %r -> %r" % (_buffer.decode(), chunk))
yield chunk yield chunk
_buffer = b""
def format_autocomplete(ctx: discord.AutocompleteContext): def format_autocomplete(ctx: discord.AutocompleteContext):