A small React (ReAct) agent implementation using LangChain and OpenAI.
- React Agent: Uses the ReAct (Reasoning and Acting) pattern to solve problems
- Multiple Tools: Includes calculator, weather, and search tools
- Streaming Output: Shows the agent's reasoning process step by step
- TypeScript: Fully typed implementation
- Install dependencies:
npm install- Create a
.envfile with your OpenAI API key:
cp .env.example .env
# Edit .env and add your OPENAI_API_KEY- Run the demo:
npm run demonpm run demoThis will run several test queries demonstrating the agent's capabilities.
npm testimport { runAgent, runAgentVerbose } from "./src/agent";
// Simple usage - returns final response
const response = await runAgent("What is 25 multiplied by 17?");
console.log(response);
// Verbose usage - shows reasoning process
await runAgentVerbose("What's the weather in Tokyo?");- Calculator - Performs basic arithmetic (add, subtract, multiply, divide)
- Weather - Gets simulated weather for major cities
- Search - Searches for information on topics
├── src/
│ ├── agent.ts # Main React agent implementation
│ ├── tools.ts # Tool definitions
│ ├── index.ts # Module exports
│ ├── demo.ts # Demo script
│ └── __tests__/ # Test files
├── package.json
├── tsconfig.json
└── jest.config.js
You can customize the agent:
import { createAgent } from "./src/agent";
const agent = createAgent({
modelName: "gpt-4", // OpenAI model to use
temperature: 0.7, // Creativity (0-1)
});- Node.js 18+
- OpenAI API key