Skip to content

Commit 358ad8b

Browse files
authored
Merge pull request supabase-community#75 from lroolle/feat/openai-base-url
chat: add configure openai api base and model
2 parents 24a181d + 1d80429 commit 358ad8b

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

apps/postgres-new/.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ NEXT_PUBLIC_IS_PREVIEW=true
44

55
OPENAI_API_KEY="<openai-api-key>"
66

7+
# Optional
8+
# OPENAI_API_BASE="<openai-compatible-api>"
9+
# OPENAI_MODEL="<model>"
10+
711
# Vercel KV (local Docker available)
812
KV_REST_API_URL="http://localhost:8080"
913
KV_REST_API_TOKEN="local_token"

apps/postgres-new/README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,20 @@ Both databases are stored locally in the browser via IndexedDB. This means that
1313

1414
Every PGlite instance runs in a Web Worker so that the main thread is not blocked.
1515

16+
1617
## AI
1718

18-
The AI component is powered by OpenAI's GPT-4o model. The project uses [Vercel's AI SDK ](https://sdk.vercel.ai/docs/introduction) to simplify message streams and tool calls.
19+
The AI component is powered by OpenAI's GPT-4o model by default. The project uses [Vercel's AI SDK](https://sdk.vercel.ai/docs/introduction) to simplify message streams and tool calls.
20+
21+
### Environment Variables
22+
23+
In addition to the required `OPENAI_API_KEY`, the following environment variables can be configured:
24+
25+
- `OPENAI_API_BASE`: (Optional) The base URL for the OpenAI API. Defaults to `https://api.openai.com/v1`.
26+
- `OPENAI_MODEL`: (Optional) The model used by the AI component. Defaults to `gpt-4o-2024-08-06`.
27+
28+
**NOTE**: The current prompts and tools are designed around the GPT-4o model. If you choose to use a different model, expect different behavior and results. Additionally, ensure that the model you select supports tool (function) call capabilities.
29+
1930

2031
## Authentication
2132

apps/postgres-new/app/api/chat/route.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { openai } from '@ai-sdk/openai'
1+
import { createOpenAI } from '@ai-sdk/openai'
22
import { Ratelimit } from '@upstash/ratelimit'
33
import { kv } from '@vercel/kv'
44
import { ToolInvocation, convertToCoreMessages, streamText } from 'ai'
@@ -27,6 +27,15 @@ type Message = {
2727
toolInvocations?: (ToolInvocation & { result: any })[]
2828
}
2929

30+
const chatModel = process.env.OPENAI_MODEL ?? 'gpt-4o-2024-08-06'
31+
32+
// Configure OpenAI client with custom base URL
33+
const openai = createOpenAI({
34+
apiKey: process.env.OPENAI_API_KEY,
35+
baseURL: process.env.OPENAI_API_BASE ?? 'https://api.openai.com/v1',
36+
compatibility: 'strict',
37+
})
38+
3039
export async function POST(req: Request) {
3140
const supabase = createClient()
3241

@@ -82,7 +91,7 @@ export async function POST(req: Request) {
8291
8392
When importing CSVs try to solve the problem yourself (eg. use a generic text column, then refine)
8493
vs. asking the user to change the CSV. No need to select rows after importing.
85-
94+
8695
You also know math. All math equations and expressions must be written in KaTex and must be wrapped in double dollar \`$$\`:
8796
- Inline: $$\\sqrt{26}$$
8897
- Multiline:
@@ -94,7 +103,7 @@ export async function POST(req: Request) {
94103
95104
Feel free to suggest corrections for suspected typos.
96105
`,
97-
model: openai('gpt-4o-2024-08-06'),
106+
model: openai(chatModel),
98107
messages: convertToCoreMessages(trimmedMessageContext),
99108
tools: convertToCoreTools(tools),
100109
async onFinish({ usage }) {

0 commit comments

Comments
 (0)