You need to enable JavaScript to run this app.
文档中心
AI 原生 BaaS 平台 Supabase 版

AI 原生 BaaS 平台 Supabase 版

复制全文
下载 pdf
大模型访问管理
APP/Agent 通过 AI-Gateway 访问 AI 模型
复制全文
下载 pdf
APP/Agent 通过 AI-Gateway 访问 AI 模型
通过 AIDAP 提供的访问大模型的能力(AI-Gateway),开发者可以灵活地在前端或后端调用 AI 模型。本文档整理了三种核心访问场景的示例代码,涵盖主流的 JavaScript 和 Python SDK。​
场景一:基于 Supabase Auth 的 access_token 从前端访问​
适用于纯前端应用或 Agent,前端代码可以使用火山 Supabase Auth 为登录用户颁发的 access_token,直接通过浏览器访问 AI 模型。​
  1. 基于 OpenAI JavaScript SDK 调用文本模型​
​
import { createClient } from '@supabase/supabase-js'​
import OpenAI from 'openai';​
​
const supabaseUrl = 'https://br-xxx.supabase.aidap.cn-beijing.volces.com'; // 火山Supabase地址​
const supabaseAnonKey = 'your-supabase-anon-key'; // 火山Supabase的anon_key​
​
// 构造 Supabase Client​
const supabase = createClient(​
supabaseUrl,​
supabaseAnonKey​
)​
​
// 通过 火山Supabase AI-Gateway 访问大模型​
async function chatWithAI() {​
// 获取已登录的APP用户的suapbase session​
const { data: { session } } = await supabase.auth.getSession();​
​
// 构造 OpenAI Client​
const openai = new OpenAI({​
baseURL: supabaseUrl + "/v1", // 基于火山Supabase地址构造得到baseURL​
apiKey: session.access_token, // 将火山Supabase Auth颁发的access_token作为apiKey​
dangerouslyAllowBrowser: true​
});​
​
// 通过 OpenAI Client 与任意模型进行对话​
const stream = await openai.chat.completions.create({​
model: 'bytedance/doubao-seed-2.0-pro', // 指定火山Supabase支持的模型​
messages: [{ role: 'user', content: '你好' }],​
stream: true,​
});​
​
for await (const chunk of stream) {​
const content = chunk.choices[0]?.delta?.content || "";​
process.stdout.write(content);​
}​
}​
​
chatWithAI();​
​
// 照常使用Supabase的其他BaaS功能​
async function getTodos() {​
const { data, error } = await supabase​
.from('todos')​
.select('*');​
if (error) console.error(error);​
else console.log('Todos:', data);​
}​
​
getTodos();​
​
  1. 基于 Vercel JavaScript SDK 调用文本模型​
​
import { createClient } from '@supabase/supabase-js'​
import { createOpenAICompatible, generateText } from "ai";​
​
const supabaseUrl = 'https://br-xxx.supabase.aidap.cn-beijing.volces.com'; // 火山Supabase地址​
const supabaseAnonKey = 'your-supabase-anon-key'; // 火山Supabase的anon_key​
​
// 构造 Supabase Client​
const supabase = createClient(​
supabaseUrl,​
supabaseAnonKey​
)​
​
// 通过火山Supabase AI-Gateway访问大模型​
async function chatWithAI() {​
// 获取已登录的APP用户的suapbase session​
const { data: { session } } = await supabase.auth.getSession();​
​
// 构造 Vercel AI Client​
const gateway = createOpenAICompatible({​
baseURL: supabaseUrl + "/v1", // 基于火山Supabase地址构造得到baseURL​
apiKey: session.access_token, // 将火山Supabase Auth颁发的access_token作为apiKey​
name: "supabase-ai-gateway", // 任意名称​
});​
​
// 通过 Vercel AI Client 与任意模型进行对话​
const result = streamText({​
model: gateway('bytedance/doubao-seed-2.0-pro'), // 指定火山Supabase支持的模型​
prompt: '你好',​
});​
​
// 流式输出​
for await (const chunk of result.textStream) {​
process.stdout.write(chunk);​
}​
}​
​
chatWithAI();​
​
// 照常使用Supabase的其他BaaS功能​
async function getTodos() {​
const { data, error } = await supabase​
.from('todos')​
.select('*');​
if (error) console.error(error);​
else console.log('Todos:', data);​
}​
​
getTodos();​
​
  1. 基于 Ark Python SDK 调用图片模型​
