|
|
|
|
from openai import OpenAI
|
|
|
|
|
from Config.Config import LLM_API_KEY, LLM_BASE_URL, LLM_MODEL_NAME
|
|
|
|
|
import asyncio
|
|
|
|
|
|
|
|
|
|
from Util.PostgreSQLUtil import init_postgres_pool
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class QuestionMatcher:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.client = OpenAI(api_key=LLM_API_KEY, base_url=LLM_BASE_URL)
|
|
|
|
|
self.model = LLM_MODEL_NAME
|
|
|
|
|
|
|
|
|
|
async def match_question_to_knowledge(self, question: str, knowledge_points: list):
|
|
|
|
|
# 构建提示词让LLM直接判断
|
|
|
|
|
prompt = f"""请从以下知识点中选择最匹配题目"{question}"的一个或多个知识点:
|
|
|
|
|
|
|
|
|
|
知识点列表:
|
|
|
|
|
"""
|
|
|
|
|
for kp in knowledge_points:
|
|
|
|
|
prompt += f"- ID:{kp[0]}, 名称:{kp[1]}\n"
|
|
|
|
|
|
|
|
|
|
prompt += "\n请直接返回最匹配知识点的ID和名称,格式为:ID|名称"
|
|
|
|
|
|
|
|
|
|
response = self.client.chat.completions.create(
|
|
|
|
|
model=self.model,
|
|
|
|
|
messages=[{"role": "user", "content": prompt}],
|
|
|
|
|
max_tokens=100
|
|
|
|
|
)
|
|
|
|
|
return response.choices[0].message.content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def main():
|
|
|
|
|
pg_pool = await init_postgres_pool()
|
|
|
|
|
matcher = QuestionMatcher()
|
|
|
|
|
|
|
|
|
|
async with pg_pool.acquire() as conn:
|
|
|
|
|
# 执行查询
|
|
|
|
|
knowledge_points = await conn.fetch('SELECT id, title,is_leaf FROM knowledge_points where is_leaf=1')
|
|
|
|
|
questions = [
|
|
|
|
|
"小明有3个苹果,又买了5个,现在有多少个苹果?",
|
|
|
|
|
"一个长方形的长是5cm,宽是3cm,面积是多少?",
|
|
|
|
|
"小明的学校在小明家的东南方向150米处,他每天中午都回家吃饭,请问小明在上学和放学的路上一天一共走了多少米?",
|
|
|
|
|
"兰兰家在学校的南面500米处,方方家在兰兰家北面200米处,请问学校在方方家什么方向的多少米处?",
|
|
|
|
|
"小强的家门面向东,放学回家后站在门前,面向家门,他的前后左右分别是什么方向?",
|
|
|
|
|
"小明和小立背对背站立,小明向北走150米,小立向南走120米,两人相距多远?",
|
|
|
|
|
"1500棵树苗平均分给5个班种植,每个班又将树苗平均分给5个小组,每个小组分得多少棵树苗?",
|
|
|
|
|
"粮店运来120吨大米,第一天卖出总数的一半,第二天卖出剩下的一半,粮店还剩大米多少吨?"
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
for question in questions:
|
|
|
|
|
match_result = await matcher.match_question_to_knowledge(question, knowledge_points)
|
|
|
|
|
print(f"题目: {question}")
|
|
|
|
|
print(f"匹配结果: {match_result}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
asyncio.run(main())
|