跳到主要内容

Supabase 教程

提示

这是由社区维护的,如果您遇到 bug,请提交 issue https://github.com/BerriAI/litellm

Supabase 是一个开源的 Firebase 替代品。使用 Postgres 数据库、身份验证、即时 API、边缘函数、实时订阅、存储和向量嵌入来启动您的项目。

使用 Supabase 记录请求并查看所有 LLM 提供商(OpenAI, Azure, Anthropic, Cohere, Replicate, PaLM)的总支出

liteLLM 提供了 success_callbacksfailure_callbacks,让您可以轻松地根据响应的状态将数据发送到特定的提供商。

在这种情况下,我们希望在成功和失败两种情况下都将请求记录到 Supabase。

创建一个 supabase 表

前往您的 Supabase 项目 > 前往 Supabase SQL 编辑器 并使用此配置创建一个新表。

注意:您可以更改表名。但不要更改列名。

create table
public.request_logs (
id bigint generated by default as identity,
created_at timestamp with time zone null default now(),
model text null default ''::text,
messages json null default '{}'::json,
response json null default '{}'::json,
end_user text null default ''::text,
status text null default ''::text,
error json null default '{}'::json,
response_time real null default '0'::real,
total_cost real null,
additional_details json null default '{}'::json,
litellm_call_id text unique,
primary key (id)
) tablespace pg_default;

使用回调

只需使用 2 行代码,即可立即使用 Supabase 查看所有提供商的成本并记录您的响应

litellm.success_callback=["supabase"]
litellm.failure_callback=["supabase"]

完整代码

from litellm import completion

## set env variables
### SUPABASE
os.environ["SUPABASE_URL"] = "your-supabase-url"
os.environ["SUPABASE_KEY"] = "your-supabase-key"

## LLM API KEY
os.environ["OPENAI_API_KEY"] = ""

# set callbacks
litellm.success_callback=["supabase"]
litellm.failure_callback=["supabase"]

# openai call
response = completion(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": "Hi 👋 - i'm openai"}],
user="ishaan22" # identify users
)

# bad call, expect this call to fail and get logged
response = completion(
model="chatgpt-test",
messages=[{"role": "user", "content": "Hi 👋 - i'm a bad call to test error logging"}]
)

附加控制

识别终端用户

user 传递给 litellm.completion 以将您的 llm 调用映射到终端用户

response = completion(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": "Hi 👋 - i'm openai"}],
user="ishaan22" # identify users
)

不同的表名

如果您修改了表名,这里是如何传递新名称的方法。

litellm.modify_integration("supabase",{"table_name": "litellm_logs"})

支持和与创始人交流