​
import os​
​
from arkruntime import Ark​
from supabase import create_client​
​
supabase_url = "https://br-xxx.supabase.aidap.cn-beijing.volces.com"​
supabase_anon_key = os.environ.get("SUPABASE_ANON_KEY")​
​
# 构造 Supabase Client​
supabase = create_client(supabase_url, supabase_anon_key)​
​
def generate_image() -> None:​
# 获取已登录 APP 用户的 Supabase session​
session = supabase.auth.get_session()​
client = Ark(​
base_url=supabase_url + "/v1",​
api_key=session.access_token,​
)​
​
response = client.images.generate(​
model="bytedance/doubao-seedream-5.0-pro",​
prompt=(​
"一张简洁的科技产品海报:银色机器人手臂在明亮工作台上绘制一朵蓝色花,"​
"写实摄影风格,高质量细节"​
),​
size="1024x1024",​
watermark=False,​
response_format="url",​
output_format="png",​
)​
print("response:", response)​
​
generate_image()​
​
  1. 基于 OpenAI Python SDK 调用图片模型​
​
import os​
​
from openai import OpenAI​
from supabase import create_client​
​
supabase_url = "https://br-xxx.supabase.aidap.cn-beijing.volces.com"​
supabase_anon_key = os.environ.get("SUPABASE_ANON_KEY")​
​
# 构造 Supabase Client​
supabase = create_client(supabase_url, supabase_anon_key)​
​
def generate_image() -> None:​
# 获取已登录 APP 用户的 Supabase session​
session = supabase.auth.get_session()​
client = OpenAI(​
base_url=supabase_url + "/v1",​
api_key=session.access_token,​
)​
​
response = client.images.generate(​
model="bytedance/doubao-seedream-5.0-pro",​
prompt=(​
"一张简洁的科技产品海报:银色机器人手臂在明亮工作台上绘制一朵蓝色花,"​
"写实摄影风格,高质量细节"​
),​
size="1024x1024",​
response_format="url",​
extra_body={​
"watermark": False,​
"output_format": "png",​
},​
)​
print("response:", response)​
​
generate_image()​
​
  1. 基于 Vercel Python SDK 调用图片模型​
​
import asyncio​
import os​
​
import ai​
from supabase import create_client​
​
supabase_url = "https://br-xxx.supabase.aidap.cn-beijing.volces.com"​
supabase_anon_key = os.environ.get("SUPABASE_ANON_KEY")​
​
# 构造 Supabase Client​
supabase = create_client(supabase_url, supabase_anon_key)​
​
async def generate_image() -> None:​
# 获取已登录 APP 用户的 Supabase session​
session = supabase.auth.get_session()​
provider = ai.get_provider(​
"openai",​
base_url=supabase_url + "/v1",​
api_key=session.access_token,​
)​
​
response = await provider.sdk_client.images.generate(​
model="bytedance/doubao-seedream-5.0-pro",​
prompt=(​
"一张简洁的科技产品海报:银色机器人手臂在明亮工作台上绘制一朵蓝色花,"​
"写实摄影风格,高质量细节"​
),​
size="1024x1024",​
response_format="url",​
extra_body={​
"watermark": False,​
"output_format": "png",​
},​
)​
print("response:", response)​
​
asyncio.run(generate_image())​
​
  1. 基于 Ark Python SDK 调用视频模型​
