You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
2.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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())