Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/search/.env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
VECTOR_STORE_ID=
STORE_ID=
MXBAI_API_KEY=
2 changes: 1 addition & 1 deletion packages/search/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"check-types": "tsc --noEmit"
},
"dependencies": {
"@mixedbread/sdk": "^0.26.0",
"@mixedbread/sdk": "^0.78.0",
"@radix-ui/react-dialog": "^1.1.14",
"@radix-ui/react-scroll-area": "^1.2.9",
"@radix-ui/react-slot": "^1.2.3",
Expand Down
4 changes: 2 additions & 2 deletions packages/search/src/app/api/search/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ interface SearchMetadata {
export const GET = adapters.nextAppHandler({
transform: (results) => {
return results.map((result) => {
const metadata = result.chunks?.[0].generated_metadata as SearchMetadata;
const metadata = (result.generated_metadata ?? {}) as SearchMetadata;

return {
id: result.id,
id: result.file_id,
url: metadata.url || "#",
title: metadata.title || "Untitled",
tag: metadata.tag || "",
Expand Down
14 changes: 7 additions & 7 deletions packages/search/src/search/lib/fumadocs.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type Mixedbread from "@mixedbread/sdk";
import type { VectorStoreSearchResponse } from "@mixedbread/sdk/resources/vector-stores";
import type { StoreSearchResponse } from "@mixedbread/sdk/resources/stores";
import type { SearchMetadata } from "./types";

export interface MixedbreadOptions {
/**
* The ID of the store to search in
*/
vectorStoreId: string;
storeId: string;

/**
* The Mixedbread SDK client instance
Expand All @@ -31,30 +31,30 @@ interface SortedResult {
url: string;
}

type VectorStoreSearchResult = VectorStoreSearchResponse["data"][number] & {
type StoreSearchResult = StoreSearchResponse["data"][number] & {
generated_metadata: SearchMetadata;
};

export async function search(
query: string,
options: MixedbreadOptions
): Promise<SortedResult[]> {
const { client, vectorStoreId, tag } = options;
const { client, storeId, tag } = options;

if (!query.trim()) {
return [];
}

const res = await client.vectorStores.search({
const res = await client.stores.search({
query,
vector_store_identifiers: [vectorStoreId],
store_identifiers: [storeId],
top_k: 10,
search_options: {
return_metadata: true,
},
});

const results = (res.data as VectorStoreSearchResult[])
const results = (res.data as StoreSearchResult[])
.filter((item) => {
const metadata = item.generated_metadata;
return !tag || metadata.tag === tag;
Expand Down
19 changes: 12 additions & 7 deletions packages/search/src/search/lib/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export async function search(
rawParams: Record<string, unknown>,
transform?: TransformFunc
): Promise<Result[]> {
if (!process.env.MXBAI_API_KEY || !process.env.VECTOR_STORE_ID) {
if (!process.env.MXBAI_API_KEY || !process.env.STORE_ID) {
throw new InternalServerError("Environment setup failed");
}

Expand All @@ -22,24 +22,29 @@ export async function search(

const { query, topK } = data;

const res = await mxbai.vectorStores.files.search({
const res = await mxbai.stores.search({
query,
vector_store_identifiers: [process.env.VECTOR_STORE_ID],
store_identifiers: [process.env.STORE_ID],
top_k: topK,
search_options: {
return_metadata: true,
return_chunks: true,
},
});

// Chunks come back best-first, so the first one per file is its best match
const files = res.data.filter(
(item, index) =>
res.data.findIndex((other) => other.file_id === item.file_id) === index
);

if (transform) {
return transform(res.data);
return transform(files);
}

const results = res.data.map((item) => {
const results = files.map((item) => {
const metadata = item.metadata as SearchMetadata;
return {
id: item.id,
id: item.file_id,
url: metadata?.url || "#",
title: metadata?.title || "Untitled",
tag: metadata?.tag || "all",
Expand Down
4 changes: 2 additions & 2 deletions packages/search/src/search/lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { FileSearchResponse } from "@mixedbread/sdk/resources/vector-stores.mjs";
import type { StoreSearchResponse } from "@mixedbread/sdk/resources/stores";
import type { z } from "zod";
import type { SearchQuerySchema } from "./validations";

Expand All @@ -21,7 +21,7 @@ export interface Result {

export type SearchQuery = z.infer<typeof SearchQuerySchema>;

export type TransformFunc = (results: FileSearchResponse["data"]) => Result[];
export type TransformFunc = (results: StoreSearchResponse["data"]) => Result[];

export type Message =
| {
Expand Down
16 changes: 8 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading