3. 第三放平台部署deepseek

发布于:2025-03-30 ⋅ 阅读:(41) ⋅ 点赞:(0)

有时候我们会发现使用deepseek服务器,异常卡顿,这是由于多方面原因造成的,比如说访问人数过多等。想要解决这个问题,我们可以选择第三方平台进行部署

第三方平台

我们可以选择的第三方平台很多,比如硅基流动、秘塔搜索、百度千帆等,常见平台官网如下

  • 阿里云:https://pai.console.aliyun.com/#/quick-start/models.
  • 腾讯云:https://console.cloud.tencent.com/tione/v2/aimarket/detail/deepseek series?regionld=1&detailTab=introducee
  • cursor:https://cursor.com 需要cursor会员
  • grok:https://groq.com/蒸馏版llama 70b,中文能力不足
  • 国家超算中心:https://www.scnet.cn/ui/mall/
  • 硅基流动:https://siliconflow.cn/zh-cn/models

我们以硅基流动为例来介绍第三方平台部署

部署

  1. 打卡硅基流动官网,我们可以看到他可以使用的模型在这里插入图片描述

  2. 我们以V3模型为例来进行部署,点击第一行第二个模型,他会出现模型的详情信息在这里插入图片描述

  3. 我们可以在线体检,也可以看api文档进行部署,我们打开API文档,在左侧我们可以看到它支持的接口在这里插入图片描述,右侧就是相关的例子了

  4. 我们以创建文本对话为例来进行部署,以官网为例写下如下代码

import requests

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

payload = {
    "model": "Qwen/QwQ-32B",
    "messages": [
        {
            "role": "user",
            "content": "What opportunities and challenges will the Chinese large model industry face in 2025?"
        }
    ],
    "stream": False,
    "max_tokens": 512,
    "stop": None,
    "temperature": 0.7,
    "top_p": 0.7,
    "top_k": 50,
    "frequency_penalty": 0.5,
    "n": 1,
    "response_format": {"type": "text"},
    "tools": [
        {
            "type": "function",
            "function": {
                "description": "<string>",
                "name": "<string>",
                "parameters": {},
                "strict": False
            }
        }
    ]
}
headers = {
    "Authorization": "Bearer <token>",
    "Content-Type": "application/json"
}

response = requests.request("POST", url, json=payload, headers=headers)

print(response.text)

对于代码解释

这段代码是使用 Python 的 requests 库向 SiliconFlow API 发送一个 POST 请求,调用 Qwen/QwQ-32B 大语言模型,并获取其对 “2025年中国大模型产业面临的机遇与挑战” 这个问题的回答。

1. 请求目标(API 端点)
url = "https://api.siliconflow.cn/v1/chat/completions"

• 这是 SiliconFlow 提供的 Chat Completions API,用于与大模型对话。

2. 请求数据(Payload)
payload = {
    "model": "Qwen/QwQ-32B",  # 指定调用的模型
    "messages": [
        {
            "role": "user",  # 用户角色
            "content": "What opportunities and challenges will the Chinese large model industry face in 2025?"  # 用户提问
        }
    ],
    "stream": False,  # 是否流式返回(False 表示一次性返回完整回答)
    "max_tokens": 512,  # 限制返回的最大 token 数量(防止回答过长)
    "temperature": 0.7,  # 控制回答的随机性(0-1,越高越有创意)
    "top_p": 0.7,  # 控制生成多样性(类似 temperature)
    "top_k": 50,  # 限制采样范围(仅从 top_k 个最可能的 token 中选择)
    "frequency_penalty": 0.5,  # 降低重复内容的概率
    "n": 1,  # 只生成 1 个回答
    "response_format": {"type": "text"},  # 返回纯文本格式
    "tools": [...]  # (可选)用于函数调用,但这里未实际使用
}

