Spaces:
Paused
Paused
File size: 2,090 Bytes
f6091f7 916e00a f6091f7 916e00a 2cd9790 eef247f 916e00a 29df9bc 916e00a bf2fc3e 916e00a 29df9bc 916e00a f6091f7 916e00a 2cd9790 916e00a 2cd9790 916e00a 2016044 916e00a 2016044 916e00a 2016044 916e00a 2016044 916e00a 2016044 916e00a 29df9bc 916e00a 2016044 916e00a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
import { GoogleCustomSearch } from "openai-function-calling-tools";
import { LLMError, LLMStream } from './stream';
// @ts-expect-error
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 config = {
};
const handler = async (req) => {
try {
const { question } = (await req.json());
await init((imports) => WebAssembly.instantiate(wasm, imports));
const googleCustomSearch = new GoogleCustomSearch({
apiKey: process.env.API_KEY,
googleCSEId: process.env.CONTEXT_KEY,
});
const messages = [
{
role: "user",
content: question,
},
];
const functions = {
googleCustomSearch,
};
const encoding = new Tiktoken(
tiktokenModel.bpe_ranks,
tiktokenModel.special_tokens,
tiktokenModel.pat_str,
);
let promptToSend = question;
if (!promptToSend) {
promptToSend = "";
}
let temperatureToUse = temperature;
if (temperatureToUse == null) {
temperatureToUse = 0.8;
}
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 LLMStream(model, promptToSend, temperatureToUse, key, messagesToSend, functions);
return new Response(stream);
} catch (error) {
console.error(error);
if (error instanceof LLMError) {
return new Response('Error', { status: 500, statusText: error.message });
} else {
return new Response('Error', { status: 500 });
}
}
};
export default handler; |