kopia lustrzana https://github.com/transitive-bullshit/chatgpt-api
old-agentic-v1^2
rodzic
3b82add15d
commit
4174d7cdbf
25
src/llm.ts
25
src/llm.ts
|
@ -8,12 +8,18 @@ export abstract class BaseLLMCallBuilder<
|
|||
TOutput extends ZodRawShape | ZodTypeAny = z.ZodType<string>,
|
||||
TModelParams extends Record<string, any> = Record<string, any>
|
||||
> extends BaseTaskCallBuilder<TInput, TOutput> {
|
||||
protected _options: types.BaseLLMOptions<TInput, TOutput, TModelParams>
|
||||
protected _provider: string
|
||||
protected _model: string
|
||||
protected _modelParams: TModelParams
|
||||
protected _examples: types.LLMExample[]
|
||||
|
||||
constructor(options: types.BaseLLMOptions<TInput, TOutput, TModelParams>) {
|
||||
super(options)
|
||||
|
||||
this._options = options
|
||||
this._provider = options.provider
|
||||
this._model = options.model
|
||||
this._modelParams = options.modelParams
|
||||
this._examples = options.examples
|
||||
}
|
||||
|
||||
override input<U extends ZodRawShape | ZodTypeAny = TInput>(
|
||||
|
@ -21,7 +27,7 @@ export abstract class BaseLLMCallBuilder<
|
|||
): BaseLLMCallBuilder<U, TOutput, TModelParams> {
|
||||
;(
|
||||
this as unknown as BaseLLMCallBuilder<U, TOutput, TModelParams>
|
||||
)._options.input = inputSchema
|
||||
)._inputSchema = inputSchema
|
||||
return this as unknown as BaseLLMCallBuilder<U, TOutput, TModelParams>
|
||||
}
|
||||
|
||||
|
@ -30,22 +36,19 @@ export abstract class BaseLLMCallBuilder<
|
|||
): BaseLLMCallBuilder<TInput, U, TModelParams> {
|
||||
;(
|
||||
this as unknown as BaseLLMCallBuilder<TInput, U, TModelParams>
|
||||
)._options.output = outputSchema
|
||||
)._outputSchema = outputSchema
|
||||
return this as unknown as BaseLLMCallBuilder<TInput, U, TModelParams>
|
||||
}
|
||||
|
||||
examples(examples: types.LLMExample[]) {
|
||||
this._options.examples = examples
|
||||
this._examples = examples
|
||||
return this
|
||||
}
|
||||
|
||||
modelParams(params: Partial<TModelParams>) {
|
||||
// We assume that modelParams does not include nested objects; if it did, we would need to do a deep merge...
|
||||
this._options.modelParams = Object.assign(
|
||||
{},
|
||||
this._options.modelParams,
|
||||
params
|
||||
)
|
||||
// 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
|
||||
}
|
||||
|
||||
|
|
|
@ -43,11 +43,11 @@ export class OpenAIChatModelBuilder<
|
|||
override async call(
|
||||
input?: types.ParsedData<TInput>
|
||||
): Promise<types.ParsedData<TOutput>> {
|
||||
if (this._options.input) {
|
||||
if (this._inputSchema) {
|
||||
const inputSchema =
|
||||
this._options.input instanceof z.ZodType
|
||||
? this._options.input
|
||||
: z.object(this._options.input)
|
||||
this._inputSchema instanceof z.ZodType
|
||||
? this._inputSchema
|
||||
: z.object(this._inputSchema)
|
||||
|
||||
// TODO: handle errors gracefully
|
||||
input = inputSchema.parse(input)
|
||||
|
@ -66,9 +66,9 @@ export class OpenAIChatModelBuilder<
|
|||
})
|
||||
.filter((message) => message.content)
|
||||
|
||||
if (this._options.examples?.length) {
|
||||
if (this._examples?.length) {
|
||||
// TODO: smarter example selection
|
||||
for (const example of this._options.examples) {
|
||||
for (const example of this._examples) {
|
||||
messages.push({
|
||||
role: 'system',
|
||||
content: `Example input: ${example.input}\n\nExample output: ${example.output}`
|
||||
|
@ -76,11 +76,11 @@ export class OpenAIChatModelBuilder<
|
|||
}
|
||||
}
|
||||
|
||||
if (this._options.output) {
|
||||
if (this._outputSchema) {
|
||||
const outputSchema =
|
||||
this._options.output instanceof z.ZodType
|
||||
? this._options.output
|
||||
: z.object(this._options.output)
|
||||
this._outputSchema instanceof z.ZodType
|
||||
? this._outputSchema
|
||||
: z.object(this._outputSchema)
|
||||
|
||||
const { node } = zodToTs(outputSchema)
|
||||
|
||||
|
@ -116,15 +116,15 @@ export class OpenAIChatModelBuilder<
|
|||
console.log(messages)
|
||||
const completion = await this._client.createChatCompletion({
|
||||
model: defaultOpenAIModel, // TODO: this shouldn't be necessary but TS is complaining
|
||||
...this._options.modelParams,
|
||||
...this._outputSchema,
|
||||
messages
|
||||
})
|
||||
|
||||
if (this._options.output) {
|
||||
if (this._outputSchema) {
|
||||
const outputSchema =
|
||||
this._options.output instanceof z.ZodType
|
||||
? this._options.output
|
||||
: z.object(this._options.output)
|
||||
this._outputSchema instanceof z.ZodType
|
||||
? this._outputSchema
|
||||
: z.object(this._outputSchema)
|
||||
|
||||
let output: any = completion.message.content
|
||||
console.log('===')
|
||||
|
|
16
src/task.ts
16
src/task.ts
|
@ -6,16 +6,22 @@ export abstract class BaseTaskCallBuilder<
|
|||
TInput extends ZodRawShape | ZodTypeAny = ZodTypeAny,
|
||||
TOutput extends ZodRawShape | ZodTypeAny = z.ZodTypeAny
|
||||
> {
|
||||
protected _options: types.BaseTaskOptions<TInput, TOutput>
|
||||
protected _inputSchema: TInput
|
||||
protected _outputSchema: TOutput
|
||||
protected _timeoutMs: number
|
||||
protected _retryConfig: types.RetryConfig
|
||||
|
||||
constructor(options: types.BaseTaskOptions<TInput, TOutput>) {
|
||||
this._options = options
|
||||
this._inputSchema = options.inputSchema
|
||||
this._outputSchema = options.outputSchema
|
||||
this._timeoutMs = options.timeoutMs
|
||||
this._retryConfig = options.retryConfig
|
||||
}
|
||||
|
||||
input<U extends ZodRawShape | ZodTypeAny = TInput>(
|
||||
inputSchema: U
|
||||
): BaseTaskCallBuilder<U, TOutput> {
|
||||
;(this as unknown as BaseTaskCallBuilder<U, TOutput>)._options.input =
|
||||
;(this as unknown as BaseTaskCallBuilder<U, TOutput>)._inputSchema =
|
||||
inputSchema
|
||||
return this as unknown as BaseTaskCallBuilder<U, TOutput>
|
||||
}
|
||||
|
@ -23,13 +29,13 @@ export abstract class BaseTaskCallBuilder<
|
|||
output<U extends ZodRawShape | ZodTypeAny = TOutput>(
|
||||
outputSchema: U
|
||||
): BaseTaskCallBuilder<TInput, U> {
|
||||
;(this as unknown as BaseTaskCallBuilder<TInput, U>)._options.output =
|
||||
;(this as unknown as BaseTaskCallBuilder<TInput, U>)._outputSchema =
|
||||
outputSchema
|
||||
return this as unknown as BaseTaskCallBuilder<TInput, U>
|
||||
}
|
||||
|
||||
retry(retryConfig: types.RetryConfig) {
|
||||
this._options.retryConfig = retryConfig
|
||||
this._retryConfig = retryConfig
|
||||
return this
|
||||
}
|
||||
|
||||
|
|
|
@ -28,8 +28,8 @@ export interface BaseTaskOptions<
|
|||
TInput extends ZodRawShape | ZodTypeAny = ZodTypeAny,
|
||||
TOutput extends ZodRawShape | ZodTypeAny = z.ZodType<string>
|
||||
> {
|
||||
input?: TInput
|
||||
output?: TOutput
|
||||
inputSchema?: TInput
|
||||
outputSchema?: TOutput
|
||||
|
||||
timeoutMs?: number
|
||||
retryConfig?: RetryConfig
|
||||
|
@ -37,6 +37,7 @@ export interface BaseTaskOptions<
|
|||
// TODO
|
||||
// caching config
|
||||
// logging config
|
||||
// reference to agentic context
|
||||
}
|
||||
|
||||
export interface BaseLLMOptions<
|
||||
|
|
Ładowanie…
Reference in New Issue