Add support for chunked streaming responses
Allows the inference engine to correctly yield a single token, even when the streaming response from the backend contains multiple tokens concatenated together.
This commit is contained in:
parent
db0ac922f0
commit
f13fcef351
1 changed files with 8 additions and 6 deletions
|
|
@ -273,12 +273,14 @@ def generate_tokens_from_api(prompt: str, voice: str = DEFAULT_VOICE, temperatur
|
||||||
try:
|
try:
|
||||||
data = json.loads(data_str)
|
data = json.loads(data_str)
|
||||||
if 'choices' in data and len(data['choices']) > 0:
|
if 'choices' in data and len(data['choices']) > 0:
|
||||||
token_text = data['choices'][0].get('text', '')
|
token_chunk = data['choices'][0].get('text', '')
|
||||||
token_counter += 1
|
for token_text in token_chunk.split('>'):
|
||||||
perf_monitor.add_tokens()
|
token_text = f'{token_text}>'
|
||||||
|
token_counter += 1
|
||||||
if token_text:
|
perf_monitor.add_tokens()
|
||||||
yield token_text
|
|
||||||
|
if token_text:
|
||||||
|
yield token_text
|
||||||
except json.JSONDecodeError as e:
|
except json.JSONDecodeError as e:
|
||||||
print(f"Error decoding JSON: {e}")
|
print(f"Error decoding JSON: {e}")
|
||||||
continue
|
continue
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue