kopia lustrzana https://github.com/transitive-bullshit/chatgpt-api
feat: improve types for extractObject and JinaClient
rodzic
3f7a24b414
commit
c9cdcf4a0d
|
@ -1,4 +1,5 @@
|
||||||
import type { SetOptional } from 'type-fest'
|
import type { SetOptional } from 'type-fest'
|
||||||
|
import type { z } from 'zod'
|
||||||
import pMap from 'p-map'
|
import pMap from 'p-map'
|
||||||
|
|
||||||
import type * as types from './types.js'
|
import type * as types from './types.js'
|
||||||
|
@ -8,6 +9,19 @@ import { Msg } from './message.js'
|
||||||
import { asSchema, augmentSystemMessageWithJsonSchema } from './schema.js'
|
import { asSchema, augmentSystemMessageWithJsonSchema } from './schema.js'
|
||||||
import { getErrorMessage } from './utils.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
|
* 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
|
* function. It is meant to simplify the process of resolving tool calls
|
||||||
|
@ -32,7 +46,7 @@ export function createAIChain<Result extends types.AIChainResult = string>({
|
||||||
maxRetries = 2,
|
maxRetries = 2,
|
||||||
toolCallConcurrency = 8,
|
toolCallConcurrency = 8,
|
||||||
injectSchemaIntoSystemMessage = true
|
injectSchemaIntoSystemMessage = true
|
||||||
}: types.AIChainParams<Result>): types.AIChain<Result> {
|
}: AIChainParams<Result>): types.AIChain<Result> {
|
||||||
const functionSet = new AIFunctionSet(tools)
|
const functionSet = new AIFunctionSet(tools)
|
||||||
const defaultParams: Partial<types.ChatParams> | undefined =
|
const defaultParams: Partial<types.ChatParams> | undefined =
|
||||||
rawSchema && !functionSet.size
|
rawSchema && !functionSet.size
|
||||||
|
|
|
@ -1,8 +1,18 @@
|
||||||
import type * as types from './types.js'
|
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>(
|
export function extractObject<Result extends types.AIChainResult = string>(
|
||||||
args: types.ExtractObjectParams<Result>
|
args: ExtractObjectParams<Result>
|
||||||
): Promise<Result> {
|
): Promise<Result> {
|
||||||
const chain = createAIChain(args)
|
const chain = createAIChain(args)
|
||||||
return chain()
|
return chain()
|
||||||
|
|
|
@ -39,7 +39,10 @@ export abstract class AIFunctionsProvider {
|
||||||
get functions(): AIFunctionSet {
|
get functions(): AIFunctionSet {
|
||||||
if (!this._functions) {
|
if (!this._functions) {
|
||||||
const metadata = this.constructor[Symbol.metadata]
|
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 =
|
const invocables =
|
||||||
(metadata?.invocables as PrivateAIFunctionMetadata[]) ?? []
|
(metadata?.invocables as PrivateAIFunctionMetadata[]) ?? []
|
||||||
// console.log({ metadata, invocables })
|
// console.log({ metadata, invocables })
|
||||||
|
|
|
@ -75,6 +75,12 @@ export namespace jina {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface ReaderResponseHtml extends JinaResponse {
|
||||||
|
data: {
|
||||||
|
html: string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export interface SearchResponse extends JinaResponse {
|
export interface SearchResponse extends JinaResponse {
|
||||||
data: ReaderData[]
|
data: ReaderData[]
|
||||||
}
|
}
|
||||||
|
@ -159,6 +165,8 @@ export class JinaClient extends AIFunctionsProvider {
|
||||||
? T['json'] extends true
|
? T['json'] extends true
|
||||||
? T['returnFormat'] extends 'screenshot'
|
? T['returnFormat'] extends 'screenshot'
|
||||||
? jina.ReaderResponseScreenshot
|
? jina.ReaderResponseScreenshot
|
||||||
|
: T['returnFormat'] extends 'html'
|
||||||
|
? jina.ReaderResponseHtml
|
||||||
: jina.ReaderResponse
|
: jina.ReaderResponse
|
||||||
: T['returnFormat'] extends 'screenshot'
|
: T['returnFormat'] extends 'screenshot'
|
||||||
? ArrayBuffer
|
? ArrayBuffer
|
||||||
|
|
26
src/types.ts
26
src/types.ts
|
@ -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 { z } from 'zod'
|
||||||
|
|
||||||
import type { AIFunctionSet } from './ai-function-set.js'
|
import type { AIFunctionSet } from './ai-function-set.js'
|
||||||
import type { AIFunctionsProvider } from './fns.js'
|
import type { AIFunctionsProvider } from './fns.js'
|
||||||
import type { Msg } from './message.js'
|
import type { Msg } from './message.js'
|
||||||
import type { Schema } from './schema.js'
|
|
||||||
|
|
||||||
export type { Msg } from './message.js'
|
export type { Msg } from './message.js'
|
||||||
export type { Schema } from './schema.js'
|
export type { Schema } from './schema.js'
|
||||||
export type { KyInstance } from 'ky'
|
export type { KyInstance } from 'ky'
|
||||||
export type { ThrottledFunction } from 'p-throttle'
|
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
|
export type Nullable<T> = T | null
|
||||||
|
|
||||||
|
@ -136,24 +135,3 @@ export type SafeParseResult<TData> =
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ValidatorFn<TData> = (value: unknown) => 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'>
|
|
||||||
}
|
|
||||||
>
|
|
||||||
|
|
Ładowanie…
Reference in New Issue