前言
大名鼎鼎的sambanova-ai今天开启了免登录使用,本人立马进行了逆向工程转换为兼容openai的api,可用于各个应用。
部署
我采用workers,省去了购买服务器。创建workers-编辑代码-将以下代码复制进去,并保存部署。
const TARGET_URL = 'https://fast.snova.ai/api/completion';
// use model override to disregard the model specified in the request and use the model specified here
// for example, you can set it to 'llama3-405b', so that even if the request said it want to use gpt-4o, it will use llama3-405b
const MODEL_OVERRIDE = ''; // Set this to null or an empty string if you don't want to override
export default {
async fetch(request) {
if (request.method === 'POST' && request.url.endsWith('/v1/chat/completions')) {
try {
const originalPayload = await request.json();
// Override the model if MODEL_OVERRIDE is set
if (MODEL_OVERRIDE && MODEL_OVERRIDE.trim() !== '') {
originalPayload.model = MODEL_OVERRIDE;
}
const modifiedPayload = {
body: {
...originalPayload,
stop: ["<|eot_id|>"]
},
env_type: "tp16405b"
};
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(modifiedPayload)
};
const response = await fetch(TARGET_URL, options);
// Recreate the Response to set the appropriate CORS headers
const newResponse = new Response(response.body, {
status: response.status,
headers: {
...Object.fromEntries(response.headers),
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization'
}
});
return newResponse;
} catch (error) {
console.error('Error processing request:', error);
return new Response(JSON.stringify({ error: 'Bad Request' }), { status: 400, headers: { 'Content-Type': 'application/json' } });
}
} else {
return new Response(JSON.stringify({ error: 'Not Found' }), { status: 404, headers: { 'Content-Type': 'application/json' } });
}
}
};
env_type 认证密钥可自定义;
llama3-405b 是模型在客户端填写,你也可以在代码中定义MODEL_OVERRIDE = 这样的话只能这个模型,留空的话,只要客户端填写sambanova.ai家的哪个模型都可以;
使用
部署完后workers分配的域名打开提示
{
"error": "Not Found"
}
说明成功了,兼容openai的客户端上配置好域名、认证密钥,指定模型llama3-405b,即可使用:
部署给的域名无法国内无法访问,添加个自定义域名(前提你的域名已经托管倒cloudflare)