Added wikipedia tools
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ztimson/ai-utils",
|
||||
"version": "0.8.15",
|
||||
"version": "0.8.16",
|
||||
"description": "AI Utility library",
|
||||
"author": "Zak Timson",
|
||||
"license": "MIT",
|
||||
|
||||
96
src/tools.ts
96
src/tools.ts
@@ -272,3 +272,99 @@ export const WebSearchTool: AiTool = {
|
||||
return results;
|
||||
}
|
||||
}
|
||||
|
||||
class WikipediaClient {
|
||||
private ua = 'AiTools-Wikipedia/1.0';
|
||||
|
||||
private async get(url: string): Promise<any> {
|
||||
const resp = await fetch(url, {headers: {'User-Agent': this.ua}});
|
||||
return resp.json();
|
||||
}
|
||||
|
||||
private api(params: Record<string, any>): Promise<any> {
|
||||
const qs = new URLSearchParams({...params, format: 'json', utf8: '1'}).toString();
|
||||
return this.get(`https://en.wikipedia.org/w/api.php?${qs}`);
|
||||
}
|
||||
|
||||
private clean(text: string): string {
|
||||
return text.replace(/\n{3,}/g, '\n\n').replace(/ {2,}/g, ' ').replace(/\[\d+\]/g, '').trim();
|
||||
}
|
||||
|
||||
private truncate(text: string, max: number): string {
|
||||
if(text.length <= max) return text;
|
||||
const cut = text.slice(0, max);
|
||||
const lastPara = cut.lastIndexOf('\n\n');
|
||||
return lastPara > max * 0.7 ? cut.slice(0, lastPara) : cut;
|
||||
}
|
||||
|
||||
private async searchTitles(query: string, limit = 6): Promise<any[]> {
|
||||
const data = await this.api({action: 'query', list: 'search', srsearch: query, srlimit: limit, srprop: 'snippet'});
|
||||
return data.query?.search || [];
|
||||
}
|
||||
|
||||
private async fetchExtract(title: string, intro = false): Promise<string> {
|
||||
const params: any = {action: 'query', prop: 'extracts', titles: title, explaintext: 1, redirects: 1};
|
||||
if(intro) params.exintro = 1;
|
||||
const data = await this.api(params);
|
||||
const page = Object.values(data.query?.pages || {})[0] as any;
|
||||
return this.clean(page?.extract || '');
|
||||
}
|
||||
|
||||
private pageUrl(title: string): string {
|
||||
return `https://en.wikipedia.org/wiki/${encodeURIComponent(title.replace(/ /g, '_'))}`;
|
||||
}
|
||||
|
||||
private stripHtml(text: string): string {
|
||||
return text.replace(/<[^>]+>/g, '');
|
||||
}
|
||||
|
||||
async lookup(query: string, detail: 'intro' | 'full' = 'intro'): Promise<string> {
|
||||
const results = await this.searchTitles(query, 6);
|
||||
if(!results.length) return `❌ No Wikipedia articles found for "${query}"`;
|
||||
|
||||
const title = results[0].title;
|
||||
const url = this.pageUrl(title);
|
||||
const content = await this.fetchExtract(title, detail === 'intro');
|
||||
|
||||
const text = this.truncate(content, detail === 'intro' ? 2000 : 8000);
|
||||
return `## ${title}\n🔗 ${url}\n\n${text}`;
|
||||
}
|
||||
|
||||
async search(query: string): Promise<string> {
|
||||
const results = await this.searchTitles(query, 8);
|
||||
if(!results.length) return `❌ No results for "${query}"`;
|
||||
|
||||
const lines = [`### Search results for "${query}"\n`];
|
||||
for(let i = 0; i < results.length; i++) {
|
||||
const r = results[i];
|
||||
const snippet = this.truncate(this.stripHtml(r.snippet || ''), 150);
|
||||
lines.push(`**${i + 1}. ${r.title}**\n${snippet}\n${this.pageUrl(r.title)}`);
|
||||
}
|
||||
return lines.join('\n\n');
|
||||
}
|
||||
}
|
||||
|
||||
export const WikipediaLookupTool: AiTool = {
|
||||
name: 'wikipedia_lookup',
|
||||
description: 'Get Wikipedia article content',
|
||||
args: {
|
||||
query: {type: 'string', description: 'Topic or article title', required: true},
|
||||
detail: {type: 'string', description: 'Content level: "intro" (summary, default) or "full" (complete article)', enum: ['intro', 'full'], default: 'intro'}
|
||||
},
|
||||
fn: async (args: {query: string; detail?: 'intro' | 'full'}) => {
|
||||
const wiki = new WikipediaClient();
|
||||
return wiki.lookup(args.query, args.detail || 'intro');
|
||||
}
|
||||
};
|
||||
|
||||
export const WikipediaSearchTool: AiTool = {
|
||||
name: 'wikipedia_search',
|
||||
description: 'Search Wikipedia for matching articles',
|
||||
args: {
|
||||
query: {type: 'string', description: 'Search terms', required: true}
|
||||
},
|
||||
fn: async (args: {query: string}) => {
|
||||
const wiki = new WikipediaClient();
|
||||
return wiki.search(args.query);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user