赞
踩
这段代码展示了如何使用 Dashscope 库结合阿里云提供的千问聊天模型(Q-Chat)来自动生成内容。它从 Excel 文件读取提示词和标题,使用这些提示词调用 API 生成文本,然后将生成的内容保存为 Markdown 文件。这种方式对于需要大量内容且希望自动化这一过程的自媒体创作者来说尤其有用。
- # For prerequisites running the following sample, visit https://help.aliyun.com/document_detail/611472.html
- from http import HTTPStatus
- import pandas as pd
- import os
- import dashscope
-
- dashscope.api_key = ''
-
-
- def call_with_messages(prompt):
- try:
- messages = [{'role': 'system', 'content': 'You are a helpful assistant.'},
- {'role': 'user', 'content': prompt}]
-
- response = dashscope.Generation.call(
- dashscope.Generation.Models.qwen_plus, # 模型选择
- messages=messages,
- max_tokens=1500, # 最大字数限制
- top_p=0.8, # 多样性设置
- repetition_penalty=1.1, # 重复惩罚设置
- temperature=1.0, # 随机性设置
- result_format='message', # 结果格式
- )
-
- return response
- except Exception as e:
- print(f"生成故事时发生错误: {e}")
- return None
-
- def read_excel_and_generate_stories(file_path, sheet_name, prompt_column, title_column):
- try:
- # 读取Excel文件
- df = pd.read_excel(file_path, sheet_name=sheet_name)
- except Exception as e:
- print(f"读取Excel文件时发生错误: {e}")
- return
-
- for index, row in df.iterrows():
- try:
- prompt = row[prompt_column]
- title = row[title_column]
-
- response = call_with_messages(prompt)
- if response and response.status_code == HTTPStatus.OK:
- output = response.output
- if output and output.get("choices"):
- message = output["choices"][0].get("message")
- if message:
- content = message.get("content")
-
- # 创建文件名和路径
- file_name = f'{title}.md'
- output_path = os.path.join(r'D:\wenjian\obsidian\笔记\自媒体\AI生成', file_name)
-
- # 保存到md文件
- with open(output_path, 'w', encoding='utf-8') as file:
- file.write(content)
- print(f'结果已保存到文件:{output_path}')
- else:
- print(f'请求失败,状态码:{response.status_code}')
- except Exception as e:
- print(f"处理Excel行时发生错误: {e}")
-
- if __name__ == '__main__':
- excel_file_path = r"D:\wenjian\onedrive\Desktop\自媒体.xlsx"
- read_excel_and_generate_stories(excel_file_path, '古诗词', '提示词', '名称')
这个脚本可以在多个领域内发挥作用,例如:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。