Skip to content

Commit

Permalink
Fix post-insert return
Browse files Browse the repository at this point in the history
  • Loading branch information
tedspare committed Oct 10, 2024
1 parent 9be2f96 commit 009f7ab
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
- [2024-10-10] [Fix post-insert return](https://github.com/RubricLab/memory/commit/a79a936cff2baadbd5411caec354579b47d8c846)
- [2024-10-10] [Support more DBs. Simplify types.](https://github.com/RubricLab/memory/commit/c9500b038646adac4a4250b9956914bafbb53bea)
- [2024-10-10] [Add main to pkg](https://github.com/RubricLab/memory/commit/ef8082790fbd93c6ae2b4b47112bc1986dd8f0ab)
- [2024-10-10] [Move DB to top-level](https://github.com/RubricLab/memory/commit/67913408246413448008f902963eb5a288b963d8)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@rubriclab/memory",
"module": "src/index.ts",
"main": "src/index.ts",
"version": "0.0.15",
"version": "0.0.16",
"private": false,
"type": "module",
"devDependencies": {
Expand Down
3 changes: 2 additions & 1 deletion src/evals/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const args = parseArgs({

if (import.meta.path === Bun.main) {
const { help, sota, dataset } = args.values

if (help) {
console.log(`
Usage: bun evals/index.ts [options]
Expand All @@ -41,7 +42,7 @@ if (import.meta.path === Bun.main) {
const bunDB = new Database(':memory:', { create: true, strict: true })

const db = {
execute: async (cmd: string) => await bunDB.prepare(cmd).get()
execute: async (cmd: string) => await bunDB.prepare(cmd).all()
}

await db.execute(
Expand Down
19 changes: 14 additions & 5 deletions src/evals/multi-turn/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ export const runMultiTurnExamples = async ({ db, model }: { model: LLM; db: Data

console.log(chalk.yellow(`\n\n"${message.content}"`))

const { facts: attempts } = await memory.extract({
// Clean up DB in between conversations
const omitted: number[] = []
await db.execute('delete from facts')

const { facts: attempts } = await memory.insert({
content: message.content
})

const omitted: number[] = []

for (const [i, fact] of message.facts.entries()) {
let correctFacts = 0

Expand All @@ -39,14 +41,21 @@ export const runMultiTurnExamples = async ({ db, model }: { model: LLM; db: Data
const correctRelation = fact.relation === relation
const correctObject = fact.object === object

if (omitted.includes(k)) {
console.log(
chalk.blackBright.italic(
`🤖 ${k + 1} of ${newFacts.length}: ${subject} ${relation} ${object}`
)
)
continue
}

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) {
Expand Down
9 changes: 4 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class Memory {
"${content}"`
})

const tags = entities?.map(({ name }) => `"${name}"`).join(', ') || ''
const tags = entities?.map(({ name }) => `'${name}'`).join(', ') || ''

const relevantFacts = (await this.db.execute(
`select * from facts where subject in (${tags}) or object in (${tags})`
Expand Down Expand Up @@ -88,12 +88,11 @@ export class Memory {

await this.db.execute(`
insert into facts (subject, relation, object)
values (${subject}, ${relation}, ${object})
on conflict (subject, object) do update set relation = ${relation}
values ("${subject}", "${relation}", "${object}")
on conflict (subject, object) do update set relation = "${relation}"
`)
}

const priorFacts = await this.db.execute('select * from facts')
console.log({ priorFacts })
return { facts }
}
}

0 comments on commit 009f7ab

Please sign in to comment.