Skip to content
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

transformers.js support #479

Closed
wants to merge 16 commits into from
Closed
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"treesitter",
"typecheck",
"vsix",
"xpai"
"xpai",
"xenova"
],
"sarif-viewer.connectToGithubCodeScanning": "on",
"cSpell.language": "en,en-US",
Expand Down
354 changes: 274 additions & 80 deletions THIRD_PARTY_LICENSES.md

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions cspell.config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: "0.2"
ignorePaths:
- yarn.lock
dictionaryDefinitions: []
dictionaries: []
words:
- testhost
ignoreWords: []
import: []
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"description": "A CLI for GenAIScript, a generative AI scripting framework.",
"license": "MIT",
"dependencies": {
"@xenova/transformers": "^2.17.1",
"@azure/identity": "^4.3.0",
"dockerode": "^4.0.2",
"pdfjs-dist": "4.3.136",
Expand Down
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"url": "https://github.com/microsoft/genaiscript"
},
"optionalDependencies": {
"@xenova/transformers": "^2.17.1",
"pdfjs-dist": "4.3.136",
"web-tree-sitter": "^0.22.2"
},
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/testhost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ export class TestHost implements Host {
return resolve(".")
}
installFolder(): string {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method installFolder has been changed to return this.projectFolder(). Please ensure that this.projectFolder() is always defined and does not throw an error. 🧐

generated by pr-review-commit unhandled_error

throw new Error("Method not implemented.")
return this.projectFolder()
}
resolvePath(...segments: string[]): string {
throw new Error("Method not implemented.")
resolvePath(...segments: string[]) {
return resolve(...segments)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method resolvePath has been changed to return resolve(...segments). Please ensure that resolve function is always defined and does not throw an error. 🧐

generated by pr-review-commit unhandled_error

}
readSecret(name: string): Promise<string> {
throw new Error("Method not implemented.")
Expand Down
16 changes: 16 additions & 0 deletions packages/core/src/transformers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { sentiment } from "./transformers"
import { readFile } from "fs/promises"
import { beforeEach, describe, test } from "node:test"
import assert from "node:assert/strict"
import { TestHost } from "./testhost"

describe("sentiment function", () => {
beforeEach(() => {
TestHost.install()
})
test("should process love", async () => {
const result = await sentiment("i love u")
assert.strictEqual(result.label, "POSITIVE")
assert(result.score > 0.9)
})
})
13 changes: 13 additions & 0 deletions packages/core/src/transformers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { TextClassificationOutput, pipeline } from "@xenova/transformers"
import { dotGenaiscriptPath } from "./util"

export async function sentiment(text: string) {
const cache_dir = dotGenaiscriptPath("cache", "transformers")
console.log(cache_dir)
const pipe = await pipeline("sentiment-analysis", undefined, {
cache_dir,
})
const out = (await pipe(text)) as TextClassificationOutput
console.log(out)
return out[0]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sentiment function does not handle any errors that may occur during the execution of the pipeline function or the pipe function. Please add error handling to ensure the function fails gracefully. 😊

generated by pr-review-commit unhandled_error

}
Loading