----------------------
The error message you're seeing is due to the fact that you're trying to run asynchronous code in a context that doesn't support it. This is common when you're using asynchronous features of LlamaIndex, such as the
QueryFusionRetriever
with
use_async=True
, inside a synchronous context.
To fix this, you can use the
nest_asyncio
library, which allows for asynchronous tasks to be nested. You can apply it at the beginning of your script or notebook. Here's how you can do it:
import nest_asyncio
nest_asyncio.apply()
After applying
nest_asyncio
, your code should look like this:
import nest_asyncio
nest_asyncio.apply()
def get_chat_engine() -> "CondensePlusContextChatEngine":
Settings.llm = OpenAI(model="gpt-4o", temperature=0.1)
index = VectorStoreIndex.from_vector_store(vector_store=vector_store)
retriever = index.as_retriever(similarity_top_k=3)
retriever = QueryFusionRetriever(
[retriever],
similarity_top_k=4,
num_queries=4,
mode="reciprocal_rerank",
use_async=True,
verbose=True,
query_gen_prompt=BOT_QUERY_GEN_PROMPT
)
chat_engine = CondensePlusContextChatEngine.from_defaults(retriever=retriever, system_prompt=SUPPORT_BOT_SYSTEM_PROMPT, streaming=True)
return chat_engine
async def chat(request: ChatRequestBody):
try:
engine = get_chat_engine()
response_stream = engine.stream_chat(message, chat_history=history)
return StreamingResponse(
stream_generator(response_stream, request.history, request.timezone),
media_type="application/x-ndjson",
)
except Exception as e:
traceback.print_exc()
raise HTTPException(
status_code=500, detail=f"An error occurred while processing the request. {str(e)}"
) from e