Skip to content

Commit 86dfa5f

Browse files
committed
chore(js/testapps): update README and deps of reranker testapp
1 parent 1403f15 commit 86dfa5f

File tree

4 files changed

+64
-121
lines changed

4 files changed

+64
-121
lines changed

js/pnpm-lock.yaml

Lines changed: 0 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
11
# .env.example
22

3-
# Firebase project configuration
43
PROJECT_ID=your_project_id_here
54
LOCATION=your_location_here
6-
7-
LOCAL_DIR=your_local_dir_here
8-
9-
# Vector Search configuration
10-
VECTOR_SEARCH_PUBLIC_DOMAIN_NAME=your_vector_search_public_endpoint_here
11-
VECTOR_SEARCH_INDEX_ENDPOINT_ID=your_vector_search_index_endpoint_id_here
12-
VECTOR_SEARCH_INDEX_ID=your_vector_search_index_id_here
13-
VECTOR_SEARCH_DEPLOYED_INDEX_ID=your_vector_search_deployed_index_id_here
Lines changed: 64 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,137 +1,109 @@
1-
# Sample Vertex AI Plugin Retriever and Indexer with Local File
1+
# Sample Vertex AI Plugin Reranker with Fake Document Content
22

3-
This sample app demonstrates the use of the Vertex AI plugin retriever and indexer with a local file for demonstration purposes. This guide will walk you through setting up and running the sample.
3+
This sample app demonstrates the use of the Vertex AI plugin for reranking a set of documents based on a query using fake document content. This guide will walk you through setting up and running the sample.
44

55
## Prerequisites
66

77
Before running this sample, ensure you have the following:
88

99
1. **Node.js** installed.
1010
2. **PNPM** (Node Package Manager) installed.
11-
3. A deployed index to an index endpoint in **Vertex AI Vector Search**.
11+
3. A **Vertex AI** project with appropriate permissions for reranking models.
1212

1313
## Getting Started
1414

1515
### Step 1: Clone the Repository and Install Dependencies
1616

17-
Clone this repository to your local machine, and follow the instructions in the root README.md to build
18-
the core packages. This sample uses `workspace:*` dependencies, so they will need to be accessible.
17+
Clone this repository to your local machine and navigate to the project directory. Then, install the necessary dependencies:
1918

