diff --git a/docs/.vuepress/config.ts b/docs/.vuepress/config.ts index 6e7d1bc..bc1daeb 100644 --- a/docs/.vuepress/config.ts +++ b/docs/.vuepress/config.ts @@ -18,6 +18,8 @@ export default defineUserConfig({ shouldPrefetch: false, theme: plumeTheme({ + /* 站点域名,启动 SEO 优化 */ + hostname: 'https://notes.simengweb.com', /* 博客文章页面链接前缀 */ article: '/article/', diff --git a/docs/blog/collect/free_model_pai.md b/docs/blog/collect/free_model_pai.md new file mode 100644 index 0000000..9409557 --- /dev/null +++ b/docs/blog/collect/free_model_pai.md @@ -0,0 +1,249 @@ +--- +title: 便宜免费的大模型 API 整合 ( 2025年11月11日 ) +createTime: 2025/11/11 13:54:02 +permalink: /article/free_model_api/ +--- + +## 免费的大模型 API 整合 + +嗨~如果你在找既温柔又省荷包的小模型,就把它们悄悄收进这里吧!它们也许不是夜空最亮的那颗星,却能在摘要、划重点、轻声问答的小角落里,给你软软又稳稳的陪伴哦~ + + +### 百度千帆大模型平台 + +先从千帆开始吧~下面是常用模型的参考价格: + +| 模型名称 | 版本名称 | 服务内容 | 子项 | 在线推理 | 批量推理 | 单位 | +|---|---|---|---|---|---|---| +| ERNIE Speed | ernie-speed-128k | 推理服务 | 输入 | 0 | 0.00012 | 元/千tokens | +| ERNIE Speed | ernie-speed-128k | 推理服务 | 输出 | 0 | 0.00024 | 元/千tokens | +| ERNIE Lite | ernie-lite-8k | 推理服务 | 输入 | 0 | 0.0012 | 元/千tokens | +| ERNIE Lite | ernie-lite-8k | 推理服务 | 输出 | 0 | 0.0024 | 元/千tokens | +| ERNIE Tiny | ernie-tiny-8k | 推理服务 | 输入 | 0 | 0.00008 | 元/千tokens | +| ERNIE Tiny | ernie-tiny-8k | 推理服务 | 输出 | 0 | 0.00016 | 元/千tokens | + +> 提示:单纯调用 API 接口属于在线推理,当前显示为 0 元;批量推理按量计费。 + +#### 快速上手 + +1. 访问控制台并获取 API Key。 +2. 使用下方示例进行快速测试。 +3. 若遇错误,请检查模型名称与凭证有效期。 + +控制台:[https://console.bce.baidu.com/qianfan/ais/console/apiKey](https://console.bce.baidu.com/qianfan/ais/console/apiKey) + +```python +import requests +import json + +def main(): + url = "https://qianfan.baidubce.com/v2/chat/completions" + API_KEY = 'YOUR_API_KEY' + payload = json.dumps({ + "model": "ernie-speed-128k", + "messages": [ + { + "role": "system", + "content": "You are a helpful assistant." + }, + { + "role": "user", + "content": "你好" + } + ] + }) + headers = { + 'Content-Type': 'application/json', + 'Authorization': F'Bearer {API_KEY}' + } + response = requests.request("POST", url, headers=headers, data=payload) + print(response.text) + + +if __name__ == '__main__': + main() +``` + +> 小贴士:将 `YOUR_API_KEY` 替换为你的密钥,建议使用环境变量管理凭证;请勿在公共仓库提交 Key。 + +更多文档:[https://cloud.baidu.com/doc/qianfan-api/s/3m9b5lqft](https://cloud.baidu.com/doc/qianfan-api/s/3m9b5lqft) + +### 讯飞星火大模型 + +接着,我们轻盈地来到星火~ + +- 官网:[https://xinghuo.xfyun.cn/sparkapi](https://xinghuo.xfyun.cn/sparkapi) +- 控制台:[https://console.xfyun.cn/services/cbm?from=desk](https://console.xfyun.cn/services/cbm?from=desk) +- 模型说明:可见 Spark Lite,Token 余量为无限。 + +#### 快速上手 + +> 小贴士:如需联网检索,启用 `web_search`;流式输出可以提升交互体验。 + +1. 在控制台获取授权凭证并替换到示例中。 +2. 选择 `Lite` 模型,按需开启 `web_search` 与 `stream`。 +3. 若需要长内容输出,注意 8K tokens 限制,合理裁剪上下文。 + +```python +# encoding:UTF-8 +import json +import requests + + +# 请替换XXXXXXXXXX为您的 APIpassword, 获取地址:https://console.xfyun.cn/services/bmx1 +api_key = "Bearer XXXXXXXXXX" +url = "https://spark-api-open.xf-yun.com/v1/chat/completions" + +# 请求模型,并将结果输出 +def get_answer(message): + #初始化请求体 + headers = { + 'Authorization':api_key, + 'content-type': "application/json" + } + body = { + "model": "Lite", + "user": "user_id", + "messages": message, + # 下面是可选参数 + "stream": True, + "tools": [ + { + "type": "web_search", + "web_search": { + "enable": True, + "search_mode":"deep" + } + } + ] + } + full_response = "" # 存储返回结果 + isFirstContent = True # 首帧标识 + + response = requests.post(url=url,json= body,headers= headers,stream= True) + # print(response) + for chunks in response.iter_lines(): + # 打印返回的每帧内容 + # print(chunks) + if (chunks and '[DONE]' not in str(chunks)): + data_org = chunks[6:] + + # print(f"DEBUG: raw data_org: {data_org}") + chunk = json.loads(data_org) + text = chunk['choices'][0]['delta'] + + # 判断最终结果状态并输出 + if ('content' in text and '' != text['content']): + content = text["content"] + if (True == isFirstContent): + isFirstContent = False + print(content, end="") + full_response += content + return full_response + + +# 管理对话历史,按序编为列表 +def getText(text,role, content): + jsoncon = {} + jsoncon["role"] = role + jsoncon["content"] = content + text.append(jsoncon) + return text + +# 获取对话中的所有角色的content长度 +def getlength(text): + length = 0 + for content in text: + temp = content["content"] + leng = len(temp) + length += leng + return length + +# 判断长度是否超长,当前限制8K tokens +def checklen(text): + while (getlength(text) > 11000): + del text[0] + return text + + +#主程序入口 +if __name__ =='__main__': + + #对话历史存储列表 + chatHistory = [] + #循环对话轮次 + while (1): + # 等待控制台输入 + Input = input("\n" + "我:") + question = checklen(getText(chatHistory,"user", Input)) + # 开始输出模型内容 + print("星火:", end="") + getText(chatHistory,"assistant", get_answer(question)) +``` + +### 腾讯混元大模型 + + - 计费与价格:[https://cloud.tencent.com/document/product/1729/97731](https://cloud.tencent.com/document/product/1729/97731) + - 文档与控制台:[https://cloud.tencent.com/document/product/1729/111008](https://cloud.tencent.com/document/product/1729/111008) + - 模型说明:`hunyuan-lite` 免费可用,适合轻量任务。 + +```python +import os +from openai import OpenAI + +# 构造 client(建议使用环境变量管理密钥) +api_key = os.getenv("HUNYUAN_API_KEY", "YOUR_API_KEY") +client = OpenAI( + api_key=api_key, # 混元 APIKey + base_url="https://api.hunyuan.cloud.tencent.com/v1", # 混元 endpoint +) + +completion = client.chat.completions.create( + model="hunyuan-lite", + messages=[ + { + "role": "user", + "content": "泥嚎" + } + ], + extra_body={ + "enable_enhancement": True, + }, +) +print(completion.choices[0].message.content) +``` + +> 小贴士:请勿在公共仓库提交任何真实密钥;使用环境变量或密钥管理服务更安全。 + +## 低价大模型 + +### SCNet 平台 + +#### 简介 +SCNet 是一个面向人工智能和科学计算的一站式算力与 AI 平台,提供从数据管理、模型训练到部署的完整链路服务,同时结合国产超算硬件和多模态模型生态,让企业和开发者能更高效地使用 AI。 + +#### 链接与文档 +- 官网:[https://www.scnet.cn/](https://www.scnet.cn/) +#### 价格一览 +| 模型 | 上下文长度 | 百万 tokens 输入价格 | 百万 tokens 输出价格 | +|---|---|---|---| +| Qwen3-235B-A22B | 32K | 0.1 元 | 0.1 元 | +| DeepSeek-R1-Distill-Qwen-7B | 32K | 0.1 元 | 0.1 元 | +| DeepSeek-R1-Distill-Qwen-32B | 32K | 1 元 | 4 元 | +| DeepSeek-R1-Distill-Llama-70B | 32K | 0.1 元 | 6 元 | +| QwQ-32B | 32K | 1 元 | 4 元 | +| Qwen3-30B-A3B | 128K | 1 元 | 6 元 | +| Qwen3-Embedding-8B | - | 0.1 元 | - | + +- 文档(计费与说明):[https://www.scnet.cn/ac/openapi/doc/2.0/moduleapi/tutorial/modulefee.html](https://www.scnet.cn/ac/openapi/doc/2.0/moduleapi/tutorial/modulefee.html) + +目前看到的价格最低的 Qwen3-235B-A22B 模型的 API,相比前面的免费模型,性能更强。 +- API 接口使用样例:[https://www.scnet.cn/ac/openapi/doc/2.0/moduleapi/tutorial/apicall.html](https://www.scnet.cn/ac/openapi/doc/2.0/moduleapi/tutorial/apicall.html) + +## 小结 + +把上面这些“零钱包”级别的模型都翻完啦~ +- 百度千帆、讯飞星火、腾讯混元都给出「免费在线额度」,日常轻量问答、摘要、润色完全够用;记得把 Key 藏进环境变量,别手滑推到 GitHub。 +- 如果任务量突然暴涨,SCNet 的 Qwen3-235B-A22B 只要 0.1 元/百万 tokens,性价比直接拉满,当“备胎”也安心。 + +一句话:先薅免费的,薅不动再掏 0.1 元,让荷包和模型一起“稳稳幸福”吧!