Skip to content

Commit d2d3c13

Browse files
authored
Merge pull request #94 from eun2ce/feat/vertex-ai-search-grounding
Add Vertex AI Search grounding
2 parents 1d64754 + e9a9c2e commit d2d3c13

File tree

4 files changed

+146
-18
lines changed

4 files changed

+146
-18
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ The functions include a built-in encryption mechanism for sensitive information:
159159
- **Advanced Image Processing**: Optimized image handling with configurable compression, resizing, and quality settings.
160160
- **Configurable Parameters**: Environment variables for image optimization (quality, max dimensions, format conversion).
161161
- Grounding with Google search with [google_search_tool.py filter](./filters/google_search_tool.py)
162+
- Grounding with Vertex AI Search with [vertex_ai_search_tool.py filter](./filters/vertex_ai_search_tool.py)
162163
- Native tool calling support
163164
- Configurable API version support
164165

docs/google-gemini-integration.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ GOOGLE_CLOUD_PROJECT="your-gcp-project-id"
157157
# The Google Cloud region for Vertex AI (e.g., "us-central1").
158158
# Defaults to "global" if not set.
159159
GOOGLE_CLOUD_LOCATION="your-gcp-location"
160+
161+
# Vertex AI RAG Store path for grounding (e.g., projects/PROJECT/locations/global/collections/default_collection/dataStores/DATA_STORE_ID)
162+
# Optional: Can also be set via metadata params or filter
163+
# Auto-enabled when USE_VERTEX_AI is true and this is set
164+
VERTEX_AI_RAG_STORE="projects/your-project/locations/global/collections/default_collection/dataStores/your-data-store-id"
160165
```
161166

162167
> [!IMPORTANT]
@@ -179,6 +184,46 @@ For instance, the following [Filter (google_search_tool.py)](../filters/google_s
179184

180185
When enabled, sources and google queries used by Gemini will be displayed with the response.
181186

187+
## Grounding with Vertex AI Search
188+
189+
Improve the accuracy and recency of Gemini responses by grounding them with your own data in Vertex AI Search.
190+
191+
### Configuration
192+
193+
To enable Vertex AI Search grounding, you need to:
194+
195+
1. **Set up a Vertex AI Search Data Store**: Follow the [Google Cloud documentation](https://cloud.google.com/vertex-ai/docs/search/overview) to create a Data Store in Discovery Engine and ingest your documents.
196+
2. **Provide the RAG Store Path**: The path should be in the format `projects/PROJECT/locations/LOCATION/ragCorpora/DATA_STORE_ID` or `projects/PROJECT/locations/global/collections/default_collection/dataStores/DATA_STORE_ID`.
197+
- Set the `VERTEX_AI_RAG_STORE` environment variable, or
198+
- Use the [Filter (vertex_ai_search_tool.py)](../filters/vertex_ai_search_tool.py) to enable the feature and optionally pass the store ID via chat metadata.
199+
3. **Enable Vertex AI**: Set `GOOGLE_GENAI_USE_VERTEXAI=true` to use Vertex AI (required for Vertex AI Search grounding).
200+
201+
When `USE_VERTEX_AI` is `true` and `VERTEX_AI_RAG_STORE` is configured, Vertex AI Search grounding will be automatically enabled. You can also explicitly enable it via the `vertex_ai_search` feature flag.
202+
203+
When enabled, Gemini will use the specified Vertex AI Search Data Store to retrieve relevant information and ground its responses, providing citations to the source documents.
204+
205+
### Example Filter Usage
206+
207+
The [vertex_ai_search_tool.py](../filters/vertex_ai_search_tool.py) filter enables Vertex AI Search grounding when the `vertex_ai_search` feature is requested:
208+
209+
```python
210+
# filters/vertex_ai_search_tool.py
211+
# ... (filter code) ...
212+
```
213+
214+
To use this filter, ensure it's enabled in your Open WebUI configuration. Then, in your chat settings or via metadata, you can enable the `vertex_ai_search` feature:
215+
216+
```json
217+
{
218+
"features": {
219+
"vertex_ai_search": true
220+
},
221+
"params": {
222+
"vertex_rag_store": "projects/your-project/locations/global/collections/default_collection/dataStores/your-data-store-id"
223+
}
224+
}
225+
```
226+
182227
## Native tool calling support
183228

184229
Native tool calling is enabled/disabled via the standard 'Function calling' Open Web UI toggle.

filters/vertex_ai_search_tool.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
title: Vertex AI Search Tool Filter for https://github.com/owndev/Open-WebUI-Functions/blob/main/pipelines/google/google_gemini.py
3+
author: owndev, eun2ce
4+
author_url: https://github.com/owndev/
5+
project_url: https://github.com/owndev/Open-WebUI-Functions
6+
funding_url: https://github.com/sponsors/owndev
7+
version: 1.0.0
8+
license: Apache License 2.0
9+
requirements:
10+
- https://github.com/owndev/Open-WebUI-Functions/blob/main/pipelines/google/google_gemini.py
11+
description: Enable Vertex AI Search grounding for RAG
12+
"""
13+
14+
import logging
15+
import os
16+
from open_webui.env import SRC_LOG_LEVELS
17+
18+
19+
class Filter:
20+
def __init__(self):
21+
self.log = logging.getLogger("google_ai.pipe")
22+
self.log.setLevel(SRC_LOG_LEVELS.get("OPENAI", logging.INFO))
23+
24+
def inlet(self, body: dict) -> dict:
25+
features = body.get("features", {})
26+
27+
metadata = body.setdefault("metadata", {})
28+
metadata_features = metadata.setdefault("features", {})
29+
metadata_params = metadata.setdefault("params", {})
30+
31+
if features.pop("vertex_ai_search", False):
32+
self.log.debug("Enabling Vertex AI Search grounding")
33+
metadata_features["vertex_ai_search"] = True
34+
35+
if "vertex_rag_store" not in metadata_params:
36+
vertex_rag_store = os.getenv("VERTEX_AI_RAG_STORE")
37+
if vertex_rag_store:
38+
metadata_params["vertex_rag_store"] = vertex_rag_store
39+
else:
40+
self.log.warning(
41+
"vertex_ai_search enabled but vertex_rag_store not provided in params or VERTEX_AI_RAG_STORE env var"
42+
)
43+
return body
44+

pipelines/google/google_gemini.py

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
author_url: https://github.com/owndev/
55
project_url: https://github.com/owndev/Open-WebUI-Functions
66
funding_url: https://github.com/sponsors/owndev
7-
version: 1.6.8
7+
version: 1.7.0
88
required_open_webui_version: 0.6.26
99
license: Apache License 2.0
1010
description: Highly optimized Google Gemini pipeline with advanced image generation capabilities, intelligent compression, and streamlined processing workflows.
@@ -26,6 +26,7 @@
2626
- Configurable safety settings with environment variable support
2727
- Military-grade encrypted storage of sensitive API keys
2828
- Intelligent grounding with Google search integration
29+
- Vertex AI Search grounding for RAG
2930
- Native tool calling support with automatic signature management
3031
- Unified image processing with consolidated helper methods
3132
- Optimized payload creation for image generation models
@@ -177,6 +178,10 @@ class Valves(BaseModel):
177178
default=os.getenv("GOOGLE_CLOUD_LOCATION", "global"),
178179
description="The Google Cloud region to use with Vertex AI.",
179180
)
181+
VERTEX_AI_RAG_STORE: str | None = Field(
182+
default=os.getenv("VERTEX_AI_RAG_STORE"),
183+
description="Vertex AI RAG Store path for grounding (e.g., projects/PROJECT/locations/LOCATION/ragCorpora/DATA_STORE_ID). Only used when USE_VERTEX_AI is true.",
184+
)
180185
USE_PERMISSIVE_SAFETY: bool = Field(
181186
default=os.getenv("USE_PERMISSIVE_SAFETY", "false").lower() == "true",
182187
description="Use permissive safety settings for content generation.",
@@ -1414,6 +1419,28 @@ def _configure_generation(
14141419
)
14151420

14161421
params = __metadata__.get("params", {})
1422+
if features.get("vertex_ai_search", False) or (
1423+
self.valves.USE_VERTEX_AI
1424+
and (self.valves.VERTEX_AI_RAG_STORE or os.getenv("VERTEX_AI_RAG_STORE"))
1425+
):
1426+
vertex_rag_store = (
1427+
params.get("vertex_rag_store")
1428+
or self.valves.VERTEX_AI_RAG_STORE
1429+
or os.getenv("VERTEX_AI_RAG_STORE")
1430+
)
1431+
if vertex_rag_store:
1432+
self.log.debug(f"Enabling Vertex AI Search grounding: {vertex_rag_store}")
1433+
gen_config_params.setdefault("tools", []).append(
1434+
types.Tool(
1435+
retrieval=types.Retrieval(
1436+
vertex_ai_search=types.VertexAISearch(datastore=vertex_rag_store)
1437+
)
1438+
)
1439+
)
1440+
else:
1441+
self.log.warning(
1442+
"Vertex AI Search requested but vertex_rag_store not provided in params, valves, or env"
1443+
)
14171444
if __tools__ is not None and params.get("function_calling") == "native":
14181445
for name, tool_def in __tools__.items():
14191446
if not name.startswith("_"):
@@ -1433,24 +1460,35 @@ def _format_grounding_chunks_as_sources(
14331460
):
14341461
formatted_sources = []
14351462
for chunk in grounding_chunks:
1436-
context = chunk.web or chunk.retrieved_context
1437-
if not context:
1438-
continue
1439-
1440-
uri = context.uri
1441-
title = context.title or "Source"
1463+
if hasattr(chunk, "retrieved_context") and chunk.retrieved_context:
1464+
context = chunk.retrieved_context
1465+
formatted_sources.append(
1466+
{
1467+
"source": {
1468+
"name": getattr(context, "title", None) or "Document",
1469+
"type": "vertex_ai_search",
1470+
"uri": getattr(context, "uri", None),
1471+
},
1472+
"document": [getattr(context, "chunk_text", None) or ""],
1473+
"metadata": [{"source": getattr(context, "title", None) or "Document"}],
1474+
}
1475+
)
1476+
elif hasattr(chunk, "web") and chunk.web:
1477+
context = chunk.web
1478+
uri = context.uri
1479+
title = context.title or "Source"
14421480

1443-
formatted_sources.append(
1444-
{
1445-
"source": {
1446-
"name": title,
1447-
"type": "web_search_results",
1448-
"url": uri,
1449-
},
1450-
"document": ["Click the link to view the content."],
1451-
"metadata": [{"source": title}],
1452-
}
1453-
)
1481+
formatted_sources.append(
1482+
{
1483+
"source": {
1484+
"name": title,
1485+
"type": "web_search_results",
1486+
"url": uri,
1487+
},
1488+
"document": ["Click the link to view the content."],
1489+
"metadata": [{"source": title}],
1490+
}
1491+
)
14541492
return formatted_sources
14551493

14561494
async def _process_grounding_metadata(

0 commit comments

Comments
 (0)