-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add transformers test * updated yarn.lock * better logging when pull with ollama * added guide on transformers.js * jsconfig only applies to .js * typo
- Loading branch information
Showing
23 changed files
with
289 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,6 @@ | |
}, | ||
"include": [ | ||
"*.js", | ||
"*.mjs", | ||
"./genaiscript.d.ts" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,6 @@ | |
}, | ||
"include": [ | ||
"*.js", | ||
"*.mjs", | ||
"./genaiscript.d.ts" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
--- | ||
title: Transformer.js | ||
sidebar: | ||
order: 20 | ||
--- | ||
import { Code } from "@astrojs/starlight/components" | ||
import sampleSrc from "../../../../../packages/sample/genaisrc/summary-with-transformers.genai?raw" | ||
|
||
|
||
HuggingFace [Transformers.js](https://huggingface.co/docs/transformers.js/index) is a JavaScript library | ||
that lets you run pretrained models locally on your machine. The library uses [onnxruntime](https://onnxruntime.ai/) | ||
to leverage the CPU/GPU capabilities of your hardware. | ||
|
||
In this guide, we will show how to create [summaries](https://huggingface.co/tasks/summarization) using the [Transformers.js](https://huggingface.co/docs/transformers.js/api/pipelines#module_pipelines.SummarizationPipeline) library. | ||
|
||
:::tip | ||
|
||
Transformers.js has an extensive list of tasks available. This guide will only cover one but checkout their [documentation](https://huggingface.co/docs/transformers.js/pipelines#tasks) | ||
for more. | ||
|
||
::: | ||
|
||
## Installation | ||
|
||
Following the [installation instructions](https://huggingface.co/docs/transformers.js/installation), | ||
we add the [@xenova/transformers](https://www.npmjs.com/package/@xenova/transformers) to the current project. | ||
|
||
```bash | ||
npm install @xenova/transformers | ||
``` | ||
|
||
You can also install this library globally to be able to use on any project | ||
|
||
```bash "-g" | ||
npm install -g @xenova/transformers | ||
``` | ||
|
||
## Import the pipeline | ||
|
||
The snippet below imports the Transformers.js library and loads the summarizer pipeline and model. | ||
You can specify a model name or let the library pick the latest and greatest. | ||
|
||
```js | ||
import { pipeline } from "@xenova/transformers" | ||
const summarizer = await pipeline("summarization") | ||
``` | ||
|
||
Allocating and loading the model can take some time, | ||
so it's best to do this at the beginning of your script | ||
and only once. | ||
|
||
:::note[Migrate your script to `.mjs`] | ||
|
||
To use the `Transformers.js` library, you need to use the `.mjs` extension for your script (or `.mts` for TypeScript support). | ||
If your script is ending in `.genai.js`, rename it to `.genai.mjs`. | ||
|
||
::: | ||
|
||
## Invoke the pipeline | ||
|
||
The summarizer pipeline has a single argument, the content to summarize. It returns an array of summaries | ||
which we need to unpack to access the final summary text. This is what we do below and `summary_index` contains the summary text. | ||
|
||
```js | ||
const [summary] = await summarizer(content) | ||
// @ts-ignore | ||
const { summary_text } = summary | ||
``` | ||
|
||
## Final code | ||
|
||
The example below generates a summary of each input file | ||
before letting the model generate a full summary. | ||
|
||
<Code | ||
title="transformers.genai.mjs" | ||
code={sampleSrc} | ||
wrap={true} | ||
lang="js" | ||
/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,6 @@ | |
}, | ||
"include": [ | ||
"*.js", | ||
"*.mjs", | ||
"./genaiscript.d.ts" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,6 @@ | |
}, | ||
"include": [ | ||
"*.js", | ||
"*.mjs", | ||
"./genaiscript.d.ts" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,6 @@ | |
}, | ||
"include": [ | ||
"*.js", | ||
"*.mjs", | ||
"./genaiscript.d.ts" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,6 @@ | |
}, | ||
"include": [ | ||
"*.js", | ||
"*.mjs", | ||
"./genaiscript.d.ts" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,6 @@ | |
}, | ||
"include": [ | ||
"*.js", | ||
"*.mjs", | ||
"./genaiscript.d.ts" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,6 @@ | |
}, | ||
"include": [ | ||
"*.js", | ||
"*.mjs", | ||
"./genaiscript.d.ts" | ||
] | ||
} |
28 changes: 28 additions & 0 deletions
28
packages/sample/genaisrc/summary-with-transformers.genai.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
script({ | ||
title: "summary of summary - transformers.js", | ||
model: "ollama:phi3", | ||
files: ["src/rag/markdown.md"], | ||
tests: { | ||
files: ["src/rag/markdown.md"], | ||
keywords: ["markdown"], | ||
}, | ||
}) | ||
|
||
console.log("loading summarizer transformer") | ||
import { pipeline } from "@xenova/transformers" | ||
const summarizer = await pipeline("summarization") | ||
|
||
for (const file of env.files) { | ||
console.log(`summarizing ${file.filename}`) | ||
const [summary] = await summarizer(file.content) | ||
// @ts-ignore | ||
const { summary_text } = summary | ||
def("FILE", { | ||
filename: file.filename, | ||
// @ts-ignore | ||
content: summary_text, | ||
}) | ||
} | ||
|
||
console.log(`summarize all summaries`) | ||
$`Summarize all the contents in FILE.` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,6 @@ | |
}, | ||
"include": [ | ||
"*.js", | ||
"*.mjs", | ||
"./genaiscript.d.ts" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,6 @@ | |
}, | ||
"include": [ | ||
"*.js", | ||
"*.mjs", | ||
"./genaiscript.d.ts" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,6 @@ | |
}, | ||
"include": [ | ||
"*.js", | ||
"*.mjs", | ||
"./genaiscript.d.ts" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,6 @@ | |
}, | ||
"include": [ | ||
"*.js", | ||
"*.mjs", | ||
"./genaiscript.d.ts" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,6 @@ | |
}, | ||
"include": [ | ||
"*.js", | ||
"*.mjs", | ||
"./genaiscript.d.ts" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,6 @@ | |
}, | ||
"include": [ | ||
"*.js", | ||
"*.mjs", | ||
"./genaiscript.d.ts" | ||
] | ||
} |
Oops, something went wrong.