​
import os​
import time​
​
from arkruntime import Ark​
from supabase import create_client​
​
supabase_url = "https://br-xxx.supabase.aidap.cn-beijing.volces.com"​
supabase_anon_key = os.environ.get("SUPABASE_ANON_KEY")​
​
# 构造 Supabase Client​
supabase = create_client(supabase_url, supabase_anon_key)​
​
def generate_video() -> None:​
# 获取已登录 APP 用户的 Supabase session​
session = supabase.auth.get_session()​
client = Ark(​
base_url=supabase_url + "/api/v3",​
api_key=session.access_token,​
)​
​
response = client.content_generation.tasks.create(​
model="bytedance/doubao-seedance-2.0-fast",​
content=[​
{​
"type": "text",​
"text": (​
"一段 5 秒的产品展示视频:银色机器人手臂在明亮工作台上绘制一朵蓝色花,"​
"镜头缓慢推进,写实摄影风格,高质量细节"​
),​
}​
],​
duration=5,​
ratio="16:9",​
resolution="720p",​
watermark=False,​
)​
​
task = client.content_generation.tasks.get(response.id)​
while str(getattr(task.status, "value", task.status)) not in {​
"succeeded",​
"failed",​
"cancelled",​
}:​
time.sleep(10)​
task = client.content_generation.tasks.get(response.id)​
​
print("response:", task)​
​
generate_video()​
​
场景二:基于 Third-Party Auth 的 JWT 从前端访问​
参考设置 Third-Party 登录,您可以配置受信任的第三方 Auth 系统或自定义 JWKS。您的前端代码可以使用受信任的第三方系统颁发为用户颁发的 JWT Token、从浏览器经过火山 Supabase AI-Gateway 访问 AI 模型。​
  1. 基于 OpenAI JavaScript SDK 调用文本模型​
​
import { createClient } from '@supabase/supabase-js'​
import OpenAI from 'openai';​
​
const supabaseUrl = 'https://br-xxx.supabase.aidap.cn-beijing.volces.com'; // 火山Supabase地址​
const supabaseAnonKey = 'your-supabase-anon-key'; // 火山Supabase的anon_key​
​
// 构造 Supabase Client​
const supabase = createClient(​
supabaseUrl,​
supabaseAnonKey​
)​
​
// 通过 火山Supabase AI-Gateway 访问大模型​
async function chatWithAI() {​
// 获取已登录的APP用户的suapbase session​
const { data: { session } } = await supabase.auth.getSession();​
​
// 构造 OpenAI Client​
const openai = new OpenAI({​
baseURL: supabaseUrl + "/v1", // 基于火山Supabase地址构造得到baseURL​
apiKey: getUserThirdPartyJwtToken(), // 获取由受信任的第三方系统颁发的 JWT Token​
dangerouslyAllowBrowser: true​
});​
​
// 通过 OpenAI Client 与任意模型进行对话​
const stream = await openai.chat.completions.create({​
model: 'bytedance/doubao-seed-2.0-pro', // 指定火山Supabase支持的模型​
messages: [{ role: 'user', content: '你好' }],​
stream: true,​
});​
​
for await (const chunk of stream) {​
const content = chunk.choices[0]?.delta?.content || "";​
process.stdout.write(content);​
}​
}​
​
chatWithAI();​
​
// 照常使用Supabase的其他BaaS功能​
async function getTodos() {​
const { data, error } = await supabase​
.from('todos')​
.select('*');​
if (error) console.error(error);​
else console.log('Todos:', data);​
}​
​
getTodos();​
​
  1. 基于 Vercel JavaScript SDK 调用文本模型​
