-
Notifications
You must be signed in to change notification settings - Fork 532
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #195 from Portkey-AI/feat/nomic-embeddings
feat: add nomic embeddings integration
- Loading branch information
Showing
7 changed files
with
149 additions
and
3 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
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,11 @@ | ||
import { ProviderAPIConfig } from "../types"; | ||
|
||
const NomicAPIConfig: ProviderAPIConfig = { | ||
baseURL: "https://api-atlas.nomic.ai/v1", | ||
headers: (API_KEY: string) => { | ||
return { Authorization: `Bearer ${API_KEY}` }; | ||
}, | ||
embed: "/embedding/text", | ||
}; | ||
|
||
export default NomicAPIConfig; |
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,116 @@ | ||
import { NOMIC } from "../../globals"; | ||
import { EmbedParams, EmbedResponse } from "../../types/embedRequestBody"; | ||
import { ErrorResponse, ProviderConfig } from "../types"; | ||
|
||
export const NomicEmbedConfig: ProviderConfig = { | ||
model: { | ||
param: "model", | ||
required: true, | ||
default: "nomic-embed-text-v1", | ||
}, | ||
input: { | ||
param: "texts", | ||
required: true, | ||
transform: (params: EmbedParams) => { | ||
if (Array.isArray(params.input)) { | ||
return params.input; | ||
} | ||
|
||
return [params.input]; | ||
}, | ||
}, | ||
task_type: { | ||
param: "task_type" | ||
} | ||
}; | ||
|
||
interface NomicEmbedResponse { | ||
embeddings: number[][]; | ||
model: string; | ||
usage: { | ||
prompt_tokens: number; | ||
total_tokens: number; | ||
}; | ||
} | ||
|
||
export interface NomicValidationErrorResponse { | ||
detail: { | ||
loc: Array<any>; | ||
msg: string; | ||
type: string; | ||
}[]; | ||
} | ||
|
||
export interface NomicErrorResponse { | ||
detail: string; | ||
} | ||
|
||
export const NomicErrorResponseTransform: (response: NomicValidationErrorResponse | NomicErrorResponse) => ErrorResponse = (response) => { | ||
let firstError: Record<string, any> | undefined; | ||
let errorField: string | null = null; | ||
let errorMessage: string | undefined; | ||
let errorType: string | null = null; | ||
|
||
if (Array.isArray(response.detail)) { | ||
[firstError] = response.detail; | ||
errorField = firstError?.loc?.join(".") ?? ""; | ||
errorMessage = firstError.msg; | ||
errorType = firstError.type; | ||
} else { | ||
errorMessage = response.detail; | ||
} | ||
|
||
return { | ||
error: { | ||
message: `${errorField ? `${errorField}: ` : ""}${errorMessage}`, | ||
type: errorType, | ||
param: null, | ||
code: null, | ||
}, | ||
provider: NOMIC, | ||
} as ErrorResponse; | ||
} | ||
|
||
|
||
|
||
export const NomicEmbedResponseTransform: ( | ||
response: NomicEmbedResponse | NomicValidationErrorResponse | NomicErrorResponse, | ||
responseStatus: number | ||
) => EmbedResponse | ErrorResponse = (response, responseStatus) => { | ||
if ( | ||
"detail" in response && | ||
responseStatus !== 200 && | ||
response.detail.length | ||
) { | ||
return NomicErrorResponseTransform(response); | ||
} | ||
|
||
if ("embeddings" in response) { | ||
return { | ||
object: "list", | ||
data: response.embeddings.map((d, index) => ({ | ||
object: "embedding", | ||
embedding: d, | ||
index: index, | ||
})), | ||
model: response.model, | ||
usage: { | ||
prompt_tokens: response.usage.prompt_tokens, | ||
total_tokens: response.usage.total_tokens, | ||
}, | ||
provider: NOMIC, | ||
}; | ||
} | ||
|
||
return { | ||
error: { | ||
message: `Invalid response recieved from ${NOMIC}: ${JSON.stringify( | ||
response | ||
)}`, | ||
type: null, | ||
param: null, | ||
code: null, | ||
}, | ||
provider: NOMIC, | ||
} as ErrorResponse; | ||
}; |
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,13 @@ | ||
import { ProviderConfigs } from "../types"; | ||
import NomicAPIConfig from "./api"; | ||
import { NomicEmbedConfig, NomicEmbedResponseTransform } from "./embed"; | ||
|
||
const NomicConfig: ProviderConfigs = { | ||
embed: NomicEmbedConfig, | ||
api: NomicAPIConfig, | ||
responseTransforms: { | ||
embed: NomicEmbedResponseTransform, | ||
}, | ||
}; | ||
|
||
export default NomicConfig; |