Skip to content

Commit

Permalink
refactor!: use baseURL instead of url
Browse files Browse the repository at this point in the history
  • Loading branch information
kwaa committed Dec 6, 2024
1 parent b606e64 commit 23eaa48
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 24 deletions.
2 changes: 1 addition & 1 deletion packages/embed/src/utils/embed-many.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface EmbedManyResult {
}

export const embedMany = async (options: EmbedManyOptions): Promise<EmbedManyResult> =>
await fetch(options.url, {
await fetch(new URL('embeddings', options.baseURL), {
body: requestBody(options),
headers: requestHeaders({
'Content-Type': 'application/json',
Expand Down
2 changes: 1 addition & 1 deletion packages/embed/src/utils/embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export interface EmbedResult {
}

export const embed = async (options: EmbedOptions): Promise<EmbedResult> =>
await fetch(options.url, {
await fetch(new URL('embeddings', options.baseURL), {
body: requestBody(options),
headers: requestHeaders({
'Content-Type': 'application/json',
Expand Down
2 changes: 1 addition & 1 deletion packages/model/src/utils/list-models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface ListModelsResponse {
}

export const listModels = async (options: ListModelsOptions): Promise<Model[]> =>
await fetch(options.url, {
await fetch(new URL('models', options.baseURL), {
headers: requestHeaders({
'Content-Type': 'application/json',
...options.headers,
Expand Down
4 changes: 2 additions & 2 deletions packages/model/src/utils/retrieve-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { type CommonRequestOptions, requestHeaders } from '@xsai/shared'

import type { Model } from '../types/model'

export interface RetrieveModelOptions extends Omit<CommonRequestOptions, 'model'> {}
export interface RetrieveModelOptions extends CommonRequestOptions {}

export const retrieveModel = async (options: RetrieveModelOptions): Promise<Model> =>
await fetch(options.url, {
await fetch(new URL(`models/${options.model}`, options.baseURL), {
headers: requestHeaders({
'Content-Type': 'application/json',
...options.headers,
Expand Down
6 changes: 4 additions & 2 deletions packages/model/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { ollama } from '@xsai/providers'
import { describe, expect, it } from 'vitest'

import { listModels, retrieveModel } from '../src'

describe('@xsai/model', () => {
it('listModels', async () => {
const models = await listModels({
url: new URL('models', 'http://localhost:11434/v1/'),
...ollama.models(),
})

const llama = models.find(({ id }) => id === 'llama3.2:latest')!
Expand All @@ -17,7 +18,8 @@ describe('@xsai/model', () => {

it('retrieveModel', async () => {
const llama = await retrieveModel({
url: new URL('models/llama3.2:latest', 'http://localhost:11434/v1/'),
...ollama.models(),
model: 'llama3.2:latest',
})

expect(llama.id).toBe('llama3.2:latest')
Expand Down
10 changes: 7 additions & 3 deletions packages/providers/src/local/ollama.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,21 @@ type OllamaEmbeddingsModel = 'all-minilm' | 'mxbai-embed-large' | 'nomic-embed-t
export interface OllamaProvider {
chat: (model: OllamaChatModel) => CommonRequestOptions
embeddings: (model: OllamaEmbeddingsModel) => CommonRequestOptions
models: () => CommonProviderOptions
}

export const createOllama = (userOptions?: CommonProviderOptions): OllamaProvider => {
const options: CommonProviderOptions = {
baseURL: new URL('http://localhost:11434/v1/'),
...userOptions,
baseURL: userOptions?.baseURL ?? new URL('http://localhost:11434/v1/'),
}

const result = (model: string) => generateCRO(model, options)

return {
chat: model => generateCRO(model, 'chat/completions', options),
embeddings: model => generateCRO(model, 'embeddings', options),
chat: result,
embeddings: result,
models: () => options,
}
}

Expand Down
10 changes: 7 additions & 3 deletions packages/providers/src/remote/google-generative-ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,20 @@ type GoogleGenerativeAIEmbeddingsModel = 'text-embedding-004' | ({} & string)
export interface GoogleGenerativeAIProvider {
chat: (model: GoogleGenerativeAIChatModel) => CommonRequestOptions
embeddings: (model: GoogleGenerativeAIEmbeddingsModel) => CommonRequestOptions
models: () => CommonProviderOptions
}

export const createGoogleGenerativeAI = (userOptions: Omit<CommonProviderOptions, 'apiKey'> & Required<Pick<CommonProviderOptions, 'apiKey'>>): GoogleGenerativeAIProvider => {
const options: CommonProviderOptions = {
baseURL: new URL('https://generativelanguage.googleapis.com/v1beta/openai/'),
...userOptions,
baseURL: userOptions.baseURL ?? new URL('https://generativelanguage.googleapis.com/v1beta/openai/'),
}

const result = (model: string) => generateCRO(model, options)

return {
chat: model => generateCRO(model, 'chat/completions', options),
embeddings: model => generateCRO(model, 'embeddings', options),
chat: result,
embeddings: result,
models: () => options,
}
}
10 changes: 7 additions & 3 deletions packages/providers/src/remote/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,20 @@ type OpenAIEmbeddingsModel = 'text-embedding-3-large' | 'text-embedding-3-small'
export interface OpenAIProvider {
chat: (model: OpenAIChatModel) => CommonRequestOptions
embeddings: (model: OpenAIEmbeddingsModel) => CommonRequestOptions
models: () => CommonProviderOptions
}

export const createOpenAI = (userOptions: Omit<CommonProviderOptions, 'apiKey'> & Required<Pick<CommonProviderOptions, 'apiKey'>>): OpenAIProvider => {
const options: CommonProviderOptions = {
baseURL: new URL('https://openai.com/v1/'),
...userOptions,
baseURL: userOptions.baseURL ?? new URL('https://openai.com/v1/'),
}

const result = (model: string) => generateCRO(model, options)

return {
chat: model => generateCRO(model, 'chat/completions', options),
embeddings: model => generateCRO(model, 'embeddings', options),
chat: result,
embeddings: result,
models: () => options,
}
}
4 changes: 1 addition & 3 deletions packages/providers/src/types/common-provider-options.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type { CommonRequestOptions } from '@xsai/shared'

export interface CommonProviderOptions extends Pick<CommonRequestOptions, 'apiKey' | 'headers'> {
baseURL?: URL
}
export interface CommonProviderOptions extends Omit<CommonRequestOptions, 'model'> {}
4 changes: 2 additions & 2 deletions packages/providers/src/utils/generate-cro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import type { CommonRequestOptions } from '@xsai/shared'

import type { CommonProviderOptions } from '../types/common-provider-options'

export const generateCRO = (model: string, path: string, { apiKey, baseURL, headers }: CommonProviderOptions): CommonRequestOptions => ({
export const generateCRO = (model: string, { apiKey, baseURL, headers }: CommonProviderOptions): CommonRequestOptions => ({
apiKey,
baseURL,
headers,
model,
url: new URL(path, baseURL),
})
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { objCamelToSnake, requestBody, requestHeaders } from '@xsai/shared'

import type { ChatCompletionOptions, Tool } from '../types'

export const chatCompletion = async <T extends ChatCompletionOptions>(options: T) => await fetch(options.url, {
export const chatCompletion = async <T extends ChatCompletionOptions>(options: T) => await fetch(new URL('chat/completions', options.baseURL), {
body: requestBody({
...objCamelToSnake(options),
tools: (options.tools as Tool[] | undefined)?.map(tool => ({
Expand Down
7 changes: 5 additions & 2 deletions packages/shared/src/types/common-request-options.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
export interface CommonRequestOptions {
abortSignal?: AbortSignal
apiKey?: string
/**
* @example `https://openai.com/v1/`
* @remarks make sure the baseURL ends with a slash.
*/
baseURL: `${string}/` | URL
/** @default `undefined` */
headers?: Headers | Record<string, string>
/** @example `gpt-4o` */
model: string
/** @example `https://openai.com/v1/chat/completions` */
url: string | URL
}

0 comments on commit 23eaa48

Please sign in to comment.