chatgpt-api/src/llm.ts

75 wiersze
2.3 KiB
TypeScript
Czysty Zwykły widok Historia

2023-05-04 03:54:32 +00:00
import { ZodRawShape, ZodTypeAny, z } from 'zod'
import * as types from './types'
2023-05-26 19:06:39 +00:00
import { BaseTaskCallBuilder } from './task'
2023-05-04 03:54:32 +00:00
export abstract class BaseLLMCallBuilder<
TInput extends ZodRawShape | ZodTypeAny = ZodTypeAny,
2023-05-04 19:48:28 +00:00
TOutput extends ZodRawShape | ZodTypeAny = z.ZodType<string>,
2023-05-04 03:54:32 +00:00
TModelParams extends Record<string, any> = Record<string, any>
2023-05-26 19:06:39 +00:00
> extends BaseTaskCallBuilder<TInput, TOutput> {
2023-05-26 19:16:13 +00:00
protected _provider: string
protected _model: string
protected _modelParams: TModelParams
protected _examples: types.LLMExample[]
constructor(options: types.BaseLLMOptions<TInput, TOutput, TModelParams>) {
2023-05-26 19:06:39 +00:00
super(options)
2023-05-26 19:16:13 +00:00
this._provider = options.provider
this._model = options.model
this._modelParams = options.modelParams
this._examples = options.examples
}
2023-05-26 19:06:39 +00:00
override input<U extends ZodRawShape | ZodTypeAny = TInput>(
2023-05-04 03:54:32 +00:00
inputSchema: U
): BaseLLMCallBuilder<U, TOutput, TModelParams> {
;(
this as unknown as BaseLLMCallBuilder<U, TOutput, TModelParams>
2023-05-26 19:16:13 +00:00
)._inputSchema = inputSchema
2023-05-04 03:54:32 +00:00
return this as unknown as BaseLLMCallBuilder<U, TOutput, TModelParams>
}
2023-05-26 19:06:39 +00:00
override output<U extends ZodRawShape | ZodTypeAny = TOutput>(
2023-05-04 03:54:32 +00:00
outputSchema: U
): BaseLLMCallBuilder<TInput, U, TModelParams> {
;(
this as unknown as BaseLLMCallBuilder<TInput, U, TModelParams>
2023-05-26 19:16:13 +00:00
)._outputSchema = outputSchema
2023-05-04 03:54:32 +00:00
return this as unknown as BaseLLMCallBuilder<TInput, U, TModelParams>
}
examples(examples: types.LLMExample[]) {
2023-05-26 19:16:13 +00:00
this._examples = examples
return this
}
modelParams(params: Partial<TModelParams>) {
2023-05-26 19:16:13 +00:00
// We assume that modelParams does not include nested objects.
// If it did, we would need to do a deep merge.
this._modelParams = Object.assign({}, this._modelParams, params)
return this
}
2023-05-02 06:44:08 +00:00
// TODO
2023-05-04 03:54:32 +00:00
// abstract stream({
2023-05-02 06:44:08 +00:00
// input: TInput,
// onProgress: types.ProgressFunction
2023-05-04 03:54:32 +00:00
// }): Promise<TOutput>
}
export abstract class ChatModelBuilder<
2023-05-04 03:54:32 +00:00
TInput extends ZodRawShape | ZodTypeAny = ZodTypeAny,
2023-05-04 19:48:28 +00:00
TOutput extends ZodRawShape | ZodTypeAny = z.ZodType<string>,
2023-05-04 03:54:32 +00:00
TModelParams extends Record<string, any> = Record<string, any>
> extends BaseLLMCallBuilder<TInput, TOutput, TModelParams> {
_messages: types.ChatMessage[]
constructor(options: types.ChatModelOptions<TInput, TOutput, TModelParams>) {
super(options)
this._messages = options.messages
}
}