昨天发现new-api里的snova渠道被禁用了,就去看了下原来key失效了 ,然后去官网看了下发现官网已经升级了,同样接口也升级了,以前仅支持流式数据,现在流和非流都支持了。免费的有限制,但自用完全够了。
正文开始
1、账号注册以及apikey申请,请直接去官网 https://cloud.sambanova.ai/ 非常简单。
2、该接口为兼容openai接口,可直接填入one-api/new-api中使用
3、接下来进行一些小改动(接口返回头无相关跨域信息),让其可在[linuxdo增强插件] linuxdo 增强插件,持续更新欢迎反馈 - 资源荟萃 - LINUX DO 中使用,
复制以下代码部署到cloudflare workers即可,需要绑定自己的域名否则国内无法访问,可参考第五步
我是隐藏代码
//API_KEY 用来接入one-api/new-api/linuxdo增强插件使用,自定义
const API_KEY = "sk-1234567890";
const SNOVA_APIURL = "https://api.sambanova.ai/v1/chat/completions";
//snova ApiKey列表,每次请求都会随机从列表里取一个token,之前申请的旧token也可使用
const SNOVA_TOKEN_LIST=["xxxx","xxxx"];
const CUSTOMER_MODEL_MAP = {
"llama-3.1-8b-instruct": "Meta-Llama-3.1-8B-Instruct",
"llama-3.1-70b-instruct": "Meta-Llama-3.1-70B-Instruct",
"llama-3.1-405b-instruct": "Meta-Llama-3.1-405B-Instruct"
};
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
try {
if (request.method === "OPTIONS") {
return new Response("", getStatus(200));
}
const authHeader = request.headers.get("Authorization");
if (!authHeader || !authHeader.startsWith("Bearer ") || authHeader.split(" ")[1] !== API_KEY) {
return new Response("Unauthorized", getStatus(401));
}
if (request.url.endsWith("/v1/models")) {
const arrs = [];
Object.keys(CUSTOMER_MODEL_MAP).map(element => arrs.push({ id: element, object: "model" }))
const response = {
data: arrs,
success: true
};
return new Response(JSON.stringify(response), getStatus(200));
}
if (request.method !== "POST") {
return new Response("Only POST requests are allowed", getStatus(405));
}
if (!request.url.endsWith("/v1/chat/completions")) {
return new Response("Not Found", getStatus(404));
}
let requestBody = await request.json();
const model = CUSTOMER_MODEL_MAP[requestBody.model] || CUSTOMER_MODEL_MAP["llama-3.1-8b-instruct"];
requestBody.model = model;
const token = SNOVA_TOKEN_LIST[Math.floor(Math.random() * SNOVA_TOKEN_LIST.length)];
const init = {
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(requestBody)
};
const response = await fetch(SNOVA_APIURL, init);
if (!response.ok) {
const errorText = await response.text();
return new Response(`Error from API: ${response.statusText} - ${errorText}`, getStatus(response.status));
}
return new Response(response.body, {
status: response.status, headers: {
"content-type": response.headers.get("content-type"),
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "*",
"Access-Control-Allow-Headers": "*"
}
});
} catch (error) {
return new Response("Internal Server Error: " + error.message, getStatus(500));
}
}
function getStatus(status) {
const responseInit = {
status: status,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "*",
"Access-Control-Allow-Headers": "*",
},
}
return responseInit;
}
4、接入linuxdo增强插件使用如图:
5、如果无法访问cloudflare workers或访问速度慢可参考该文章https://linux.do/t/topic/166793 设置完之后速度直接起飞
6、来都来了点个赞再走呗