| | |
| | | api_key="5017c24f-581f-48fb-abef-8ac4654e9018", # 直接填写密钥 |
| | | ) |
| | | |
| | | |
| | | def generate_xiaohongshu_article(film_name, locations): |
| | | """生成小红书风格的文章""" |
| | | try: |
| | | prompt = f"""请为电影《{film_name}》的拍摄地写一篇小红书风格的文章。要求: |
| | | DEFAULT_PROMPTS = { |
| | | "promptArticle": """请为电影《{film_name}》的拍摄地写一篇小红书风格的文章要求: |
| | | 1. 标题要吸引人,包含电影名称和拍摄地 |
| | | 2. 开头要吸引人,可以用电影中的经典台词或场景引入 |
| | | 3. 详细介绍每个拍摄地点的特色和亮点 |
| | |
| | | 5. 使用emoji表情增加趣味性 |
| | | 6. 最后要有总结和推荐 |
| | | 7. 添加相关标签 |
| | | |
| | | 请以html格式返回,包含标题、正文和标签.生成内容字体要符合在小程序上合适的大小阅读,标题不要超过h3。""" |
| | | |
| | | completion = client.chat.completions.create( |
| | | model="bot-20250512103613-6rwj8", |
| | | messages=[ |
| | | {"role": "system", "content": "你是一个专业的小红书文案写手,擅长写吸引人的旅游攻略。"}, |
| | | {"role": "user", "content": prompt}, |
| | | ], |
| | | ) |
| | | |
| | | return completion.choices[0].message.content |
| | | except Exception as e: |
| | | print(f"生成小红书文章失败: {str(e)}") |
| | | return "" |
| | | |
| | | def generate_travel_route(film_name, locations): |
| | | """生成游玩路线""" |
| | | try: |
| | | prompt = f"""请为电影《{film_name}》的拍摄地设计一条最优游玩路线。要求: |
| | | 请以html格式返回,包含标题、正文和标签.生成内容字体要符合在小程序上合适的大小阅读,标题不要超过h3。""", |
| | | "promptRoute": """请为电影《{film_name}》的拍摄地设计一条最优游玩路线。要求: |
| | | 1. 考虑各个景点之间的距离和交通方式 |
| | | 2. 合理安排游览顺序,避免来回奔波 |
| | | 3. 预估每个景点的游览时间 |
| | |
| | | "description": "路线说明" |
| | | }} |
| | | ] |
| | | }}""" |
| | | |
| | | completion = client.chat.completions.create( |
| | | model="bot-20250512103613-6rwj8", |
| | | messages=[ |
| | | {"role": "system", "content": "你是一个专业的旅游路线规划师,擅长设计最优游览路线。"}, |
| | | {"role": "user", "content": prompt}, |
| | | ], |
| | | ) |
| | | |
| | | try: |
| | | response_text = completion.choices[0].message.content |
| | | cleaned_text = re.sub(r'```json|```', '', response_text) |
| | | cleaned_text = re.sub(r'^[^{[]*', '', cleaned_text) |
| | | return json.loads(cleaned_text) |
| | | except Exception as e: |
| | | print(f"解析路线失败: {str(e)}\n原始响应: {response_text}") |
| | | return None |
| | | except Exception as e: |
| | | print(f"生成游玩路线失败: {str(e)}") |
| | | return None |
| | | |
| | | def generate_xiaohongshu_route(film_name, locations): |
| | | """生成小红书风格的路线攻略""" |
| | | try: |
| | | prompt = f"""请为电影《{film_name}》的拍摄地写一篇小红书风格的路线攻略。要求: |
| | | }}""", |
| | | "promptTravel": """请为电影《{film_name}》的拍摄地写一篇小红书风格的路线攻略要求: |
| | | 1. 标题要吸引人,突出"最佳路线"或"完美行程"等关键词 |
| | | 2. 开头要说明这条路线是如何规划的,为什么这样安排 |
| | | 3. 详细介绍每个景点的游览时间和交通方式 |
| | |
| | | 7. 最后要有总结和注意事项 |
| | | 8. 添加相关标签 |
| | | |
| | | 请以html格式返回,包含标题、正文和标签,生成内容字体要符合在小程序上合适的大小阅读,标题不要超过h3。""" |
| | | |
| | | completion = client.chat.completions.create( |
| | | model="bot-20250512103613-6rwj8", |
| | | messages=[ |
| | | {"role": "system", "content": "你是一个专业的小红书文案写手,擅长写吸引人的旅游路线攻略。"}, |
| | | {"role": "user", "content": prompt}, |
| | | ], |
| | | ) |
| | | |
| | | return completion.choices[0].message.content |
| | | except Exception as e: |
| | | print(f"生成小红书路线攻略失败: {str(e)}") |
| | | return "" |
| | | |
| | | def get_film_works(): |
| | | try: |
| | | url = "http://192.168.1.213:8090/flower/api/filmWorks/pending/create" |
| | | response = requests.get(url) |
| | | if response.status_code == 200: |
| | | return response.json().get("data", []) |
| | | else: |
| | | print(f"获取电影作品失败,状态码: {response.status_code}") |
| | | return [] |
| | | except Exception as e: |
| | | print(f"获取电影作品失败: {str(e)}") |
| | | return [] |
| | | |
| | | |
| | | def get_location_info_from_model(film_name): |
| | | """使用模型获取多个拍摄地点信息""" |
| | | try: |
| | | prompt = f"""请为电影《{film_name}》生成所有主要拍摄地点的详细信息。每部电影通常有多个拍摄地点,请尽可能详细地列出所有重要的拍摄地点。 |
| | | |
| | | 请以html格式返回,包含标题、正文和标签,生成内容字体要符合在小程序上合适的大小阅读,标题不要超过h3。""", |
| | | "promptLocation": """请为电影《{film_name}》生成所有主要拍摄地点细信息。每部电影通常有多个拍摄地点,请尽可能详细地列出所有重要的拍摄地点。 |
| | | 对于每个拍摄地点,请提供以下信息: |
| | | 1. 拍摄地点名称 |
| | | 2. 详细地址 |
| | |
| | | ] |
| | | |
| | | 请确保返回所有重要的拍摄地点,每个地点都要包含完整的信息。""" |
| | | } |
| | | |
| | | |
| | | def get_ai_task_config(): |
| | | """从API获取AI任务配置,保留原始HTML内容""" |
| | | try: |
| | | # 设置请求超时时间 |
| | | response = requests.get( |
| | | "http://14.103.144.28:8090/flower/api/aiTaskConfig/all", |
| | | timeout=100 |
| | | ) |
| | | |
| | | # 检查HTTP状态码 |
| | | response.raise_for_status() |
| | | |
| | | # 解析JSON响应 |
| | | response_data = response.json() |
| | | |
| | | # 验证响应结构 |
| | | if response_data.get("code") != "0" or not isinstance(response_data.get("data"), list): |
| | | print(f"无效的API响应结构: {response_data}") |
| | | return {} |
| | | |
| | | config_list = response_data["data"] |
| | | |
| | | # 返回第一条有效配置(保留原始HTML内容) |
| | | if config_list: |
| | | return { |
| | | "promptArticle": config_list[0].get("promptArticle", ""), |
| | | "promptRoute": config_list[0].get("promptRoute", ""), |
| | | "promptTravel": config_list[0].get("promptTravel", ""), |
| | | "promptLocation": config_list[0].get("promptLocation", "") # 注意字段名拼写差异 |
| | | } |
| | | return {} |
| | | |
| | | except requests.exceptions.RequestException as e: |
| | | print(f"请求AI任务配置失败: {str(e)}") |
| | | return {} |
| | | except json.JSONDecodeError as e: |
| | | print(f"解析API响应JSON失败: {str(e)}") |
| | | return {} |
| | | except Exception as e: |
| | | print(f"获取AI任务配置发生未知错误: {str(e)}") |
| | | return {} |
| | | |
| | | def get_prompt(prompt_type): |
| | | """获取指定类型的提示模板 |
| | | 参数: |
| | | prompt_type: 提示类型,必须是DEFAULT_PROMPTS中定义的键 |
| | | |
| | | 返回: |
| | | 从API获取的提示模板,如果为空则返回默认模板 |
| | | """ |
| | | # 验证输入参数 |
| | | if prompt_type not in DEFAULT_PROMPTS: |
| | | raise ValueError(f"无效的prompt类型: {prompt_type}") |
| | | |
| | | # 获取配置 |
| | | config = get_ai_task_config() |
| | | |
| | | # 获取指定类型的提示,确保内容非空 |
| | | # custom_prompt = config.get(prompt_type, "").strip() |
| | | custom_prompt = config.get(prompt_type) |
| | | |
| | | # 更严格的空值检查 |
| | | if custom_prompt is not None and str(custom_prompt).strip(): |
| | | return str(custom_prompt).strip() |
| | | # 返回有效的提示模板 |
| | | return custom_prompt if custom_prompt else DEFAULT_PROMPTS[prompt_type] |
| | | |
| | | def generate_xiaohongshu_article(film_name, locations): |
| | | """生成小红书风格的文章""" |
| | | prompt = get_prompt("promptArticle").format(film_name=film_name) |
| | | try: |
| | | completion = client.chat.completions.create( |
| | | model="bot-20250512103613-6rwj8", |
| | | messages=[ |
| | | {"role": "system", "content": "你是一个专业的小红书文案写手,擅长写吸引人的旅游攻略。"}, |
| | | {"role": "user", "content": prompt}, |
| | | ], |
| | | ) |
| | | |
| | | return completion.choices[0].message.content |
| | | except Exception as e: |
| | | print(f"生成小红书文章失败: {str(e)}") |
| | | return "" |
| | | |
| | | def generate_travel_route(film_name, locations): |
| | | """生成游玩路线""" |
| | | prompt = get_prompt("promptRoute").format(film_name=film_name) |
| | | try: |
| | | completion = client.chat.completions.create( |
| | | model="bot-20250512103613-6rwj8", |
| | | messages=[ |
| | | {"role": "system", "content": "你是一个专业的旅游路线规划师,擅长设计最优游览路线。"}, |
| | | {"role": "user", "content": prompt}, |
| | | ], |
| | | ) |
| | | |
| | | try: |
| | | response_text = completion.choices[0].message.content |
| | | cleaned_text = re.sub(r'```json|```', '', response_text) |
| | | cleaned_text = re.sub(r'^[^{[]*', '', cleaned_text) |
| | | return json.loads(cleaned_text) |
| | | except Exception as e: |
| | | print(f"解析路线失败: {str(e)}\n原始响应: {response_text}") |
| | | return None |
| | | except Exception as e: |
| | | print(f"生成游玩路线失败: {str(e)}") |
| | | return None |
| | | |
| | | def generate_xiaohongshu_route(film_name, locations): |
| | | """生成小红书风格的路线攻略""" |
| | | prompt = get_prompt("promptTravel").format(film_name=film_name) |
| | | try: |
| | | |
| | | completion = client.chat.completions.create( |
| | | model="bot-20250512103613-6rwj8", |
| | | messages=[ |
| | | {"role": "system", "content": "你是一个专业的小红书文案写手,擅长写吸引人的旅游路线攻略。"}, |
| | | {"role": "user", "content": prompt}, |
| | | ], |
| | | ) |
| | | |
| | | return completion.choices[0].message.content |
| | | except Exception as e: |
| | | print(f"生成小红书路线攻略失败: {str(e)}") |
| | | return "" |
| | | |
| | | def get_film_works(): |
| | | try: |
| | | url = "http://14.103.144.28:8090/flower/api/filmWorks/pending/create" |
| | | response = requests.get(url) |
| | | if response.status_code == 200: |
| | | return response.json().get("data", []) |
| | | else: |
| | | print(f"获取电影作品失败,状态码: {response.status_code}") |
| | | return [] |
| | | except Exception as e: |
| | | print(f"获取电影作品失败: {str(e)}") |
| | | return [] |
| | | |
| | | |
| | | def get_location_info_from_model(film_name): |
| | | """使用模型获取多个拍摄地点信息""" |
| | | prompt = get_prompt("promptLocation").format(film_name=film_name) |
| | | try: |
| | | # Non-streaming: |
| | | print("----- standard request -----") |
| | | completion = client.chat.completions.create( |
| | |
| | | |
| | | def create_film_location(film_id, film_name, location_info, article, route_article): |
| | | try: |
| | | url = "http://192.168.1.213:8090/flower/api/filmLocation/new" |
| | | url = "http://14.103.144.28:8090/flower/api/filmLocation/new" |
| | | |
| | | # 默认值设置 |
| | | default_data = { |
| | |
| | | except Exception as e: |
| | | print(f"保存文件失败: {str(e)}") |
| | | |
| | | def main(): |
| | | def process_films(): |
| | | # 获取所有电影作品 |
| | | try: |
| | | film_works = get_film_works() |
| | |
| | | |
| | | # 保存到新接口 |
| | | try: |
| | | save_url = "http://192.168.1.213:8090/flower/api/filmWorks/edit" |
| | | save_url = "http://14.103.144.28:8090/flower/api/filmWorks/edit" |
| | | save_data = { |
| | | "id": film_id, |
| | | "type": "sys", |
| | |
| | | print(f"处理电影 {film.get('nameCn', '未知电影')} 时发生错误: {str(e)}") |
| | | continue |
| | | |
| | | def main(): |
| | | process_films() |
| | | |
| | | if __name__ == "__main__": |
| | | main() |
| | | # if __name__ == "__main__": |
| | | # main() |