Skip to content

Commit 6269620

Browse files
caohy1988claude
andcommitted
feat: Add comprehensive bigquery-ai skill for generative AI
Replaces bq-ai-operator with a comprehensive bigquery-ai skill covering all BigQuery AI capabilities following Agent Skills standard best practices. New bigquery-ai skill includes: - SKILL.md: Concise root with quick start and function reference - references/TEXT_GENERATION.md: Complete AI.GENERATE_TEXT guide - references/EMBEDDINGS.md: ML.GENERATE_EMBEDDING best practices - references/VECTOR_SEARCH.md: Semantic search patterns - references/RAG_WORKFLOW.md: End-to-end RAG implementation - references/REMOTE_MODELS.md: CREATE MODEL for all LLM providers - references/CLOUD_AI_SERVICES.md: Translation, NLP, Vision, etc. - scripts/: Helper scripts for setup and common operations Also updates: - bqml skill: Focused on traditional ML (classification, regression, etc.) - agent_skills_demo: Updated system instruction for new skill structure Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent f3b507a commit 6269620

File tree

19 files changed

+4233
-1116
lines changed

19 files changed

+4233
-1116
lines changed

contributing/samples/agent_skills_demo/agent.py

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -87,41 +87,52 @@
8787
bigquery_toolset = None
8888

