This project implements an AI agent using the ReAct (Reasoning and Acting) paradigm, which enables the AI to reason about tasks and take actions using external tools. It leverages large language models to process natural language queries and interact with real-world data through a structured reasoning framework.
In this implementation, the agent can:
- Understand natural language queries
- Reason about how to solve a problem
- Take appropriate actions using available tools
- Observe the results of those actions
- Continue reasoning until reaching a final answer
- ReAct Paradigm: Implements the Reasoning-Action-Observation loop
- Weather Tool Integration: Allows querying weather information for specific locations
- Flexible Architecture: Easily extendable to add more tools
- Conversation History Management: Maintains context throughout interactions
- Regex-based Response Parsing: Extracts actions and final answers from LLM responses
- Go 1.21.3 or higher
- OpenAI API key or DashScope API key (Alibaba Cloud)
-
Clone the repository:
git clone https://github.com/your-username/agent-example.git cd agent-example -
Copy the environment template and add your API keys:
cp .env.template .env -
Edit the
.envfile with your API keys:DASH_SCOPE_API_KEY="your-dashscope-api-key" DASH_SCOPE_URL="https://dashscope.aliyuncs.com/compatible-mode/v1" DASH_SCOPE_MODEL="qwen-turbo" -
Install dependencies:
go mod tidy
Run the application:
go run main.go
The default query asks about the current weather in Shenzhen and whether it's suitable for outdoor activities. You can modify the query in the main.go file.
- The agent receives a natural language query
- It follows the ReAct format:
- Thought: Reasoning about what to do
- Action: Which tool to use
- Action Input: Parameters for the tool
- Observation: Result from the tool
- (Steps repeat until a conclusion is reached)
- Final Answer: The response to the original query
main.go: Main application logic and ReAct implementationtools/weather.go: Weather tool implementationprompt/prompt.go: Template for structuring agent prompts.env: Configuration file for API keys
To add a new tool:
- Create a new file in the
toolsdirectory - Implement the tool interface
- Register the tool in
main.go
This project is licensed under the terms of the included LICENSE file.
- ReAct paper: ReAct: Synergizing Reasoning and Acting in Language Models
- DashScope API (Alibaba Cloud)