-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
115 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const EXAMPLES = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { EXAMPLES } from './examples' | ||
|
||
export const runMultiTurnExamples = async ({ | ||
fast | ||
}: { | ||
fast?: boolean | ||
}) => { | ||
for await (const eg of EXAMPLES) { | ||
console.log(eg) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { openai } from '@ai-sdk/openai' | ||
import { generateObject } from 'ai' | ||
import chalk from 'chalk' | ||
import { z } from 'zod' | ||
import { clean, format } from '../../utils/string.ts' | ||
import { EXAMPLES } from './examples.ts' | ||
|
||
export const runOneShotExamples = async ({ fast }: { fast?: boolean }) => { | ||
let totalFacts = 0 | ||
let totalRecall = 0 | ||
let totalAttempts = 0 | ||
|
||
for await (const eg of EXAMPLES) { | ||
let correctFacts = 0 | ||
|
||
totalFacts += eg.facts.length | ||
|
||
console.log(chalk.yellow(`\n\n"${eg.content}"`)) | ||
|
||
const { | ||
object: { facts: attempts } | ||
} = await generateObject({ | ||
model: openai(fast ? 'gpt-4o-mini' : 'gpt-4o-2024-08-06'), | ||
schema: z.object({ | ||
facts: z.array( | ||
z.object({ | ||
subject: z.string(), | ||
relation: z.string().describe('a verb phrase'), | ||
object: z.string(), | ||
data: z.record(z.string(), z.string()).optional().describe('to capture any additional info') | ||
}) | ||
) | ||
}), | ||
prompt: clean`Please extract all probable and implicit facts from the following passage. | ||
Portray the first-person as "user". | ||
Capture new relationships. | ||
Try to capture the most up-to-date state of affairs in present tense. | ||
Passage: | ||
"${eg.content}" | ||
` | ||
// messages: [ | ||
// { | ||
// role: 'system', | ||
// content: clean`Please extract all probable and implicit facts from the following passage. | ||
// Portray the first-person as "user". | ||
// Capture new relationships. | ||
// Try to capture the most up-to-date state of affairs in present tense.` | ||
// }, | ||
// { | ||
// role: 'user', | ||
// content: eg.content | ||
// } | ||
// ] | ||
}) | ||
|
||
const omitted: number[] = [] | ||
|
||
for (const [i, fact] of eg.facts.entries()) { | ||
console.log( | ||
`\n🎯 ${i + 1} of ${eg.facts.length}: ${chalk.magenta(fact.subject)} ${chalk.yellow(fact.relation)} ${chalk.blue(fact.object)}` | ||
) | ||
|
||
for (const [j, attempt] of attempts.entries()) { | ||
const { subject, relation, object } = attempt | ||
|
||
const correctSubject = fact.subject === subject | ||
const correctRelation = fact.relation === relation | ||
const correctObject = fact.object === object | ||
|
||
if (omitted.includes(j)) continue | ||
console.log( | ||
`🤖 ${j + 1} of ${attempts.length}: ${chalk.magenta(format(subject, correctSubject))} ${chalk.yellow( | ||
format(relation, correctRelation) | ||
)} ${chalk.blue(format(object, correctObject))}` | ||
) | ||
|
||
correctFacts += Number(correctSubject && correctRelation && correctObject) | ||
|
||
if (correctFacts) { | ||
omitted.push(j) | ||
break | ||
} | ||
} | ||
} | ||
|
||
totalRecall += correctFacts | ||
totalAttempts += attempts.length | ||
} | ||
|
||
console.log( | ||
`\n\nPrecision (% of attempts true): ${totalRecall} of ${totalAttempts} ${chalk.green(`(${~~((totalRecall / totalAttempts) * 100)}%)`)}` | ||
) | ||
console.log( | ||
`Recall (% of total facts correctly returned): ${totalRecall} of ${totalFacts} ${chalk.green(`(${~~((totalRecall / totalFacts) * 100)}%)`)}` | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters