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
.env
in the root of the project. - Add your OpenAI API key to the
.env
file:OPENAI_API_KEY=sk-YourSecretOpenAiApiKeyGoesHere
- Create a file named
- Prepare the target file: Ensure the file
target_code.js
exists in the root directory and contains JavaScript code with linting errors (according to the.eslintrc.json
rules). 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
runLinter
tool, which executes ESLint to find errors. - The LLM analyzes the errors and attempts to fix the code.
- It uses the
runLinter
tool 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_PATH
variable inindex.js
to point to a different file. - Linting Rules: Modify the
.eslintrc.json
file to adjust ESLint rules. - LLM Model: Change the
model
value insrc/config.js
. - System Prompt: Adjust the instructions in
src/config.js
(be careful, this affects agent behavior significantly).