Skip to content

Commit

Permalink
fix: simplify examples
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusschiesser committed Dec 2, 2024
1 parent 314a2b8 commit 6387bc6
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 25 deletions.
50 changes: 50 additions & 0 deletions examples/vercel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Vercel Examples

These examples demonstrate how to integrate LlamaIndexTS with Vercel's AI SDK. The examples show how to use LlamaIndex for search and retrieval in both local vector store and LlamaCloud environments.

## Setup

To run these examples, first install the required dependencies from the parent folder `examples`:

```bash
npm i
```

## Running the Examples

Make sure to run the examples from the parent folder called `examples`. The following examples are available:

### Vector Store Example

Run the local vector store example with:

```bash
npx tsx vercel/vector-store.ts
```

This example demonstrates:

- Creating a vector store index from one document
- Using Vercel's AI SDK with LlamaIndex for streaming responses

### LlamaCloud Example

To run the LlamaCloud example:

```bash
npx tsx vercel/llamacloud.ts
```

This example requires a LlamaCloud API key set in your environment and an embedding model set in the `EMBEDDING_MODEL` environment variable:

```bash
export LLAMA_CLOUD_API_KEY=your_api_key_here
export EMBEDDING_MODEL="text-embedding-3-small"
```

The example demonstrates:

- Creating a LlamaCloud index from one document
- Streaming responses using Vercel's AI SDK

For more detailed information about the Vercel integration, check out [the documentation](https://ts.llamaindex.ai/docs/llamaindex/integration/vercel).
26 changes: 15 additions & 11 deletions examples/vercel/llamacloud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,24 @@ async function main() {
projectName: "Default",
apiKey: process.env.LLAMA_CLOUD_API_KEY,
});
const queryTool = llamaindex({
index,
description: "Search through the documents", // optional description
});
console.log("Successfully created index and queryTool");
console.log("Successfully created index");

streamText({
tools: { queryTool },
prompt: "Cost of moving cat from Russia to UK?",
const result = streamText({
model: openai("gpt-4o"),
onFinish({ response }) {
console.log("Response:", JSON.stringify(response.messages, null, 2));
prompt: "Cost of moving cat from Russia to UK?",
tools: {
queryTool: llamaindex({
index,
description:
"get information from your knowledge base to answer questions.", // optional description
}),
},
}).toDataStream();
maxSteps: 5,
});

for await (const textPart of result.textStream) {
process.stdout.write(textPart);
}
}

main().catch(console.error);
27 changes: 16 additions & 11 deletions examples/vercel/vector-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { openai } from "@ai-sdk/openai";
import { llamaindex } from "@llamaindex/vercel";

Check failure on line 2 in examples/vercel/vector-store.ts

View workflow job for this annotation

GitHub Actions / typecheck

Cannot find module '@llamaindex/vercel' or its corresponding type declarations.
import { streamText } from "ai";
import { Document, VectorStoreIndex } from "llamaindex";

import fs from "node:fs/promises";

async function main() {
Expand All @@ -10,20 +11,24 @@ async function main() {
const document = new Document({ text: essay, id_: path });

const index = await VectorStoreIndex.fromDocuments([document]);
const queryTool = llamaindex({
index,
description: "Search through the documents", // optional description
});
console.log("Successfully created index and queryTool");
console.log("Successfully created index");

streamText({
tools: { queryTool },
prompt: "Cost of moving cat from Russia to UK?",
const result = streamText({
model: openai("gpt-4o"),
onFinish({ response }) {
console.log("Response:", JSON.stringify(response.messages, null, 2));
prompt: "Cost of moving cat from Russia to UK?",
tools: {
queryTool: llamaindex({
index,
description:
"get information from your knowledge base to answer questions.", // optional description
}),
},
}).toDataStream();
maxSteps: 5,
});

for await (const textPart of result.textStream) {
process.stdout.write(textPart);
}
}

main().catch(console.error);
6 changes: 3 additions & 3 deletions packages/providers/vercel/src/tool.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { BaseQueryEngine } from "@llamaindex/core/query-engine";
import type { CoreTool } from "ai";
import { type CoreTool, tool } from "ai";
import { z } from "zod";

interface DatasourceIndex {
Expand All @@ -14,7 +14,7 @@ export function llamaindex({
description?: string;
}): CoreTool {
const queryEngine = index.asQueryEngine();
return {
return tool({
description: description ?? "Get information about your documents.",
parameters: z.object({
query: z
Expand All @@ -25,5 +25,5 @@ export function llamaindex({
const result = await queryEngine?.query({ query });
return result?.message.content ?? "No result found in documents.";
},
};
});
}

0 comments on commit 6387bc6

Please sign in to comment.