Skip to content

Commit

Permalink
add AI functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenxu2022 committed Nov 18, 2024
1 parent cf1556a commit fb7e52b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
57 changes: 55 additions & 2 deletions WeiBanHelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import encrypted

from openai import OpenAI
import configparser

class WeibanHelper:
tenantCode = 0
Expand Down Expand Up @@ -388,6 +390,46 @@ def get_verify_code():
now = time.time()
content = retry_request_2("GET", get_verify_code_url + str(now), headers=self.headers).content
return self.ocr.classification(content), now

def ai_response(input, type):
client = OpenAI(base_url = config['AI']['API_ENDPOINT'],api_key = config['AI']['API_KEY'])

if type == 1:
completion = client.chat.completions.create(
model = config['AI']['MODEL'],
messages=[
{
"role": "system",
"content": "本题为单选题,请根据题目和选项回答问题,以json格式输出正确的选项对应的id(即正确选项'id'键对应的值)和内容(即正确选项'content'键对应的值),除此之外不要输出任何多余的内容。"
},
{
"role": "user",
"content": input
}
]
)
if type == 2:
completion = client.chat.completions.create(
model = config['AI']['MODEL'],
messages=[
{
"role": "system",
"content": "本题为多选题,请根据题目和选项回答问题,以json格式输出正确的选项对应的id(即正确选项'id'键对应的值)和内容(即正确选项'content'键对应的值),并使用逗号连接多个值,除此之外不要输出任何多余的内容。"
},
{
"role": "user",
"content": input
}
]
)

response = completion.choices[0].message.content
data = json.loads(response)

id_value = data['id']
content_value = data['content']

return id_value, content_value

# 获取当前系统时间
current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
Expand Down Expand Up @@ -464,9 +506,12 @@ def get_verify_code():
# 提取题目列表
question_list = paper_data['questionList']
match_count = 0
answerIds = None

for question in question_list:
question_title = question['title']
question_type = question['type'] # 1是单选,2是多选
question_type_name = question['typeLabel']
option_list = question['optionList']
submit_answer_id_list = []

Expand All @@ -475,6 +520,8 @@ def get_verify_code():

print(f"题目: {question_title}")

config = configparser.ConfigParser()
config.read('ai.conf')
# 检查题目标题是否匹配
if answer_list:
found_match = False
Expand All @@ -490,12 +537,18 @@ def get_verify_code():
print("<===答案匹配成功===>\n")
else:
print("<——————————!!!题目匹配但选项未找到匹配项!!!——————————>\n")
elif not config['AI'].get('API_ENDPOINT') or not config['AI'].get('API_KEY') or not config['AI'].get('MODEL'):
print("<——————————!!!未匹配到答案,可配置ai.conf文件通过大模型答题!!!——————————>\n")
else:
print("<——————————!!!未匹配到答案,题库暂未收录此题!!!——————————>\n")
print("<——————————未匹配到答案,将使用AI获取答案——————————>\n")
problemInput = f"{question_title}\n{option_list}"
answerIds, content = ai_response(problemInput, question_type)
print(f"{question_type_name},AI获取的答案: {content}")
match_count += 1

# 记录答案
record_data = {
"answerIds": ",".join(submit_answer_id_list),
"answerIds": answerIds if answerIds is not None else ",".join(submit_answer_id_list),
"questionId": question['id'],
"tenantCode": self.tenantCode,
"userId": self.userId,
Expand Down
4 changes: 4 additions & 0 deletions ai.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[AI]
API_ENDPOINT=
API_KEY=
MODEL=
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ pycryptodomex
fastapi
uvicorn
ddddocr
openai

0 comments on commit fb7e52b

Please sign in to comment.