​
import { createClient } from '@supabase/supabase-js'​
import { createOpenAICompatible, generateText } from "ai";​
​
const supabaseUrl = 'https://br-xxx.supabase.aidap.cn-beijing.volces.com'; // 火山Supabase地址​
const supabaseAnonKey = 'your-supabase-anon-key'; // 火山Supabase的anon_key​
​
// 构造 Supabase Client​
const supabase = createClient(​
supabaseUrl,​
supabaseAnonKey​
)​
​
// 通过 火山Supabase AI-Gateway 访问大模型​
async function chatWithAI() {​
// 获取已登录的APP用户的suapbase session​
const { data: { session } } = await supabase.auth.getSession();​
​
// 构造 Vercel AI Client​
const gateway = createOpenAICompatible({​
baseURL: supabaseUrl + "/v1", // 基于火山Supabase地址构造得到baseURL​
apiKey: getUserThirdPartyJwtToken(), // 获取由受信任的第三方系统颁发的 JWT Token​
name: "supabase-ai-gateway", // 任意名称​
});​
​
// 通过 Vercel AI Client 与任意模型进行对话​
const result = streamText({​
model: gateway('bytedance/doubao-seed-2.0-pro'), // 指定火山Supabase支持的模型​
prompt: '你好',​
});​
​
// 流式输出​
for await (const chunk of result.textStream) {​
process.stdout.write(chunk);​
}​
}​
​
chatWithAI();​
​
// 照常使用Supabase的其他BaaS功能​
async function getTodos() {​
const { data, error } = await supabase​
.from('todos')​
.select('*');​
if (error) console.error(error);​
else console.log('Todos:', data);​
}​
​
getTodos();​
​
  1. 基于 Ark Python SDK 调用图片模型​
​
import os​
​
from arkruntime import Ark​
from supabase import create_client​
​
supabase_url = "https://br-xxx.supabase.aidap.cn-beijing.volces.com"​
supabase_anon_key = os.environ.get("SUPABASE_ANON_KEY")​
​
# 构造 Supabase Client​
supabase = create_client(supabase_url, supabase_anon_key)​
​
def generate_image() -> None:​
# 获取已登录 APP 用户的 Supabase session​
session = supabase.auth.get_session()​
client = Ark(​
base_url=supabase_url + "/v1",​
api_key=getUserThirdPartyJwtToken(),​
)​
​
response = client.images.generate(​
model="bytedance/doubao-seedream-5.0-pro",​
prompt=(​
"一张简洁的科技产品海报:银色机器人手臂在明亮工作台上绘制一朵蓝色花,"​
"写实摄影风格,高质量细节"​
),​
size="1024x1024",​
watermark=False,​
response_format="url",​
output_format="png",​
)​
print("response:", response)​
​
generate_image()​
​
  1. 基于 OpenAI Python SDK 调用图片模型​
​
import os​
​
from openai import OpenAI​
from supabase import create_client​
​
supabase_url = "https://br-xxx.supabase.aidap.cn-beijing.volces.com"​
supabase_anon_key = os.environ.get("SUPABASE_ANON_KEY")​
​
# 构造 Supabase Client​
supabase = create_client(supabase_url, supabase_anon_key)​
​
def generate_image() -> None:​
# 获取已登录 APP 用户的 Supabase session​
session = supabase.auth.get_session()​
client = OpenAI(​
base_url=supabase_url + "/v1",​
api_key=getUserThirdPartyJwtToken(),​
)​
​
response = client.images.generate(​
model="bytedance/doubao-seedream-5.0-pro",​
prompt=(​
"一张简洁的科技产品海报:银色机器人手臂在明亮工作台上绘制一朵蓝色花,"​
"写实摄影风格,高质量细节"​
),​
size="1024x1024",​
response_format="url",​
extra_body={​
"watermark": False,​
"output_format": "png",​
},​
)​
print("response:", response)​
​
generate_image()​
​
  1. 基于 Vercel Python SDK 调用图片模型​
​
import asyncio​
import os​
​
import ai​
from supabase import create_client​
​
supabase_url = "https://br-xxx.supabase.aidap.cn-beijing.volces.com"​
supabase_anon_key = os.environ.get("SUPABASE_ANON_KEY")​
​
# 构造 Supabase Client​
supabase = create_client(supabase_url, supabase_anon_key)​
​
async def generate_image() -> None:​
# 获取已登录 APP 用户的 Supabase session​
session = supabase.auth.get_session()​
provider = ai.get_provider(​
"openai",​
base_url=supabase_url + "/v1",​
api_key=getUserThirdPartyJwtToken(),​
)​
​
response = await provider.sdk_client.images.generate(​
model="bytedance/doubao-seedream-5.0-pro",​
prompt=(​
"一张简洁的科技产品海报:银色机器人手臂在明亮工作台上绘制一朵蓝色花,"​
"写实摄影风格,高质量细节"​
),​
size="1024x1024",​
response_format="url",​
extra_body={​
"watermark": False,​
"output_format": "png",​
},​
)​
print("response:", response)​
​
asyncio.run(generate_image())​
​
  1. 基于 Ark Python SDK 调用视频模型 ​
