Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Create Llama
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mirrored_repos
MachineLearning
run-llama
Create Llama
Commits
563b51d7
Unverified
Commit
563b51d7
authored
9 months ago
by
Huu Le
Committed by
GitHub
9 months ago
Browse files
Options
Downloads
Patches
Plain Diff
Fix: Vercel streaming (python) does not stream data events instantly (#111)
parent
88c88bf1
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
.changeset/few-geckos-confess.md
+5
-0
5 additions, 0 deletions
.changeset/few-geckos-confess.md
templates/types/streaming/fastapi/app/api/routers/chat.py
+34
-27
34 additions, 27 deletions
templates/types/streaming/fastapi/app/api/routers/chat.py
with
39 additions
and
27 deletions
.changeset/few-geckos-confess.md
0 → 100644
+
5
−
0
View file @
563b51d7
---
"
create-llama"
:
patch
---
Fix Vercel streaming (python) to stream data events instantly
This diff is collapsed.
Click to expand it.
templates/types/streaming/fastapi/app/api/routers/chat.py
+
34
−
27
View file @
563b51d7
...
...
@@ -33,14 +33,6 @@ async def chat(
event_handler
=
EventCallbackHandler
()
chat_engine
.
callback_manager
.
handlers
.
append
(
event_handler
)
# type: ignore
try
:
response
=
await
chat_engine
.
astream_chat
(
last_message_content
,
messages
)
except
Exception
as
e
:
logger
.
exception
(
"
Error in chat engine
"
,
exc_info
=
True
)
raise
HTTPException
(
status_code
=
status
.
HTTP_500_INTERNAL_SERVER_ERROR
,
detail
=
f
"
Error in chat engine:
{
e
}
"
,
)
from
e
async
def
content_generator
():
# Yield the additional data
...
...
@@ -49,12 +41,26 @@ async def chat(
yield
VercelStreamResponse
.
convert_data
(
data_response
)
# Yield the text response
async
def
_text_generator
():
async
def
_chat_response_generator
():
response
=
await
chat_engine
.
astream_chat
(
last_message_content
,
messages
)
async
for
token
in
response
.
async_response_gen
():
yield
VercelStreamResponse
.
convert_text
(
token
)
# the text_generator is the leading stream, once it's finished, also finish the event stream
event_handler
.
is_done
=
True
# Yield the source nodes
yield
VercelStreamResponse
.
convert_data
(
{
"
type
"
:
"
sources
"
,
"
data
"
:
{
"
nodes
"
:
[
SourceNodes
.
from_source_node
(
node
).
dict
()
for
node
in
response
.
source_nodes
]
},
}
)
# Yield the events from the event handler
async
def
_event_generator
():
async
for
event
in
event_handler
.
async_event_gen
():
...
...
@@ -62,27 +68,28 @@ async def chat(
if
event_response
is
not
None
:
yield
VercelStreamResponse
.
convert_data
(
event_response
)
combine
=
stream
.
merge
(
_text_generator
(),
_event_generator
())
combine
=
stream
.
merge
(
_chat_response_generator
(),
_event_generator
())
is_stream_started
=
False
async
with
combine
.
stream
()
as
streamer
:
async
for
item
in
streamer
:
async
for
output
in
streamer
:
if
not
is_stream_started
:
is_stream_started
=
True
# Stream a blank message to start the stream
yield
VercelStreamResponse
.
convert_text
(
""
)
yield
output
if
await
request
.
is_disconnected
():
break
yield
item
# Yield the source nodes
yield
VercelStreamResponse
.
convert_data
(
{
"
type
"
:
"
sources
"
,
"
data
"
:
{
"
nodes
"
:
[
SourceNodes
.
from_source_node
(
node
).
dict
()
for
node
in
response
.
source_nodes
]
},
}
)
return
VercelStreamResponse
(
content
=
content_generator
())
try
:
return
VercelStreamResponse
(
content
=
content_generator
())
except
Exception
as
e
:
logger
.
exception
(
"
Error in chat engine
"
,
exc_info
=
True
)
raise
HTTPException
(
status_code
=
status
.
HTTP_500_INTERNAL_SERVER_ERROR
,
detail
=
f
"
Error in chat engine:
{
e
}
"
,
)
from
e
# non-streaming endpoint - delete if not needed
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment