kopia lustrzana https://github.com/transitive-bullshit/chatgpt-api
feat: minor fixes for diffbot and parsing of structured output
rodzic
e22d3c8b3f
commit
64bdb88a4b
|
@ -35,14 +35,17 @@ export function parseStructuredOutput<T>(
|
||||||
outputSchema: ZodType<T>
|
outputSchema: ZodType<T>
|
||||||
): T {
|
): T {
|
||||||
let result
|
let result
|
||||||
if (outputSchema instanceof z.ZodArray) {
|
if (outputSchema instanceof z.ZodArray || 'element' in outputSchema) {
|
||||||
result = parseArrayOutput(output)
|
result = parseArrayOutput(output)
|
||||||
} else if (outputSchema instanceof z.ZodObject) {
|
} else if (outputSchema instanceof z.ZodObject || 'omit' in outputSchema) {
|
||||||
result = parseObjectOutput(output)
|
result = parseObjectOutput(output)
|
||||||
} else if (outputSchema instanceof z.ZodBoolean) {
|
} else if (outputSchema instanceof z.ZodBoolean) {
|
||||||
result = parseBooleanOutput(output)
|
result = parseBooleanOutput(output)
|
||||||
} else if (outputSchema instanceof z.ZodNumber) {
|
} else if (
|
||||||
result = parseNumberOutput(output, outputSchema)
|
outputSchema instanceof z.ZodNumber ||
|
||||||
|
'nonnegative' in outputSchema
|
||||||
|
) {
|
||||||
|
result = parseNumberOutput(output, outputSchema as unknown as z.ZodNumber)
|
||||||
} else {
|
} else {
|
||||||
// Default to string output...
|
// Default to string output...
|
||||||
result = output
|
result = output
|
||||||
|
@ -69,6 +72,8 @@ export function safeParseStructuredOutput<T>(
|
||||||
data
|
data
|
||||||
}
|
}
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
|
console.error(err)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
error: err.message
|
error: err.message
|
||||||
|
|
|
@ -1,16 +1,9 @@
|
||||||
import type { Simplify } from 'type-fest'
|
|
||||||
import defaultKy, { type KyInstance } from 'ky'
|
import defaultKy, { type KyInstance } from 'ky'
|
||||||
import pThrottle from 'p-throttle'
|
import pThrottle from 'p-throttle'
|
||||||
import { z } from 'zod'
|
import { z } from 'zod'
|
||||||
|
|
||||||
import { aiFunction, AIFunctionsProvider } from '../fns.js'
|
import { aiFunction, AIFunctionsProvider } from '../fns.js'
|
||||||
import {
|
import { assert, getEnv, sanitizeSearchParams, throttleKy } from '../utils.js'
|
||||||
assert,
|
|
||||||
getEnv,
|
|
||||||
omit,
|
|
||||||
sanitizeSearchParams,
|
|
||||||
throttleKy
|
|
||||||
} from '../utils.js'
|
|
||||||
|
|
||||||
export namespace diffbot {
|
export namespace diffbot {
|
||||||
export const API_BASE_URL = 'https://api.diffbot.com'
|
export const API_BASE_URL = 'https://api.diffbot.com'
|
||||||
|
@ -335,7 +328,7 @@ export namespace diffbot {
|
||||||
hits: number
|
hits: number
|
||||||
kgversion: string
|
kgversion: string
|
||||||
request_ctx: RequestCtx
|
request_ctx: RequestCtx
|
||||||
data: EnhanceEntityResponseDatum[]
|
data: EnhanceEntityResult[]
|
||||||
errors: any[]
|
errors: any[]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -353,7 +346,7 @@ export namespace diffbot {
|
||||||
search: string
|
search: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface EnhanceEntityResponseDatum {
|
export interface EnhanceEntityResult {
|
||||||
score: number
|
score: number
|
||||||
esscore: number
|
esscore: number
|
||||||
entity: Entity
|
entity: Entity
|
||||||
|
@ -608,27 +601,6 @@ export namespace diffbot {
|
||||||
name: string
|
name: string
|
||||||
type: string
|
type: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export function pruneEntity(entity: diffbot.Entity) {
|
|
||||||
return omit(
|
|
||||||
entity,
|
|
||||||
'allOriginHashes',
|
|
||||||
'locations',
|
|
||||||
'images',
|
|
||||||
'nationalities',
|
|
||||||
'awards',
|
|
||||||
'interests',
|
|
||||||
'suppliers',
|
|
||||||
'partnerships',
|
|
||||||
'industries',
|
|
||||||
'categories',
|
|
||||||
'technographics',
|
|
||||||
'employeeCategories',
|
|
||||||
'diffbotClassification'
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
export type PrunedEntity = Simplify<ReturnType<typeof pruneEntity>>
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -721,8 +693,8 @@ export class DiffbotClient extends AIFunctionsProvider {
|
||||||
})
|
})
|
||||||
async enhanceEntity(
|
async enhanceEntity(
|
||||||
opts: diffbot.EnhanceEntityOptions
|
opts: diffbot.EnhanceEntityOptions
|
||||||
): Promise<diffbot.PrunedEntity[]> {
|
): Promise<diffbot.EnhanceEntityResponse> {
|
||||||
const res = await this.kyKnowledgeGraph
|
return this.kyKnowledgeGraph
|
||||||
.get('kg/v3/enhance', {
|
.get('kg/v3/enhance', {
|
||||||
searchParams: sanitizeSearchParams({
|
searchParams: sanitizeSearchParams({
|
||||||
...opts,
|
...opts,
|
||||||
|
@ -730,8 +702,6 @@ export class DiffbotClient extends AIFunctionsProvider {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.json<diffbot.EnhanceEntityResponse>()
|
.json<diffbot.EnhanceEntityResponse>()
|
||||||
|
|
||||||
return res.data.map((datum) => diffbot.pruneEntity(datum.entity))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async searchKnowledgeGraph(options: diffbot.KnowledgeGraphSearchOptions) {
|
async searchKnowledgeGraph(options: diffbot.KnowledgeGraphSearchOptions) {
|
||||||
|
|
|
@ -1880,7 +1880,7 @@ export namespace proxycurl {
|
||||||
export type PersonProfile = z.infer<typeof PersonProfileSchema>
|
export type PersonProfile = z.infer<typeof PersonProfileSchema>
|
||||||
|
|
||||||
export type ResolvedPersonProfile = {
|
export type ResolvedPersonProfile = {
|
||||||
profile: PersonProfile
|
profile?: PersonProfile
|
||||||
url?: string
|
url?: string
|
||||||
name_similarity_score?: number
|
name_similarity_score?: number
|
||||||
company_similarity_score?: number
|
company_similarity_score?: number
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import isRelativeUrlImpl from 'is-relative-url'
|
import isRelativeUrlImpl from 'is-relative-url'
|
||||||
import normalizeUrlImpl, {
|
import normalizeUrlImpl, {
|
||||||
type Options as NormalizeUrlOptions
|
type Options as NormalizeUrlImplOptions
|
||||||
} from 'normalize-url'
|
} from 'normalize-url'
|
||||||
import QuickLRU from 'quick-lru'
|
import QuickLRU from 'quick-lru'
|
||||||
|
|
||||||
|
@ -39,9 +39,13 @@ export function isRelativeUrl(url: string): boolean {
|
||||||
return isRelativeUrlImpl(url) && !url.startsWith('//')
|
return isRelativeUrlImpl(url) && !url.startsWith('//')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type NormalizeUrlOptions = NormalizeUrlImplOptions & {
|
||||||
|
allowSloppyUris?: boolean
|
||||||
|
}
|
||||||
|
|
||||||
export function normalizeUrl(
|
export function normalizeUrl(
|
||||||
url?: string,
|
url?: string,
|
||||||
options?: NormalizeUrlOptions
|
{ allowSloppyUris = true, ...options }: NormalizeUrlOptions = {}
|
||||||
): string | undefined {
|
): string | undefined {
|
||||||
let normalizedUrl: string | undefined
|
let normalizedUrl: string | undefined
|
||||||
|
|
||||||
|
@ -50,7 +54,7 @@ export function normalizeUrl(
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isRelativeUrl(url)) {
|
if (isRelativeUrl(url)) {
|
||||||
if (!/^[./]/.test(url) && url.indexOf('.') > 0) {
|
if (allowSloppyUris && !/^[#./]/.test(url) && url.indexOf('.') > 0) {
|
||||||
url = `https://${url}`
|
url = `https://${url}`
|
||||||
} else {
|
} else {
|
||||||
return undefined
|
return undefined
|
||||||
|
|
Ładowanie…
Reference in New Issue