Skip to content

Commit

Permalink
feat(Memory): Add route to retrieve documents by metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
zAlweNy26 committed Apr 11, 2024
1 parent a8b30ce commit 9cb6a4b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/memory/vector-memory-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,11 @@ export class VectorMemoryCollection {
return documents
}

async getAllPoints(limit = 10000) {
async getAllPoints(limit = 10000, filter?: Record<string, FilterMatch>) {
const list = await vectorDb.scroll(this.name, {
filter: filter ? this.filterFromDict(filter) : undefined,
with_vector: true,
with_payload: true,
limit,
})
return list.points as PointData[]
Expand Down
72 changes: 70 additions & 2 deletions src/routes/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,69 @@ export const memory: FastifyPluginCallback = (fastify, opts, done) => {
}
})

fastify.post<{
Params: { collectionId: string }
Querystring: {
k?: number
}
Body: {
metadata: Record<string, any>
}
}>('/collections/:collectionId/documents', { schema: {
description: 'Get list of documents filtered by metadata.',
tags: ['Memory'],
summary: 'Get collection\'s documents by metadata',
externalDocs: {
description: 'Metadata filtering conditions',
url: 'https://qdrant.tech/documentation/concepts/filtering/#filtering-conditions',
},
params: {
type: 'object',
properties: {
collectionId: { type: 'string' },
},
},
querystring: {
type: 'object',
required: [],
properties: {
k: { type: 'number', default: 10 },
},
},
body: {
type: 'object',
properties: {
metadata: { type: 'object' },
},
},
response: {
200: {
type: 'object',
properties: {
documents: { type: 'array', items: { type: 'object', additionalProperties: true } },
},
},
404: { $ref: 'HttpError' },
500: { $ref: 'HttpError' },
},
} }, async (req, rep) => {
const id = req.params.collectionId
const limit = req.query.k
const metadata = req.body.metadata
try {
const collections = Object.keys(cheshireCat.currentMemory.collections)
if (!collections.includes(id)) { return rep.notFound('Collection not found.') }
const points = await cheshireCat.currentMemory.collections[id]!.getAllPoints(limit, metadata)
return {
documents: points.map(p => ({ ...p.payload, id: p.id })),
}
}
catch (error) {
log.error(error)
return rep.internalServerError(`Error while retrieving "${id}" collection's documents.`)
}
})

fastify.delete<{
Params: { collectionId: string }
Body: {
Expand All @@ -146,6 +209,10 @@ export const memory: FastifyPluginCallback = (fastify, opts, done) => {
description: 'Delete points in memory by filter.',
tags: ['Memory'],
summary: 'Wipe memory points by metadata',
externalDocs: {
description: 'Metadata filtering conditions',
url: 'https://qdrant.tech/documentation/concepts/filtering/#filtering-conditions',
},
params: {
type: 'object',
properties: {
Expand Down Expand Up @@ -183,7 +250,7 @@ export const memory: FastifyPluginCallback = (fastify, opts, done) => {
collectionId: string
pointId: string
}
}>('/collections/:collectionId/points/:memoryId', { schema: {
}>('/collections/:collectionId/points/:pointId', { schema: {
description: 'Delete a specific point in memory.',
tags: ['Memory'],
summary: 'Wipe memory point',
Expand Down Expand Up @@ -240,8 +307,9 @@ export const memory: FastifyPluginCallback = (fastify, opts, done) => {
response: {
204: { description: 'The conversation history was wiped successfully.', type: 'null' },
},
} }, (req) => {
} }, (req, rep) => {
req.stray.clearHistory()
return rep.code(204)
})

done()
Expand Down

0 comments on commit 9cb6a4b

Please sign in to comment.