Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add OpenAI embedding compatibility * Use OPENAI_API_KEY by default * lint * Add default OpenAI URL replace `authorization` by `apiKey` * Add a note in readme --------- Co-authored-by: Nathan Sarrazin <[email protected]>
- Loading branch information
Showing
4 changed files
with
61 additions
and
1 deletion.
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
51 changes: 51 additions & 0 deletions
51
src/lib/server/embeddingEndpoints/openai/embeddingEndpoints.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,51 @@ | ||
import { z } from "zod"; | ||
import type { EmbeddingEndpoint, Embedding } from "../embeddingEndpoints"; | ||
import { chunk } from "$lib/utils/chunk"; | ||
import { OPENAI_API_KEY } from "$env/static/private"; | ||
|
||
export const embeddingEndpointOpenAIParametersSchema = z.object({ | ||
weight: z.number().int().positive().default(1), | ||
model: z.any(), | ||
type: z.literal("openai"), | ||
url: z.string().url().default("https://api.openai.com/v1/embeddings"), | ||
apiKey: z.string().default(OPENAI_API_KEY), | ||
}); | ||
|
||
export async function embeddingEndpointOpenAI( | ||
input: z.input<typeof embeddingEndpointOpenAIParametersSchema> | ||
): Promise<EmbeddingEndpoint> { | ||
const { url, model, apiKey } = embeddingEndpointOpenAIParametersSchema.parse(input); | ||
|
||
const maxBatchSize = model.maxBatchSize || 100; | ||
|
||
return async ({ inputs }) => { | ||
const requestURL = new URL(url); | ||
|
||
const batchesInputs = chunk(inputs, maxBatchSize); | ||
|
||
const batchesResults = await Promise.all( | ||
batchesInputs.map(async (batchInputs) => { | ||
const response = await fetch(requestURL, { | ||
method: "POST", | ||
headers: { | ||
Accept: "application/json", | ||
"Content-Type": "application/json", | ||
...(apiKey ? { Authorization: `Bearer ${apiKey}` } : {}), | ||
}, | ||
body: JSON.stringify({ input: batchInputs, model: model.name }), | ||
}); | ||
|
||
const embeddings: Embedding[] = []; | ||
const responseObject = await response.json(); | ||
for (const embeddingObject of responseObject.data) { | ||
embeddings.push(embeddingObject.embedding); | ||
} | ||
return embeddings; | ||
}) | ||
); | ||
|
||
const flatAllEmbeddings = batchesResults.flat(); | ||
|
||
return flatAllEmbeddings; | ||
}; | ||
} |
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