Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Llama Index
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
Llama Index
Commits
a9ff814f
Unverified
Commit
a9ff814f
authored
1 year ago
by
Sherif Abdekarim
Committed by
GitHub
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
Create lazy initialization for async elements in StreamingAgentChatResponse (#12116)
parent
a94a68d2
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
llama-index-core/llama_index/core/chat_engine/types.py
+13
-3
13 additions, 3 deletions
llama-index-core/llama_index/core/chat_engine/types.py
with
13 additions
and
3 deletions
llama-index-core/llama_index/core/chat_engine/types.py
+
13
−
3
View file @
a9ff814f
...
...
@@ -72,17 +72,17 @@ class StreamingAgentChatResponse:
source_nodes
:
List
[
NodeWithScore
]
=
field
(
default_factory
=
list
)
_unformatted_response
:
str
=
""
_queue
:
queue
.
Queue
=
field
(
default_factory
=
queue
.
Queue
)
_aqueue
:
asyncio
.
Queue
=
field
(
default_factory
=
asyncio
.
Queue
)
_aqueue
:
Optional
[
asyncio
.
Queue
]
=
None
# flag when chat message is a function call
_is_function
:
Optional
[
bool
]
=
None
# flag when processing done
_is_done
=
False
# signal when a new item is added to the queue
_new_item_event
:
asyncio
.
Event
=
field
(
default_factory
=
asyncio
.
Event
)
_new_item_event
:
Optional
[
asyncio
.
Event
]
=
None
# NOTE: async code uses two events rather than one since it yields
# control when waiting for queue item
# signal when the OpenAI functions stop executing
_is_function_false_event
:
asyncio
.
Event
=
field
(
default_factory
=
asyncio
.
Event
)
_is_function_false_event
:
Optional
[
asyncio
.
Event
]
=
None
# signal when an OpenAI function is being executed
_is_function_not_none_thread_event
:
Event
=
field
(
default_factory
=
Event
)
...
...
@@ -100,11 +100,20 @@ class StreamingAgentChatResponse:
self
.
response
=
self
.
_unformatted_response
.
strip
()
return
self
.
response
def
_ensure_async_setup
(
self
)
->
None
:
if
self
.
_aqueue
is
None
:
self
.
_aqueue
=
asyncio
.
Queue
()
if
self
.
_new_item_event
is
None
:
self
.
_new_item_event
=
asyncio
.
Event
()
if
self
.
_is_function_false_event
is
None
:
self
.
_is_function_false_event
=
asyncio
.
Event
()
def
put_in_queue
(
self
,
delta
:
Optional
[
str
])
->
None
:
self
.
_queue
.
put_nowait
(
delta
)
self
.
_is_function_not_none_thread_event
.
set
()
def
aput_in_queue
(
self
,
delta
:
Optional
[
str
])
->
None
:
self
.
_ensure_async_setup
()
self
.
_aqueue
.
put_nowait
(
delta
)
self
.
_new_item_event
.
set
()
...
...
@@ -207,6 +216,7 @@ class StreamingAgentChatResponse:
self
.
response
=
self
.
_unformatted_response
.
strip
()
async
def
async_response_gen
(
self
)
->
AsyncGenerator
[
str
,
None
]:
self
.
_ensure_async_setup
()
while
True
:
if
not
self
.
_aqueue
.
empty
()
or
not
self
.
_is_done
:
try
:
...
...
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