Skip to content

Commit 2c31cd0

Browse files
committed
Added functions for LLM & embeddings to be generated using huggingface
1 parent 219317c commit 2c31cd0

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

ai-assistant/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ Temporary Items
3434
scratch/
3535

3636
test.ts
37+
credentials.ts

ai-assistant/src/core/embeddings/minilml6.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { IHttp } from "@rocket.chat/apps-engine/definition/accessors";
2+
import { HF_TOKEN } from "../../credentials";
23
import { IEmbeddingModel } from "./embeddings.types";
34

45
export class MiniLML6 implements IEmbeddingModel {
@@ -10,7 +11,32 @@ export class MiniLML6 implements IEmbeddingModel {
1011
this.http = http;
1112
}
1213

14+
async fromHuggingFace(text: string): Promise<number[] | null> {
15+
const res = await this.http.post(
16+
`https://api-inference.huggingface.co/pipeline/feature-extraction/sentence-transformers/all-MiniLM-L6-v2`,
17+
{
18+
headers: {
19+
accept: "application/json",
20+
"Content-Type": "application/json",
21+
authorization: "Bearer " + HF_TOKEN,
22+
},
23+
data: {
24+
inputs: [text],
25+
options: {
26+
wait_for_model: true,
27+
},
28+
},
29+
}
30+
);
31+
if (!res || res.statusCode !== 200) return null;
32+
33+
const data = res.data[0] as number[];
34+
return data;
35+
}
36+
1337
async generate(text: string): Promise<number[] | null> {
38+
return await this.fromHuggingFace(text);
39+
1440
const res = await this.http.post(this.baseURL, {
1541
headers: {
1642
accept: "application/json",

ai-assistant/src/core/llm/llama3_70B.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { IHttp } from "@rocket.chat/apps-engine/definition/accessors";
22

3+
import { HF_TOKEN } from "../../credentials";
34
import { Prompt } from "../prompt/prompt";
45
import { ILLMModel } from "./llm.types";
56

@@ -12,20 +13,40 @@ export class Llama3_70B implements ILLMModel {
1213
this.http = http;
1314
}
1415

16+
async fromHuggingFace(prompt: Prompt): Promise<string | null> {
17+
const url = `https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.1/v1/chat/completions`;
18+
const res = await this.http.post(url, {
19+
headers: {
20+
"Content-Type": "application/json",
21+
authorization: "Bearer " + HF_TOKEN,
22+
},
23+
data: {
24+
messages: prompt.messages,
25+
model: "mistralai/Mistral-7B-Instruct-v0.1",
26+
stream: false,
27+
},
28+
});
29+
if (!res.content) return null;
30+
31+
const message = JSON.parse(res.content).choices[0].message.content;
32+
return message;
33+
}
34+
1535
async ask(prompt: Prompt): Promise<string | null> {
36+
return await this.fromHuggingFace(prompt);
37+
1638
const url = `${this.baseURL}/chat/completions`;
1739
const res = await this.http.post(url, {
1840
headers: {
1941
"Content-Type": "application/json",
2042
},
2143
data: {
22-
model: this.model,
23-
temprature: 0,
2444
messages: prompt.messages,
2545
},
2646
});
2747
if (!res.content) return null;
2848

49+
// @ts-ignore
2950
const message = JSON.parse(res.content).choices[0].message.content;
3051
return message;
3152
}

0 commit comments

Comments
 (0)