Skip to content

Commit 235e9b4

Browse files
committed
wip
1 parent d5802da commit 235e9b4

File tree

4 files changed

+100
-35
lines changed

4 files changed

+100
-35
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@
6060
"shiki": "^3.6.0",
6161
"strip-markdown": "^6.0.0",
6262
"unified": "^11.0.5",
63-
"vfile-matter": "^5.0.1"
63+
"vfile-matter": "^5.0.1",
64+
"zod": "^3.25.76"
6465
},
6566
"devDependencies": {
6667
"@tailwindcss/postcss": "^4.1.9",

pnpm-lock.yaml

Lines changed: 36 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lib/rag.mjs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { tool } from "ai";
2+
import { z } from "zod";
3+
import { getContext } from "./smart-search.mjs";
4+
5+
export const smartSearchTool = tool({
6+
description:
7+
"Search for information about Faust using WP Engine Smart Search. Use this to answer questions about Faust, its features, capabilities, and more when the information is not already known.",
8+
parameters: z.object({
9+
query: z
10+
.string()
11+
.describe(
12+
"The search query to find relevant Faust information based on the user's question.",
13+
),
14+
}),
15+
execute: async ({ query }) => {
16+
console.log(`[Tool Execution] Searching with query: "${query}"`);
17+
try {
18+
const context = await getContext(query);
19+
20+
if (context.errors && context.errors.length > 0) {
21+
console.error(
22+
"[Tool Execution] Error fetching context:",
23+
context.errors,
24+
);
25+
// Return a structured error message that the LLM can understand
26+
return {
27+
error: `Error fetching context: ${context.errors[0].message}`,
28+
};
29+
}
30+
31+
if (context?.data?.similarity?.docs?.length === 0) {
32+
console.log("[Tool Execution] No documents found for query:", query);
33+
return {
34+
searchResults: "No relevant information found for your query.",
35+
};
36+
}
37+
38+
const formattedResults = context.data.similarity.docs.map((doc) => {
39+
if (!doc) {
40+
return {};
41+
}
42+
43+
return {
44+
id: doc.id,
45+
title: doc.data.post_title,
46+
content: doc.data.post_content,
47+
url: doc.data.post_url,
48+
categories: doc.data.categories.map((category) => category.name),
49+
searchScore: doc.score,
50+
};
51+
});
52+
53+
// console.log("[Tool Execution] Search results:", formattedResults);
54+
55+
return { searchResults: formattedResults }; // Return the formatted string
56+
} catch (error) {
57+
console.error("[Tool Execution] Exception:", error);
58+
return { error: `An error occurred while searching: ${error.message}` };
59+
}
60+
},
61+
});

src/lib/smart-search.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const token = env.NEXT_SEARCH_ACCESS_TOKEN ?? "";
55

66
// Field might need to be adjusted to include the correct field for your search index, e.g. "content", "post_title", etc.
77
export async function getContext(message) {
8-
const query = `query GetRagContext($message: String!) {
8+
const query = `query GetContext($message: String!) {
99
similarity(
1010
input: {
1111
nearest: {

0 commit comments

Comments
 (0)