kopia lustrzana https://github.com/transitive-bullshit/chatgpt-api
feat: refactor to task
rodzic
bcbc2f48e8
commit
3b82add15d
|
@ -16,22 +16,22 @@ async function main() {
|
||||||
// .call()
|
// .call()
|
||||||
// console.log(ex0)
|
// console.log(ex0)
|
||||||
|
|
||||||
// const ex1 = await $.gpt4(`give me fake data`)
|
const ex1 = await $.gpt4(`give me fake data`)
|
||||||
// .output(z.object({ foo: z.string(), bar: z.number() }))
|
.output(z.object({ foo: z.string(), bar: z.number() }))
|
||||||
// // .output(z.string())
|
// .output(z.string())
|
||||||
// // .retry({ attempts: 3 })
|
// .retry({ attempts: 3 })
|
||||||
// .call()
|
.call()
|
||||||
// console.log(ex1)
|
console.log(ex1)
|
||||||
|
|
||||||
const getBoolean = $.gpt4(`are you {{mood}}?`)
|
// const getBoolean = $.gpt4(`are you {{mood}}?`)
|
||||||
.input(z.object({ mood: z.string() }))
|
// .input(z.object({ mood: z.string() }))
|
||||||
.output(z.boolean())
|
// .output(z.boolean())
|
||||||
|
|
||||||
const results = await Promise.all([
|
// const results = await Promise.all([
|
||||||
getBoolean.call({ mood: 'happy' }),
|
// getBoolean.call({ mood: 'happy' }),
|
||||||
getBoolean.call({ mood: 'sad' })
|
// getBoolean.call({ mood: 'sad' })
|
||||||
])
|
// ])
|
||||||
console.log(results)
|
// console.log(results)
|
||||||
}
|
}
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|
20
src/llm.ts
20
src/llm.ts
|
@ -1,19 +1,22 @@
|
||||||
import { ZodRawShape, ZodTypeAny, z } from 'zod'
|
import { ZodRawShape, ZodTypeAny, z } from 'zod'
|
||||||
|
|
||||||
import * as types from './types'
|
import * as types from './types'
|
||||||
|
import { BaseTaskCallBuilder } from './task'
|
||||||
|
|
||||||
export abstract class BaseLLMCallBuilder<
|
export abstract class BaseLLMCallBuilder<
|
||||||
TInput extends ZodRawShape | ZodTypeAny = ZodTypeAny,
|
TInput extends ZodRawShape | ZodTypeAny = ZodTypeAny,
|
||||||
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> {
|
||||||
_options: types.BaseLLMOptions<TInput, TOutput, TModelParams>
|
protected _options: types.BaseLLMOptions<TInput, TOutput, TModelParams>
|
||||||
|
|
||||||
constructor(options: types.BaseLLMOptions<TInput, TOutput, TModelParams>) {
|
constructor(options: types.BaseLLMOptions<TInput, TOutput, TModelParams>) {
|
||||||
|
super(options)
|
||||||
|
|
||||||
this._options = options
|
this._options = options
|
||||||
}
|
}
|
||||||
|
|
||||||
input<U extends ZodRawShape | ZodTypeAny = TInput>(
|
override input<U extends ZodRawShape | ZodTypeAny = TInput>(
|
||||||
inputSchema: U
|
inputSchema: U
|
||||||
): BaseLLMCallBuilder<U, TOutput, TModelParams> {
|
): BaseLLMCallBuilder<U, TOutput, TModelParams> {
|
||||||
;(
|
;(
|
||||||
|
@ -22,7 +25,7 @@ export abstract class BaseLLMCallBuilder<
|
||||||
return this as unknown as BaseLLMCallBuilder<U, TOutput, TModelParams>
|
return this as unknown as BaseLLMCallBuilder<U, TOutput, TModelParams>
|
||||||
}
|
}
|
||||||
|
|
||||||
output<U extends ZodRawShape | ZodTypeAny = TOutput>(
|
override output<U extends ZodRawShape | ZodTypeAny = TOutput>(
|
||||||
outputSchema: U
|
outputSchema: U
|
||||||
): BaseLLMCallBuilder<TInput, U, TModelParams> {
|
): BaseLLMCallBuilder<TInput, U, TModelParams> {
|
||||||
;(
|
;(
|
||||||
|
@ -46,15 +49,6 @@ export abstract class BaseLLMCallBuilder<
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
retry(retryConfig: types.LLMRetryConfig) {
|
|
||||||
this._options.retryConfig = retryConfig
|
|
||||||
return this
|
|
||||||
}
|
|
||||||
|
|
||||||
abstract call(
|
|
||||||
input?: types.ParsedData<TInput>
|
|
||||||
): Promise<types.ParsedData<TOutput>>
|
|
||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
// abstract stream({
|
// abstract stream({
|
||||||
// input: TInput,
|
// input: TInput,
|
||||||
|
|
|
@ -102,7 +102,7 @@ export class OpenAIChatModelBuilder<
|
||||||
|
|
||||||
messages.push({
|
messages.push({
|
||||||
role: 'system',
|
role: 'system',
|
||||||
content: dedent`Output JSON only in the following format:
|
content: dedent`Output JSON only in the following TypeScript format:
|
||||||
\`\`\`ts
|
\`\`\`ts
|
||||||
${tsTypeString}
|
${tsTypeString}
|
||||||
\`\`\``
|
\`\`\``
|
||||||
|
@ -162,6 +162,7 @@ export class OpenAIChatModelBuilder<
|
||||||
if (booleanOutput !== undefined) {
|
if (booleanOutput !== undefined) {
|
||||||
output = booleanOutput
|
output = booleanOutput
|
||||||
} else {
|
} else {
|
||||||
|
// TODO
|
||||||
throw new Error(`invalid boolean output: ${output}`)
|
throw new Error(`invalid boolean output: ${output}`)
|
||||||
}
|
}
|
||||||
} else if (outputSchema instanceof z.ZodNumber) {
|
} else if (outputSchema instanceof z.ZodNumber) {
|
||||||
|
@ -172,6 +173,7 @@ export class OpenAIChatModelBuilder<
|
||||||
: parseFloat(output)
|
: parseFloat(output)
|
||||||
|
|
||||||
if (isNaN(numberOutput)) {
|
if (isNaN(numberOutput)) {
|
||||||
|
// TODO
|
||||||
throw new Error(`invalid number output: ${output}`)
|
throw new Error(`invalid number output: ${output}`)
|
||||||
} else {
|
} else {
|
||||||
output = numberOutput
|
output = numberOutput
|
||||||
|
|
|
@ -0,0 +1,45 @@
|
||||||
|
import { ZodRawShape, ZodTypeAny, z } from 'zod'
|
||||||
|
|
||||||
|
import * as types from './types'
|
||||||
|
|
||||||
|
export abstract class BaseTaskCallBuilder<
|
||||||
|
TInput extends ZodRawShape | ZodTypeAny = ZodTypeAny,
|
||||||
|
TOutput extends ZodRawShape | ZodTypeAny = z.ZodTypeAny
|
||||||
|
> {
|
||||||
|
protected _options: types.BaseTaskOptions<TInput, TOutput>
|
||||||
|
|
||||||
|
constructor(options: types.BaseTaskOptions<TInput, TOutput>) {
|
||||||
|
this._options = options
|
||||||
|
}
|
||||||
|
|
||||||
|
input<U extends ZodRawShape | ZodTypeAny = TInput>(
|
||||||
|
inputSchema: U
|
||||||
|
): BaseTaskCallBuilder<U, TOutput> {
|
||||||
|
;(this as unknown as BaseTaskCallBuilder<U, TOutput>)._options.input =
|
||||||
|
inputSchema
|
||||||
|
return this as unknown as BaseTaskCallBuilder<U, TOutput>
|
||||||
|
}
|
||||||
|
|
||||||
|
output<U extends ZodRawShape | ZodTypeAny = TOutput>(
|
||||||
|
outputSchema: U
|
||||||
|
): BaseTaskCallBuilder<TInput, U> {
|
||||||
|
;(this as unknown as BaseTaskCallBuilder<TInput, U>)._options.output =
|
||||||
|
outputSchema
|
||||||
|
return this as unknown as BaseTaskCallBuilder<TInput, U>
|
||||||
|
}
|
||||||
|
|
||||||
|
retry(retryConfig: types.RetryConfig) {
|
||||||
|
this._options.retryConfig = retryConfig
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract call(
|
||||||
|
input?: types.ParsedData<TInput>
|
||||||
|
): Promise<types.ParsedData<TOutput>>
|
||||||
|
|
||||||
|
// TODO
|
||||||
|
// abstract stream({
|
||||||
|
// input: TInput,
|
||||||
|
// onProgress: types.ProgressFunction
|
||||||
|
// }): Promise<TOutput>
|
||||||
|
}
|
24
src/types.ts
24
src/types.ts
|
@ -24,20 +24,30 @@ export type SafeParsedData<T extends ZodRawShape | ZodTypeAny> =
|
||||||
? SafeParseReturnType<ZodObject<T>, ParsedData<T>>
|
? SafeParseReturnType<ZodObject<T>, ParsedData<T>>
|
||||||
: never
|
: never
|
||||||
|
|
||||||
|
export interface BaseTaskOptions<
|
||||||
|
TInput extends ZodRawShape | ZodTypeAny = ZodTypeAny,
|
||||||
|
TOutput extends ZodRawShape | ZodTypeAny = z.ZodType<string>
|
||||||
|
> {
|
||||||
|
input?: TInput
|
||||||
|
output?: TOutput
|
||||||
|
|
||||||
|
timeoutMs?: number
|
||||||
|
retryConfig?: RetryConfig
|
||||||
|
|
||||||
|
// TODO
|
||||||
|
// caching config
|
||||||
|
// logging config
|
||||||
|
}
|
||||||
|
|
||||||
export interface BaseLLMOptions<
|
export interface BaseLLMOptions<
|
||||||
TInput extends ZodRawShape | ZodTypeAny = ZodTypeAny,
|
TInput extends ZodRawShape | ZodTypeAny = ZodTypeAny,
|
||||||
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 BaseTaskOptions<TInput, TOutput> {
|
||||||
provider?: string
|
provider?: string
|
||||||
model?: string
|
model?: string
|
||||||
modelParams?: TModelParams
|
modelParams?: TModelParams
|
||||||
timeoutMs?: number
|
|
||||||
|
|
||||||
input?: TInput
|
|
||||||
output?: TOutput
|
|
||||||
examples?: LLMExample[]
|
examples?: LLMExample[]
|
||||||
retryConfig?: LLMRetryConfig
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface LLMOptions<
|
export interface LLMOptions<
|
||||||
|
@ -70,7 +80,7 @@ export interface LLMExample {
|
||||||
output: string
|
output: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface LLMRetryConfig {
|
export interface RetryConfig {
|
||||||
attempts: number
|
attempts: number
|
||||||
strategy: string
|
strategy: string
|
||||||
}
|
}
|
||||||
|
|
Ładowanie…
Reference in New Issue