Skip to content

chat: add configure openai api base and model #75

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions apps/postgres-new/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ NEXT_PUBLIC_IS_PREVIEW=true

OPENAI_API_KEY="<openai-api-key>"

# Optional
# OPENAI_API_BASE="<openai-compatible-api>"
# OPENAI_MODEL="<model>"

# Vercel KV (local Docker available)
KV_REST_API_URL="http://localhost:8080"
KV_REST_API_TOKEN="local_token"
13 changes: 12 additions & 1 deletion apps/postgres-new/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,20 @@ Both databases are stored locally in the browser via IndexedDB. This means that

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


## AI

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.
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.

### Environment Variables

In addition to the required `OPENAI_API_KEY`, the following environment variables can be configured:

- `OPENAI_API_BASE`: (Optional) The base URL for the OpenAI API. Defaults to `https://api.openai.com/v1`.
- `OPENAI_MODEL`: (Optional) The model used by the AI component. Defaults to `gpt-4o-2024-08-06`.

**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.


## Authentication

Expand Down
15 changes: 12 additions & 3 deletions apps/postgres-new/app/api/chat/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { openai } from '@ai-sdk/openai'
import { createOpenAI } from '@ai-sdk/openai'
import { Ratelimit } from '@upstash/ratelimit'
import { kv } from '@vercel/kv'
import { ToolInvocation, convertToCoreMessages, streamText } from 'ai'
Expand Down Expand Up @@ -27,6 +27,15 @@ type Message = {
toolInvocations?: (ToolInvocation & { result: any })[]
}

const chatModel = process.env.OPENAI_MODEL ?? 'gpt-4o-2024-08-06'

// Configure OpenAI client with custom base URL
const openai = createOpenAI({
apiKey: process.env.OPENAI_API_KEY,
baseURL: process.env.OPENAI_API_BASE ?? 'https://api.openai.com/v1',
compatibility: 'strict',
})

export async function POST(req: Request) {
const supabase = createClient()

Expand Down Expand Up @@ -82,7 +91,7 @@ export async function POST(req: Request) {

When importing CSVs try to solve the problem yourself (eg. use a generic text column, then refine)
vs. asking the user to change the CSV. No need to select rows after importing.

You also know math. All math equations and expressions must be written in KaTex and must be wrapped in double dollar \`$$\`:
- Inline: $$\\sqrt{26}$$
- Multiline:
Expand All @@ -94,7 +103,7 @@ export async function POST(req: Request) {

Feel free to suggest corrections for suspected typos.
`,
model: openai('gpt-4o-2024-08-06'),
model: openai(chatModel),
messages: convertToCoreMessages(trimmedMessageContext),
tools: convertToCoreTools(tools),
async onFinish({ usage }) {
Expand Down