web-search について
web-searchは、z-ai-web-dev-sdkを使ったアプリへのウェブ検索実装や、z-ai CLIでの簡易検索を案内するスキルです。最新の検索結果を取得し、件数や期間の指定、アプリ内での処理に対応します。
主な機能
- 01SDKによるウェブ検索
- 02z-ai CLIでのクエリ実行
- 03結果件数の制限と期間指定
- 04結果のJSON保存
- 05検索結果の処理と検証
活用シーン
- 01最新ニュースなどの情報取得
- 02トピック調査とドキュメント検索
- 03バックエンドアプリへのウェブ検索組み込み
- 04ファクトチェック用の結果収集
インストール
コマンドをコピーしてターミナルで実行します。
npx skills add https:/ / github.com/ answerzhao/ agent-skills --skill web-searchインストール後、Claude Code で/web-searchを入力して読み込まれたか確認します。
同梱ファイルLICENSE.txtscripts/web_search.ts
詳細
Web 検索スキル
このスキルは、z-ai-web-dev-sdk パッケージを使用した Web 検索機能の実装をガイドし、アプリケーションが Web を検索して最新情報を取得できるようにします。
インストールパス
推奨される場所: {project_path}/skills/web-search
このスキルパッケージをプロジェクト内の上記のパスに展開してください。
参照スクリプト: 簡単なテストと参照用のサンプルテストスクリプトが {project_path}/skills/web-search/scripts/ ディレクトリに用意されています。動作する例については {project_path}/skills/web-search/scripts/web_search.ts を参照してください。
概要
Web 検索スキルを使用すると、インターネットを検索し、最新情報を取得し、Web ソースからリアルタイムデータにアクセスできるアプリケーションを構築できます。
重要: z-ai-web-dev-sdk はバックエンドコードでのみ使用する必要があります。クライアントサイドのコードでは決して使用しないでください。
前提条件
z-ai-web-dev-sdk パッケージはすでにインストールされています。以下の例に示すようにインポートしてください。
CLI の使用法(簡単なタスク向け)
簡単な Web 検索クエリには、コードを書く代わりに z-ai CLI を使用できます。これは、迅速な情報取得、検索機能のテスト、またはコマンドラインの自動化に最適です。
基本的な Web 検索
# Simple search query
z-ai function --name "web_search" --args '{"query": "artificial intelligence"}'
# Using short options
z-ai function -n web_search -a '{"query": "latest tech news"}'
カスタムパラメータを使用した検索
# Limit number of results
z-ai function \
-n web_search \
-a '{"query": "machine learning", "num": 5}'
# Search with recency filter (results from last N days)
z-ai function \
-n web_search \
-a '{"query": "cryptocurrency news", "num": 10, "recency_days": 7}'
検索結果の保存
# Save results to JSON file
z-ai function \
-n web_search \
-a '{"query": "climate change research", "num": 5}' \
-o search_results.json
# Recent news with file output
z-ai function \
-n web_search \
-a '{"query": "AI breakthroughs", "num": 3, "recency_days": 1}' \
-o ai_news.json
高度な検索例
# Search for specific topics
z-ai function \
-n web_search \
-a '{"query": "quantum computing applications", "num": 8}' \
-o quantum.json
# Find recent scientific papers
z-ai function \
-n web_search \
-a '{"query": "genomics research", "num": 5, "recency_days": 30}' \
-o genomics.json
# Technology news from last 24 hours
z-ai function \
-n web_search \
-a '{"query": "tech industry updates", "recency_days": 1}' \
-o today_tech.json
CLI パラメータ
--name, -n: 必須 - 関数名("web_search" を使用)--args, -a: 必須 - 以下を含む JSON 引数オブジェクト:query(string, 必須): 検索キーワードnum(number, 任意): 結果の数(デフォルト: 10)recency_days(number, 任意): 直近 N 日間の結果に絞り込む
--output, -o <path>: 任意 - 出力ファイルパス(JSON 形式)
検索結果の構造
各結果には以下が含まれます:
url: 結果の完全な URLname: ページのタイトルsnippet: プレビューテキスト/説明host_name: ドメイン名rank: 結果のランキングdate: 公開/更新日favicon: ファビコンの URL
CLI と SDK の使い分け
CLI を使用する場合:
- 迅速な情報検索
- 検索クエリのテスト
- 簡単な自動化スクリプト
- 単発の調査タスク
SDK を使用する場合:
- アプリケーション内での動的な検索
- 多段階の検索ワークフロー
- カスタムの結果処理とフィルタリング
- 複雑なロジックを持つ本番アプリケーション
検索結果の型
各検索結果は、以下の構造を持つ SearchFunctionResultItem です:
interface SearchFunctionResultItem {
url: string; // Full URL of the result
name: string; // Title of the page
snippet: string; // Preview text/description
host_name: string; // Domain name
rank: number; // Result ranking
date: string; // Publication/update date
favicon: string; // Favicon URL
}
基本的な Web 検索
シンプルな検索クエリ
import ZAI from 'z-ai-web-dev-sdk';
async function searchWeb(query) {
const zai = await ZAI.create();
const results = await zai.functions.invoke('web_search', {
query: query,
num: 10
});
return results;
}
// Usage
const searchResults = await searchWeb('What is the capital of France?');
console.log('Search Results:', searchResults);
カスタム結果数を使用した検索
import ZAI from 'z-ai-web-dev-sdk';
async function searchWithLimit(query, numberOfResults) {
const zai = await ZAI.create();
const results = await zai.functions.invoke('web_search', {
query: query,
num: numberOfResults
});
return results;
}
// Usage - Get top 5 results
const topResults = await searchWithLimit('artificial intelligence news', 5);
// Usage - Get top 20 results
const moreResults = await searchWithLimit('JavaScript frameworks', 20);
整形された検索結果
import ZAI from 'z-ai-web-dev-sdk';
async function getFormattedResults(query) {
const zai = await ZAI.create();
const results = await zai.functions.invoke('web_search', {
query: query,
num: 10
});
// Format results for display
const formatted = results.map((item, index) => ({
position: index + 1,
title: item.name,
url: item.url,
description: item.snippet,
domain: item.host_name,
publishDate: item.date
}));
return formatted;
}
// Usage
const results = await getFormattedResults('climate change solutions');
results.forEach(result => {
console.log(`${result.position}. ${result.title}`);
console.log(` ${result.url}`);
console.log(` ${result.description}`);
console.log('');
});
高度なユースケース
結果処理を伴う検索
import ZAI from 'z-ai-web-dev-sdk';
class SearchProcessor {
constructor() {
this.zai = null;
}
async initialize() {
this.zai = await ZAI.create();
}
async search(query, options = {}) {
const {
num = 10,
filterDomain = null,
minSnippetLength = 0
} = options;
const results = await this.zai.functions.invoke('web_search', {
query: query,
num: num
});
// Filter results
let filtered = results;
if (filterDomain) {
filtered = filtered.filter(item =>
item.host_name.includes(filterDomain)
);
}
if (minSnippetLength > 0) {
filtered = filtered.filter(item =>
item.snippet.length >= minSnippetLength
);
}
return filtered;
}
extractDomains(results) {
return [...new Set(results.map(item => item.host_name))];
}
groupByDomain(results) {
const grouped = {};
results.forEach(item => {
if (!grouped[item.host_name]) {
grouped[item.host_name] = [];
}
grouped[item.host_name].push(item);
});
return grouped;
}
sortByDate(results, ascending = false) {
return results.sort((a, b) => {
const dateA = new Date(a.date);
const dateB = new Date(b.date);
return ascending ? dateA - dateB : dateB - dateA;
});
}
}
// Usage
const processor = new SearchProcessor();
await processor.initialize();
const results = await processor.search('machine learning tutorials', {
num: 15,
minSnippetLength: 50
});
console.log('Domains found:', processor.extractDomains(results));
console.log('Grouped by domain:', processor.groupByDomain(results));
console.log('Sorted by date:', processor.sortByDate(results));
ニュース検索
import ZAI from 'z-ai-web-dev-sdk';
async function searchNews(topic, timeframe = 'recent') {
const zai = await ZAI.create();
// Add time-based keywords to query
const timeKeywords = {
recent: 'latest news',
today: 'today news',
week: 'this week news',
month: 'this month news'
};
const query = `${topic} ${timeKeywords[timeframe] || timeKeywords.recent}`;
const results = await zai.functions.invoke('web_search', {
query: query,
num: 10
});
// Sort by date (most recent first)
const sortedResults = results.sort((a, b) => {
return new Date(b.date) - new Date(a.date);
});
return sortedResults;
}
// Usage
const aiNews = await searchNews('artificial intelligence', 'today');
const techNews = await searchNews('technology', 'week');
console.log('Latest AI News:');
aiNews.forEach(item => {
console.log(`${item.name} (${item.date})`);
console.log(`${item.snippet}\n`);
});
リサーチアシスタント
import ZAI from 'z-ai-web-dev-sdk';
class ResearchAssistant {
constructor() {
this.zai = null;
}
async initialize() {
this.zai = await ZAI.create();
}
async researchTopic(topic, depth = 'standard') {
const numResults = {
quick: 5,
standard: 10,
deep: 20
};
const results = await this.zai.functions.invoke('web_search', {
query: topic,
num: numResults[depth] || 10
});
// Analyze results
const analysis = {
topic: topic,
totalResults: results.length,
sources: this.extractDomains(results),
topResults: results.slice(0, 5).map(r => ({
title: r.name,
url: r.url,
summary: r.snippet
})),
dateRange: this.getDateRange(results)
};
return analysis;
}
extractDomains(results) {
const domains = {};
results.forEach(item => {
domains[item.host_name] = (domains[item.host_name] || 0) + 1;
});
return domains;
}
getDateRange(results) {
const dates = results
.map(r => new Date(r.date))
.filter(d => !isNaN(d));
if (dates.length === 0) return null;
return {
earliest: new Date(Math.min(...dates)),
latest: new Date(Math.max(...dates))
};
}
async compareTopics(topic1, topic2) {
const [results1, results2] = await Promise.all([
this.zai.functions.invoke('web_search', { query: topic1, num: 10 }),
this.zai.functions.invoke('web_search', { query: topic2, num: 10 })
]);
const domains1 = new Set(results1.map(r => r.host_name));
const domains2 = new Set(results2.map(r => r.host_name));
const commonDomains = [...domains1].filter(d => domains2.has(d));
return {
topic1: {
name: topic1,
results: results1.length,
uniqueDomains: domains1.size
},
topic2: {
name: topic2,
results: results2.length,
uniqueDomains: domains2.size
},
commonDomains: commonDomains
};
}
}
// Usage
const assistant = new ResearchAssistant();
await assistant.initialize();
const research = await assistant.researchTopic('quantum computing', 'deep');
console.log('Research Analysis:', research);
const comparison = await assistant.compareTopics(
'renewable energy',
'solar power'
);
console.log('Topic Comparison:', comparison);
検索結果の検証
import ZAI from 'z-ai-web-dev-sdk';
async function validateSearchResults(query) {
const zai = await ZAI.create();
const results = await zai.functions.invoke('web_search', {
query: query,
num: 10
});
// Validate and score results
const validated = results.map(item => {
let score = 0;
let flags = [];
// Check snippet quality
if (item.snippet && item.snippet.length > 50) {
score += 20;
} else {
flags.push('short_snippet');
}
// Check date availability
if (item.date && item.date !== 'N/A') {
score += 20;
} else {
flags.push('no_date');
}
// Check URL validity
try {
new URL(item.url);
score += 20;
} catch (e) {
flags.push('invalid_url');
}
// Check domain quality (not perfect, but basic check)
if (!item.host_name.includes('spam') &&
!item.host_name.includes('ads')) {
score += 20;
} else {
flags.push('suspicious_domain');
}
// Check title quality
if (item.name && item.name.length > 10) {
score += 20;
} else {
flags.push('short_title');
}
return {
...item,
qualityScore: score,
validationFlags: flags,
isHighQuality: score >= 80
};
});
// Sort by quality score
return validated.sort((a, b) => b.qualityScore - a.qualityScore);
}
// Usage
const validated = await validateSearchResults('best programming practices');
console.log('High quality results:',
validated.filter(r => r.isHighQuality).length
);
ベストプラクティス
1. クエリの最適化
// Bad: Too vague
const bad = await searchWeb('information');
// Good: Specific and targeted
const good = await searchWeb('JavaScript async/await best practices 2024');
// Good: Include context
const goodWithContext = await searchWeb('React hooks tutorial for beginners');
2. エラーハンドリング
import ZAI from 'z-ai-web-dev-sdk';
async function safeSearch(query, retries = 3) {
let lastError;
for (let attempt = 1; attempt <= retries; attempt++) {
try {
const zai = await ZAI.create();
const results = await zai.functions.invoke('web_search', {
query: query,
num: 10
});
if (!Array.isArray(results) || results.length === 0) {
throw new Error('No results found or invalid response');
}
return {
success: true,
results: results,
attempts: attempt
};
} catch (error) {
lastError = error;
console.error(`Attempt ${attempt} failed:`, error.message);
if (attempt < retries) {
// Wait before retry (exponential backoff)
await new Promise(resolve => setTimeout(resolve, 1000 * attempt));
}
}
}
return {
success: false,
error: lastError.message,
attempts: retries
};
}
3. 結果のキャッシュ
import ZAI from 'z-ai-web-dev-sdk';
class CachedSearch {
constructor(cacheDuration = 3600000) { // 1 hour default
this.cache = new Map();
this.cacheDuration = cacheDuration;
this.zai = null;
}
async initialize() {
this.zai = await ZAI.create();
}
getCacheKey(query, num) {
return `${query}_${num}`;
}
async search(query, num = 10) {
const cacheKey = this.getCacheKey(query, num);
const cached = this.cache.get(cacheKey);
// Check if cached and not expired
if (cached && Date.now() - cached.timestamp < this.cacheDuration) {
console.log('Returning cached results');
return {
...cached.data,
cached: true
};
}
// Perform fresh search
const results = await this.zai.functions.invoke('web_search', {
query: query,
num: num
});
// Cache results
this.cache.set(cacheKey, {
data: results,
timestamp: Date.now()
});
return {
results: results,
cached: false
};
}
clearCache() {
this.cache.clear();
}
getCacheSize() {
return this.cache.size;
}
}
// Usage
const search = new CachedSearch(1800000); // 30 minutes cache
await search.initialize();
const result1 = await search.search('TypeScript tutorial');
console.log('Cached:', result1.cached); // false
const result2 = await search.search('TypeScript tutorial');
console.log('Cached:', result2.cached); // true
4. レート制限
class RateLimitedSearch {
constructor(requestsPerMinute = 60) {
this.zai = null;
this.requestsPerMinute = requestsPerMinute;
this.requests = [];
}
async initialize() {
this.zai = await ZAI.create();
}
async search(query, num = 10) {
await this.checkRateLimit();
const results = await this.zai.functions.invoke('web_search', {
query: query,
num: num
});
this.requests.push(Date.now());
return results;
}
async checkRateLimit() {
const now = Date.now();
const oneMinuteAgo = now - 60000;
// Remove requests older than 1 minute
this.requests = this.requests.filter(time => time > oneMinuteAgo);
if (this.requests.length >= this.requestsPerMinute) {
const oldestRequest = this.requests[0];
const waitTime = 60000 - (now - oldestRequest);
console.log(`Rate limit reached. Waiting ${waitTime}ms`);
await new Promise(resolve => setTimeout(resolve, waitTime));
// Recheck after waiting
return this.checkRateLimit();
}
}
}
一般的なユースケース
- リアルタイム情報の取得: 最新のニュース、株価、天気を取得する
- 調査と分析: 特定のトピックに関する情報を収集する
- コンテンツの発見: 記事、チュートリアル、ドキュメントを見つける
- 競合分析: 競合他社や市場動向を調査する
- ファクトチェック: Web ソースと照らし合わせて情報を検証する
- SEO とコンテンツリサーチ: コンテンツ戦略のために検索結果を分析する
- ニュースの集約: さまざまなソースからニュースを収集する
- 学術研究: 論文、研究、学術コンテンツを見つける
統合の例
Express.js 検索 API
import express from 'express';
import ZAI from 'z-ai-web-dev-sdk';
const app = express();
app.use(express.json());
let zaiInstance;
async function initZAI() {
zaiInstance = await ZAI.create();
}
app.get('/api/search', async (req, res) => {
try {
const { q: query, num = 10 } = req.query;
if (!query) {
return res.status(400).json({ error: 'Query parameter "q" is required' });
}
const numResults = Math.min(parseInt(num) || 10, 20);
const results = await zaiInstance.functions.invoke('web_search', {
query: query,
num: numResults
});
res.json({
success: true,
query: query,
totalResults: results.length,
results: results
});
} catch (error) {
res.status(500).json({
success: false,
error: error.message
});
}
});
app.get('/api/search/news', async (req, res) => {
try {
const { topic, timeframe = 'recent' } = req.query;
if (!topic) {
return res.status(400).json({ error: 'Topic parameter is required' });
}
const timeKeywords = {
recent: 'latest news',
today: 'today news',
week: 'this week news'
};
const query = `${topic} ${timeKeywords[timeframe] || timeKeywords.recent}`;
const results = await zaiInstance.functions.invoke('web_search', {
query: query,
num: 15
});
// Sort by date
const sortedResults = results.sort((a, b) => {
return new Date(b.date) - new Date(a.date);
});
res.json({
success: true,
topic: topic,
timeframe: timeframe,
results: sortedResults
});
} catch (error) {
res.status(500).json({
success: false,
error: error.message
});
}
});
initZAI().then(() => {
app.listen(3000, () => {
console.log('Search API running on port 3000');
});
});
AI 要約付きの検索
import ZAI from 'z-ai-web-dev-sdk';
async function searchAndSummarize(query) {
const zai = await ZAI.create();
// Step 1: Search the web
const searchResults = await zai.functions.invoke('web_search', {
query: query,
num: 10
});
// Step 2: Create summary using chat completions
const searchContext = searchResults
.slice(0, 5)
.map((r, i) => `${i + 1}. ${r.name}\n${r.snippet}`)
.join('\n\n');
const completion = await zai.chat.completions.create({
messages: [
{
role: 'assistant',
content: 'You are a research assistant. Summarize search results clearly and concisely.'
},
{
role: 'user',
content: `Query: "${query}"\n\nSearch Results:\n${searchContext}\n\nProvide a comprehensive summary of these results.`
}
],
thinking: { type: 'disabled' }
});
const summary = completion.choices[0]?.message?.content;
return {
query: query,
summary: summary,
sources: searchResults.slice(0, 5).map(r => ({
title: r.name,
url: r.url
})),
totalResults: searchResults.length
};
}
// Usage
const result = await searchAndSummarize('benefits of renewable energy');
console.log('Summary:', result.summary);
console.log('Sources:', result.sources);
トラブルシューティング
問題: 「SDK must be used in backend」
- 解決策: z-ai-web-dev-sdk がサーバー側のコードでのみインポートされ、使用されていることを確認してください
問題: 結果が空、または返されない
- 解決策: 別のクエリ用語を試し、インターネット接続を確認し、API のステータスを検証してください
問題: 予期しないレスポンス形式
- 解決策: レスポンスが配列であることを確認し、API の変更をチェックし、型の検証を追加してください
問題: レート制限エラー
- 解決策: リクエストのスロットリングを実装し、検索間に遅延を追加し、キャッシュを使用してください
問題: 検索結果の品質が低い
- 解決策: クエリ用語を絞り込み、ドメインや日付で結果をフィルタリングし、結果の品質を検証してください
パフォーマンスのヒント
- SDK インスタンスの再利用: ZAI インスタンスを一度作成し、検索間で再利用します
- キャッシュの実装: 検索結果をキャッシュして API 呼び出しを減らします
- クエリ用語の最適化: より良い結果を得るために、具体的で的を絞ったクエリを使用します
- 結果件数の制限: 必要な件数の結果のみをリクエストします
- 並列検索: 複数の独立した検索には Promise.all を使用します
- 結果のフィルタリング: 可能な場合はクライアント側で結果をフィルタリングします
セキュリティに関する考慮事項
- 入力の検証: ユーザーの検索クエリをサニタイズして検証します
- レート制限: 悪用を防ぐためにレート制限を実装します
- API キーの保護: クライアント側のコードで SDK の認証情報を絶対に公開しないでください
- 結果のフィルタリング: 有害または不適切な可能性のあるコンテンツをフィルタリングします
- URL の検証: ユーザーをリダイレクトする前に URL を検証します
- プライバシー: 機密性の高いユーザーの検索クエリをログに記録しないでください
覚えておくこと
- z-ai-web-dev-sdk は常にバックエンドのコードでのみ使用してください
- SDK はすでにインストールされています - 例に示すようにインポートしてください
- 検索結果は SearchFunctionResultItem オブジェクトの配列として返されます
- 本番環境では適切なエラーハンドリングと再試行を実装してください
- API 呼び出しを減らすために、適切な場合は結果をキャッシュしてください
- より良い検索結果を得るために、具体的なクエリ用語を使用してください
- ユーザーに表示する前に結果を検証してフィルタリングしてください
- クイックスタートの例については
scripts/web_search.tsを確認してください