model: 指定调用的模型(这里是 Qwen/QwQ-32B)。
messages: 对话历史,这里只有用户的一条提问。
stream: 是否流式返回(False 表示一次性返回完整回答)。
max_tokens: 限制回答长度(防止过长)。
temperaturetop_ptop_k: 控制回答的随机性和多样性。
frequency_penalty: 减少重复内容。
tools: 可用于 函数调用(Function Calling)

3. 请求头(Headers)
headers = {
    "Authorization": "Bearer <token>",  # 替换为你的 API Key
    "Content-Type": "application/json"  # 告诉服务器发送的是 JSON 数据
}

Authorization: 需要替换 <token> 为你的 SiliconFlow API Key(否则会返回 401 未授权错误)。
Content-Type: 指定数据格式为 application/json

4. 发送请求
response = requests.request("POST", url, json=payload, headers=headers)

• 使用 requests.post() 发送 POST 请求,传递 payload(JSON 格式)和 headers(包含认证信息)。

5. 打印响应
print(response.text)

• 打印 API 返回的 JSON 格式 的完整响应。

对于各个参数的取值,官网会有更详细的说明,点击

申请API KEY

在代码中,我们需要替换<token>,也就是API KEY来进行正确的调用,所以我们应该先进行申请 API KEY

  1. 进入申请API KEY 的页面,并登录

  2. 点击新建API KEY 在这里插入图片描述

  3. 点击余额充值,我们可以看到剩余的金额在这里插入图片描述

  4. 新建好后,我们将API KEY替换,运行上面代码,得到如下回答

{
	"id": "0195e27f092c94bd150980adec837ef5",
	"object": "chat.completion",
	"created": 1743261731,
	"model": "Qwen/QwQ-32B",
	"choices": [{
		"index": 0,
		"message": {
			"role": "assistant",
			"content": "",
			"reasoning_content": "Okay, the user is asking about the opportunities and challenges the Chinese large model industry will face in 2025. Hmm, I need to break this down. First, I should consider what tools are available. The provided functions might include something like a market analysis tool 
			or a future prediction model.\n\ nLooking at the tools given,
			there 's a function called \"analyze_market_trends\" which takes parameters like region, industry, and year. That seems relevant here. The user is focusing on China'
			s large model industry,
			so region would be\ "China\", industry \"AI/ML\" or \"Large Language Models\", and year 2025.\n\nWait, the parameters for analyze_market_trends include region (string), industry (string), year (integer), and maybe others like specific_subsector. Since the question is about large models specifically, maybe adding \"large language models\" as the specific_subsector would help narrow it down.\n\nI should also check if there's another function called \"predict_future_impact\" which takes technology (string), region (string), timeframe (string like \"2025\"), and factors like economic, regulatory. That could cover both opportunities and challenges by considering different factors.\n\nSo combining both functions might give a comprehensive answer. First use analyze_market_trends to get market trends in China's AI/ML sector focusing on large models by 2025. Then use predict_future_impact to look at technological, economic, regulatory factors affecting them.\n\nWait but the user's question is about opportunities and challenges specifically. The functions might already capture that in their outputs. Let me make sure each function's parameters are correctly filled. For analyze_market_trends: region China, industry \"AI/ML\", year 2025, specific_subsector \"large language models\". For predict_future_impact: technology \"large language models\", region China, timeframe \"2025\", factors including economic, regulatory, technological.\n\nI need to structure the tool calls correctly in XML tags as specified. Each tool_call should be separate. Let me format that properly.\n",
			"tool_calls": [{
				"id": "0195e27f26b353d40e8c41265d8faa9f",
				"type": "function",
				"function": {
					"name": "analyze_market_trends",
					"arguments": "{\"region\": \"China\", \"industry\": \"AI/ML\", \"year\": 2025, \"specific_subsector\": \"large language models\"}"
				}
			}]
		},
		"finish_reason": "tool_calls"
	}],
	"usage": {
		"prompt_tokens": 140,
		"completion_tokens": 512,
		"total_tokens": 652
	},
	"system_fingerprint": ""
}