20-
Then
19+
\`\`\`bash
20+
pnpm install
21+
\`\`\`
2122

22-
```bash
23-
cd js/testapps/vertex-vector-search-custom && pnpm i
24-
```
23+
### Step 2: Set Up Environment Variables
2524

26-
### Step 3: Set Up Environment Variables
25+
Create a \`.env\` file in the root directory and set the following variables. You can use the provided \`.env.example\` as a reference.
2726

28-
Ensure you have a deployed index in Vertex AI Vector Search.
27+
\`\`\`plaintext
28+
PROJECT_ID=your_project_id_here
29+
LOCATION=your_location_here
30+
\`\`\`
2931

30-
Create a `.env` file in the root directory and set the following variables (see the .env.example as well if needed)
32+
These variables are required to configure the Vertex AI project and location for reranking.
3133

32-
```plaintext
33-
PROJECT_ID=your-google-cloud-project-id
34-
LOCATION=your-vertex-ai-location
35-
LOCAL_DIR=./data
36-
VECTOR_SEARCH_PUBLIC_DOMAIN_NAME=your-vector-search-public-domain-name
37-
VECTOR_SEARCH_INDEX_ENDPOINT_ID=your-index-endpoint-id
38-
VECTOR_SEARCH_INDEX_ID=your-index-id
39-
VECTOR_SEARCH_DEPLOYED_INDEX_ID=your-deployed-index-id
40-
GOOGLE_APPLICATION_CREDENTIALS=path-to-your-service-account-key.json
41-
```
42-
43-
### Step 4: Run the Sample
34+
### Step 3: Run the Sample
4435

4536
Start the Genkit server:
4637

47-
```bash
38+
\`\`\`bash
4839
genkit start
49-
```
40+
\`\`\`
41+
42+
This will launch the server that hosts the reranking flow.
5043

5144
## Sample Explanation
5245

5346
### Overview
5447

55-
This sample demonstrates how to define a custom document indexer and retriever using local JSON files. It integrates with Vertex AI for indexing and retrieval of documents.
48+
This sample demonstrates how to use the Vertex AI plugin to rerank a predefined list of fake document content based on a query input. It utilizes a semantic reranker model from Vertex AI.
5649

5750
### Key Components
5851

59-
- **Custom Document Indexer**: Stores documents in a local JSON file.
60-
- **Custom Document Retriever**: Retrieves documents from the local JSON file based on neighbor IDs.
61-
- **Genkit Configuration**: Configures Genkit with the Vertex AI plugin, setting up the project, location, and vector search index options.
62-
- **Indexing Flow**: Defines a flow for indexing documents.
63-
- **Query Flow**: Defines a flow for querying indexed documents.
64-
65-
### Custom Document Indexer
66-
67-
The `localDocumentIndexer` function reads existing documents from a local file, adds new documents, and writes them back to the file:
68-
69-
```typescript
70-
const localDocumentIndexer: DocumentIndexer = async (documents: Document[]) => {
71-
const content = await fs.promises.readFile(localFilePath, 'utf-8');
72-
const currentLocalFile = JSON.parse(content);
73-
const docsWithIds = Object.fromEntries(
74-
documents.map((doc) => [
75-
generateRandomId(),
76-
{ content: JSON.stringify(doc.content) },
77-
])
78-
);
79-
const newLocalFile = { ...currentLocalFile, ...docsWithIds };
80-
await fs.promises.writeFile(
81-
localFilePath,
82-
JSON.stringify(newLocalFile, null, 2)
83-
);
84-
return Object.keys(docsWithIds);
85-
};
86-
```
87-
88-
### Custom Document Retriever
89-
90-
The `localDocumentRetriever` function reads the local file and retrieves documents based on neighbor IDs:
91-
92-
```typescript
93-
const localDocumentRetriever: DocumentRetriever = async (
94-
neighbors: Neighbor[]
95-
) => {
96-
const content = await fs.promises.readFile(localFilePath, 'utf-8');
97-
const currentLocalFile = JSON.parse(content);
98-
const ids = neighbors
99-
.map((neighbor) => neighbor.datapoint?.datapointId)
100-
.filter(Boolean) as string[];
101-
const docs = ids
102-
.map((id) => {
103-
const doc = currentLocalFile[id];
104-
if (!doc || !doc.content) return null;
105-
const parsedContent = JSON.parse(doc.content);
106-
const text = parsedContent[0]?.text;
107-
return text ? Document.fromText(text) : null;
108-
})
109-
.filter(Boolean) as Document[];
110-
return docs;
111-
};
112-
```
113-
114-
### Defining Flows
115-
116-
Two flows are defined: `indexFlow` for indexing documents and `queryFlow` for querying documents.
117-
118-
- **Index Flow**: Converts text inputs to documents and indexes them.
119-
- **Query Flow**: Retrieves documents based on a query and returns the results sorted by distance.
52+
- **Fake Document Content**: A hardcoded array of strings representing document content.
53+
- **Rerank Flow**: A flow that reranks the fake documents based on the provided query.
54+
- **Genkit Configuration**: Configures Genkit with the Vertex AI plugin, setting up the project and reranking model.
55+
56+
### Rerank Flow
57+
58+
The \`rerankFlow\` function takes a query as input, reranks the predefined document content using the Vertex AI semantic reranker, and returns the documents sorted by relevance score.
59+
60+
\`\`\`typescript
61+
export const rerankFlow = defineFlow(
62+
{
63+
name: 'rerankFlow',
64+
inputSchema: z.object({ query: z.string() }),
65+
outputSchema: z.array(
66+
z.object({
67+
text: z.string(),
68+
score: z.number(),
69+
})
70+
),
71+
},
72+
async ({ query }) => {
73+
const documents = FAKE_DOCUMENT_CONTENT.map((text) =>
74+
Document.fromText(text)
75+
);
76+
const reranker = 'vertexai/reranker';
77+
78+
const rerankedDocuments = await rerank({
79+
reranker,
80+
query: Document.fromText(query),
81+
documents,
82+
});
83+
84+
return rerankedDocuments.map((doc) => ({
85+
text: doc.text(),
86+
score: doc.metadata.score,
87+
}));
88+
89+
}
90+
);
91+
\`\`\`
12092

12193
### Running the Server
12294

123-
The server is started using the `startFlowsServer` function, which sets up the Genkit server to handle flow requests.
95+
The server is started using the \`startFlowsServer\` function, which sets up the Genkit server to handle flow requests.
12496

125-
```typescript
97+
\`\`\`typescript
12698
startFlowsServer();
127-
```
99+
\`\`\`
128100

129101
## License
130102

131103
This project is licensed under the Apache License, Version 2.0. See the [LICENSE](LICENSE) file for details.
132104

133105
## Conclusion
134106

135-
This sample provides a basic demonstration of using Vertex AI plugins with Genkit for document indexing and retrieval. It can be extended and adapted to suit more complex use cases and integrations with other data sources and services.
107+
This sample provides a basic demonstration of using the Vertex AI plugin with Genkit for reranking documents based on a query. It can be extended and adapted to suit more complex use cases and integrations with other data sources and services.
136108

137109
For more information, please refer to the official [Firebase Genkit documentation](https://firebase.google.com/docs/genkit).

js/testapps/vertexai-reranker/package.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,9 @@
2121
"@genkit-ai/evaluator": "workspace:*",
2222
"@genkit-ai/firebase": "workspace:*",
2323
"@genkit-ai/flow": "workspace:*",
24-
"@genkit-ai/googleai": "workspace:*",
2524
"@genkit-ai/vertexai": "workspace:*",
26-
"@google-cloud/bigquery": "^7.8.0",
2725
"dotenv": "^16.4.5",
2826
"express": "^4.19.2",
29-
"genkitx-chromadb": "workspace:*",
30-
"genkitx-langchain": "workspace:*",
31-
"genkitx-pinecone": "workspace:*",
3227
"google-auth-library": "^9.11.0",
3328
"zod": "3.22.4"
3429
},

0 commit comments

Comments
 (0)