Skip to content

Commit

Permalink
feat: minor fixes for diffbot and parsing of structured output
Browse files Browse the repository at this point in the history
  • Loading branch information
transitive-bullshit committed Jul 24, 2024
1 parent e22d3c8 commit 64bdb88
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 43 deletions.
13 changes: 9 additions & 4 deletions src/parse-structured-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,17 @@ export function parseStructuredOutput<T>(
outputSchema: ZodType<T>
): T {
let result
if (outputSchema instanceof z.ZodArray) {
if (outputSchema instanceof z.ZodArray || 'element' in outputSchema) {
result = parseArrayOutput(output)
} else if (outputSchema instanceof z.ZodObject) {
} else if (outputSchema instanceof z.ZodObject || 'omit' in outputSchema) {
result = parseObjectOutput(output)
} else if (outputSchema instanceof z.ZodBoolean) {
result = parseBooleanOutput(output)
} else if (outputSchema instanceof z.ZodNumber) {
result = parseNumberOutput(output, outputSchema)
} else if (
outputSchema instanceof z.ZodNumber ||
'nonnegative' in outputSchema
) {
result = parseNumberOutput(output, outputSchema as unknown as z.ZodNumber)
} else {
// Default to string output...
result = output
Expand All @@ -69,6 +72,8 @@ export function safeParseStructuredOutput<T>(
data
}
} catch (err: any) {
console.error(err)

return {
success: false,
error: err.message
Expand Down
40 changes: 5 additions & 35 deletions src/services/diffbot-client.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
import type { Simplify } from 'type-fest'
import defaultKy, { type KyInstance } from 'ky'
import pThrottle from 'p-throttle'
import { z } from 'zod'

import { aiFunction, AIFunctionsProvider } from '../fns.js'
import {
assert,
getEnv,
omit,
sanitizeSearchParams,
throttleKy
} from '../utils.js'
import { assert, getEnv, sanitizeSearchParams, throttleKy } from '../utils.js'

export namespace diffbot {
export const API_BASE_URL = 'https://api.diffbot.com'
Expand Down Expand Up @@ -335,7 +328,7 @@ export namespace diffbot {
hits: number
kgversion: string
request_ctx: RequestCtx
data: EnhanceEntityResponseDatum[]
data: EnhanceEntityResult[]
errors: any[]
}

Expand All @@ -353,7 +346,7 @@ export namespace diffbot {
search: string
}

export interface EnhanceEntityResponseDatum {
export interface EnhanceEntityResult {
score: number
esscore: number
entity: Entity
Expand Down Expand Up @@ -608,27 +601,6 @@ export namespace diffbot {
name: 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>>
}

/**
Expand Down Expand Up @@ -721,17 +693,15 @@ export class DiffbotClient extends AIFunctionsProvider {
})
async enhanceEntity(
opts: diffbot.EnhanceEntityOptions
): Promise<diffbot.PrunedEntity[]> {
const res = await this.kyKnowledgeGraph
): Promise<diffbot.EnhanceEntityResponse> {
return this.kyKnowledgeGraph
.get('kg/v3/enhance', {
searchParams: sanitizeSearchParams({
...opts,
token: this.apiKey
})
})
.json<diffbot.EnhanceEntityResponse>()

return res.data.map((datum) => diffbot.pruneEntity(datum.entity))
}

async searchKnowledgeGraph(options: diffbot.KnowledgeGraphSearchOptions) {
Expand Down
2 changes: 1 addition & 1 deletion src/services/proxycurl-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1880,7 +1880,7 @@ export namespace proxycurl {
export type PersonProfile = z.infer<typeof PersonProfileSchema>

export type ResolvedPersonProfile = {
profile: PersonProfile
profile?: PersonProfile
url?: string
name_similarity_score?: number
company_similarity_score?: number
Expand Down
10 changes: 7 additions & 3 deletions src/url-utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import isRelativeUrlImpl from 'is-relative-url'
import normalizeUrlImpl, {
type Options as NormalizeUrlOptions
type Options as NormalizeUrlImplOptions
} from 'normalize-url'
import QuickLRU from 'quick-lru'

Expand Down Expand Up @@ -39,9 +39,13 @@ export function isRelativeUrl(url: string): boolean {
return isRelativeUrlImpl(url) && !url.startsWith('//')
}

export type NormalizeUrlOptions = NormalizeUrlImplOptions & {
allowSloppyUris?: boolean
}

export function normalizeUrl(
url?: string,
options?: NormalizeUrlOptions
{ allowSloppyUris = true, ...options }: NormalizeUrlOptions = {}
): string | undefined {
let normalizedUrl: string | undefined

Expand All @@ -50,7 +54,7 @@ export function normalizeUrl(
}

if (isRelativeUrl(url)) {
if (!/^[./]/.test(url) && url.indexOf('.') > 0) {
if (allowSloppyUris && !/^[#./]/.test(url) && url.indexOf('.') > 0) {
url = `https://${url}`
} else {
return undefined
Expand Down

0 comments on commit 64bdb88

Please sign in to comment.