Skip to content

Commit

Permalink
Prompt eng and handle errors
Browse files Browse the repository at this point in the history
  • Loading branch information
tedspare committed Oct 15, 2024
1 parent 82888de commit 457e98f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 32 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-15] [Prompt eng and handle errors](https://github.com/RubricLab/memory/commit/fd5267164d4af76b0ec26846cb0f54f906825f82)
- [2024-10-15] [Remove user filter](https://github.com/RubricLab/memory/commit/a879c56559e8cbbd82b317ed53d8c49cf0aab98d)
- [2024-10-15] [Parallelize embedding, upsertion, and update for perf](https://github.com/RubricLab/memory/commit/38f574d2ba7c00f74f9a97a8e21e05315b47f118)
- [2024-10-15] [Clean up](https://github.com/RubricLab/memory/commit/0d5f66305ae9c973f7667576b8b7a966c67acbd7)
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.26",
"version": "0.0.27",
"private": false,
"type": "module",
"devDependencies": {
Expand Down
66 changes: 35 additions & 31 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ export class Memory {
}),
prompt: clean`Please extract all facts from the following passage.
In case of first-person statements, portray the first-person as "user".
In case of second-person statements, refer to yourself as "system".
In case of third-person statements, portray the third-party directly.
In case of contradiction, try to capture the most up-to-date state of affairs in present tense.
Passage:
"${content}"`
Expand Down Expand Up @@ -117,19 +119,26 @@ export class Memory {
{ userId = this.userId }: { userId?: string }
) {
const tags = 'tags' in props ? props.tags : [props.tag]
const ids = tags.map(() => uid())

if (tags.length === 0) return []

const vectors = await this.embed({ texts: tags })
const vectorStrings = vectors.map(v => JSON.stringify(v))
const rows = tags.map((t, i) => `('${ids[i]}', '${t}', '${vectorStrings[i]}', ${userId})`)

const template = clean`insert into tag (id, body, vector, "userId")
const values = tags.map((t, i) => ({
id: uid(),
body: t,
vector: vectors[i],
userId
}))

const rows = values.map(t => Prisma.sql`(${t.id}, ${t.body}, ${t.vector}, ${t.userId})`)

const query = Prisma.sql`insert into tag (id, body, vector, "userId")
values
${rows.join(', ')}
${Prisma.join(rows, ',')}
on conflict (body, "userId") do nothing
returning id`

const query = Prisma.sql([template])

const inserted: { id: string }[] = await this.db.$queryRaw(query)

return inserted
Expand Down Expand Up @@ -185,6 +194,11 @@ export class Memory {
this.extractFacts({ content })
])

console.log('facts', facts)
console.log('tags', tags)

console.log(`extract completed: ${(performance.now() - start).toFixed(2)}ms`)

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

const similarTagSearchRes = await Promise.all(uniqueTags.map(tag => this.search(tag, { userId })))
Expand All @@ -205,26 +219,19 @@ export class Memory {

const combinedTagIds = [...uniqueSimilarTagIds, ...netNewTagIds]

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

Expand All @@ -233,7 +240,7 @@ export class Memory {
const {
object: { corrections }
} = await generateObject({
model: openai(this.model),
model: openai('gpt-4o-2024-08-06'),
schema: z.object({
corrections: z
.array(
Expand All @@ -244,19 +251,19 @@ export class Memory {
)
.optional()
}),
prompt: clean`Given the following statements and some new information, please identify any statements which have been falsified.
prompt: clean`Given the following statements and some new information, please identify any statements which have been strictly falsified.
Prior statements:
"""
${relatedFacts.map((r, i) => `${i}. ${r.fact.body}`).join('\n')}
${relatedFacts.map((r, i) => `${i}. ${r.body}`).join('\n')}
"""
New information:
"""
${facts.map(f => `- ${f}`).join('\n')}
"""
It is not always necessary to update the returned statements.
New information will be appended by default.
`
})

Expand All @@ -269,16 +276,12 @@ export class Memory {

if (!outdated) return []

return this.db.relationship.update({
return this.db.fact.update({
where: {
id: outdated.id
},
data: {
fact: {
update: {
body: newStatement
}
}
body: newStatement
}
})
}) || []
Expand Down Expand Up @@ -316,6 +319,7 @@ export class Memory {
const created = await this.db.$transaction([...updates, ...creates])

console.log(chalk.green(`Added ${created?.length} facts`))
console.log(chalk.yellow(`Updated ${updates?.length} facts`))

console.log(`Completed in ${(performance.now() - start).toFixed(2)}ms`)

Expand Down

0 comments on commit 457e98f

Please sign in to comment.