​
import os​
import time​
​
from arkruntime import Ark​
from supabase import create_client​
​
supabase_url = "https://br-xxx.supabase.aidap.cn-beijing.volces.com"​
supabase_anon_key = os.environ.get("SUPABASE_ANON_KEY")​
​
# 构造 Supabase Client​
supabase = create_client(supabase_url, supabase_anon_key)​
​
def generate_video() -> None:​
# 获取已登录 APP 用户的 Supabase session​
session = supabase.auth.get_session()​
client = Ark(​
base_url=supabase_url + "/api/v3",​
api_key=getUserThirdPartyJwtToken(),​
)​
​
response = client.content_generation.tasks.create(​
model="bytedance/doubao-seedance-2.0-fast",​
content=[​
{​
"type": "text",​
"text": (​
"一段 5 秒的产品展示视频:银色机器人手臂在明亮工作台上绘制一朵蓝色花,"​
"镜头缓慢推进,写实摄影风格,高质量细节"​
),​
}​
],​
duration=5,​
ratio="16:9",​
resolution="720p",​
watermark=False,​
)​
​
task = client.content_generation.tasks.get(response.id)​
while str(getattr(task.status, "value", task.status)) not in {​
"succeeded",​
"failed",​
"cancelled",​
}:​
time.sleep(10)​
task = client.content_generation.tasks.get(response.id)​
​
print("response:", task)​
​
generate_video()​
​
场景三:基于 service_role_key 从后端访问​
您的后端代码可以使用火山 Supabase 的 service_role_key 、从后端经过 Supabase AI-Gateway 访问 AI 模型。​
注意​
请妥善保管您的service_role_key,切勿在前端代码中暴露,以防权限被滥用。​
  1. 基于 OpenAI Python SDK 调用文本模型​
​
import os​
from openai import OpenAI​
​
supabase_url = 'https://br-xxx.supabase.aidap.cn-beijing.volces.com' # 火山Supabase地址​
supabase_service_role_key = os.environ.get('SERVICE_ROLE_KEY') # 火山Supabase的service_role_key​
​
# 构造 OpenAI Client​
openai_client = OpenAI(​
base_url=supabase_url+"/v1", # 基于火山Supabase地址构造得到baseURL​
api_key=supabase_service_role_key # 将火山Supabase Auth颁发的access_token作为apiKey​
)​
​
# 通过 OpenAI Client 与任意模型进行对话​
response = openai_client.chat.completions.create(​
model="bytedance/doubao-seed-2.0-pro", # 指定火山Supabase支持的模型​
messages=[​
{​
"role": "user", ​
"content": "你好,请简单介绍一下自己"​
}​
]​
)​
​
# 打印大模型的响应​
print("response:", response.choices[0].message.content)​
​
  1. 基于 Vercel Python SDK 调用文本模型​
​
import asyncio ​
from vercel_ai import generate_text, stream_text ​
from vercel_ai.providers import openai_compatible​
​
supabase_url = 'https://br-xxx.supabase.aidap.cn-beijing.volces.com' # 火山Supabase地址​
supabase_service_role_key = os.environ.get('SERVICE_ROLE_KEY') # 火山Supabase的service_role_key​
​
# 构造 Vercel AI Client​
gateway = openai_compatible({​
"baseURL": supabase_url+"/v1", # 基于火山Supabase地址构造得到baseURL​
"apiKey": supabase_service_role_key, # 将火山Supabase Auth颁发的access_token作为apiKey​
})​
​
async def chart_with_ai():​
# 通过 Vercel AI Client 与任意模型进行对话​
stream = stream_text(​
model=gateway("bytedance/doubao-seed-2.0-pro"), # 指定火山Supabase支持的模型​
prompt="写一个Python异步爬虫示例",​
)​
​
print("流式输出: ")​
async for chunk in stream.text_stream:​
print(chunk, end="")​
​
  1. 基于 Ark Python SDK 调用图片模型​
