-
Notifications
You must be signed in to change notification settings - Fork 64
将AI审核功能添加到地下室预约流程中 #944
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Morgen-Kornblume
wants to merge
15
commits into
Yuanpei-Intelligence:develop
Choose a base branch
from
Morgen-Kornblume:AI-Inspection
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
将AI审核功能添加到地下室预约流程中 #944
Changes from 13 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
99be42f
添加了基本的config文件示例
Morgen-Kornblume 88ed378
未完成的AI审核功能接口
Morgen-Kornblume eea51aa
将预约事项审核插入地下室预约的流程
Morgen-Kornblume 1808eeb
feat(ai-inspection): 接入HTTP审核API,返回合规/不合规并附带理由
KeZhao783 ac93892
Update AI_Inspection.py
KeZhao783 3c7870e
Update config.example.json
KeZhao783 1c0df71
加入了ollama审核方法
Morgen-Kornblume 53c5e19
Merge branch 'AI-Inspection' into AI-Inspection
Morgen-Kornblume 3e56067
Merge pull request #1 from KeZhao783/AI-Inspection
Morgen-Kornblume 4bf8cc7
整合了 ollama 方法和 GLM 方法
Morgen-Kornblume 897afec
优化了用户体验和提示措辞
Morgen-Kornblume 1e67741
取消对docker-compose.yml的修改
Morgen-Kornblume cafe442
Merge branch 'develop' into AI-Inspection
Deophius b3b6b65
修正了遇到的一系列问题
Morgen-Kornblume 51de5b3
Merge branch 'develop' into AI-Inspection
Morgen-Kornblume File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| from Appointment.config import appointment_config as CONFIG | ||
| import os | ||
| import json | ||
| import requests | ||
|
|
||
| # 模型人设:只返回“合规/不合规”,JSON格式 | ||
| SYSTEM_PROMPT = ( | ||
| "你是一个审查学生预约功能房间信息的老师,请审核以下预约信息,审核要求是预约事由和房间用途匹配,且不能包含政治敏感信息和色情内容。" | ||
| "你的输出必须严格为JSON,字段为:" | ||
| '{"decision": 0 或 1, "reason":"一句话理由"}' | ||
| "其中 0 代表不允许预约,1 代表允许预约" | ||
| ) | ||
|
|
||
|
|
||
| def _call_glm_decision(text: str, timeout: int = 30) -> tuple[str, str]: | ||
| """ | ||
| 调用智谱 HTTP 接口,返回 0 或 1,超时/网络错误报出异常。 | ||
| """ | ||
| ENDPOINT = "https://open.bigmodel.cn/api/paas/v4/chat/completions" | ||
| MODEL = "glm-4.5-flash" | ||
| api_key = CONFIG.GLM_API_KEY | ||
| if not api_key: | ||
| raise RuntimeError("缺少参数 API_KEY") | ||
|
|
||
| headers = { | ||
| "Authorization": f"Bearer {api_key}", | ||
| "Content-Type": "application/json", | ||
| } | ||
| payload = { | ||
| "model": MODEL, | ||
| "temperature": 0.2, # 可调 | ||
| "top_p": 0.7, # 可调 | ||
| "response_format": {"type": "json_object"}, | ||
| "messages": [ | ||
| {"role": "system", "content": SYSTEM_PROMPT}, | ||
| {"role": "user", "content": f"待审核文本:\n{text}"}, | ||
| ], | ||
| } | ||
|
|
||
| resp = requests.post(ENDPOINT, headers=headers, | ||
| json=payload, timeout=timeout) | ||
| if resp.status_code != 200: | ||
| # 把上游错误透出一小段,便于排查 | ||
| if resp.status_code == 400: | ||
| return False, "请勿在预约理由中包含敏感词汇!" | ||
| raise RuntimeError(f"Upstream {resp.status_code}: {resp.text[:300]}") | ||
|
|
||
| data = resp.json() | ||
| try: | ||
| content_str = data["choices"][0]["message"]["content"] | ||
| obj = json.loads(content_str) | ||
| except Exception as e: | ||
| raise RuntimeError(f"Bad JSON content from provider: {e}; raw={data}") | ||
|
|
||
| decision = obj.get("decision") | ||
| reason = obj.get("reason", "") | ||
| if decision not in (0, 1): | ||
| raise ValueError(f"unexpected decision: {decision};obj={obj}") | ||
| if not isinstance(reason, str): | ||
| reason = str(reason) | ||
| if decision == 1: | ||
| return True, reason | ||
| else: | ||
| return False, reason | ||
|
|
||
|
|
||
| def Ollama_Inspection(room_name, reason) -> tuple[bool, str]: | ||
| # Ollama 审核功能接口,将预约房间(名称)和事由发送至 API,然后接收判断结果(合格/不合格) | ||
| # 完整的处理办法(需要在API调用失败时抛出错误) | ||
| try: | ||
| response = requests.post( | ||
| CONFIG.Ollama_ADDR + "/api/generate", | ||
| json={ | ||
| "model": "gpt-oss:20b", | ||
| "prompt": f"你是一个审查学生预约功能房间信息的老师,请审核以下预约信息,审核要求是预约事由和房间用途匹配,且不能包含政治敏感信息和色情内容。房间名称:{room_name}。事由:{reason}。如果审核通过,只输出 1,不通过,则输出 0,然后接一个空格,在空格后输出不通过的原因,原因尽量简要;如无法判断,默认不通过", | ||
Morgen-Kornblume marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "stream": False, | ||
| "think": False | ||
| } | ||
| ) # GPT-OSS:20b 实测效果最好 | ||
| response.raise_for_status() # 如果响应状态码不是 200,将抛出异常 | ||
| result = response.json() | ||
| # 根据上述格式解析 response 部分,给出返回值 | ||
| output = result.get("response", "").strip() | ||
| if output.startswith("1"): | ||
| return True, "Approved" | ||
| elif output.startswith("0"): | ||
| reason = output[1:].strip() # 获取不通过的原因 | ||
| return False, "房间用途不合规:" + reason if reason else "Not approved" | ||
| else: | ||
| return False, "Unexpected response format" | ||
|
|
||
| except requests.RequestException as e: | ||
| # 处理请求异常 | ||
| return False, str(e) | ||
|
|
||
|
|
||
| def GLM_Inspection(room_name, reason) -> tuple[bool, str]: | ||
| # GLM 审核 | ||
| # 组装要审核的文本 | ||
| content = f"房间:{room_name}\n事由:{reason}" | ||
| # 超时时间可从配置读取,默认30秒 | ||
Morgen-Kornblume marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| timeout = 30 | ||
| try: | ||
| decision, why = _call_glm_decision(content, timeout=timeout) | ||
| passed = (decision == 1) | ||
| # 返回是否通过和理由 True通过,False不通过 | ||
| if passed: | ||
| return True, "合规" | ||
| else: | ||
| return False, f"房间用途不合规:{why or '无具体理由'}" | ||
| except Exception as e: | ||
| # API 调用失败:不通过并附带错误信息 | ||
Morgen-Kornblume marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return False, str(e) | ||
|
|
||
|
|
||
| def AI_Inspection(room_name, reason) -> tuple[bool, str]: | ||
| # AI 审核功能接口,将预约房间(名称)和事由发送至 API,然后接收判断结果(合格/不合格) | ||
| # Additional:最好能在不合格时附带理由 | ||
|
|
||
| if not CONFIG.AI_Inspection_Enabled: | ||
| # 默认通过 | ||
| return True, "AI Inspection is disabled" | ||
|
|
||
| # 在此处实现 API 的调用,并处理 API 调用失败的情况 | ||
|
|
||
| if CONFIG.AI_Inspection_Method == "Ollama": | ||
| return Ollama_Inspection(room_name, reason) | ||
|
|
||
| if CONFIG.AI_Inspection_Method == "GLM": | ||
| return GLM_Inspection(room_name, reason) | ||
|
|
||
| return False, "AI Inspection method is not recognized" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.