8989
# Build the system instruction
90-
SYSTEM_INSTRUCTION = f"""You are a data science assistant with access to BigQuery ML skills and BigQuery tools.
90+
SYSTEM_INSTRUCTION = f"""You are a data science assistant with access to BigQuery AI and ML skills.
9191
9292
## Available Skills
9393
9494
{discovery_prompt}
9595
96+
## Skill Overview
97+
98+
- **bigquery-ai**: Generative AI operations - text generation with LLMs (Gemini, Claude), embeddings, vector search, and RAG workflows
99+
- **bqml**: Traditional ML - classification, regression, clustering, time series forecasting, recommendations
100+
96101
## How to Use Skills
97102
98-
1. **Discover**: When asked about ML or AI capabilities, review the available skills above
103+
1. **Discover**: Review the available skills above to find the right one for your task
99104
2. **Activate**: Use the skill tool with action="activate" to get detailed instructions
100-
3. **Execute**: Use action="run_script" to run helper scripts or action="load_reference" to load detailed documentation
105+
3. **Load References**: Use action="load_reference" to load detailed documentation for specific topics
106+
4. **Run Scripts**: Use action="run_script" to execute helper scripts for setup and validation
101107
102108
## BigQuery Tools
103109
104-
You also have access to BigQuery tools for:
105-
- Executing SQL queries
110+
You also have direct access to BigQuery tools for:
111+
- Executing SQL queries (including CREATE MODEL, ML functions)
106112
- Exploring datasets and tables
107113
- Getting table schemas and metadata
108114
109115
## Guidelines
110116
117+
- For generative AI (text generation, embeddings, semantic search, RAG): Use **bigquery-ai** skill
118+
- For predictive ML (classification, regression, forecasting): Use **bqml** skill
111119
- Always activate a skill before using its detailed features
112-
- Load references when you need detailed documentation
113-
- Run scripts for validation and utility tasks
114-
- Use BigQuery tools to explore data and run queries
115-
- Explain what you're doing and why
116-
117-
## Example Workflow
118-
119-
User: "How do I create a classification model in BigQuery?"
120-
121-
1. Activate the bqml skill to get instructions
122-
2. Load MODEL_TYPES.md reference for model type details
123-
3. Use BigQuery tools to explore available data
124-
4. Provide guidance based on the skill's documentation
120+
- Load specific reference docs when you need in-depth information
121+
- Use BigQuery tools to run the actual SQL queries
122+
123+
## Example Workflows
124+
125+
**Generative AI Example:**
126+
User: "How do I build a RAG system in BigQuery?"
127+
1. Activate bigquery-ai skill
128+
2. Load RAG_WORKFLOW.md reference
129+
3. Use BigQuery tools to create models and run queries
130+
131+
**Traditional ML Example:**
132+
User: "How do I train a churn prediction model?"
133+
1. Activate bqml skill
134+
2. Load MODEL_TYPES.md reference for classifier options
135+
3. Use BigQuery tools to create and evaluate the model
125136
"""
126137

127138
# Combine all tools
Lines changed: 272 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,272 @@
1+
---
2+
name: bigquery-ai
3+
description: Execute generative AI operations in BigQuery - text generation, embeddings, vector search, and RAG workflows using Gemini, Claude, and other LLMs. Use when working with AI/ML inference, semantic search, or building RAG applications in BigQuery.
4+
license: Apache-2.0
5+
compatibility: BigQuery, Vertex AI, Gemini, Cloud AI APIs
6+
metadata:
7+
author: Google Cloud
8+
version: "2.0"
9+
category: generative-ai
10+
adk:
11+
config:
12+
timeout_seconds: 180
13+
max_parallel_calls: 10
14+
allow_network: true
15+
allowed_callers:
16+
- bigquery_agent
17+
- ai_agent
18+
- rag_agent
19+
---
20+
21+
# BigQuery AI Skill
22+
23+
Execute generative AI operations directly in BigQuery using SQL. This skill covers text generation, embeddings, vector search, and retrieval-augmented generation (RAG) workflows.
24+
25+
## When to Use This Skill
26+
27+
Use this skill when you need to:
28+
- Generate text using LLMs (Gemini, Claude, Llama, Mistral) on BigQuery data
29+
- Create embeddings for semantic search and similarity matching
30+
- Build vector search and RAG pipelines entirely in SQL
31+
- Process documents, translate text, or analyze images at scale
32+
- Connect BigQuery to Vertex AI models for inference
33+
34+
## Core Capabilities
35+
36+
| Capability | Function | Description |
37+
|------------|----------|-------------|
38+
| Text Generation | `AI.GENERATE_TEXT` | Generate text using remote LLM models |
39+
| Embeddings | `ML.GENERATE_EMBEDDING` | Create vector embeddings from text/images |
40+
| Vector Search | `VECTOR_SEARCH` | Find semantically similar items |
41+
| Semantic Search | `AI.SEARCH` | Search with autonomous embeddings |
42+
| Remote Models | `CREATE MODEL` | Connect to Vertex AI endpoints |
43+
44+
## Quick Start
45+
46+
### 1. Create a Remote Model Connection
47+
48+
```sql
49+
-- Create connection to Gemini
50+
CREATE OR REPLACE MODEL `project.dataset.gemini_model`
51+
REMOTE WITH CONNECTION `project.region.connection_id`
52+
OPTIONS (ENDPOINT = 'gemini-2.0-flash');
53+
```
54+
55+
### 2. Generate Text
56+
57+
```sql
58+
SELECT ml_generate_text_result
59+
FROM ML.GENERATE_TEXT(
60+
MODEL `project.dataset.gemini_model`,
61+
(SELECT 'Summarize this text: ' || content AS prompt FROM my_table),
62+
STRUCT(256 AS max_output_tokens, 0.2 AS temperature)
63+
);
64+
```
65+
66+
### 3. Create Embeddings
67+
68+
```sql
69+
-- Create embedding model
70+
CREATE OR REPLACE MODEL `project.dataset.embedding_model`
71+
REMOTE WITH CONNECTION DEFAULT
72+
OPTIONS (ENDPOINT = 'text-embedding-005');
73+
74+
-- Generate embeddings
75+
SELECT * FROM ML.GENERATE_EMBEDDING(
76+
MODEL `project.dataset.embedding_model`,
77+
(SELECT content FROM my_table)
78+
);
79+
```
80+
81+
### 4. Vector Search
82+
83+
```sql
84+
SELECT base.id, base.content, distance
85+
FROM VECTOR_SEARCH(
86+
TABLE `project.dataset.embeddings`, 'embedding',
87+
(SELECT embedding FROM query_embeddings),
88+
top_k => 10,
89+
distance_type => 'COSINE'
90+
);
91+
```
92+
93+
## AI Functions Reference
94+
95+
### AI.GENERATE_TEXT
96+
97+
Full control over text generation with model parameters:
98+
99+
```sql
100+
SELECT * FROM AI.GENERATE_TEXT(
101+
MODEL `project.dataset.model`,
102+
(SELECT prompt FROM prompts_table),
103+
STRUCT(
104+
512 AS max_output_tokens,
105+
0.7 AS temperature,
106+
0.95 AS top_p,
107+
TRUE AS ground_with_google_search
108+
)
109+
);
110+
```
111+
112+
**Key Parameters:**
113+
- `max_output_tokens`: 1-8192 (default: 128)
114+
- `temperature`: 0.0-1.0 (default: 0, higher = more creative)
115+
- `top_p`: 0.0-1.0 (default: 0.95)
116+
- `ground_with_google_search`: Enable web grounding
117+
118+
### ML.GENERATE_EMBEDDING
119+
120+
Generate vector embeddings for semantic operations:
121+
122+
```sql
123+
SELECT * FROM ML.GENERATE_EMBEDDING(
124+
MODEL `project.dataset.embedding_model`,
125+
(SELECT id, text_column AS content FROM source_table)
126+
)
127+
WHERE LENGTH(ml_generate_embedding_status) = 0; -- Filter errors
128+
```
129+
130+
**Supported Models:**
131+
- `text-embedding-005` (recommended)
132+
- `text-embedding-004`
133+
- `text-multilingual-embedding-002`
134+
- `multimodalembedding@001` (text + images)
135+
136+
### VECTOR_SEARCH
137+
138+
Find nearest neighbors using embeddings:
139+
140+
```sql
141+
SELECT query.id, base.id, base.content, distance
142+
FROM VECTOR_SEARCH(
143+
TABLE `project.dataset.base_embeddings`, 'embedding',
144+
TABLE `project.dataset.query_embeddings`,
145+
top_k => 5,
146+
distance_type => 'COSINE',
147+
options => '{"fraction_lists_to_search": 0.01}'
148+
);
149+
```
150+
151+
**Distance Types:** `COSINE`, `EUCLIDEAN`, `DOT_PRODUCT`
152+
153+
## Supported Models
154+
155+
| Provider | Models | Use Case |
156+
|----------|--------|----------|
157+
| Google | Gemini 2.0, 1.5 Pro/Flash | Text generation, multimodal |
158+
| Anthropic | Claude 3.5, 3 Opus/Sonnet | Complex reasoning |
159+
| Meta | Llama 3.1, 3.2 | Open-source alternative |
160+
| Mistral | Mistral Large, Medium | European compliance |
161+
162+
## Prerequisites
163+
164+
1. **BigQuery Connection**: Create a Cloud resource connection
165+
2. **IAM Permissions**: Grant `bigquery.connectionUser` and `aiplatform.user`
166+
3. **APIs Enabled**: BigQuery API, Vertex AI API, BigQuery Connection API
167+
168+
## References
169+
170+
Load detailed documentation as needed:
171+
172+
- `TEXT_GENERATION.md` - Complete AI.GENERATE_TEXT guide with all parameters
173+
- `EMBEDDINGS.md` - Embedding models, multimodal embeddings, best practices
174+
- `VECTOR_SEARCH.md` - Vector indexes, search optimization, recall tuning
175+
- `REMOTE_MODELS.md` - CREATE MODEL syntax for all supported providers
176+
- `RAG_WORKFLOW.md` - End-to-end RAG implementation patterns
177+
- `CLOUD_AI_SERVICES.md` - Translation, NLP, document processing, vision
178+
179+
## Scripts
180+
181+
Helper scripts for common operations:
182+
183+
- `setup_remote_model.py` - Create remote model connections
184+
- `generate_embeddings.py` - Batch embedding generation
185+
- `semantic_search.py` - Build semantic search pipelines
186+
- `rag_pipeline.py` - Complete RAG workflow setup
187+
188+
## Common Patterns
189+
190+
### Batch Text Classification
191+
192+
```sql
193+
SELECT id, content,
194+
JSON_VALUE(ml_generate_text_result, '$.predictions[0].content') AS category
195+
FROM ML.GENERATE_TEXT(
196+
MODEL `project.dataset.gemini`,
197+
(SELECT id, content,
198+
CONCAT('Classify this text into one of: Tech, Sports, Politics\n\nText: ', content) AS prompt
199+
FROM articles)
200+
);
201+
```
202+
203+
### Semantic Similarity Search
204+
205+
```sql
206+
-- Find similar documents to a query
207+
WITH query_embedding AS (
208+
SELECT ml_generate_embedding_result AS embedding
209+
FROM ML.GENERATE_EMBEDDING(
210+
MODEL `project.dataset.embedding_model`,
211+
(SELECT 'machine learning best practices' AS content)
212+
)
213+
)
214+
SELECT d.title, d.content, distance
215+
FROM VECTOR_SEARCH(
216+
TABLE `project.dataset.doc_embeddings`, 'embedding',
217+
TABLE query_embedding,
218+
top_k => 10
219+
)
220+
JOIN `project.dataset.documents` d ON d.id = base.id;
221+
```
222+
223+
### RAG with Context Injection
224+
225+
```sql
226+
-- Retrieve relevant context and generate answer
227+
WITH context AS (
228+
SELECT STRING_AGG(content, '\n\n') AS retrieved_context
229+
FROM VECTOR_SEARCH(
230+
TABLE `project.dataset.knowledge_base`, 'embedding',
231+
(SELECT embedding FROM ML.GENERATE_EMBEDDING(MODEL m, (SELECT @query AS content))),
232+
top_k => 5
233+
)
234+
)
235+
SELECT ml_generate_text_result AS answer
236+
FROM ML.GENERATE_TEXT(
237+
MODEL `project.dataset.gemini`,
238+
(SELECT CONCAT(
239+
'Answer based on context:\n\n', retrieved_context,
240+
'\n\nQuestion: ', @query
241+
) AS prompt FROM context)
242+
);
243+
```
244+
245+
## Error Handling
246+
247+
Check status columns for errors:
248+
249+
```sql
250+
-- Text generation errors
251+
SELECT * FROM ML.GENERATE_TEXT(...)
252+
WHERE ml_generate_text_status != '';
253+
254+
-- Embedding errors
255+
SELECT * FROM ML.GENERATE_EMBEDDING(...)
256+
WHERE LENGTH(ml_generate_embedding_status) > 0;
257+
```
258+
259+
## Performance Tips
260+
261+
1. **Use Vector Indexes**: Create indexes for tables with >100K embeddings
262+
2. **Batch Requests**: Process multiple rows in single function calls
263+
3. **Filter Before AI**: Apply WHERE clauses before expensive AI operations
264+
4. **Cache Embeddings**: Store embeddings in tables, don't regenerate
265+
5. **Tune Search**: Adjust `fraction_lists_to_search` for speed vs recall
266+
267+
## Limitations
268+
269+
- Max 10,000 rows per AI function call
270+
- Embedding dimensions vary by model (768-3072)
271+
- Rate limits apply based on Vertex AI quotas
272+
- Some models require specific regions

0 commit comments

Comments
 (0)