Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use JSONL for streaming rather then SSE #356

Closed
pmeier opened this issue Mar 12, 2024 · 2 comments · Fixed by #357
Closed

Use JSONL for streaming rather then SSE #356

pmeier opened this issue Mar 12, 2024 · 2 comments · Fixed by #357
Labels
type: RFD ⚖️ Decision making
Milestone

Comments

@pmeier
Copy link
Member

pmeier commented Mar 12, 2024

Looking at the builtin assistants, there are currently three ways of streaming:

  1. Using SSE
  2. Using streaming responses
    1. with one object per line (JSONL):
    2. with streaming JSON:

Out of those, I think 2.ii. is by far the worst as it is the hardest to parse. When we implemented streaming for Ragna, I went with 1., since that is what I was familiar with and seen other providers to as well. At that point, I was not aware of streaming JSONL (2.i.) as an alternative.

2.i. has the upside, that one does not need an extra library to handle as it is "required" for the other two cases (of course we could write the logic ourselves as well).

Due to its simplicity I would actually prefer JSONL streaming. We should make a decision here before we release streaming with 0.2.0.

@pmeier pmeier added the type: RFD ⚖️ Decision making label Mar 12, 2024
@pmeier
Copy link
Member Author

pmeier commented Mar 12, 2024

I've added a PR for this in #357. We eliminate the dependency on sse-starlette, httpx_sse becomes an optional dependency for Anthropic and OpenAI assistants, and we simplify the streaming code on the user side from

import httpx_sse
import json
chunks = []
with httpx_sse.connect_sse(
client,
"POST",
f"/chats/{chat['id']}/answer",
json={"prompt": "What is Ragna?", "stream": True},
) as event_source:
for sse in event_source.iter_sse():
chunks.append(json.loads(sse.data))

to

import json

with client.stream(
    "POST",
    f"/chats/{chat['id']}/answer",
    json={"prompt": "What is Ragna?", "stream": True},
) as response:
    chunks = [json.loads(data) for data in response.iter_lines()]

I currently don't see a downside.

@pmeier pmeier added this to the 0.2.0 milestone Mar 13, 2024
@aktech
Copy link
Member

aktech commented Mar 14, 2024

That's very nicely explained issue, thanks for doing that!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: RFD ⚖️ Decision making
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants