Slide/backend/text_analyzer.py
2024-12-09 22:26:29 +08:00

67 lines
2.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import json
import logging
from openai import OpenAI
def analyze_text(text):
logging.info("开始分析文本:" + text)
client = OpenAI(
base_url="https://api.wlai.vip/v1",
api_key='sk-oDyv8xZqchRiUcjCODa5AT4f16H2rIjMidY3Ua0sZNf5u8al',
timeout=120
)
try:
logging.info("发送请求到OpenAI API")
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "你是一个PPT生成助手。你的任务是分析给定的文本并提取出适合制作PPT的主标题、副标题和关键点。请严格按照指定的JSON格式输出结果。"},
{"role": "user", "content": f"""请分析以下文本并提取出主标题、副标题和5-7个关键点。主标题应该简洁有力副标题可以稍微详细一些关键点应该是文本中的重要信息每个关键点应该简短精炼便于在PPT中呈现。
请严格按照以下JSON格式输出结果
{{
"title": "主标题",
"subtitle": "副标题",
"key_points": [
"关键点1",
"关键点2",
"关键点3",
"关键点4",
"关键点5"
]
}}
关键点应该至少有5个最多7个。以下是需要分析的文本
{text}"""},
],
temperature=0.7
)
logging.info("成功接收API响应")
analysis = response.choices[0].message.content
# 解析和验证JSON结果
logging.info("开始解析分析结果:" + analysis)
try:
result = json.loads(analysis)
# 验证JSON结构
assert 'title' in result, "缺少主标题"
assert 'subtitle' in result, "缺少副标题"
assert 'key_points' in result, "缺少关键点"
assert isinstance(result['key_points'], list), "关键点必须是一个列表"
assert 5 <= len(result['key_points']) <= 7, "关键点数量必须在5到7之间"
logging.info(f"分析完成,结果:{result}")
return result
except json.JSONDecodeError:
logging.error("JSON解析失败")
raise ValueError("API返回的结果不是有效的JSON格式")
except AssertionError as e:
logging.error(f"JSON结构验证失败: {str(e)}")
raise ValueError(f"API返回的JSON结构不符合要求: {str(e)}")
except Exception as e:
logging.error(f"分析过程中发生错误: {str(e)}")
raise