Skip to content

Commit

Permalink
Merge pull request #75 from lroolle/feat/openai-base-url
Browse files Browse the repository at this point in the history
chat: add configure openai api base and model
  • Loading branch information
gregnr authored Aug 19, 2024
2 parents 24a181d + 1d80429 commit 358ad8b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
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

0 comments on commit 358ad8b

Please sign in to comment.