from openai import OpenAI client = OpenAI(api_key="YOUR_KEY", base_url="https://api.siliconflow.cn/v1") response = client.chat.completions.create( model="deepseek-ai/DeepSeek-V3", messages=[ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Write a haiku about recursion in programming."} ], temperature=0.7, max_tokens=1024, 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)
分析一幅图像(点击查看详情)
from openai import OpenAIclient = OpenAI(api_key="YOUR_KEY", base_url="https://api.siliconflow.cn/v1")response = client.chat.completions.create( model="deepseek-ai/deepseek-vl2", messages=[ { "role": "user", "content": [ { "type": "image_url", "image_url": { "url": "https://sf-maas-uat-prod.oss-cn-shanghai.aliyuncs.com/outputs/658c7434-ec12-49cc-90e6-fe22ccccaf62_00001_.png", }, }, { "type": "text", "text": "What's in this image?" } ], } ], temperature=0.7, max_tokens=1024, 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)
生成json数据(点击查看详情)
import json from openai import OpenAIclient = OpenAI( api_key="您的 APIKEY", # 从https://cloud.siliconflow.cn/account/ak获取 base_url="https://api.siliconflow.cn/v1")response = client.chat.completions.create( model="deepseek-ai/DeepSeek-V2.5", messages=[ {"role": "system", "content": "You are a helpful assistant designed to output JSON."}, {"role": "user", "content": "? 2020 年世界奥运会乒乓球男子和女子单打冠军分别是谁? " "Please respond in the format {\"男子冠军\": ..., \"女子冠军\": ...}"} ], response_format={"type": "json_object"} )print(response.choices[0].message.content)