​
import os​
​
from arkruntime import Ark​
​
supabase_url = "https://br-xxx.supabase.aidap.cn-beijing.volces.com"​
supabase_service_role_key = os.environ.get("SERVICE_ROLE_KEY")​
​
def generate_image() -> None:​
client = Ark(​
base_url=supabase_url + "/v1",​
api_key=supabase_service_role_key,​
)​
​
response = client.images.generate(​
model="bytedance/doubao-seedream-5.0-pro",​
prompt=(​
"一张简洁的科技产品海报:银色机器人手臂在明亮工作台上绘制一朵蓝色花,"​
"写实摄影风格,高质量细节"​
),​
size="1024x1024",​
watermark=False,​
response_format="url",​
output_format="png",​
)​
print("response:", response)​
​
generate_image()​
​
  1. 基于 OpenAI Python SDK 调用图片模型​
​
import os​
​
from openai import OpenAI​
​
supabase_url = "https://br-xxx.supabase.aidap.cn-beijing.volces.com"​
supabase_service_role_key = os.environ.get("SERVICE_ROLE_KEY")​
​
def generate_image() -> None:​
client = OpenAI(​
base_url=supabase_url + "/v1",​
api_key=supabase_service_role_key,​
)​
​
response = client.images.generate(​
model="bytedance/doubao-seedream-5.0-pro",​
prompt=(​
"一张简洁的科技产品海报:银色机器人手臂在明亮工作台上绘制一朵蓝色花,"​
"写实摄影风格,高质量细节"​
),​
size="1024x1024",​
response_format="url",​
extra_body={​
"watermark": False,​
"output_format": "png",​
},​
)​
print("response:", response)​
​
generate_image()​
​
  1. 基于 Vercel Python SDK 调用图片模型​
​
import asyncio​
import os​
​
import ai​
​
supabase_url = "https://br-xxx.supabase.aidap.cn-beijing.volces.com"​
supabase_service_role_key = os.environ.get("SERVICE_ROLE_KEY")​
​
async def generate_image() -> None:​
provider = ai.get_provider(​
"openai",​
base_url=supabase_url + "/v1",​
api_key=supabase_service_role_key,​
)​
​
response = await provider.sdk_client.images.generate(​
model="bytedance/doubao-seedream-5.0-pro",​
prompt=(​
"一张简洁的科技产品海报:银色机器人手臂在明亮工作台上绘制一朵蓝色花,"​
"写实摄影风格,高质量细节"​
),​
size="1024x1024",​
response_format="url",​
extra_body={​
"watermark": False,​
"output_format": "png",​
},​
)​
print("response:", response)​
​
asyncio.run(generate_image())​
​
  1. 基于 Ark Python SDK 调用视频模型​
​
import os​
import time​
​
from arkruntime import Ark​
​
supabase_url = "https://br-xxx.supabase.aidap.cn-beijing.volces.com"​
supabase_service_role_key = os.environ.get("SERVICE_ROLE_KEY")​
​
def generate_video() -> None:​
client = Ark(​
base_url=supabase_url + "/api/v3",​
api_key=supabase_service_role_key,​
)​
​
response = client.content_generation.tasks.create(​
model="bytedance/doubao-seedance-2.0-fast",​
content=[​
{​
"type": "text",​
"text": (​
"一段 5 秒的产品展示视频:银色机器人手臂在明亮工作台上绘制一朵蓝色花,"​
"镜头缓慢推进,写实摄影风格,高质量细节"​
),​
}​
],​
duration=5,​
ratio="16:9",​
resolution="720p",​
watermark=False,​
)​
​
task = client.content_generation.tasks.get(response.id)​
while str(getattr(task.status, "value", task.status)) not in {​
"succeeded",​
"failed",​
"cancelled",​
}:​
time.sleep(10)​
task = client.content_generation.tasks.get(response.id)​
​
print("response:", task)​
​
generate_video()​
​
最近更新时间:2026.09.16 14:30:10
这个页面对您有帮助吗?
有用
有用
无用
无用