langchain-learning/helloworld/openai_siliconflow.py
2026-04-12 17:39:33 +08:00

28 lines
814 B
Python

from openai import OpenAI
import os
import dotenv
dotenv.load_dotenv()
## 设置环境变量
os.environ['OPENAI_API_KEY'] = os.getenv("SILICONFLOW_API_KEY")
os.environ['OPENAI_BASE_URL'] = os.getenv("SILICONFLOW_BASE_URL")
client = OpenAI()
response = client.chat.completions.create(
# model='Pro/deepseek-ai/DeepSeek-R1',
model="deepseek-ai/DeepSeek-R1-0528-Qwen3-8B",
messages=[
{'role': 'user',
'content': "推理模型会给市场带来哪些新的机会"}
],
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)