old-agentic-v1^2
Travis Fischer 2023-05-03 22:54:32 -05:00
rodzic e25eab97eb
commit 8d4bfa2a91
5 zmienionych plików z 128 dodań i 31 usunięć

Wyświetl plik

@ -15,6 +15,10 @@
TODO TODO
## TODO
https://github.com/colinhacks/zod#writing-generic-functions
## Use Cases ## Use Cases
https://platform.openai.com/examples https://platform.openai.com/examples

Wyświetl plik

@ -1,4 +1,4 @@
import type { ZodType } from 'zod' import { ZodRawShape, ZodTypeAny, z } from 'zod'
import * as types from './types' import * as types from './types'
@ -66,21 +66,33 @@ export class Agentic {
} }
} }
export abstract class BaseLLMCallBuilder<TInput, TOutput, TModelParams> { export abstract class BaseLLMCallBuilder<
TInput extends ZodRawShape | ZodTypeAny = ZodTypeAny,
TOutput extends ZodRawShape | ZodTypeAny = ZodTypeAny,
TModelParams extends Record<string, any> = Record<string, any>
> {
_options: types.BaseLLMOptions<TInput, TOutput, TModelParams> _options: types.BaseLLMOptions<TInput, TOutput, TModelParams>
constructor(options: types.BaseLLMOptions<TInput, TOutput, TModelParams>) { constructor(options: types.BaseLLMOptions<TInput, TOutput, TModelParams>) {
this._options = options this._options = options
} }
input(inputSchema: ZodType<TInput>) { input<U extends ZodRawShape | ZodTypeAny = TInput>(
this._options.input = inputSchema inputSchema: U
return this ): BaseLLMCallBuilder<U, TOutput, TModelParams> {
;(
this as unknown as BaseLLMCallBuilder<U, TOutput, TModelParams>
)._options.input = inputSchema
return this as unknown as BaseLLMCallBuilder<U, TOutput, TModelParams>
} }
output(outputSchema: ZodType<TOutput>) { output<U extends ZodRawShape | ZodTypeAny = TOutput>(
this._options.output = outputSchema outputSchema: U
return this ): BaseLLMCallBuilder<TInput, U, TModelParams> {
;(
this as unknown as BaseLLMCallBuilder<TInput, U, TModelParams>
)._options.output = outputSchema
return this as unknown as BaseLLMCallBuilder<TInput, U, TModelParams>
} }
examples(examples: types.LLMExample[]) { examples(examples: types.LLMExample[]) {
@ -93,21 +105,21 @@ export abstract class BaseLLMCallBuilder<TInput, TOutput, TModelParams> {
return this return this
} }
abstract call(input?: TInput): Promise<TOutput> abstract call(
input?: types.ParsedData<TInput>
): Promise<types.ParsedData<TOutput>>
// TODO // TODO
// abstract stream( // abstract stream({
// input: TInput, // input: TInput,
// onProgress: types.ProgressFunction // onProgress: types.ProgressFunction
// ): Promise<TOutput> // }): Promise<TOutput>
// abstract stream(onProgress: types.ProgressFunction): Promise<TOutput>
} }
export abstract class ChatModelBuilder< export abstract class ChatModelBuilder<
TInput, TInput extends ZodRawShape | ZodTypeAny = ZodTypeAny,
TOutput, TOutput extends ZodRawShape | ZodTypeAny = ZodTypeAny,
TModelParams TModelParams extends Record<string, any> = Record<string, any>
> extends BaseLLMCallBuilder<TInput, TOutput, TModelParams> { > extends BaseLLMCallBuilder<TInput, TOutput, TModelParams> {
_messages: types.ChatMessage[] _messages: types.ChatMessage[]
@ -118,7 +130,10 @@ export abstract class ChatModelBuilder<
} }
} }
export class OpenAIChatModelBuilder<TInput, TOutput> extends ChatModelBuilder< export class OpenAIChatModelBuilder<
TInput extends ZodRawShape | ZodTypeAny = ZodTypeAny,
TOutput extends ZodRawShape | ZodTypeAny = ZodTypeAny
> extends ChatModelBuilder<
TInput, TInput,
TOutput, TOutput,
Omit<types.openai.ChatCompletionParams, 'messages'> Omit<types.openai.ChatCompletionParams, 'messages'>
@ -141,7 +156,11 @@ export class OpenAIChatModelBuilder<TInput, TOutput> extends ChatModelBuilder<
this._client = client this._client = client
} }
override async call(input?: TInput): Promise<TOutput> { override async call(
input?: types.ParsedData<TInput>
): Promise<types.ParsedData<TOutput>> {
// this._options.output?.describe
// TODO // TODO
return true as types.ParsedData<TOutput>
} }
} }

