Skip to content

Commit

Permalink
fix: support config bitdeer embedding api key via env (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mini256 committed Apr 17, 2024
1 parent 7470fdf commit 3773635
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 17 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"license": "Apache-2.0",
"dependencies": {
"@hookform/resolvers": "^3.3.4",
"@llamaindex/env": "^0.0.7",
"@mdx-js/loader": "^3.0.0",
"@mdx-js/react": "^3.0.0",
"@next/mdx": "^14.1.0",
Expand Down
21 changes: 18 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions src/app/api/test/bitdeer-embedding/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {getEmbedding} from "@/lib/llamaindex/converters/embedding";
import {BitdeerEmbedding, BitdeerEmbeddingModelType} from "@/lib/llamaindex/embeddings/BitdeerEmbedding";
import {baseRegistry} from "@/rag-spec/base";
import {getFlow} from "@/rag-spec/createFlow";
import {NextRequest, NextResponse} from 'next/server';
Expand All @@ -9,10 +8,7 @@ const flow = await getFlow(baseRegistry);
export async function GET (req: NextRequest) {
const url = new URL(req.url);
const input = url.searchParams.get('input') || 'I want a database to replace MySQL.';
const bitdeerEmbedding = getEmbedding(flow, 'bitdeer', {
model: BitdeerEmbeddingModelType.MISTRAL_EMBED_LARGE,
apiSecretAccessKey: process.env.BITDEER_API_SECRET_ACCESS_KEY!
});
const bitdeerEmbedding = getEmbedding(flow, 'bitdeer', {});

const embedding = await bitdeerEmbedding.getQueryEmbedding(input);

Expand Down
17 changes: 8 additions & 9 deletions src/lib/llamaindex/embeddings/BitdeerEmbedding.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import {getEnv} from "@llamaindex/env";
import {BaseEmbedding} from "llamaindex";
import util from "node:util";


export enum BitdeerEmbeddingModelType {
MISTRAL_EMBED_LARGE = "mxbai-embed-large",
}

export class BitdeerEmbedding extends BaseEmbedding {
baseURL: string = "https://www.bitdeer.ai/public/v1";
model: BitdeerEmbeddingModelType;
model: BitdeerEmbeddingModelType = BitdeerEmbeddingModelType.MISTRAL_EMBED_LARGE;

apiSecretAccessKey: string;
apiSecretAccessKey?: string = getEnv("BITDEER_API_SECRET_ACCESS_KEY");
requestTimeout: number = 60 * 1000; // Default is 60 seconds

constructor(init?: Partial<BitdeerEmbedding>) {
super();
this.model = BitdeerEmbeddingModelType.MISTRAL_EMBED_LARGE;
if (typeof init?.apiSecretAccessKey !== "string") {
throw new Error("Bitdeer API secret access key is required.");
Object.assign(this, init);
if (!this.apiSecretAccessKey) {
throw new Error("BITDEER_API_SECRET_ACCESS_KEY is required.");
}
this.apiSecretAccessKey = init?.apiSecretAccessKey;
}

private async getBitdeerEmbedding(input: string) {
Expand All @@ -31,10 +30,10 @@ export class BitdeerEmbedding extends BaseEmbedding {
const response = await fetch(url, {
body: JSON.stringify(payload),
method: "POST",
// signal: AbortSignal.timeout(this.requestTimeout),
signal: AbortSignal.timeout(this.requestTimeout),
headers: {
"Content-Type": "application/json",
"X-Api-Key": this.apiSecretAccessKey,
"X-Api-Key": this.apiSecretAccessKey!,
},
});

Expand Down

0 comments on commit 3773635

Please sign in to comment.