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