FEAT: implement temp version of main tooling feedback loop

This commit is contained in:
William Jeynes
2026-02-09 20:25:36 +00:00
parent 5841e8a922
commit cd2c8621e8
13 changed files with 119 additions and 72 deletions
-40
View File
@@ -1,40 +0,0 @@
import { tool } from "@langchain/core/tools";
import * as z from "zod";
// Define tools
const add = tool(({ a, b }) => a + b, {
name: "add",
description: "Add two numbers",
schema: z.object({
a: z.number().describe("First number"),
b: z.number().describe("Second number"),
}),
});
const multiply = tool(({ a, b }) => a * b, {
name: "multiply",
description: "Multiply two numbers",
schema: z.object({
a: z.number().describe("First number"),
b: z.number().describe("Second number"),
}),
});
const divide = tool(({ a, b }) => a / b, {
name: "divide",
description: "Divide two numbers",
schema: z.object({
a: z.number().describe("First number"),
b: z.number().describe("Second number"),
}),
});
// Augment the LLM with tools
export const arithmeticToolsByName = {
[add.name]: add,
[multiply.name]: multiply,
[divide.name]: divide,
};
//const arithmeticTools = Object.values(arithmeticToolsByName);
+1
View File
@@ -3,6 +3,7 @@ import fs from "fs";
import { pipeline, cos_sim } from "@huggingface/transformers";
import { logger } from "../../utils/logger";
//TODO, am getting duplicates, is it from the multi files?
const CSV_PATHS = [
"./tools/clan/dev-eng.csv",
// "./tools/clan/test-eng.csv",
+45
View File
@@ -0,0 +1,45 @@
import { tool } from "@langchain/core/tools";
import * as z from "zod";
import { queryScraper } from "./webSearch";
import { extractWebpageContent } from "./webpageFetch";
function rankAndDisplayData(data: string[]):string {
//TODO: hybrid re-ranking of the provided data
return data.join("\n")
}
// Define tools
const webSearch = tool(
async ({ a }) => {
const data = await queryScraper(a);
return rankAndDisplayData(data);
},
{
name: "WebSearch",
description: "Search DuckDuckGo for the provided query",
schema: z.object({
a: z.string().describe("Search term"),
}),
}
);
const openWebpage = tool(
async ({ a }) => {
const data = await extractWebpageContent(a);
return rankAndDisplayData(data);
},
{
name: "OpenWebpage",
description: "Opens webpage and returns most relevent snippets",
schema: z.object({
a: z.string().describe("URL"),
}),
}
);
// Augment the LLM with tools
export const triggerEventToolsByName = {
[webSearch.name]: webSearch,
[openWebpage.name]: openWebpage
};
+16 -5
View File
@@ -1,6 +1,6 @@
import axios from "axios";
export async function queryScraper(query: string) {
export async function queryScraper(query: string): Promise<string[]> {
const instance = process.env.SCRAPER_INSTANCE;
if (!instance) {
throw new Error("SCRAPER_INSTANCE environment variable is not set");
@@ -10,8 +10,8 @@ export async function queryScraper(query: string) {
const url = `${instance}/api/v1/web`;
const params : Record<string, string> = Object.entries(process.env)
.filter(([key, value]) => key.startsWith("SCRAPER_PARAM_") && value !== undefined)
const params: Record<string, string> = Object.entries(process.env)
.filter(([key, value]) => key.startsWith("SCRAPER_PARAM_") && value !== undefined)
.reduce((acc: Record<string, string>, [key, value]) => {
const paramName = key.replace(/^SCRAPER_PARAM_/, "").toLowerCase();
acc[paramName] = value!;
@@ -35,12 +35,23 @@ export async function queryScraper(query: string) {
const data = response.data;
// Basic validation
if (data?.status !== "ok") {
throw new Error(`API returned status: ${data?.status}`);
}
return data;
// TEMP?: Convert API results to array of formatted strings.
const context = data.web ?? [];
const lines: string[] = context.map((item: any) => {
const title = (item.title ?? "").trim();
const desc = (item.description ?? "").trim();
const link = (item.url ?? "").trim();
return `- ${title}\n ${desc}\n ${link}`;
});
return lines;
}
+7 -2
View File
@@ -1,7 +1,7 @@
import { Builder, Browser } from "selenium-webdriver";
import firefox from "selenium-webdriver/firefox";
async function extractWebpageContent(url: string) : Promise<string>{
export async function extractWebpageContent(url: string) : Promise<string[]>{
const options = new firefox.Options();
options.addArguments("--headless");
@@ -18,7 +18,12 @@ async function extractWebpageContent(url: string) : Promise<string>{
"return document.body.innerText;"
) as string;
return readableText
const filteredLines = readableText
.split(/\r?\n/)
.map(line => line.trim())
.filter(line => line.split(/\s+/).length > 1);
return filteredLines;
} finally {
await driver.quit()
}