20
src/temp.ts 100644
Wyświetl plik

@ -0,0 +1,20 @@
import dotenv from 'dotenv-safe'
import { OpenAIClient } from 'openai-fetch'
import { z } from 'zod'
import { Agentic } from './llm'
dotenv.config()
async function main() {
const openai = new OpenAIClient({ apiKey: process.env.OPENAI_API_KEY! })
const $ = new Agentic(openai)
const ex0 = await $.gpt4(`give me a single boolean value`)
.output(z.boolean())
// .retry({ attempts: 3 })
.call()
console.log(ex0)
}
main()

28
src/temp2.ts 100644
Wyświetl plik

@ -0,0 +1,28 @@
import { ZodRawShape, ZodType, ZodTypeAny, z } from 'zod'
import * as types from './types'
class Test<T extends ZodRawShape | ZodTypeAny = ZodTypeAny> {
_schema: T
schema<U extends ZodRawShape | ZodTypeAny>(schema: U): Test<U> {
;(this as unknown as Test<U>)._schema = schema
return this as unknown as Test<U>
}
call(value: types.ParsedData<T>): types.ParsedData<T> {
const finalSchema =
this._schema instanceof ZodType ? this._schema : z.object(this._schema)
return finalSchema.parse(value)
}
}
async function main() {
const t = new Test()
const t2 = t.schema(z.string())
const t3 = t2.call('foo')
console.log(t3)
}
main()

Wyświetl plik

@ -1,43 +1,67 @@
import * as openai from 'openai-fetch' import * as openai from 'openai-fetch'
import type { ZodType } from 'zod' import {
SafeParseReturnType,
ZodObject,
ZodRawShape,
ZodSchema,
ZodTypeAny,
output,
z
} from 'zod'
export { openai } export { openai }
export type ParsedData<T extends ZodRawShape | ZodTypeAny> =
T extends ZodTypeAny
? output<T>
: T extends ZodRawShape
? output<ZodObject<T>>
: never
export type SafeParsedData<T extends ZodRawShape | ZodTypeAny> =
T extends ZodTypeAny
? SafeParseReturnType<z.infer<T>, ParsedData<T>>
: T extends ZodRawShape
? SafeParseReturnType<ZodObject<T>, ParsedData<T>>
: never
export interface BaseLLMOptions< export interface BaseLLMOptions<
TInput = any, TInput extends ZodRawShape | ZodTypeAny = ZodTypeAny,
TOutput = any, TOutput extends ZodRawShape | ZodTypeAny = ZodTypeAny,
TModelParams = Record<string, any> TModelParams extends Record<string, any> = Record<string, any>
> { > {
provider?: string provider?: string
model?: string model?: string
modelParams?: TModelParams modelParams?: TModelParams
timeoutMs?: number timeoutMs?: number
input?: ZodType<TInput> input?: TInput
output?: ZodType<TOutput> output?: TOutput
examples?: LLMExample[] examples?: LLMExample[]
retryConfig?: LLMRetryConfig retryConfig?: LLMRetryConfig
} }
export interface LLMOptions< export interface LLMOptions<
TInput = any, TInput extends ZodRawShape | ZodTypeAny = ZodTypeAny,
TOutput = any, TOutput extends ZodRawShape | ZodTypeAny = ZodTypeAny,
TModelParams = Record<string, any> TModelParams extends Record<string, any> = Record<string, any>
> extends BaseLLMOptions<TInput, TOutput, TModelParams> { > extends BaseLLMOptions<TInput, TOutput, TModelParams> {
promptTemplate?: string promptTemplate?: string
promptPrefix?: string promptPrefix?: string
promptSuffix?: string promptSuffix?: string
} }
export type ChatMessageRole = 'user' | 'system' | 'assistant' | 'tool'
export interface ChatMessage { export interface ChatMessage {
role: 'user' | 'system' | 'assistant' | 'tool' role: ChatMessageRole
content: string content: string
} }
export interface ChatModelOptions< export interface ChatModelOptions<
TInput = any, TInput extends ZodRawShape | ZodTypeAny = ZodTypeAny,
TOutput = any, TOutput extends ZodRawShape | ZodTypeAny = ZodTypeAny,
TModelParams = Record<string, any> TModelParams extends Record<string, any> = Record<string, any>
> extends BaseLLMOptions<TInput, TOutput, TModelParams> { > extends BaseLLMOptions<TInput, TOutput, TModelParams> {
messages: ChatMessage[] messages: ChatMessage[]
} }
@ -51,3 +75,5 @@ export interface LLMRetryConfig {
attempts: number attempts: number
strategy: string strategy: string
} }
// export type ProgressFunction = (partialResponse: ChatMessage) => void