Skip to content

Commit 7429ea3

Browse files
committed
feat(js): add 4 new LLMs
1 parent b9ce969 commit 7429ea3

File tree

5 files changed

+96
-79
lines changed

5 files changed

+96
-79
lines changed

dashboard/lib/utils.ts

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -151,47 +151,21 @@ const huggingFaceStream = async (modelUrl: string, payload: HuggingFacePayload):
151151
controller.close();
152152
},
153153
});
154-
155-
// const encoder = new TextEncoder();
156-
157-
// payload.stream = true;
158-
// const response = await fetch(modelUrl, {
159-
// headers: {
160-
// 'Content-Type': 'application/json',
161-
// // Authorization: `Bearer ${process.env.HUGGINGFACE_API_KEY ?? ''}`,
162-
// Accept: 'text/event-stream'
163-
// },
164-
// method: 'POST',
165-
// body: JSON.stringify(payload),
166-
// });
167-
168-
// return new ReadableStream({
169-
// async start(controller) {
170-
// const decoder = new TextDecoder();
171-
// const parser = createParser((event: ParsedEvent | ReconnectInterval) => {
172-
// console.log('event', event);
173-
174-
// if (event.type === 'event') {
175-
// let data = JSON.parse(event.data)
176-
// data = data?.token?.text
177-
// if (data?.token?.special) return null;
178-
// if (data === null) return null;
179-
// console.log('data', data);
180-
// const queue = encoder.encode(data)
181-
// controller.enqueue(queue);
182-
// return data;
183-
// }
184-
// return null;
185-
// });
186-
187-
// for await (const buffer of response.body as any) {
188-
// const text = decoder.decode(buffer);
189-
// console.log('text', text);
190-
// parser.feed(text);
191-
// }
192-
// },
193-
// });
194154
}
195155

196-
export { generateText, huggingFaceStream }
156+
const openaiCompletion = async (url: string, model: string, prompt: string, maxTokens = 100) => fetch(`${url}/v1/completions`, {
157+
method: "POST",
158+
headers: {
159+
"Content-Type": "application/json",
160+
Authorization: "Bearer EMPTY",
161+
},
162+
body: JSON.stringify({
163+
model: model,
164+
prompt: prompt,
165+
max_tokens: maxTokens,
166+
stream: false,
167+
})
168+
}).then((response) => response.json()).then((response) => response.choices?.[0]?.text || response)
169+
170+
export { generateText, huggingFaceStream, openaiCompletion }
197171

dashboard/pages/api/chat.ts

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { OpenAIPayload, OpenAIStream, generateText, huggingFaceStream } from '@/lib/utils'
1+
import { OpenAIPayload, OpenAIStream, generateText, huggingFaceStream, openaiCompletion } from '@/lib/utils'
22
import cors from '@/utils/cors'
33
import * as Sentry from '@sentry/nextjs'
44
import { defaultChatSystem } from '../../utils/constants'
@@ -31,11 +31,12 @@ const track = async (userId: string, model: string) => {
3131
}
3232
type LLM = 'openai/gpt-4' | 'openai/gpt-3.5-turbo' | 'openai/gpt-3.5-turbo-16k' | 'tiiuae/falcon-7b' | 'google/bison' | 'bigscience/bloomz-7b1'
3333

34+
3435
interface RequestPayload {
3536
prompt: string
3637
history: Chat[]
3738
system?: string
38-
model: LLM
39+
model: LLM | string
3940
stream: boolean
4041
max_new_tokens?: number;
4142
stop?: string[];
@@ -103,7 +104,7 @@ const handler = async (req: Request, res: Response): Promise<Response> => {
103104
})
104105
}
105106

106-
console.log('streaming chat with model', model)
107+
console.log('generating text with model', model, 'stream', stream, 'max_new_tokens', max_new_tokens)
107108

