2024-07-07 01:58:23 +08:00
|
|
|
import os
|
|
|
|
|
from dotenv import load_dotenv
|
2024-07-09 05:04:20 +08:00
|
|
|
from core import HistoryManager
|
2024-07-09 16:38:05 +08:00
|
|
|
from core import PreRetrieval
|
2024-07-07 01:58:23 +08:00
|
|
|
from config import Config
|
2024-07-07 17:21:07 +08:00
|
|
|
from models import select_model
|
2024-07-07 01:58:23 +08:00
|
|
|
|
|
|
|
|
load_dotenv()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
config = Config("config/base.yaml")
|
2024-07-07 17:21:07 +08:00
|
|
|
model = select_model(config)
|
2024-07-09 16:38:05 +08:00
|
|
|
pre_retrieval = PreRetrieval(config)
|
|
|
|
|
# pre_retrieval.add_file("/home/zwj/workspace/ProjectAthena/src/data/file/鉴定工作报告、技术报告-0708.pdf")
|
2024-07-07 01:58:23 +08:00
|
|
|
|
2024-07-07 17:21:07 +08:00
|
|
|
print(f"[{config.model_provider}:{config.get('model_name', 'default')}] Type 'exit' to quit")
|
2024-07-07 01:58:23 +08:00
|
|
|
|
|
|
|
|
history_manager = HistoryManager()
|
|
|
|
|
while True:
|
|
|
|
|
message = input("\nUser: ")
|
|
|
|
|
if message == "exit":
|
|
|
|
|
break
|
|
|
|
|
|
2024-07-09 05:04:20 +08:00
|
|
|
external = ""
|
|
|
|
|
|
|
|
|
|
if config.enable_knowledge_base:
|
2024-07-09 16:38:05 +08:00
|
|
|
kb_res = pre_retrieval.search(message)
|
2024-07-09 05:04:20 +08:00
|
|
|
if kb_res:
|
|
|
|
|
kb_res = "\n".join([f"{r['id']}: {r['entity']['text']}" for r in kb_res[0]])
|
|
|
|
|
kb_res = f"知识库信息: {kb_res}"
|
|
|
|
|
external += kb_res
|
|
|
|
|
|
|
|
|
|
if len(external) > 0:
|
|
|
|
|
message = f"以下是参考资料:\n\n\n {external} 请根据前面的知识回答:{message}"
|
|
|
|
|
|
2024-07-07 01:58:23 +08:00
|
|
|
messages = history_manager.add_user(message)
|
|
|
|
|
response = model.predict(messages, stream=config.stream)
|
|
|
|
|
|
|
|
|
|
if config.stream:
|
|
|
|
|
content = ""
|
|
|
|
|
print(f"AI: ", end='', flush=True)
|
|
|
|
|
for chunk in response:
|
|
|
|
|
content += chunk.content
|
|
|
|
|
print(f"{chunk.content}", end='', flush=True)
|
|
|
|
|
print()
|
|
|
|
|
else:
|
|
|
|
|
content = response.content
|
|
|
|
|
print(f"AI: {content}")
|
|
|
|
|
|
|
|
|
|
history_manager.add_ai(content)
|