Skip to content

Commit 2f73cc3

Browse files
committed
fix vercel AI RAG sample
1 parent 59fa610 commit 2f73cc3

File tree

3 files changed

+116
-285
lines changed

3 files changed

+116
-285
lines changed
Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import "dotenv/config";
22

3-
import { generateText, tool } from "ai";
3+
import { generateText } from "ai";
44
import { FGAFilter } from "@auth0/ai";
55
import {
66
DocumentWithScore,
@@ -11,6 +11,9 @@ import { openai } from "@ai-sdk/openai";
1111
import { z } from "zod";
1212

1313
async function main() {
14+
console.log(
15+
"\n..:: Vercel AI SDK Example: Retrievers with Auth0 FGA (Fine-Grained Authorization)\n\n"
16+
);
1417
// User ID
1518
const user = "user1";
1619
// User query
@@ -31,33 +34,42 @@ async function main() {
3134
}),
3235
});
3336

34-
// 4. Generate a response using Vercel AI SDK
35-
const result = await generateText({
37+
// 3. Search for relevant documents
38+
const results = await vectorStore.search(prompt, 20);
39+
40+
// 4. Filter documents based on user permissions
41+
const context = await retriever.filter(results);
42+
43+
// 5. Generate a response using Vercel AI SDK
44+
const { text } = await generateText({
3645
model: openai("gpt-4o-mini"),
37-
prompt,
38-
system: `You are a helpful assistant. Use the tool to get information regarding ZEKO and answer the user's question based on that information.`,
39-
tools: {
40-
getInformation: tool({
41-
description: `get information from your knowledge base to answer questions.`,
42-
parameters: z.object({
43-
question: z.string().describe("the users question"),
44-
}),
45-
execute: async ({ question }) => {
46-
// Search for relevant documents
47-
const results = await vectorStore.search(question, 20);
48-
49-
// Filter documents based on user permissions
50-
const context = await retriever.filter(results);
51-
52-
// return context.map((c) => c.document.text).join("\n\n");
53-
return context;
54-
},
55-
}),
56-
},
46+
prompt: `Answer the following question based only on the provided context:
47+
${context.map((c) => c.document.text).join("\n\n")}
48+
49+
Question: ${prompt}`,
5750
});
5851

59-
// 5. Print the answer
60-
console.log(result.text);
52+
// 6. Print the answer
53+
console.log(text);
54+
55+
/**
56+
* Can also be used as a tool to provide context to the Agent
57+
const getFinancialInfo = tool({
58+
description: `get information from your knowledge base to answer questions.`,
59+
parameters: z.object({
60+
question: z.string().describe("the users question"),
61+
}),
62+
execute: async ({ question }) => {
63+
// Search for relevant documents
64+
const results = await vectorStore.search(question, 20);
65+
66+
// Filter documents based on user permissions
67+
const context = await retriever.filter(results);
68+
69+
return context.map((c) => c.document.text).join("\n\n");
70+
},
71+
});
72+
*/
6173
}
6274

6375
main().catch(console.error);

0 commit comments

Comments
 (0)