Spaces:
Paused
Paused
matt HOFFNER
commited on
Commit
·
b37005e
1
Parent(s):
ee8e5b5
remove token stuff
Browse files- src/pages/api/llm.js +1 -34
src/pages/api/llm.js
CHANGED
@@ -1,20 +1,10 @@
|
|
1 |
import { GoogleCustomSearch } from "openai-function-calling-tools";
|
2 |
import { LLMError, LLMStream } from './stream';
|
3 |
|
4 |
-
// @ts-expect-error
|
5 |
-
import wasm from '@dqbd/tiktoken/lite/tiktoken_bg.wasm?module';
|
6 |
-
|
7 |
-
import tiktokenModel from '@dqbd/tiktoken/encoders/cl100k_base.json';
|
8 |
-
import { Tiktoken, init } from '@dqbd/tiktoken/lite/init';
|
9 |
-
|
10 |
-
export const config = {};
|
11 |
-
|
12 |
const handler = async (req) => {
|
13 |
try {
|
14 |
const { question } = (await req.json());
|
15 |
|
16 |
-
await init((imports) => WebAssembly.instantiate(wasm, imports));
|
17 |
-
|
18 |
const googleCustomSearch = new GoogleCustomSearch({
|
19 |
apiKey: process.env.API_KEY,
|
20 |
googleCSEId: process.env.CONTEXT_KEY,
|
@@ -30,11 +20,6 @@ const handler = async (req) => {
|
|
30 |
const functions = {
|
31 |
googleCustomSearch,
|
32 |
};
|
33 |
-
const encoding = new Tiktoken(
|
34 |
-
tiktokenModel.bpe_ranks,
|
35 |
-
tiktokenModel.special_tokens,
|
36 |
-
tiktokenModel.pat_str,
|
37 |
-
);
|
38 |
|
39 |
let promptToSend = question;
|
40 |
if (!promptToSend) {
|
@@ -46,25 +31,7 @@ const handler = async (req) => {
|
|
46 |
temperatureToUse = 0.8;
|
47 |
}
|
48 |
|
49 |
-
const
|
50 |
-
|
51 |
-
let tokenCount = prompt_tokens.length;
|
52 |
-
let messagesToSend = [];
|
53 |
-
|
54 |
-
for (let i = messages.length - 1; i >= 0; i--) {
|
55 |
-
const message = messages[i];
|
56 |
-
const tokens = encoding.encode(message.content);
|
57 |
-
|
58 |
-
if (tokenCount + tokens.length + 1000 > model.tokenLimit) {
|
59 |
-
break;
|
60 |
-
}
|
61 |
-
tokenCount += tokens.length;
|
62 |
-
messagesToSend = [message, ...messagesToSend];
|
63 |
-
}
|
64 |
-
|
65 |
-
encoding.free();
|
66 |
-
|
67 |
-
const stream = await LLMStream(model, promptToSend, temperatureToUse, key, messagesToSend, functions);
|
68 |
|
69 |
return new Response(stream);
|
70 |
} catch (error) {
|
|
|
1 |
import { GoogleCustomSearch } from "openai-function-calling-tools";
|
2 |
import { LLMError, LLMStream } from './stream';
|
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
const handler = async (req) => {
|
5 |
try {
|
6 |
const { question } = (await req.json());
|
7 |
|
|
|
|
|
8 |
const googleCustomSearch = new GoogleCustomSearch({
|
9 |
apiKey: process.env.API_KEY,
|
10 |
googleCSEId: process.env.CONTEXT_KEY,
|
|
|
20 |
const functions = {
|
21 |
googleCustomSearch,
|
22 |
};
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
let promptToSend = question;
|
25 |
if (!promptToSend) {
|
|
|
31 |
temperatureToUse = 0.8;
|
32 |
}
|
33 |
|
34 |
+
const stream = await LLMStream(model, promptToSend, temperatureToUse, key, messages, functions);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
return new Response(stream);
|
37 |
} catch (error) {
|