-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
js_release_alpha/1.5.12 #1314
js_release_alpha/1.5.12 #1314
Changes from 2 commits
e1c382f
f1a0e62
7a7fbd6
70d3075
d79530d
5fb0238
4cfeb3f
06d18ae
104ab4b
b11fc24
43d96cd
3da858c
f8bd662
a2370bc
dd055c4
fbaa9c4
6045ba0
f25236c
dff2238
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,26 +5,56 @@ let CohereAiApi: any; | |
export class CohereEmbeddingFunction implements IEmbeddingFunction { | ||
private api_key: string; | ||
private model: string; | ||
private cohereAiApi?: any; | ||
|
||
constructor({ cohere_api_key, model }: { cohere_api_key: string, model?: string }) { | ||
try { | ||
// eslint-disable-next-line global-require,import/no-extraneous-dependencies | ||
CohereAiApi = require("cohere-ai"); | ||
CohereAiApi.init(cohere_api_key); | ||
} catch { | ||
throw new Error( | ||
"Please install the cohere-ai package to use the CohereEmbeddingFunction, `npm install -S cohere-ai`" | ||
); | ||
} | ||
// we used to construct the client here, but we need to async import the types | ||
// for the openai npm package, and the constructor can not be async | ||
this.api_key = cohere_api_key; | ||
this.model = model || "large"; | ||
} | ||
|
||
public async generate(texts: string[]) { | ||
const response = await CohereAiApi.embed({ | ||
|
||
if (this.cohereAiApi === undefined) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would move this part into the import function. Because it is import logic and not generation logic. It would make the generate function cleaner if you would do await CohereEmbeddingFunction.import();
const response = await this.cohereAiApi.embed({ The import function would then immediately return if the package is already imported. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you could move it to another function called "loadClient" private async loadClient(){
if(this.cohereAiApi) return;
try {
// eslint-disable-next-line global-require,import/no-extraneous-dependencies
const { cohere } = await CohereEmbeddingFunction.import();
CohereAiApi = cohere;
CohereAiApi.init(this.api_key);
} catch (_a) {
// @ts-ignore
if (_a.code === 'MODULE_NOT_FOUND') {
throw new Error("Please install the cohere-ai package to use the CohereEmbeddingFunction, `npm install -S cohere-ai`");
}
throw _a; // Re-throw other errors
}
this.cohereAiApi = CohereAiApi;
}
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i like this change - thanks! will implement |
||
|
||
try { | ||
// eslint-disable-next-line global-require,import/no-extraneous-dependencies | ||
const { cohere } = await CohereEmbeddingFunction.import(); | ||
CohereAiApi = cohere; | ||
CohereAiApi.init(this.api_key); | ||
} catch (_a) { | ||
// @ts-ignore | ||
if (_a.code === 'MODULE_NOT_FOUND') { | ||
throw new Error("Please install the cohere-ai package to use the CohereEmbeddingFunction, `npm install -S cohere-ai`"); | ||
} | ||
throw _a; // Re-throw other errors | ||
} | ||
this.cohereAiApi = CohereAiApi; | ||
|
||
} | ||
|
||
const response = await this.cohereAiApi.embed({ | ||
texts: texts, | ||
model: this.model, | ||
}); | ||
return response.body.embeddings; | ||
} | ||
|
||
/** @ignore */ | ||
static async import(): Promise<{ | ||
// @ts-ignore | ||
cohere: typeof import("cohere-ai"); | ||
}> { | ||
try { | ||
// @ts-ignore | ||
const { default: cohere } = await import("cohere-ai"); | ||
return { cohere }; | ||
} catch (e) { | ||
throw new Error( | ||
"Please install cohere-ai as a dependency with, e.g. `yarn add cohere-ai`" | ||
); | ||
} | ||
} | ||
|
||
} |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should commit this file, if you remove yarn.lock