Skip to content

Commit

Permalink
feat: conversational forms now work
Browse files Browse the repository at this point in the history
  • Loading branch information
zAlweNy26 committed Apr 9, 2024
1 parent d24498d commit 060f2dc
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions src/mad_hatter/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ JSON:

const res = await this.cat.llm(confirmPrompt, true)

return res.toLowerCase().includes('true')
return JSON.stringify(res).toLowerCase().includes('true')
}

private async checkExitIntent() {
Expand Down Expand Up @@ -143,7 +143,7 @@ JSON:

const res = await this.cat.llm(checkExitPrompt, true)

return res.toLowerCase().includes('true')
return JSON.stringify(res).toLowerCase().includes('true')
}

async next(): Promise<AgentFastReply> {
Expand Down Expand Up @@ -183,35 +183,42 @@ JSON:

private async extract() {
const history = this.stringifyChatHistory()
let structure = '{'
for (const key in this.schema.shape) {
const zodType = ((this.schema.shape[key]!._def as any).typeName as string).replace('Zod', '').toLowerCase()
structure += `\n\t"${key}": // ${this.schema.shape[key]!.description ?? ''} must be of type ${zodType}`
}
structure += '\n}'

const prompt = `Your task is to fill up a JSON out of a conversation.
The JSON must have this format:
${structure}
\`\`\`json
{structure}
\`\`\`
This is the current JSON:
${JSON.stringify(this.model, null, 4)}
\`\`\`json
${JSON.stringify(this.model, null, 4).replace('{', '{{').replace('}', '}}')}
\`\`\`
This is the conversation:
${history}
Updated JSON:`
Updated JSON:
\`\`\`json`

log.debug(prompt)

const extractionChain = new LLMChain({
llm: this.cat.currentLLM,
prompt: PromptTemplate.fromTemplate(prompt.replace('{', '{{').replace('}', '}}')),
prompt: PromptTemplate.fromTemplate(prompt),
verbose: parsedEnv.verbose,
outputKey: 'output',
})

const json = (await extractionChain.invoke({ stop: ['}'] })).output
let structure = '{'
for (const key in this.schema.shape) {
const zodType = ((this.schema.shape[key]!._def as any).typeName as string).replace('Zod', '').toLowerCase()
structure += `\n\t"${key}": // ${this.schema.shape[key]!.description ?? ''} must be of type ${zodType}`
}
structure += '\n}'

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

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

Expand Down

0 comments on commit 060f2dc

Please sign in to comment.