-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'justinmilner1-MakeArrowsScroll' into preview
- Loading branch information
Showing
32 changed files
with
2,984 additions
and
2,468 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,27 +1,30 @@ | ||
{ | ||
"root": true, | ||
"parser": "@typescript-eslint/parser", | ||
"parserOptions": { | ||
"ecmaVersion": 6, | ||
"sourceType": "module" | ||
}, | ||
"extends": ["plugin:require-extensions/recommended"], | ||
"plugins": ["@typescript-eslint", "import", "require-extensions"], | ||
"rules": { | ||
"quotes": ["warn", "double", {}], | ||
"import/extensions": ["error", "always", { | ||
"js": "never", | ||
"jsx": "never", | ||
"ts": "never", | ||
"tsx": "never" | ||
}], | ||
"@typescript-eslint/naming-convention": "warn", | ||
"@typescript-eslint/semi": "warn", | ||
"curly": "warn", | ||
"eqeqeq": "warn", | ||
"no-throw-literal": "warn", | ||
"semi": "off" | ||
}, | ||
"ignorePatterns": ["out", "dist", "**/*.d.ts"] | ||
} | ||
|
||
"root": true, | ||
"parser": "@typescript-eslint/parser", | ||
"parserOptions": { | ||
"ecmaVersion": 6, | ||
"sourceType": "module" | ||
}, | ||
"extends": ["plugin:require-extensions/recommended"], | ||
"plugins": ["@typescript-eslint", "import", "require-extensions"], | ||
"rules": { | ||
"quotes": ["warn", "double", {}], | ||
"import/extensions": [ | ||
"error", | ||
"always", | ||
{ | ||
"js": "never", | ||
"jsx": "never", | ||
"ts": "never", | ||
"tsx": "never" | ||
} | ||
], | ||
"@typescript-eslint/naming-convention": "warn", | ||
"@typescript-eslint/semi": "warn", | ||
"curly": "warn", | ||
"eqeqeq": "warn", | ||
"no-throw-literal": "warn", | ||
"semi": "off" | ||
}, | ||
"ignorePatterns": ["out", "dist", "**/*.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
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
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { Response } from "node-fetch"; | ||
import { withExponentialBackoff } from "../../util/withExponentialBackoff.js"; | ||
import BaseEmbeddingsProvider, { | ||
IBaseEmbeddingsProvider, | ||
} from "./BaseEmbeddingsProvider.js"; | ||
import { | ||
EmbedContentRequest, | ||
EmbedContentResponse, | ||
} from "@google/generative-ai"; | ||
|
||
/** | ||
* [View the Gemini Text Embedding docs.](https://ai.google.dev/gemini-api/docs/models/gemini#text-embedding-and-embedding) | ||
*/ | ||
class GeminiEmbeddingsProvider extends BaseEmbeddingsProvider { | ||
static maxBatchSize = 2048; | ||
|
||
static defaultOptions = { | ||
apiBase: "https://generativelanguage.googleapis.com/v1/", | ||
model: "models/text-embedding-004", | ||
}; | ||
|
||
get urlPath(): string { | ||
return `${this.options.model}:embedContent`; | ||
} | ||
|
||
async getSingleBatchEmbedding(batch: string[]) { | ||
const body: EmbedContentRequest = { | ||
content: { | ||
/** | ||
* Listed as optional in the [docs](https://ai.google.dev/api/rest/v1/Content) | ||
* but is required in the interface. | ||
*/ | ||
role: "user", | ||
parts: batch.map((part) => ({ text: part })), | ||
}, | ||
}; | ||
|
||
const fetchWithBackoff = () => | ||
withExponentialBackoff<Response>(() => | ||
this.fetch(new URL(this.urlPath, this.options.apiBase), { | ||
method: "POST", | ||
body: JSON.stringify(body), | ||
headers: { | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
"x-goog-api-key": this.options.apiKey, | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
"Content-Type": "application/json", | ||
}, | ||
}), | ||
); | ||
|
||
const resp = await fetchWithBackoff(); | ||
|
||
if (!resp.ok) { | ||
throw new Error(await resp.text()); | ||
} | ||
|
||
const data = (await resp.json()) as EmbedContentResponse; | ||
|
||
return data.embedding.values; | ||
} | ||
|
||
async embed(chunks: string[]) { | ||
const batches = GeminiEmbeddingsProvider.getBatchedChunks(chunks); | ||
|
||
return await Promise.all( | ||
batches.map((batch) => this.getSingleBatchEmbedding(batch)), | ||
); | ||
} | ||
} | ||
|
||
export default GeminiEmbeddingsProvider; |
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
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
Oops, something went wrong.