mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-26 17:13:54 +08:00
优化chat中异步调用方式
This commit is contained in:
+4
-15
@@ -203,23 +203,11 @@ class ConnectionHandler:
|
|||||||
return not self.is_device_verified
|
return not self.is_device_verified
|
||||||
|
|
||||||
|
|
||||||
def async_run(self, coro):
|
|
||||||
loop = asyncio.new_event_loop()
|
|
||||||
asyncio.set_event_loop(loop)
|
|
||||||
try:
|
|
||||||
return loop.run_until_complete(coro)
|
|
||||||
finally:
|
|
||||||
loop.close()
|
|
||||||
|
|
||||||
def chat(self, query):
|
def chat(self, query):
|
||||||
if self.isNeedAuth():
|
if self.isNeedAuth():
|
||||||
self.llm_finish_task = True
|
self.llm_finish_task = True
|
||||||
loop = asyncio.new_event_loop()
|
future = asyncio.run_coroutine_threadsafe(self._check_and_broadcast_auth_code(), self.loop)
|
||||||
asyncio.set_event_loop(loop)
|
future.result()
|
||||||
try:
|
|
||||||
loop.run_until_complete(self._check_and_broadcast_auth_code())
|
|
||||||
finally:
|
|
||||||
loop.close()
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
self.dialogue.put(Message(role="user", content=query))
|
self.dialogue.put(Message(role="user", content=query))
|
||||||
@@ -228,7 +216,8 @@ class ConnectionHandler:
|
|||||||
try:
|
try:
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
# 使用带记忆的对话
|
# 使用带记忆的对话
|
||||||
memory_str = self.async_run(self.memory.query_memory(query))
|
future = asyncio.run_coroutine_threadsafe(self.memory.query_memory(query), self.loop)
|
||||||
|
memory_str = future.result()
|
||||||
|
|
||||||
self.logger.bind(tag=TAG).info(f"记忆内容: {memory_str}")
|
self.logger.bind(tag=TAG).info(f"记忆内容: {memory_str}")
|
||||||
llm_responses = self.llm.response(
|
llm_responses = self.llm.response(
|
||||||
|
|||||||
Reference in New Issue
Block a user