Python调用深度学习大模型:以Deepseek和Qwen为例详解过程

代码

import requests
import json
from openai import OpenAI

预备(以BERTopic聚合主题为例)

prompt = """
I have a topic that is described by the following keywords: 
{INPUT}

Based on the information above, extract a short but highly descriptive topic label of at most 5 words. Make sure it is in the following format:
topic: <topic label>
"""

test = "xxx"

msg = [{"role": "system", "content": prompt},
       {"role": "user",   "content": test}
]

url = "https://api.siliconflow.cn/v1/chat/completions"

model="deepseek-ai/DeepSeek-V3" # 替换为模型广场对应模型名

client = OpenAI(
    api_key="sk-xxx",  # 从https://cloud.siliconflow.cn/i/Dyv2uzaz获取,链接为邀请。API密钥栏获取密钥,注意隐私保密。
    base_url="https://api.siliconflow.cn/v1"
)
# 或
headers = {
    "Authorization": "Bearer <token>",
    "Content-Type": "application/json"
}

设定模型

payload = {
    "model": model,
    "messages": msg,
    "stream": False,   # 流输出
    "max_tokens": 4096,  #max_tokens必须小于等于16384
    "stop": None,
    "temperature": 0,  #求稳定的话尽可能温度为0
    "frequency_penalty": 0.5,
    "n": 1
    # 注意:根据API文档,你可能需要移除或适当地填充tools字段 
    # https://docs.siliconflow.cn/cn/api-reference/chat-completions/chat-completions
}

会话

response = client.chat.completions.create(**payload)
print(response)
#或
response = requests.request("POST", url, json=payload, headers=headers)
print(response.text)

作者:桥苯环萘我老婆

物联沃分享整理
物联沃-IOTWORD物联网 » Python调用深度学习大模型:以Deepseek和Qwen为例详解过程

发表回复