2023-05-04 03:54:32 +00:00
|
|
|
import { ZodRawShape, ZodTypeAny, z } from 'zod'
|
2023-05-02 02:32:03 +00:00
|
|
|
|
|
|
|
import * as types from './types'
|
2023-05-26 19:06:39 +00:00
|
|
|
import { BaseTaskCallBuilder } from './task'
|
2023-05-02 02:32:03 +00:00
|
|
|
|
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[]
|
2023-05-02 02:32:03 +00:00
|
|
|
|
|
|
|
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-02 02:32:03 +00:00
|
|
|
}
|
|
|
|
|
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-02 02:32:03 +00:00
|
|
|
}
|
|
|
|
|
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>
|
2023-05-02 02:32:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
examples(examples: types.LLMExample[]) {
|
2023-05-26 19:16:13 +00:00
|
|
|
this._examples = examples
|
2023-05-02 02:32:03 +00:00
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
2023-05-25 21:59:22 +00:00
|
|
|
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)
|
2023-05-25 21:59:22 +00:00
|
|
|
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>
|
2023-05-02 02:32:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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>
|
2023-05-02 02:32:03 +00:00
|
|
|
> extends BaseLLMCallBuilder<TInput, TOutput, TModelParams> {
|
|
|
|
_messages: types.ChatMessage[]
|
|
|
|
|
|
|
|
constructor(options: types.ChatModelOptions<TInput, TOutput, TModelParams>) {
|
|
|
|
super(options)
|
|
|
|
|
|
|
|
this._messages = options.messages
|
|
|
|
}
|
|
|
|
}
|