open source
This commit is contained in:
parent
70b6e17c69
commit
a93bfc1ec9
61 changed files with 4013 additions and 126 deletions
25
vocode/streaming/agent/utils.py
Normal file
25
vocode/streaming/agent/utils.py
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
from typing import Generator
|
||||
|
||||
SENTENCE_ENDINGS = [".", "!", "?"]
|
||||
|
||||
|
||||
def stream_llm_response(
|
||||
gen, get_text=lambda choice: choice.get("text"), sentence_endings=SENTENCE_ENDINGS
|
||||
) -> Generator:
|
||||
buffer = ""
|
||||
for response in gen:
|
||||
choices = response.get("choices", [])
|
||||
if len(choices) == 0:
|
||||
break
|
||||
choice = choices[0]
|
||||
if choice["finish_reason"]:
|
||||
break
|
||||
token = get_text(choice)
|
||||
if not token:
|
||||
continue
|
||||
buffer += token
|
||||
if any(token.endswith(ending) for ending in sentence_endings):
|
||||
yield buffer.strip()
|
||||
buffer = ""
|
||||
if buffer.strip():
|
||||
yield buffer
|
||||
Loading…
Add table
Add a link
Reference in a new issue