-
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
135 additions
and
11 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
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 |
---|---|---|
@@ -1 +1,32 @@ | ||
export const EXAMPLES = [] | ||
import type { Fact } from '@/types' | ||
|
||
type Example = { | ||
messages: { facts: Fact[]; content: string }[] | ||
} | ||
|
||
export const EXAMPLES: Example[] = [ | ||
{ | ||
messages: [ | ||
{ | ||
content: 'I am vegan', | ||
facts: [ | ||
{ | ||
subject: 'user', | ||
relation: 'is', | ||
object: 'vegan' | ||
} | ||
] | ||
}, | ||
{ | ||
content: 'I am not vegan', | ||
facts: [ | ||
{ | ||
subject: 'user', | ||
relation: 'is not', | ||
object: 'vegan' | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] |
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 |
---|---|---|
@@ -1,11 +1,93 @@ | ||
import { Database } from 'bun:sqlite' | ||
import { Memory } from '@/index' | ||
import type { Fact } from '@/types' | ||
import { format } from '@/utils/string' | ||
import type { openai } from '@ai-sdk/openai' | ||
import { Memory } from '../..' | ||
import chalk from 'chalk' | ||
import { EXAMPLES } from './examples' | ||
|
||
const db = new Database(':memory:', { create: true, strict: true }) | ||
|
||
await db | ||
.prepare( | ||
'create table if not exists facts (subject text, relation text, object text, primary key (subject, object))' | ||
) | ||
.get() | ||
|
||
export const runMultiTurnExamples = async ({ model }: { model: Parameters<typeof openai>[0] }) => { | ||
const memory = new Memory({ model }) | ||
|
||
let totalFacts = 0 | ||
let totalRecall = 0 | ||
let totalAttempts = 0 | ||
|
||
for await (const eg of EXAMPLES) { | ||
console.log(eg) | ||
for await (const message of eg.messages) { | ||
totalFacts += message.facts.length | ||
|
||
console.log(chalk.yellow(`\n\n"${message.content}"`)) | ||
|
||
const { facts: attempts } = await memory.extract({ | ||
content: message.content | ||
}) | ||
|
||
const omitted: number[] = [] | ||
|
||
for (const [i, fact] of message.facts.entries()) { | ||
let correctFacts = 0 | ||
|
||
console.log( | ||
`\n🎯 ${i + 1} of ${message.facts.length}: ${chalk.magenta(fact.subject)} ${chalk.yellow(fact.relation)} ${chalk.blue(fact.object)}` | ||
) | ||
|
||
for (const attempt of attempts) { | ||
const { subject, relation, object } = attempt | ||
|
||
db | ||
.prepare(` | ||
insert into facts (subject, relation, object) | ||
values ($1, $2, $3) | ||
on conflict (subject, object) do update set relation = $2 | ||
`) | ||
.run(subject, relation, object) | ||
} | ||
|
||
const newFacts = db.query('select * from facts').all() | ||
console.log({ newFacts }) | ||
|
||
for (const [k, newFact] of newFacts.entries()) { | ||
const { subject, relation, object } = newFact as Fact | ||
|
||
const correctSubject = fact.subject === subject | ||
const correctRelation = fact.relation === relation | ||
const correctObject = fact.object === object | ||
|
||
console.log( | ||
`🤖 ${k + 1} of ${newFacts.length}: ${chalk.magenta(format(subject, correctSubject))} ${chalk.yellow( | ||
format(relation, correctRelation) | ||
)} ${chalk.blue(format(object, correctObject))}` | ||
) | ||
|
||
if (omitted.includes(k)) continue | ||
|
||
correctFacts += Number(correctSubject && correctRelation && correctObject) | ||
|
||
if (correctFacts) { | ||
omitted.push(k) | ||
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
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ESNext", | ||
"baseUrl": ".", | ||
"paths": { | ||
"@/*": ["./src/*"] | ||
|