Skip to content

Commit

Permalink
fix: save embeds to db
Browse files Browse the repository at this point in the history
  • Loading branch information
jedwards1230 committed May 15, 2024
1 parent e0754ce commit 26c2b79
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 25 deletions.
6 changes: 0 additions & 6 deletions apps/server/src/modules/LLMNexusController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,6 @@ export class LLMNexusController {
`\n\nThe following are your memories, influenced by recent conversation:\n\n` +
JSON.stringify(ragRes.map((d) => d.metadata));

logger.debug("Generating chat response", {
activeMessageContent,
systemMessage,
functionName: "LLMNexusController.generateChatResponse",
});

const completion = await llmServce.createChatCompletion(
messages.map((m) => m.toJSON()),
opts
Expand Down
6 changes: 2 additions & 4 deletions packages/db/src/migration/1715762548551-typeorm_override.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@ export class TypeormOverride1715762548551 implements MigrationInterface {

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`CREATE EXTENSION IF NOT EXISTS vector;`);
await queryRunner.query(`DROP TABLE IF EXISTS embed_item;`);
await queryRunner.query(
`CREATE TABLE embed_item (id bigserial PRIMARY KEY, embedding vector(1536) NOT NULL)`
`ALTER TABLE embed_item ALTER COLUMN embedding TYPE vector(1536) USING embedding::vector(1536);`
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`DROP TABLE IF EXISTS embed_item;`);
await queryRunner.query(
`CREATE TABLE embed_item (id bigserial PRIMARY KEY, embedding text NOT NULL)`
`ALTER TABLE embed_item ALTER COLUMN embedding TYPE text USING embedding::text;`
);
}
}
30 changes: 15 additions & 15 deletions packages/db/src/repository/DocumentRepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ export const extendedDocumentRepo = (ds: DataSource) => {
return ds.getRepository(DatabaseDocument).extend({
async addDocuments(...docs: DocumentInsertParams[]): Promise<DatabaseDocument[]> {
const embeddings = await generateEmbeddings(docs.map((doc) => doc.decoded));
const docsPromise = docs.map((doc, i) => {
try {
const embedding = embedItemRepo.create({
embedding: pgvector.toSql(embeddings[i]),
});
return this.create({ ...doc, embedding: { id: embedding.id } });
} catch (error) {
logger.error("Error adding document", {
error,
functionName: "extendedDocumentRepo.addDocuments",
});
throw error;
}
});
return Promise.all(docsPromise);

const savedEmbeddings = await embedItemRepo.save(
embeddings.map((e) =>
embedItemRepo.create({ embedding: pgvector.toSql(e) })
)
);

const savedDocs = await this.save(
docs.map((doc, i) => ({
...doc,
embedding: savedEmbeddings[i],
}))
);

return savedDocs;
},

async searchDocuments(
Expand Down

0 comments on commit 26c2b79

Please sign in to comment.