matt HOFFNER
cleanup
29df9bc
raw
history blame
2.5 kB
import { OpenAIError, OpenAIStream } from '../../stream';
import wasm from '../../../node_modules/@dqbd/tiktoken/lite/tiktoken_bg.wasm?module';
import tiktokenModel from '@dqbd/tiktoken/encoders/cl100k_base.json';
import { Tiktoken, init } from '@dqbd/tiktoken/lite/init';
export const DEFAULT_SYSTEM_PROMPT =
process.env.NEXT_PUBLIC_DEFAULT_SYSTEM_PROMPT ||
"You are ChatGPT, a large language model trained by OpenAI. Follow the user's instructions carefully. Respond using markdown.";
export const OPENAI_API_HOST =
process.env.OPENAI_API_HOST || 'https://api.openai.com';
export const DEFAULT_TEMPERATURE =
parseFloat(process.env.NEXT_PUBLIC_DEFAULT_TEMPERATURE || "1");
export const OPENAI_API_TYPE =
process.env.OPENAI_API_TYPE || 'openai';
export const OPENAI_API_VERSION =
process.env.OPENAI_API_VERSION || '2023-03-15-preview';
export const OPENAI_ORGANIZATION =
process.env.OPENAI_ORGANIZATION || '';
export const AZURE_DEPLOYMENT_ID =
process.env.AZURE_DEPLOYMENT_ID || '';
export const config = {
runtime: 'edge',
};
const handler = async (req) => {
try {
const { model, messages, key, prompt, temperature } = (await req.json());
await init((imports) => WebAssembly.instantiate(wasm, imports));
const encoding = new Tiktoken(
tiktokenModel.bpe_ranks,
tiktokenModel.special_tokens,
tiktokenModel.pat_str,
);
let promptToSend = prompt;
if (!promptToSend) {
promptToSend = DEFAULT_SYSTEM_PROMPT;
}
let temperatureToUse = temperature;
if (temperatureToUse == null) {
temperatureToUse = DEFAULT_TEMPERATURE;
}
const prompt_tokens = encoding.encode(promptToSend);
let tokenCount = prompt_tokens.length;
let messagesToSend = [];
for (let i = messages.length - 1; i >= 0; i--) {
const message = messages[i];
const tokens = encoding.encode(message.content);
if (tokenCount + tokens.length + 1000 > model.tokenLimit) {
break;
}
tokenCount += tokens.length;
messagesToSend = [message, ...messagesToSend];
}
encoding.free();
const stream = await OpenAIStream(model, promptToSend, temperatureToUse, key, messagesToSend);
return new Response(stream);
} catch (error) {
console.error(error);
if (error instanceof OpenAIError) {
return new Response('Error', { status: 500, statusText: error.message });
} else {
return new Response('Error', { status: 500 });
}
}
};
export default handler;