Skip to content

Commit

Permalink
update provider openai
Browse files Browse the repository at this point in the history
  • Loading branch information
xavimondev committed Jun 16, 2024
1 parent f44171c commit 74af0d9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 173 deletions.
57 changes: 26 additions & 31 deletions apps/web/app/api/code-generation/route.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { NextResponse } from 'next/server'
import { cookies } from 'next/headers'
import OpenAI from 'openai'
import { OpenAIStream, StreamingTextResponse } from 'ai'
import { streamText } from 'ai'
import { createOpenAI } from '@ai-sdk/openai'
import { PROMPT } from '@/prompt'
// import { Ratelimit } from "@upstash/ratelimit";
// import { Redis } from "@upstash/redis";
// import { TOTAL_GENERATIONS } from "@/constants";
import { PROMPT } from '@/prompt'

const openai = new OpenAI()

export const runtime = 'edge'

Expand All @@ -24,7 +22,7 @@ export const runtime = 'edge'
// : false;

export async function POST(req: Request) {
const customApiKey = cookies().get('api-key')?.value
let customApiKey = cookies().get('api-key')?.value

if (process.env.NODE_ENV === 'production' && !customApiKey) {
return NextResponse.json(
Expand All @@ -49,9 +47,8 @@ export async function POST(req: Request) {
)
}

if (customApiKey) {
// Set user's api key
openai.apiKey = customApiKey as string
if (process.env.NODE_ENV === 'development') {
customApiKey = process.env.OPENAI_API_KEY
}

// const hasCustomApiKey = customApiKey && customApiKey.trim().length > 0;
Expand All @@ -75,14 +72,16 @@ export async function POST(req: Request) {
// }
// }

const openai = createOpenAI({
apiKey: customApiKey,
compatibility: 'strict'
})

const { prompt: base64 } = await req.json()

try {
const response = await openai.chat.completions.create({
model: 'gpt-4-turbo',
stream: true,
max_tokens: 4096,
temperature: 0.2,
const result = await streamText({
model: openai('gpt-4o'),
messages: [
{
role: 'user',
Expand All @@ -92,29 +91,25 @@ export async function POST(req: Request) {
text: PROMPT
},
{
type: 'image_url',
image_url: {
url: base64
}
type: 'image',
image: base64
}
]
}
]
],
maxTokens: 4096,
temperature: 0.2
})

const stream = OpenAIStream(response)
return new StreamingTextResponse(stream)
return result.toAIStreamResponse()
} catch (error) {
if (error instanceof OpenAI.APIError) {
let errorMessage = 'An error has ocurred with API Completions. Please try again.'
if (error.code === 'invalid_api_key') {
errorMessage = 'The provided API Key is invalid. Please enter a valid API Key.'
}

const { name, status, headers } = error
return NextResponse.json({ name, status, headers, message: errorMessage }, { status })
} else {
throw error
let errorMessage = 'An error has ocurred with API Completions. Please try again.'
// @ts-ignore
if (error.status === 401) {
errorMessage = 'The provided API Key is invalid. Please enter a valid API Key.'
}
// @ts-ignore
const { name, status, headers } = error
return NextResponse.json({ name, status, headers, message: errorMessage }, { status })
}
}
2 changes: 1 addition & 1 deletion apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
},
"dependencies": {
"@ai-sdk/google": "0.0.21",
"@ai-sdk/openai": "0.0.29",
"@ai-sdk/react": "0.0.2",
"@monaco-editor/react": "4.6.0",
"@radix-ui/react-icons": "1.3.0",
Expand All @@ -31,7 +32,6 @@
"nanoid": "5.0.7",
"next": "14.1.1",
"next-themes": "0.3.0",
"openai": "4.38.1",
"postgres": "3.4.4",
"react": "18.2.0",
"react-dom": "18.2.0",
Expand Down
Loading

0 comments on commit 74af0d9

Please sign in to comment.