Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d2e711fbf2 | |||
| 596e99daa7 | |||
| eda4eed87d |
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ztimson/ai-utils",
|
||||
"version": "0.8.13",
|
||||
"version": "0.8.16",
|
||||
"description": "AI Utility library",
|
||||
"author": "Zak Timson",
|
||||
"license": "MIT",
|
||||
|
||||
19
src/llm.ts
19
src/llm.ts
@@ -358,7 +358,7 @@ class LLM {
|
||||
* @returns {Promise<{} | {} | RegExpExecArray | null>}
|
||||
*/
|
||||
async json(text: string, schema: string, options?: LLMRequest): Promise<any> {
|
||||
let system = `Your job is to convert input to JSON. Call the \`submit\` tool exactly once with JSON matching this schema:\n\`\`\`json\n${schema}\n\`\`\``;
|
||||
let system = `Your job is to convert input to JSON using tool calls. Call the \`submit\` tool at least once with JSON matching this schema:\n\`\`\`json\n${schema}\n\`\`\`\n\nResponses are ignored`;
|
||||
if(options?.system) system += '\n\n' + options.system;
|
||||
return new Promise(async (resolve, reject) => {
|
||||
let done = false;
|
||||
@@ -376,23 +376,23 @@ class LLM {
|
||||
resolve(json);
|
||||
done = true;
|
||||
} catch { return 'Invalid JSON'; }
|
||||
return 'Done';
|
||||
return 'Saved';
|
||||
}
|
||||
}, ...(options?.tools || [])],
|
||||
});
|
||||
if(!done) reject(`AI failed to create summary: ${resp}`);
|
||||
if(!done) reject(`AI failed to create JSON:\n${resp}`);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a summary of some text
|
||||
* @param {string} text Text to summarize
|
||||
* @param {number} tokens Max number of tokens
|
||||
* @param {number} length Max number of words
|
||||
* @param options LLM request options
|
||||
* @returns {Promise<string>} Summary
|
||||
*/
|
||||
async summarize(text: string, tokens: number = 500, options?: LLMRequest): Promise<string | null> {
|
||||
let system = `Your job is to summarize the users message. Call the \`submit\` tool exactly once with the shortest summary possible that's <= ${tokens} tokens. Output nothing else`;
|
||||
async summarize(text: string, length: number = 500, options?: LLMRequest): Promise<string | null> {
|
||||
let system = `Your job is to summarize the users message using tool calls. Call the \`submit\` tool at least once with the shortest summary possible that's <= ${length} words. The tool call will respond with the token count. Responses are ignored`;
|
||||
if(options?.system) system += '\n\n' + options.system;
|
||||
return new Promise(async (resolve, reject) => {
|
||||
let done = false;
|
||||
@@ -405,13 +405,16 @@ class LLM {
|
||||
description: 'Submit summary',
|
||||
args: {summary: {type: 'string', description: 'Text summarization', required: true}},
|
||||
fn: (args) => {
|
||||
if(!args.summary) return 'No summary provided';
|
||||
const count = args.summary.split(' ').length;
|
||||
if(count > length) return `Too long: ${length} words`;
|
||||
done = true;
|
||||
resolve(args.summary || null);
|
||||
return 'Done';
|
||||
return `Saved: ${length} words`;
|
||||
}
|
||||
}, ...(options?.tools || [])],
|
||||
});
|
||||
if(!done) reject(`AI failed to create summary: ${resp}`);
|
||||
if(!done) reject(`AI failed to create summary:\n${resp}`);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
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