From 033b7509d7f864b3dc2ecdf3cad19425ce9a3c36 Mon Sep 17 00:00:00 2001 From: hubian <908234780@qq.com> Date: Mon, 13 Apr 2026 16:56:43 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20AI=E8=87=AA=E5=8A=A8=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=EF=BC=8C=E6=99=BA=E8=83=BD=E8=AF=86=E5=88=AB?= =?UTF-8?q?=E6=96=87=E6=9C=AC=E7=B1=BB=E5=9E=8B=E5=B9=B6=E6=95=B4=E7=90=86?= =?UTF-8?q?=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- xian_favor/api.py | 206 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 206 insertions(+) diff --git a/xian_favor/api.py b/xian_favor/api.py index 536e3a7..3395156 100644 --- a/xian_favor/api.py +++ b/xian_favor/api.py @@ -164,6 +164,82 @@ def get_stats(): return jsonify({'success': True, 'data': stats}) +@app.route('/api/ai-process', methods=['POST']) +def ai_process(): + """AI处理文本""" + import requests + + data = request.get_json() + text = data.get('text', '').strip() + + if not text: + return jsonify({'success': False, 'error': '请输入文本内容'}), 400 + + # 大模型配置 + llm_url = "http://192.168.2.17:19007/v1/chat/completions" + llm_key = "xxxx" + + prompt = f"""请分析以下文本内容,识别其类型并提取关键信息。 + +文本内容: +{text} + +请按以下JSON格式返回结果(只返回JSON,不要其他内容): +{ + "type": "text/link/column/todo", + "title": "提取的标题(简短概括)", + "content": "主要内容(如果是文本类型)", + "url": "如果是链接或专栏,提取URL", + "source": "如果是专栏,提取来源", + "tags": ["相关标签1", "标签2"], + "note": "补充说明或备注", + "status": "如果是待办,默认pending", + "priority": "如果是待办,默认medium" +} + +类型判断规则: +- link: 包含http/https链接,且不是专栏订阅地址 +- column: 专栏订阅地址或RSS链接 +- todo: 包含任务、待办、提醒等关键词 +- text: 其他文本内容""" + + try: + response = requests.post( + llm_url, + headers={ + "Content-Type": "application/json", + "Authorization": f"Bearer {llm_key}" + }, + json={ + "model": "auto", + "messages": [{"role": "user", "content": prompt}], + "temperature": 0.3 + }, + timeout=30 + ) + + if response.status_code != 200: + return jsonify({'success': False, 'error': f'模型调用失败: {response.status_code}'}), 500 + + result = response.json() + content = result['choices'][0]['message']['content'] + + # 解析JSON + import json + import re + + # 提取JSON部分 + json_match = re.search(r'\{.*\}', content, re.DOTALL) + if json_match: + parsed = json.loads(json_match.group()) + return jsonify({'success': True, 'data': parsed}) + else: + return jsonify({'success': False, 'error': '无法解析模型返回'}), 500 + + except Exception as e: + return jsonify({'success': False, 'error': str(e)}), 500 + + @app.route('/api/search', methods=['GET']) def search_items(): """搜索条目""" @@ -263,6 +339,9 @@ INDEX_TEMPLATE = ''' + @@ -533,6 +612,44 @@ INDEX_TEMPLATE = ''' + + +