Find answers from the community

Updated last year

if I want to have more users to chat simultaneously with llamaindex, do I have to create m

At a glance

The community member asked if they need to create multiple instances of chat_engine to handle simultaneous chats with LlamaIndex. The response from another community member explains that while creating multiple instances is an option, LlamaIndex also supports concurrent chats, which means multiple users can chat simultaneously with a single instance of chat_engine. The response includes an example of how to handle concurrent chats using ThreadPoolExecutor and asyncio. The community member is also directed to the TestConcurrency notebook in the LlamaIndex GitHub repository for more details.

@kapa.ai if I want to have more users to chat simultaneously with llamaindex, do I have to create multiple instances
chat_engine = index.as_chat_engine(...) one for each user?
k
2 comments
Please stand by @Elioerr as I search the knowledge base. I normally take about 20 seconds or so. :wave:
----------------------
Yes, you can create multiple instances of chat_engine for each user to handle simultaneous chats. However, it's important to note that LlamaIndex also supports concurrent chats. This means that you can have multiple users chatting simultaneously with a single instance of chat_engine.

Here's an example of how you can handle concurrent chats:

Plain Text
from concurrent.futures import ThreadPoolExecutor
import asyncio

def run_in_event_loop(func: Callable):
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    loop.run_until_complete(func())
    loop.close()

async def run_task_n_times(task: Callable, n: int):
    def thread_task():
        run_in_event_loop(task)

    # Run two calls to run_chat concurrently and wait for them to finish
    with ThreadPoolExecutor(max_workers=5) as executor:
        futures = [executor.submit(thread_task) for _ in range(n)]
        for future in futures:
            future.result()

await run_task_n_times(run_chat, 5)

In this example, run_chat is a function that handles a single chat interaction. run_task_n_times is used to run run_chat concurrently for n times. This allows multiple users to chat simultaneously with the same chat_engine.

You can find more details about this in the TestConcurrency notebook () from the LlamaIndex GitHub repository.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Add a reply
Sign up and join the conversation on Discord