Skip to content

Commit

Permalink
Add getAll route
Browse files Browse the repository at this point in the history
  • Loading branch information
tedspare committed Oct 11, 2024
1 parent c992b2a commit 686a932
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 26 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-11] [Add getAll route](https://github.com/RubricLab/memory/commit/71eb1f5d7afbb26390d2b2617081b46301e9022c)
- [2024-10-10] [Expand on unique tagging](https://github.com/RubricLab/memory/commit/3c550db0b9cce3ce9f54fc56ecc6716d8475b397)
- [2024-10-10] [Add typesafe SQL setup](https://github.com/RubricLab/memory/commit/9acd10a3be5e0335970ca8f551a03847e04fcdda)
- [2024-10-10] [Use relative import](https://github.com/RubricLab/memory/commit/2161e8cc919ac63da4b092b61ff811566417bc08)
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.19",
"version": "0.0.20",
"private": false,
"type": "module",
"devDependencies": {
Expand Down
85 changes: 60 additions & 25 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,40 @@ export class Memory {
const vector = await this.embed(query)

// biome-ignore lint/suspicious/noExplicitAny: <explanation>
return await this.db.$queryRawTyped(searchVector(vector as any, threshold, limit))
const res = await this.db.$queryRawTyped(searchVector(vector as any, threshold, limit))
return res
}

async getAll(args?: { where: { tags: string[] } }) {
const { where } = args || {}

const relations = await this.db.relationship.findMany({
where: {
...(where?.tags
? {
tag: {
body: {
in: where.tags
}
}
}
: {})
},
select: {
fact: {
select: {
body: true
}
},
tag: {
select: {
body: true
}
}
}
})

return relations
}

async ingest({ content }: { content: string }) {
Expand All @@ -133,6 +166,7 @@ export class Memory {

const uniqueTags = [...new Set(tags)]

// TODO: pass these back to AI to update existing facts
const similarTags = await Promise.all(uniqueTags.map(tag => this.search(tag)))
const similarTagIds = similarTags.flatMap(t => t[0]?.id || [])
console.log('similarTags', similarTags)
Expand Down Expand Up @@ -172,31 +206,32 @@ export class Memory {
})

const created = await this.db.$transaction(creates)
console.log({ created })

const relatedFacts = await this.db.relationship.findMany({
where: {
tag: {
id: {
in: allTagIds
}
}
},
select: {
fact: {
select: {
body: true
}
},
tag: {
select: {
body: true
}
}
}
})

console.log('relatedFacts', relatedFacts)
console.log(`Added ${created?.length} facts`)

// const relatedFacts = await this.db.relationship.findMany({
// where: {
// tag: {
// id: {
// in: allTagIds
// }
// }
// },
// select: {
// fact: {
// select: {
// body: true
// }
// },
// tag: {
// select: {
// body: true
// }
// }
// }
// })

// console.log('relatedFacts', relatedFacts)

return { tags, facts }
}
Expand Down

0 comments on commit 686a932

Please sign in to comment.