Skip to content

Commit

Permalink
feat: improve types for extractObject and JinaClient
Browse files Browse the repository at this point in the history
  • Loading branch information
transitive-bullshit committed Jul 27, 2024
1 parent 3f7a24b commit c9cdcf4
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 29 deletions.
16 changes: 15 additions & 1 deletion src/create-ai-chain.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { SetOptional } from 'type-fest'
import type { z } from 'zod'
import pMap from 'p-map'

import type * as types from './types.js'
Expand All @@ -8,6 +9,19 @@ import { Msg } from './message.js'
import { asSchema, augmentSystemMessageWithJsonSchema } from './schema.js'
import { getErrorMessage } from './utils.js'

export type AIChainParams<Result extends types.AIChainResult = string> = {
chatFn: types.ChatFn
params?: types.Simplify<
Partial<Omit<types.ChatParams, 'tools' | 'functions'>>
>
tools?: types.AIFunctionLike[]
schema?: z.ZodType<Result> | types.Schema<Result>
maxCalls?: number
maxRetries?: number
toolCallConcurrency?: number
injectSchemaIntoSystemMessage?: boolean
}

/**
* Creates a chain of chat completion calls that can be invoked as a single
* function. It is meant to simplify the process of resolving tool calls
Expand All @@ -32,7 +46,7 @@ export function createAIChain<Result extends types.AIChainResult = string>({
maxRetries = 2,
toolCallConcurrency = 8,
injectSchemaIntoSystemMessage = true
}: types.AIChainParams<Result>): types.AIChain<Result> {
}: AIChainParams<Result>): types.AIChain<Result> {
const functionSet = new AIFunctionSet(tools)
const defaultParams: Partial<types.ChatParams> | undefined =
rawSchema && !functionSet.size
Expand Down
14 changes: 12 additions & 2 deletions src/extract-object.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import type * as types from './types.js'
import { createAIChain } from './create-ai-chain.js'
import { type AIChainParams, createAIChain } from './create-ai-chain.js'

export type ExtractObjectParams<Result extends types.AIChainResult = string> =
types.Simplify<
types.SetRequired<
Omit<AIChainParams<Result>, 'tools' | 'toolCallConcurrency' | 'params'>,
'schema'
> & {
params: types.SetRequired<Partial<types.ChatParams>, 'messages'>
}
>

export function extractObject<Result extends types.AIChainResult = string>(
args: types.ExtractObjectParams<Result>
args: ExtractObjectParams<Result>
): Promise<Result> {
const chain = createAIChain(args)
return chain()
Expand Down
5 changes: 4 additions & 1 deletion src/fns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ export abstract class AIFunctionsProvider {
get functions(): AIFunctionSet {
if (!this._functions) {
const metadata = this.constructor[Symbol.metadata]
assert(metadata)
assert(
metadata,
'Your runtime does not appear to support ES decorator metadata: https://github.com/tc39/proposal-decorator-metadata/issues/14'
)
const invocables =
(metadata?.invocables as PrivateAIFunctionMetadata[]) ?? []
// console.log({ metadata, invocables })
Expand Down
10 changes: 9 additions & 1 deletion src/services/jina-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ export namespace jina {
}
}

export interface ReaderResponseHtml extends JinaResponse {
data: {
html: string
}
}

export interface SearchResponse extends JinaResponse {
data: ReaderData[]
}
Expand Down Expand Up @@ -159,7 +165,9 @@ export class JinaClient extends AIFunctionsProvider {
? T['json'] extends true
? T['returnFormat'] extends 'screenshot'
? jina.ReaderResponseScreenshot
: jina.ReaderResponse
: T['returnFormat'] extends 'html'
? jina.ReaderResponseHtml
: jina.ReaderResponse
: T['returnFormat'] extends 'screenshot'
? ArrayBuffer
: string
Expand Down
26 changes: 2 additions & 24 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import type { Jsonifiable, SetOptional, SetRequired, Simplify } from 'type-fest'
import type { Jsonifiable, SetOptional, Simplify } from 'type-fest'
import type { z } from 'zod'

import type { AIFunctionSet } from './ai-function-set.js'
import type { AIFunctionsProvider } from './fns.js'
import type { Msg } from './message.js'
import type { Schema } from './schema.js'

export type { Msg } from './message.js'
export type { Schema } from './schema.js'
export type { KyInstance } from 'ky'
export type { ThrottledFunction } from 'p-throttle'
export type { Simplify } from 'type-fest'
export type { SetRequired, Simplify } from 'type-fest'

export type Nullable<T> = T | null

Expand Down Expand Up @@ -136,24 +135,3 @@ export type SafeParseResult<TData> =
}

export type ValidatorFn<TData> = (value: unknown) => SafeParseResult<TData>

export type AIChainParams<Result extends AIChainResult = string> = {
chatFn: ChatFn
params?: Simplify<Partial<Omit<ChatParams, 'tools' | 'functions'>>>
tools?: AIFunctionLike[]
schema?: z.ZodType<Result> | Schema<Result>
maxCalls?: number
maxRetries?: number
toolCallConcurrency?: number
injectSchemaIntoSystemMessage?: boolean
}

export type ExtractObjectParams<Result extends AIChainResult = string> =
Simplify<
SetRequired<
Omit<AIChainParams<Result>, 'tools' | 'toolCallConcurrency' | 'params'>,
'schema'
> & {
params: SetRequired<Partial<ChatParams>, 'messages'>
}
>

0 comments on commit c9cdcf4

Please sign in to comment.