Skip to content

Commit 5c7fddd

Browse files
committed
working !
1 parent 8aa2581 commit 5c7fddd

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

packages/compass-vector-embedding-visualizer/src/components/vector-visualizer.tsx

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,45 @@
11
import React, { useEffect, useState } from 'react';
22
import { connect } from 'react-redux';
33
import Plotly from 'plotly.js';
4-
const PCA = require('ml-pca');
4+
import * as PCA from 'ml-pca';
55
import type { Binary } from 'mongodb';
66
import type { Document } from 'bson';
77

88
import type { VectorEmbeddingVisualizerState } from '../stores/reducer';
99
import { loadDocuments } from '../stores/visualization';
1010
import { ErrorSummary } from '@mongodb-js/compass-components';
11+
import { collectionModelLocator } from '@mongodb-js/compass-app-stores/provider';
1112

1213
type HoverInfo = { x: number; y: number; text: string } | null;
1314

1415
export interface VectorVisualizerProps {
15-
onFetchDocs: (namespace: string) => void;
16+
onFetchDocs: () => void;
1617
docs: Document[];
1718
loadingDocumentsState: 'initial' | 'loading' | 'loaded' | 'error';
1819
loadingDocumentsError: Error | null;
19-
collection: { namespace: string };
2020
}
2121

2222
function normalizeTo2D(vectors: Binary[]): { x: number; y: number }[] {
2323
const raw = vectors.map((v) => Array.from(v.toFloat32Array()));
24-
const pca = new PCA(raw);
24+
const pca = new PCA.PCA(raw);
2525
const reduced = pca.predict(raw, { nComponents: 2 }).to2DArray();
26-
return reduced.map(([x, y]: [number, number]) => ({ x, y }));
26+
return reduced.map(([x, y]) => ({ x, y }));
2727
}
2828

2929
const VectorVisualizer: React.FC<VectorVisualizerProps> = ({
3030
onFetchDocs,
3131
docs,
3232
loadingDocumentsState,
3333
loadingDocumentsError,
34-
collection,
3534
}) => {
3635
const [hoverInfo, setHoverInfo] = useState<HoverInfo>(null);
3736

3837
useEffect(() => {
3938
if (loadingDocumentsState === 'initial') {
4039
// Fetch the documents when the component mounts when they aren't already loaded.
41-
onFetchDocs(collection.namespace);
40+
onFetchDocs();
4241
}
43-
}, [loadingDocumentsState, onFetchDocs, collection.namespace]);
42+
}, [loadingDocumentsState, onFetchDocs]);
4443

4544
useEffect(() => {
4645
const container = document.getElementById('vector-plot');
@@ -50,14 +49,11 @@ const VectorVisualizer: React.FC<VectorVisualizerProps> = ({
5049

5150
const plot = async () => {
5251
try {
53-
const ns = collection?.namespace;
54-
if (!ns) return;
55-
5652
const vectors = docs.map((doc) => doc.review_vec).filter(Boolean);
5753

5854
if (!vectors.length) return;
5955

60-
const points = normalizeTo2D(vectors);
56+
const points = normalizeTo2D(vectors.slice(0, 50));
6157

6258
await Plotly.newPlot(
6359
container,
@@ -134,7 +130,7 @@ const VectorVisualizer: React.FC<VectorVisualizerProps> = ({
134130
return () => {
135131
abortController.abort();
136132
};
137-
}, [docs, collection.namespace]);
133+
}, [docs]);
138134

139135
return (
140136
<div style={{ position: 'relative', width: '100%', height: '100%' }}>

packages/compass-vector-embedding-visualizer/src/stores/visualization.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,23 @@ export const visualizationReducer: Reducer<VisualizationState> = (
6969
return state;
7070
};
7171

72-
export function loadDocuments(
73-
namespace: string
74-
): VectorEmbeddingVisualizerThunkAction<
72+
export function loadDocuments(): VectorEmbeddingVisualizerThunkAction<
7573
Promise<void>,
7674
| FetchDocumentsStartedAction
7775
| FetchDocumentsSuccessAction
7876
| FetchDocumentsFailedAction
7977
> {
80-
return async (dispatch, getState, { dataService }) => {
78+
return async (dispatch, getState, { dataService, collection }) => {
8179
dispatch({
8280
type: VisualizationActionTypes.FETCH_DOCUMENTS_STARTED,
8381
});
8482

8583
try {
86-
const docs = await dataService.find(namespace, {}, { limit: 1000 });
84+
const docs = await dataService.find(
85+
`${collection.database}.${collection.name}`,
86+
{},
87+
{ limit: 1000 }
88+
);
8789

8890
dispatch({
8991
type: VisualizationActionTypes.FETCH_DOCUMENTS_SUCCESS,

0 commit comments

Comments
 (0)