A basic Node.js example demonstrating the "LLM + Loop + Tools" pattern for an AI agent. This agent uses an LLM (like GPT-4o Mini) and ESLint via a custom tool to automatically fix JavaScript linting errors in a target file.
- Node.js (v18 or later recommended)
- npm (usually comes with Node.js)
- An OpenAI API Key
- Clone the repository:
git clone <your-repository-url> cd <repository-directory-name>
- Install dependencies:
npm install
- Configure your API Key:
- Create a file named
.envin the root of the project. - Add your OpenAI API key to the
.envfile:OPENAI_API_KEY=sk-YourSecretOpenAiApiKeyGoesHere
- Create a file named
- Prepare the target file: Ensure the file
target_code.jsexists in the root directory and contains JavaScript code with linting errors (according to the.eslintrc.jsonrules). You can modify this file or create your own. - Run the agent:
node index.js
- Reads the content of
target_code.js. - Calls the OpenAI API (configured model, e.g.,
gpt-4o-mini). - Uses the
runLintertool, which executes ESLint to find errors. - The LLM analyzes the errors and attempts to fix the code.
- It uses the
runLintertool again to verify the fixes. - Prints the final corrected code to the console.
- Saves the corrected code to
target_code_corrected.js.
- Target File: Change the
TARGET_FILE_PATHvariable inindex.jsto point to a different file. - Linting Rules: Modify the
.eslintrc.jsonfile to adjust ESLint rules. - LLM Model: Change the
modelvalue insrc/config.js. - System Prompt: Adjust the instructions in
src/config.js(be careful, this affects agent behavior significantly).