main
HuangHai 2 weeks ago
parent 3943058d84
commit dfe681c58e

@ -1,25 +1,59 @@
from Util.MySQLUtil import init_mysql_pool
from openai import OpenAI
from Config.Config import LLM_API_KEY, LLM_BASE_URL, LLM_MODEL_NAME
import asyncio
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():
mysql_pool = await init_mysql_pool()
matcher = QuestionMatcher()
async with mysql_pool.acquire() as conn:
await conn.ping()
async with conn.cursor() as cur:
# 查询获取所有知识点
await cur.execute("""
SELECT id,
title,
parent_id,
is_leaf
FROM knowledge_points
ORDER BY parent_id, id
""")
rows = await cur.fetchall()
print(rows)
await cur.execute("SELECT id, title,is_leaf FROM knowledge_points where is_leaf=1")
knowledge_points = await cur.fetchall()
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__':
import asyncio
if __name__ == '__main__':
asyncio.run(main())

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save