Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion shared/epistula.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ async def make_openai_query(
choices = []
chunks = []
chunk_timings = []
last_finish_reason = None # Only track the finish reason of the last chunk

async for chunk in chat:
if not chunk.choices:
continue
Expand All @@ -248,11 +250,19 @@ async def make_openai_query(
choices.append("")
if choice.delta.content:
choices[i] += choice.delta.content
# Save finish reason from the last chunk, safely handling the attribute
if hasattr(choice, "finish_reason") and choice.finish_reason is not None:
last_finish_reason = choice.finish_reason
if chunk.choices[0].delta.content:
chunks.append(chunk.choices[0].delta.content)
chunk_timings.append(time.perf_counter() - start_time)

choices = [
Choice(index=i, message=ChatCompletionMessage(content=choice, role="assistant"), finish_reason="stop")
Choice(
index=i,
message=ChatCompletionMessage(content=choice, role="assistant"),
finish_reason=last_finish_reason or "stop", # Use the captured finish_reason or fallback to "stop"
)
for i, choice in enumerate(choices)
]
# TODO: We need to find a better way to do this instead of sometimes returning a tuple and sometimes not, but for now this has to do
Expand Down