50 lines
1.2 KiB
Python
50 lines
1.2 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""
|
|
主运行模块
|
|
"""
|
|
|
|
# 列举导入模块
|
|
import asyncio
|
|
from uuid import uuid4
|
|
|
|
from utils.agent import BaseAgent
|
|
from utils.memory import Memory
|
|
|
|
|
|
# 主函数
|
|
async def main():
|
|
print("进入会话模式,输入 exit 结束会话")
|
|
|
|
# 实例智能体
|
|
agent = BaseAgent(instructions="请用一句话简洁回复。")
|
|
# 实例记忆体
|
|
memory = Memory()
|
|
|
|
# 生成会话唯一标识
|
|
session_id = uuid4().hex.lower()
|
|
|
|
dialogue_round = 1
|
|
while True:
|
|
user_prompt = input("用户:").strip().lower()
|
|
if user_prompt == "exit":
|
|
print("会话结束")
|
|
break
|
|
# 查询会话历史消息
|
|
message_history = memory.read(session_id=session_id)
|
|
result = await agent.run(
|
|
user_prompt=user_prompt, message_history=message_history
|
|
)
|
|
# 记录会话历史消息
|
|
memory.create(
|
|
session_id=session_id,
|
|
dialogue_round=dialogue_round,
|
|
dialogue_message=result.new_messages(),
|
|
)
|
|
|
|
print("智能体:", result.output)
|
|
dialogue_round += 1
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main=main())
|