Conditionally instantiate OpenAI
This commit is contained in:
parent
28f6310fe1
commit
7e1c2b57fb
@ -4,12 +4,14 @@ import OpenAI from 'openai';
|
|||||||
import { createStreamableValue, render } from 'ai/rsc';
|
import { createStreamableValue, render } from 'ai/rsc';
|
||||||
import { kv } from '@vercel/kv';
|
import { kv } from '@vercel/kv';
|
||||||
import { Ratelimit } from '@upstash/ratelimit';
|
import { Ratelimit } from '@upstash/ratelimit';
|
||||||
import { HAS_VERCEL_KV } from '@/site/config';
|
import { AI_TEXT_GENERATION_ENABLED, HAS_VERCEL_KV } from '@/site/config';
|
||||||
|
|
||||||
const RATE_LIMIT_IDENTIFIER = 'openai-image-query';
|
const RATE_LIMIT_IDENTIFIER = 'openai-image-query';
|
||||||
const RATE_LIMIT_MAX_QUERIES_PER_HOUR = 100;
|
const RATE_LIMIT_MAX_QUERIES_PER_HOUR = 100;
|
||||||
|
|
||||||
const provider = new OpenAI({ apiKey: process.env.OPENAI_SECRET_KEY });
|
const provider = AI_TEXT_GENERATION_ENABLED
|
||||||
|
? new OpenAI({ apiKey: process.env.OPENAI_SECRET_KEY })
|
||||||
|
: undefined;
|
||||||
|
|
||||||
// Allows 100 requests per hour
|
// Allows 100 requests per hour
|
||||||
const ratelimit = HAS_VERCEL_KV
|
const ratelimit = HAS_VERCEL_KV
|
||||||
@ -33,31 +35,33 @@ export const streamOpenAiImageQuery = async (
|
|||||||
|
|
||||||
const stream = createStreamableValue('');
|
const stream = createStreamableValue('');
|
||||||
|
|
||||||
render({
|
if (provider) {
|
||||||
provider,
|
render({
|
||||||
model: 'gpt-4-vision-preview',
|
provider,
|
||||||
messages: [{
|
model: 'gpt-4-vision-preview',
|
||||||
'role': 'user',
|
messages: [{
|
||||||
'content': [
|
'role': 'user',
|
||||||
{
|
'content': [
|
||||||
'type': 'text',
|
{
|
||||||
'text': query,
|
'type': 'text',
|
||||||
}, {
|
'text': query,
|
||||||
'type': 'image_url',
|
}, {
|
||||||
'image_url': {
|
'type': 'image_url',
|
||||||
'url': imageBase64,
|
'image_url': {
|
||||||
|
'url': imageBase64,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
],
|
||||||
],
|
}],
|
||||||
}],
|
text: ({ content, done }): any => {
|
||||||
text: ({ content, done }): any => {
|
if (done) {
|
||||||
if (done) {
|
stream.done(content);
|
||||||
stream.done(content);
|
} else {
|
||||||
} else {
|
stream.update(content);
|
||||||
stream.update(content);
|
}
|
||||||
}
|
},
|
||||||
},
|
});
|
||||||
});
|
}
|
||||||
|
|
||||||
return stream.value;
|
return stream.value;
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user