02_通过调用硅基流动平台deepseekapi按输入的标题生成文章
from openai import OpenAI
client = OpenAI(
base_url='https://api.siliconflow.cn/v1',
api_key='you api-key'
)
# 定义关键词变量
keyword = "人性的弱点都有哪些?"
# 发送带有流式输出的请求
response = client.chat.completions.create(
model="deepseek-ai/DeepSeek-V2.5",
messages=[
{"role": "user", "content": f"根据输入的关键词:{keyword},并站在中国人立场进行写作,要有鲜明的观点和一个例证,字数控制在100至200个以内,输出标题变量为t,内容变量为c"}
],
stream=True # 启用流式输出
)
# 逐步接收并处理响应
for chunk in response:
if not chunk.choices:
continue
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)
if chunk.choices[0].delta.reasoning_content:
print(chunk.choices[0].delta.reasoning_content, end="", flush=True)
如何获取you api-key?