diff options
| author | sigoden <sigoden@gmail.com> | 2024-12-10 12:44:37 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-12-10 12:44:37 +0800 |
| commit | 0f0ae607d3ffcc2c43c67536d8ac0bc16ce9998a (patch) | |
| tree | 81ed685b7daf772a9b4090174a8f9449ccc0fb41 | |
| parent | de2515ae1ce2baf7c6c21c81ec8f12053b868eb7 (diff) | |
| download | llm-functions-docker-0f0ae607d3ffcc2c43c67536d8ac0bc16ce9998a.tar.gz | |
feat(agent): add json-viewer (#134)
| -rw-r--r-- | agents/json-viewer/README.md | 10 | ||||
| -rw-r--r-- | agents/json-viewer/index.yaml | 5 | ||||
| -rw-r--r-- | agents/json-viewer/package.json | 7 | ||||
| -rw-r--r-- | agents/json-viewer/tools.js | 49 |
4 files changed, 71 insertions, 0 deletions
diff --git a/agents/json-viewer/README.md b/agents/json-viewer/README.md new file mode 100644 index 0000000..1b75169 --- /dev/null +++ b/agents/json-viewer/README.md @@ -0,0 +1,10 @@ +# Json-Viewer + +An AI agent to view and filter json data + +The agent only sends the JSON schema instead of the JSON data to the LLM, which has the following advantages: + +- Less data transmission, faster response speed, and lower token costs. +- More privacy, as no actual JSON data is transmitted. + +
\ No newline at end of file diff --git a/agents/json-viewer/index.yaml b/agents/json-viewer/index.yaml new file mode 100644 index 0000000..82e6868 --- /dev/null +++ b/agents/json-viewer/index.yaml @@ -0,0 +1,5 @@ +name: Json-Viewer +description: An AI agent to view and filter json data +version: 0.1.0 +instructions: "" +dynamic_instructions: true
\ No newline at end of file diff --git a/agents/json-viewer/package.json b/agents/json-viewer/package.json new file mode 100644 index 0000000..3b6bbc6 --- /dev/null +++ b/agents/json-viewer/package.json @@ -0,0 +1,7 @@ +{ + "dependencies": { + "@inquirer/input": "^4.0.2", + "json-schema-generator": "^2.0.6", + "node-jq": "^6.0.1" + } +} diff --git a/agents/json-viewer/tools.js b/agents/json-viewer/tools.js new file mode 100644 index 0000000..b1f032d --- /dev/null +++ b/agents/json-viewer/tools.js @@ -0,0 +1,49 @@ +const fs = require("node:fs/promises"); +const { exec } = require("node:child_process"); +const { promisify } = require("node:util"); +const path = require("node:path"); +const { tmpdir } = require("node:os"); + +const jq = require("node-jq"); +const jsonSchemaGenerator = require("json-schema-generator"); +const input = require("@inquirer/input").default; + +exports._instructions = async function () { + const value = await input({ message: "Enter the json file path or command to generate json", required: true }); + let json_file_path; + let generate_json_command_context = ""; + try { + await fs.access(value); + json_file_path = value; + } catch { + generate_json_command_context = `command_to_generate_json: \`${value}\`\n`; + const { stdout } = await promisify(exec)(value, { maxBuffer: 100 * 1024 * 1024}); + json_file_path = path.join(tmpdir(), `${process.env.LLM_AGENT_NAME}-${process.pid}.data.json`); + await fs.writeFile(json_file_path, stdout); + console.log(`ⓘ Generated json data saved to: ${json_file_path}`); + } + + const json_data = await fs.readFile(json_file_path, "utf8"); + const json_schema = jsonSchemaGenerator(JSON.parse(json_data)); + + return `You are a AI agent that can view and filter json data with jq. + +## Context +${generate_json_command_context}json_file_path: ${json_file_path} +json_schema: ${JSON.stringify(json_schema, null, 2)} +` +} + +/** + * Print the json data. + * + * @typedef {Object} Args + * @property {string} json_file_path The json file path + * @property {string} jq_expr The jq expression + * @param {Args} args + */ +exports.print_json = async function (args) { + const { json_file_path, jq_expr } = args; + const result = await jq.run(jq_expr, json_file_path, { raw: true }); + console.log(result); +} |
