Skip to content

Commit

Permalink
feat: Add parseJson utils
Browse files Browse the repository at this point in the history
  • Loading branch information
zAlweNy26 committed Apr 22, 2024
1 parent b76e69f commit 485ffc3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/looking_glass/output-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const agentOutputSchema = z.object({
type AgentOutput = z.infer<typeof agentOutputSchema>

export class ProceduresOutputParser extends AgentActionOutputParser {
lc_namespace = ['looking_glass', 'output-parser']
lc_namespace = ['looking_glass', 'procedures-output-parser']

async parse(output: string): Promise<AgentFinish | AgentAction> {
output += '}'
Expand Down
9 changes: 5 additions & 4 deletions src/looking_glass/stray-cat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,17 @@ export class StrayCat {
async run(msg: Message, save = true) {
log.info(`Received message from user "${this.userId}":`)
log.info(msg)

const response = this.userMessage = madHatter.executeHook('beforeReadMessage', msg, this)

// TODO: Find another way to handle this
if (response.text.length > cheshireCat.embedderSize) {
log.warn(`The input is too long. Storing it as document...`)
rabbitHole.ingestContent(this, response.text)
await rabbitHole.ingestContent(this, response.text)
return
}

try {
await this.recallRelevantMemories()
}
try { await this.recallRelevantMemories() }
catch (error) {
log.error(error)
return
Expand Down
9 changes: 4 additions & 5 deletions src/mad_hatter/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { PromptTemplate } from '@langchain/core/prompts'
import { kebabCase } from 'scule'
import type { AgentFastReply, StrayCat } from '@lg'
import { log } from '@logger'
import { parsedEnv } from '@utils'
import { parseJson, parsedEnv } from '@utils'

export enum FormState {
/**
Expand Down Expand Up @@ -246,12 +246,11 @@ Updated JSON:
structure += '\n}'

const json = (await extractionChain.invoke({ structure, stop: ['```'] })).output

log.debug(`Form JSON after parser:\n${json}`)

let output: Record<string, any> = {}

try {
output = safeDestr(json)
output = parseJson(json, this.schema)
log.debug('Form JSON after parsing:\n', output)
}
catch (error) {
output = {}
Expand Down
14 changes: 14 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { readFileSync, readdirSync } from 'node:fs'
import { type CriteriaLike, loadEvaluator } from 'langchain/evaluation'
import { z } from 'zod'
import { extendZodWithOpenApi } from 'zod-openapi'
import { safeDestr } from 'destr'

extendZodWithOpenApi(z)

Expand Down Expand Up @@ -205,3 +206,16 @@ export function getZodDefaults<T extends z.ZodTypeAny>(schema: T, discriminant?:
else if (schema instanceof z.ZodDefault) return schema._def.defaultValue()
else return undefined
}

/**
* Parses a JSON string using the specified Zod schema.
* It also cleans a few common issues with generated JSON strings.
* @param text The JSON string to parse.
* @param schema The Zod schema to use for parsing.
* @throws If the JSON string is invalid or does not match the schema.
*/
export async function parseJson<T extends z.AnyZodObject>(text: string, schema: T) {
const cleaned = text.trim().replace('\_', '_').replace('\-', '-')
const json = cleaned.includes('```') ? text.split(/```(?:json)?/)[1]! : text
return await schema.parseAsync(safeDestr(json)) as z.infer<T>
}

0 comments on commit 485ffc3

Please sign in to comment.