old-agentic-v1^2
Travis Fischer 2023-05-04 14:48:28 -05:00
rodzic 8d4bfa2a91
commit c7fb3d37b3
5 zmienionych plików z 57 dodań i 26 usunięć

Wyświetl plik

@ -28,6 +28,7 @@
"openai-fetch": "^1.2.0", "openai-fetch": "^1.2.0",
"p-map": "^6.0.0", "p-map": "^6.0.0",
"parse-json": "^7.0.0", "parse-json": "^7.0.0",
"type-fest": "^3.9.0",
"zod": "^3.21.4", "zod": "^3.21.4",
"zod-validation-error": "^1.3.0" "zod-validation-error": "^1.3.0"
}, },

Wyświetl plik

@ -16,6 +16,9 @@ dependencies:
parse-json: parse-json:
specifier: ^7.0.0 specifier: ^7.0.0
version: 7.0.0 version: 7.0.0
type-fest:
specifier: ^3.9.0
version: 3.9.0
zod: zod:
specifier: ^3.21.4 specifier: ^3.21.4
version: 3.21.4 version: 3.21.4

Wyświetl plik

@ -1,7 +1,10 @@
import type { SetRequired } from 'type-fest'
import { ZodRawShape, ZodTypeAny, z } from 'zod' import { ZodRawShape, ZodTypeAny, z } from 'zod'
import * as types from './types' import * as types from './types'
const defaultOpenAIModel = 'gpt-3.5-turbo'
export class Agentic { export class Agentic {
_client: types.openai.OpenAIClient _client: types.openai.OpenAIClient
_verbosity: number _verbosity: number
@ -10,21 +13,19 @@ export class Agentic {
'provider' | 'model' | 'modelParams' | 'timeoutMs' | 'retryConfig' 'provider' | 'model' | 'modelParams' | 'timeoutMs' | 'retryConfig'
> >
constructor( constructor(opts: {
client: types.openai.OpenAIClient, openai: types.openai.OpenAIClient
opts: {
verbosity?: number verbosity?: number
defaults?: Pick< defaults?: Pick<
types.BaseLLMOptions, types.BaseLLMOptions,
'provider' | 'model' | 'modelParams' | 'timeoutMs' | 'retryConfig' 'provider' | 'model' | 'modelParams' | 'timeoutMs' | 'retryConfig'
> >
} = {} }) {
) { this._client = opts.openai
this._client = client
this._verbosity = opts.verbosity ?? 0 this._verbosity = opts.verbosity ?? 0
this._defaults = { this._defaults = {
provider: 'openai', provider: 'openai',
model: 'gpt-3.5-turbo', model: defaultOpenAIModel,
modelParams: {}, modelParams: {},
timeoutMs: 30000, timeoutMs: 30000,
retryConfig: { retryConfig: {
@ -54,7 +55,7 @@ export class Agentic {
options = promptOrChatCompletionParams options = promptOrChatCompletionParams
if (!options.messages) { if (!options.messages) {
throw new Error() throw new Error('messages must be provided')
} }
} }
@ -68,7 +69,7 @@ export class Agentic {
export abstract class BaseLLMCallBuilder< export abstract class BaseLLMCallBuilder<
TInput extends ZodRawShape | ZodTypeAny = ZodTypeAny, TInput extends ZodRawShape | ZodTypeAny = ZodTypeAny,
TOutput extends ZodRawShape | ZodTypeAny = ZodTypeAny, TOutput extends ZodRawShape | ZodTypeAny = z.ZodType<string>,
TModelParams extends Record<string, any> = Record<string, any> TModelParams extends Record<string, any> = Record<string, any>
> { > {
_options: types.BaseLLMOptions<TInput, TOutput, TModelParams> _options: types.BaseLLMOptions<TInput, TOutput, TModelParams>
@ -118,7 +119,7 @@ export abstract class BaseLLMCallBuilder<
export abstract class ChatModelBuilder< export abstract class ChatModelBuilder<
TInput extends ZodRawShape | ZodTypeAny = ZodTypeAny, TInput extends ZodRawShape | ZodTypeAny = ZodTypeAny,
TOutput extends ZodRawShape | ZodTypeAny = ZodTypeAny, TOutput extends ZodRawShape | ZodTypeAny = z.ZodType<string>,
TModelParams extends Record<string, any> = Record<string, any> TModelParams extends Record<string, any> = Record<string, any>
> extends BaseLLMCallBuilder<TInput, TOutput, TModelParams> { > extends BaseLLMCallBuilder<TInput, TOutput, TModelParams> {
_messages: types.ChatMessage[] _messages: types.ChatMessage[]
@ -132,11 +133,11 @@ export abstract class ChatModelBuilder<
export class OpenAIChatModelBuilder< export class OpenAIChatModelBuilder<
TInput extends ZodRawShape | ZodTypeAny = ZodTypeAny, TInput extends ZodRawShape | ZodTypeAny = ZodTypeAny,
TOutput extends ZodRawShape | ZodTypeAny = ZodTypeAny TOutput extends ZodRawShape | ZodTypeAny = z.ZodType<string>
> extends ChatModelBuilder< > extends ChatModelBuilder<
TInput, TInput,
TOutput, TOutput,
Omit<types.openai.ChatCompletionParams, 'messages'> SetRequired<Omit<types.openai.ChatCompletionParams, 'messages'>, 'model'>
> { > {
_client: types.openai.OpenAIClient _client: types.openai.OpenAIClient
@ -150,6 +151,7 @@ export class OpenAIChatModelBuilder<
) { ) {
super({ super({
provider: 'openai', provider: 'openai',
model: defaultOpenAIModel,
...options ...options
}) })
@ -159,8 +161,26 @@ 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>> {
// this._options.output?.describe // TODO: construct messages
// TODO
return true as types.ParsedData<TOutput> const completion = await this._client.createChatCompletion({
model: defaultOpenAIModel, // TODO: this shouldn't be necessary
...this._options.modelParams,
messages: this._messages
})
if (this._options.output) {
const schema =
this._options.output instanceof z.ZodType
? this._options.output
: z.object(this._options.output)
// TODO: convert string => object if necessary
// TODO: handle errors, retry logic, and self-healing
return schema.parse(completion.message.content)
} else {
return completion.message.content as any
}
} }
} }

Wyświetl plik

@ -5,9 +5,10 @@ import { z } from 'zod'
import { Agentic } from './llm' import { Agentic } from './llm'
dotenv.config() dotenv.config()
async function main() { async function main() {
const openai = new OpenAIClient({ apiKey: process.env.OPENAI_API_KEY! }) const openai = new OpenAIClient({ apiKey: process.env.OPENAI_API_KEY! })
const $ = new Agentic(openai) const $ = new Agentic({ openai })
const ex0 = await $.gpt4(`give me a single boolean value`) const ex0 = await $.gpt4(`give me a single boolean value`)
.output(z.boolean()) .output(z.boolean())
@ -15,6 +16,13 @@ async function main() {
.call() .call()
console.log(ex0) console.log(ex0)
const ex1 = await $.gpt4(`give me fake data conforming to this schema`)
.output(z.object({ foo: z.string(), bar: z.number() }))
// .retry({ attempts: 3 })
.call()
console.log(ex1)
} }
main() main()

Wyświetl plik

@ -3,7 +3,6 @@ import {
SafeParseReturnType, SafeParseReturnType,
ZodObject, ZodObject,
ZodRawShape, ZodRawShape,
ZodSchema,
ZodTypeAny, ZodTypeAny,
output, output,
z z
@ -27,7 +26,7 @@ export type SafeParsedData<T extends ZodRawShape | ZodTypeAny> =
export interface BaseLLMOptions< export interface BaseLLMOptions<
TInput extends ZodRawShape | ZodTypeAny = ZodTypeAny, TInput extends ZodRawShape | ZodTypeAny = ZodTypeAny,
TOutput extends ZodRawShape | ZodTypeAny = ZodTypeAny, TOutput extends ZodRawShape | ZodTypeAny = z.ZodType<string>,
TModelParams extends Record<string, any> = Record<string, any> TModelParams extends Record<string, any> = Record<string, any>
> { > {
provider?: string provider?: string
@ -43,7 +42,7 @@ export interface BaseLLMOptions<
export interface LLMOptions< export interface LLMOptions<
TInput extends ZodRawShape | ZodTypeAny = ZodTypeAny, TInput extends ZodRawShape | ZodTypeAny = ZodTypeAny,
TOutput extends ZodRawShape | ZodTypeAny = ZodTypeAny, TOutput extends ZodRawShape | ZodTypeAny = z.ZodType<string>,
TModelParams extends Record<string, any> = 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
@ -51,7 +50,7 @@ export interface LLMOptions<
promptSuffix?: string promptSuffix?: string
} }
export type ChatMessageRole = 'user' | 'system' | 'assistant' | 'tool' export type ChatMessageRole = 'user' | 'system' | 'assistant'
export interface ChatMessage { export interface ChatMessage {
role: ChatMessageRole role: ChatMessageRole
@ -60,7 +59,7 @@ export interface ChatMessage {
export interface ChatModelOptions< export interface ChatModelOptions<
TInput extends ZodRawShape | ZodTypeAny = ZodTypeAny, TInput extends ZodRawShape | ZodTypeAny = ZodTypeAny,
TOutput extends ZodRawShape | ZodTypeAny = ZodTypeAny, TOutput extends ZodRawShape | ZodTypeAny = z.ZodType<string>,
TModelParams extends Record<string, any> = 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[]