108109
const messages: Chat[] = [
109110
{
@@ -129,37 +130,7 @@ const handler = async (req: Request, res: Response): Promise<Response> => {
129130
let readableStream: ReadableStream
130131

131132

132-
// TODO: not supported atm
133-
if (model === 'tiiuae/falcon-7b') {
134-
const url = 'http://34.127.99.191:9090'
135-
if (!stream) {
136-
const res = await generateText(url, {
137-
inputs: prompt,
138-
stream: false,
139-
parameters: {
140-
max_new_tokens: max_new_tokens || 1000,
141-
return_full_text: false,
142-
stop: stop || [],
143-
},
144-
})
145-
console.log('res', res)
146-
return new Response(JSON.stringify({
147-
generated_text: res.generated_text
148-
}), {
149-
status: 200,
150-
})
151-
}
152-
readableStream = await huggingFaceStream(url, {
153-
inputs: prompt,
154-
stream: true,
155-
parameters: {
156-
// { model_id: "tiiuae/falcon-7b", revision: None, sharded: None, num_shard: Some(1), quantize: None, trust_remote_code: false, max_concurrent_requests: 128, max_best_of: 2, max_stop_sequences: 4, max_input_length: 1000, max_total_tokens: 1512, max_batch_size: None, waiting_served_ratio: 1.2, max_batch_total_tokens: 32000, max_waiting_tokens: 20, port: 80, shard_uds_path: "/tmp/text-generation-server", master_addr: "localhost", master_port: 29500, huggingface_hub_cache: Some("/data"), weights_cache_override: None, disable_custom_kernels: false, json_output: false, otlp_endpoint: None, cors_allow_origin: [], watermark_gamma: None, watermark_delta: None, env: false }
157-
max_new_tokens: max_new_tokens || 1000,
158-
return_full_text: false,
159-
stop: stop || [],
160-
}
161-
})
162-
} else if (model === 'bigscience/bloomz-7b1') {
133+
if (model === 'bigscience/bloomz-7b1') {
163134
const url = 'https://api.differentai.xyz'
164135
if (!stream) {
165136
const res = await generateText(url, {
@@ -249,6 +220,38 @@ const handler = async (req: Request, res: Response): Promise<Response> => {
249220
})
250221
}
251222
readableStream = await OpenAIStream(payload)
223+
} else if (model === 'NousResearch/Nous-Hermes-13b') {
224+
const text = await openaiCompletion(
225+
'https://6976-35-203-131-148.ngrok-free.app', 'NousResearch/Nous-Hermes-13b', prompt, max_new_tokens || 100)
226+
return new Response(JSON.stringify({
227+
generated_text: text || ''
228+
}), {
229+
status: 200,
230+
})
231+
} else if (model === 'TheBloke/mpt-7b-chat-GGML') {
232+
const text = await openaiCompletion(
233+
'https://3e85-34-139-159-248.ngrok-free.app', 'TheBloke/mpt-7b-chat-GGML', prompt, max_new_tokens || 100)
234+
return new Response(JSON.stringify({
235+
generated_text: text || ''
236+
}), {
237+
status: 200,
238+
})
239+
} else if (model === 'TheBloke/Nous-Hermes-13B-GGML') {
240+
const text = await openaiCompletion(
241+
'https://28b6-2a01-e0a-3ee-1cb0-505a-5158-140c-80f8.ngrok-free.app', 'TheBloke/Nous-Hermes-13B-GGML', prompt, max_new_tokens || 100)
242+
return new Response(JSON.stringify({
243+
generated_text: text || ''
244+
}), {
245+
status: 200,
246+
})
247+
} else if (model === 'nomic-ai/ggml-replit-code-v1-3b') {
248+
const text = await openaiCompletion(
249+
'https://430699a51145-11712225068814657101.ngrok-free.app', 'nomic-ai/ggml-replit-code-v1-3b', prompt, max_new_tokens || 100)
250+
return new Response(JSON.stringify({
251+
generated_text: text || ''
252+
}), {
253+
status: 200,
254+
})
252255
} else {
253256
if (!stream) {
254257
payload.stream = stream

sdk/embedbase-js/src/EmbedbaseClient.ts

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,8 @@ export default class EmbedbaseClient {
738738
return data.results
739739
}
740740

741+
742+
741743
/**
742744
* Retrieves a list of LLM models available for use.
743745
* @returns {Promise<LLMDescription[]>} - Returns a list of available LLM models.
@@ -747,12 +749,14 @@ export default class EmbedbaseClient {
747749
* console.log(models);
748750
*/
749751
public async getModels(): Promise<LLMDescription[]> {
752+
const { data: otherModels } = await listModels();
750753
const models: LLMDescription[] = [
751754
{ name: "openai/gpt-4", description: "OpenAI's GPT-4 model" },
752755
{ name: "openai/gpt-3.5-turbo-16k", description: "OpenAI's GPT-3.5 Turbo 16k model" },
756+
{ name: "openai/gpt-3.5-turbo", description: "OpenAI's GPT-3.5 Turbo model" },
753757
{ name: "google/bison", description: "Google's Bison model" },
754-
// { name: "tiiuae/falcon-7b", description: "Tiiuae's Falcon 7b model" },
755-
// { name: "bigscience/bloomz-7b1", description: "BigScience's Bloomz 7b1 model" },
758+
{ name: "bigscience/bloomz-7b1", description: "BigScience's Bloomz 7b1 model" },
759+
...otherModels.map((model: any) => ({ name: model.model, description: JSON.stringify(model) })),
756760
];
757761
return models;
758762
}
@@ -771,7 +775,7 @@ export default class EmbedbaseClient {
771775
* const generatedText = await gpt4.generateText("input-text");
772776
* console.log(generatedText);
773777
*/
774-
public useModel(modelName: LLM): {
778+
public useModel(modelName: LLM | string): {
775779
generateText: (input: string, options?: GenerateOptions) => Promise<string>;
776780
streamText: (input: string, options?: GenerateOptions) => AsyncGenerator<string>;
777781
} {
@@ -825,3 +829,32 @@ export default class EmbedbaseClient {
825829
}
826830
};
827831
}
832+
833+
834+
/**
835+
* Lists all models available on the platform.
836+
* @async
837+
* @returns {Promise<Object>} A promise that resolves to an object containing a list of models.
838+
* @example
839+
* listModels().then(models => console.log(models));
840+
*/
841+
async function listModels() {
842+
const response = await fetch("https://api.airtable.com/v0/appwJMZ6IAUnKpSwV/all", {
843+
headers: {
844+
Authorization: "Bearer patBrBkdsFw0ArVlF.89a5669f5fd05d20e1d0f77216d072d929b13a215c0471b9a1a2d764537cbe8d"
845+
}
846+
});
847+
const data = await response.json();
848+
return {
849+
data: data.records
850+
.filter((record) => record.fields["url"] !== undefined)
851+
.map((record) => ({
852+
id: record.id,
853+
object: "model",
854+
owned_by: record.fields["contact"] || "anonymous",
855+
permission: ["read"],
856+
createdTime: record.createdTime,
857+
...record.fields
858+
}))
859+
}
860+
}

sdk/embedbase-js/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export interface ClientDatasets {
7171
export type LLM = 'openai/gpt-4' | 'openai/gpt-3.5-turbo-16k' | 'google/bison' | 'bigscience/bloomz-7b1' //| 'tiiuae/falcon-7b'
7272

7373
export interface LLMDescription {
74-
name: LLM
74+
name: LLM | string
7575
description: string
7676
}
7777

sdk/embedbase-js/test/client.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,13 @@ test('should be able to generate text sync', async () => {
396396
expect(res).toBeDefined()
397397
}, TIMEOUT)
398398

399+
test('should be able to list models', async () => {
400+
const models = await embedbase.getModels()
401+
console.log(JSON.stringify(models.map((m: any) => m.name), null, 2))
402+
expect(models).toBeDefined()
403+
}, TIMEOUT)
404+
405+
399406

400407
test('should be able to replace my documents', async () => {
401408
const documents = [

0 commit comments

Comments
 (0)