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 = ''' + + +