82 lines
2.6 KiB
TypeScript
82 lines
2.6 KiB
TypeScript
import { ProviderID, type ModelID } from "../schema"
|
|
import * as OpenAICompatibleChat from "../protocols/openai-compatible-chat"
|
|
import type { RouteDefaultsInput } from "../route/client"
|
|
import { AuthOptions, type ProviderAuthOption } from "../route/auth-options"
|
|
import type { ProviderPackage } from "../provider-package"
|
|
import { profiles, type OpenAICompatibleProfile } from "./openai-compatible-profile"
|
|
|
|
export const id = ProviderID.make("openai-compatible")
|
|
|
|
type GenericModelOptions = RouteDefaultsInput &
|
|
ProviderAuthOption<"optional"> & {
|
|
readonly provider?: string
|
|
readonly baseURL: string
|
|
}
|
|
|
|
export interface Settings extends ProviderPackage.Settings {
|
|
readonly apiKey?: string
|
|
readonly baseURL: string
|
|
readonly provider?: string
|
|
}
|
|
|
|
export type FamilyModelOptions = RouteDefaultsInput &
|
|
ProviderAuthOption<"optional"> & {
|
|
readonly baseURL?: string
|
|
}
|
|
|
|
export const routes = [OpenAICompatibleChat.route]
|
|
|
|
export const configure = (input: GenericModelOptions) => {
|
|
const provider = input.provider ?? "openai-compatible"
|
|
const { provider: _, baseURL, apiKey: _apiKey, auth: _auth, ...rest } = input
|
|
const route = OpenAICompatibleChat.route.with({
|
|
...rest,
|
|
provider,
|
|
endpoint: { baseURL },
|
|
auth: AuthOptions.bearer(input, []),
|
|
})
|
|
return {
|
|
id: ProviderID.make(provider),
|
|
model: (modelID: string | ModelID) => route.model({ id: modelID, provider: ProviderID.make(provider) }),
|
|
configure,
|
|
}
|
|
}
|
|
|
|
const define = (profile: OpenAICompatibleProfile) => {
|
|
const configureProfile = (input: FamilyModelOptions = {}) => {
|
|
const facade = configure({
|
|
...input,
|
|
baseURL: input.baseURL ?? profile.baseURL,
|
|
provider: profile.provider,
|
|
})
|
|
return {
|
|
id: ProviderID.make(profile.provider),
|
|
model: facade.model,
|
|
configure: configureProfile,
|
|
}
|
|
}
|
|
return configureProfile()
|
|
}
|
|
|
|
export const provider = {
|
|
id,
|
|
configure,
|
|
}
|
|
|
|
export const model: ProviderPackage.Definition<Settings>["model"] = (modelID, settings) =>
|
|
configure({
|
|
apiKey: settings.apiKey,
|
|
baseURL: settings.baseURL,
|
|
headers: settings.headers === undefined ? undefined : { ...settings.headers },
|
|
http: settings.body === undefined ? undefined : { body: { ...settings.body } },
|
|
limits: settings.limits,
|
|
provider: settings.provider,
|
|
}).model(modelID)
|
|
|
|
export const baseten = define(profiles.baseten)
|
|
export const cerebras = define(profiles.cerebras)
|
|
export const deepinfra = define(profiles.deepinfra)
|
|
export const deepseek = define(profiles.deepseek)
|
|
export const fireworks = define(profiles.fireworks)
|
|
export const groq = define(profiles.groq)
|
|
export const togetherai = define(profiles.togetherai)
|