1"use strict";(globalThis.webpackChunkmy_website=globalThis.webpackChunkmy_website||[]).push([[5947],{20351(e,s,a){a.r(s),a.d(s,{assets:()=>i,contentTitle:()=>r,default:()=>u,frontMatter:()=>o,metadata:()=>n,toc:()=>c});let n=JSON.parse('{"type":"mdx","permalink":"/observability/supabase_integration","source":"@site/src/pages/observability/supabase_integration.md","title":"Supabase Tutorial","description":"Supabase is an open source Firebase alternative.","frontMatter":{},"unlisted":false}');var t=a(74848),l=a(28453);let o={},r="Supabase Tutorial",i={},c=[{value:"Use Supabase to log requests and see total spend across all LLM Providers (OpenAI, Azure, Anthropic, Cohere, Replicate, PaLM)",id:"use-supabase-to-log-requests-and-see-total-spend-across-all-llm-providers-openai-azure-anthropic-cohere-replicate-palm",level:2},{value:"Create a supabase table",id:"create-a-supabase-table",level:3},{value:"Use Callbacks",id:"use-callbacks",level:3},{value:"Additional Controls",id:"additional-controls",level:3}];function d(e){let s={a:"a",code:"code",h1:"h1",h2:"h2",h3:"h3",header:"header",p:"p",pre:"pre",strong:"strong",...(0,l.R)(),...e.components};return(0,t.jsxs)(t.Fragment,{children:[(0,t.jsx)(s.header,{children:(0,t.jsx)(s.h1,{id:"supabase-tutorial",children:"Supabase Tutorial"})}),"\n",(0,t.jsxs)(s.p,{children:[(0,t.jsx)(s.a,{href:"https://supabase.com/",children:"Supabase"})," is an open source Firebase alternative.\nStart your project with a Postgres database, Authentication, instant APIs, Edge Functions, Realtime subscriptions, Storage, and Vector embeddings."]}),"\n",(0,t.jsx)(s.h2,{id:"use-supabase-to-log-requests-and-see-total-spend-across-all-llm-providers-openai-azure-anthropic-cohere-replicate-palm",children:"Use Supabase to log requests and see total spend across all LLM Providers (OpenAI, Azure, Anthropic, Cohere, Replicate, PaLM)"}),"\n",(0,t.jsxs)(s.p,{children:["liteLLM provides ",(0,t.jsx)(s.code,{children:"success_callbacks"})," and ",(0,t.jsx)(s.code,{children:"failure_callbacks"}),", making it easy for you to send data to a particular provider depending on the status of your responses."]}),"\n",(0,t.jsx)(s.p,{children:"In this case, we want to log requests to Supabase in both scenarios - when it succeeds and fails."}),"\n",(0,t.jsx)(s.h3,{id:"create-a-supabase-table",children:"Create a supabase table"}),"\n",(0,t.jsxs)(s.p,{children:["Go to your Supabase project > go to the ",(0,t.jsx)(s.a,{href:"https://supabase.com/dashboard/projects",children:"Supabase SQL Editor"})," and create a new table with this configuration."]}),"\n",(0,t.jsx)(s.p,{children:"Note: You can change the table name. Just don't change the column names."}),"\n",(0,t.jsx)(s.pre,{children:(0,t.jsx)(s.code,{className:"language-sql",children:"create table\n public.request_logs (\n id bigint generated by default as identity,\n created_at timestamp with time zone null default now(),\n model text null default ''::text,\n messages json null default '{}'::json,\n response json null default '{}'::json,\n end_user text null default ''::text,\n error json null default '{}'::json,\n response_time real null default '0'::real,\n total_cost real null,\n additional_details json null default '{}'::json,\n constraint request_logs_pkey primary key (id)\n ) tablespace pg_default;\n"})}),"\n",(0,t.jsx)(s.h3,{id:"use-callbacks",children:"Use Callbacks"}),"\n",(0,t.jsxs)(s.p,{children:["Use just 2 lines of code, to instantly see costs and log your responses ",(0,t.jsx)(s.strong,{children:"across all providers"})," with Supabase:"]}),"\n",(0,t.jsx)(s.pre,{children:(0,t.jsx)(s.code,{children:'litellm.success_callback=["supabase"]\nlitellm.failure_callback=["supabase"]\n'})}),"\n",(0,t.jsx)(s.p,{children:"Complete code"}),"\n",(0,t.jsx)(s.pre,{children:(0,t.jsx)(s.code,{className:"language-python",children:'from litellm import completion\n\n## set env variables\n### SUPABASE\nos.environ["SUPABASE_URL"] = "your-supabase-url" \nos.environ["SUPABASE_KEY"] = "your-supabase-key" \n\n## LLM API KEY\nos.environ["OPENAI_API_KEY"] = ""\n\n# set callbacks\nlitellm.success_callback=["supabase"]\nlitellm.failure_callback=["supabase"]\n\n#openai call\nresponse = completion(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hi \u{1F44B} - i\'m openai"}]) \n\n#bad call\nresponse = completion(model="chatgpt-test", messages=[{"role": "user", "content": "Hi \u{1F44B} - i\'m a bad call to test error logging"}]) \n'})}
1),"\n",(0,t.jsx)(s.h3,{id:"additional-controls",children:"Additional Controls"}),"\n",(0,t.jsx)(s.p,{children:(0,t.jsx)(s.strong,{children:"Different Table name"})}),"\n",(0,t.jsx)(s.p,{children:"If you modified your table name, here's how to pass the new name."}),"\n",(0,t.jsx)(s.pre,{children:(0,t.jsx)(s.code,{className:"language-python",children:'litellm.modify_integration("supabase",{"table_name": "litellm_logs"})\n'})}),"\n",(0,t.jsx)(s.p,{children:(0,t.jsx)(s.strong,{children:"Identify end-user"})}),"\n",(0,t.jsx)(s.p,{children:"Here's how to map your llm call to an end-user"}),"\n",(0,t.jsx)(s.pre,{children:(0,t.jsx)(s.code,{className:"language-python",children:'litellm.identify({"end_user": "[email protected]"})\n'})})]})}function u(e={}){let{wrapper:s}={...(0,l.R)(),...e.components};return s?(0,t.jsx)(s,{...e,children:(0,t.jsx)(d,{...e})}):d(e)}},28453(e,s,a){a.d(s,{R:()=>o,x:()=>r});var n=a(96540);let t={},l=n.createContext(t);function o(e){let s=n.useContext(l);return n.useMemo(function(){return"function"==typeof e?e(s):{...s,...e}},[s,e])}function r(e){let s;return s=e.disableParentContext?"function"==typeof e.components?e.components(t):e.components||t:o(e.components),n.createElement(l.Provider,{value:s},e.children)}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.