Skip to content

[Store] Add InMemory #109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

Conversation

Guikingone
Copy link

@Guikingone Guikingone commented Jul 13, 2025

Q A
Bug fix? no
New feature? yes
Docs? yes
Issues None
License MIT

Hi 👋🏻

This PR aim to introduce an InMemoryStore to store vectors (mostly used for tests environments and/or POCs), the implementation is based around cosine similarity (others search algorithms can be used but will requires a dedicated interface like SearchStrategyInterface or other).

This store also allows to retrieve only a specific amount of items if required.

This store don't use initialize as everything is stored in an array.

PS: An optimized approach would be to use fpow but it requires 8.4 so ... Well, maybe for later 😄

Copy link
Member

@Nyholm Nyholm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea. Thank you

@chr-hertel chr-hertel added the Store Issues & PRs about the AI Store component label Jul 13, 2025
Copy link
Contributor

@chr-hertel chr-hertel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is super cool - found only smaller items 👍

Thanks already @Guikingone!

Comment on lines +26 to +28
foreach ($documents as $document) {
$this->documents[] = $document;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could use array_push instead of iterating over all documents to add:

Suggested change
foreach ($documents as $document) {
$this->documents[] = $document;
}
array_push($this->documents, $documents);

see https://www.php.net/array_push

Comment on lines +39 to +43
callback: fn (VectorDocument $vectorDocument): array => [
'distance' => $this->cosineSimilarity($vectorDocument, $vector),
'document' => $vectorDocument,
],
array: $this->documents,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

named arguments don't add value here or do I miss something?

Suggested change
callback: fn (VectorDocument $vectorDocument): array => [
'distance' => $this->cosineSimilarity($vectorDocument, $vector),
'document' => $vectorDocument,
],
array: $this->documents,
fn (VectorDocument $vectorDocument): array => [
'distance' => $this->cosineSimilarity($vectorDocument, $vector),
'document' => $vectorDocument,
],
$this->documents,

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same for the others down below

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Needs Work Store Issues & PRs about the AI Store component
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants