pycityagent 1.0.0__tar.gz → 2.0.0a1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pycityagent-2.0.0a1/PKG-INFO +208 -0
- pycityagent-2.0.0a1/README.md +169 -0
- pycityagent-2.0.0a1/pycityagent/__init__.py +8 -0
- pycityagent-2.0.0a1/pycityagent/agent.py +214 -0
- pycityagent-2.0.0a1/pycityagent/economy/__init__.py +5 -0
- pycityagent-2.0.0a1/pycityagent/economy/econ_client.py +307 -0
- pycityagent-2.0.0a1/pycityagent/environment/__init__.py +7 -0
- pycityagent-2.0.0a1/pycityagent/environment/interact/interact.py +141 -0
- pycityagent-2.0.0a1/pycityagent/environment/sence/__init__.py +0 -0
- {pycityagent-1.0.0/pycityagent/brain → pycityagent-2.0.0a1/pycityagent/environment/sence}/static.py +1 -1
- pycityagent-2.0.0a1/pycityagent/environment/sidecar/__init__.py +8 -0
- pycityagent-2.0.0a1/pycityagent/environment/sidecar/sidecarv2.py +109 -0
- pycityagent-2.0.0a1/pycityagent/environment/sim/__init__.py +27 -0
- pycityagent-2.0.0a1/pycityagent/environment/sim/aoi_service.py +38 -0
- pycityagent-2.0.0a1/pycityagent/environment/sim/client.py +126 -0
- pycityagent-2.0.0a1/pycityagent/environment/sim/clock_service.py +43 -0
- pycityagent-2.0.0a1/pycityagent/environment/sim/economy_services.py +191 -0
- pycityagent-2.0.0a1/pycityagent/environment/sim/lane_service.py +110 -0
- pycityagent-2.0.0a1/pycityagent/environment/sim/light_service.py +120 -0
- pycityagent-2.0.0a1/pycityagent/environment/sim/person_service.py +294 -0
- pycityagent-2.0.0a1/pycityagent/environment/sim/road_service.py +38 -0
- pycityagent-2.0.0a1/pycityagent/environment/sim/social_service.py +58 -0
- pycityagent-2.0.0a1/pycityagent/environment/simulator.py +369 -0
- pycityagent-2.0.0a1/pycityagent/environment/utils/__init__.py +8 -0
- pycityagent-2.0.0a1/pycityagent/environment/utils/geojson.py +26 -0
- pycityagent-2.0.0a1/pycityagent/environment/utils/grpc.py +57 -0
- pycityagent-2.0.0a1/pycityagent/environment/utils/map_utils.py +157 -0
- pycityagent-2.0.0a1/pycityagent/environment/utils/protobuf.py +39 -0
- pycityagent-2.0.0a1/pycityagent/llm/__init__.py +6 -0
- pycityagent-2.0.0a1/pycityagent/llm/embedding.py +136 -0
- pycityagent-2.0.0a1/pycityagent/llm/llm.py +430 -0
- pycityagent-2.0.0a1/pycityagent/llm/llmconfig.py +15 -0
- pycityagent-2.0.0a1/pycityagent/llm/utils.py +6 -0
- pycityagent-2.0.0a1/pycityagent/memory/__init__.py +11 -0
- pycityagent-2.0.0a1/pycityagent/memory/const.py +41 -0
- pycityagent-2.0.0a1/pycityagent/memory/memory.py +453 -0
- pycityagent-2.0.0a1/pycityagent/memory/memory_base.py +168 -0
- pycityagent-2.0.0a1/pycityagent/memory/profile.py +165 -0
- pycityagent-2.0.0a1/pycityagent/memory/self_define.py +165 -0
- pycityagent-2.0.0a1/pycityagent/memory/state.py +173 -0
- pycityagent-2.0.0a1/pycityagent/memory/utils.py +27 -0
- pycityagent-2.0.0a1/pycityagent/message/__init__.py +0 -0
- pycityagent-2.0.0a1/pycityagent/simulation/__init__.py +7 -0
- pycityagent-2.0.0a1/pycityagent/simulation/interview.py +36 -0
- pycityagent-2.0.0a1/pycityagent/simulation/simulation.py +286 -0
- pycityagent-2.0.0a1/pycityagent/simulation/survey/__init__.py +9 -0
- pycityagent-2.0.0a1/pycityagent/simulation/survey/manager.py +67 -0
- pycityagent-2.0.0a1/pycityagent/simulation/survey/models.py +49 -0
- pycityagent-2.0.0a1/pycityagent/simulation/ui/__init__.py +3 -0
- pycityagent-2.0.0a1/pycityagent/simulation/ui/interface.py +602 -0
- pycityagent-2.0.0a1/pycityagent/utils/__init__.py +0 -0
- pycityagent-2.0.0a1/pycityagent/utils/decorators.py +89 -0
- pycityagent-2.0.0a1/pycityagent/utils/parsers/__init__.py +12 -0
- pycityagent-2.0.0a1/pycityagent/utils/parsers/code_block_parser.py +37 -0
- pycityagent-2.0.0a1/pycityagent/utils/parsers/json_parser.py +86 -0
- pycityagent-2.0.0a1/pycityagent/utils/parsers/parser_base.py +60 -0
- pycityagent-2.0.0a1/pycityagent/workflow/__init__.py +22 -0
- pycityagent-2.0.0a1/pycityagent/workflow/block.py +137 -0
- pycityagent-2.0.0a1/pycityagent/workflow/prompt.py +72 -0
- pycityagent-2.0.0a1/pycityagent/workflow/tool.py +246 -0
- pycityagent-2.0.0a1/pycityagent/workflow/trigger.py +66 -0
- pycityagent-2.0.0a1/pyproject.toml +51 -0
- pycityagent-1.0.0/LICENSE +0 -21
- pycityagent-1.0.0/PKG-INFO +0 -181
- pycityagent-1.0.0/README.md +0 -134
- pycityagent-1.0.0/pycityagent/__init__.py +0 -4
- pycityagent-1.0.0/pycityagent/ac/__init__.py +0 -6
- pycityagent-1.0.0/pycityagent/ac/ac.py +0 -50
- pycityagent-1.0.0/pycityagent/ac/action.py +0 -14
- pycityagent-1.0.0/pycityagent/ac/controled.py +0 -13
- pycityagent-1.0.0/pycityagent/ac/converse.py +0 -31
- pycityagent-1.0.0/pycityagent/ac/idle.py +0 -17
- pycityagent-1.0.0/pycityagent/ac/shop.py +0 -80
- pycityagent-1.0.0/pycityagent/ac/trip.py +0 -37
- pycityagent-1.0.0/pycityagent/agent.py +0 -318
- pycityagent-1.0.0/pycityagent/brain/__init__.py +0 -10
- pycityagent-1.0.0/pycityagent/brain/brain.py +0 -52
- pycityagent-1.0.0/pycityagent/brain/brainfc.py +0 -10
- pycityagent-1.0.0/pycityagent/brain/memory.py +0 -541
- pycityagent-1.0.0/pycityagent/brain/persistence/social.py +0 -1
- pycityagent-1.0.0/pycityagent/brain/persistence/spatial.py +0 -14
- pycityagent-1.0.0/pycityagent/brain/reason/shop.py +0 -37
- pycityagent-1.0.0/pycityagent/brain/reason/social.py +0 -148
- pycityagent-1.0.0/pycityagent/brain/reason/trip.py +0 -67
- pycityagent-1.0.0/pycityagent/brain/reason/user.py +0 -122
- pycityagent-1.0.0/pycityagent/brain/retrive/social.py +0 -6
- pycityagent-1.0.0/pycityagent/brain/scheduler.py +0 -408
- pycityagent-1.0.0/pycityagent/brain/sence.py +0 -375
- pycityagent-1.0.0/pycityagent/cc/__init__.py +0 -5
- pycityagent-1.0.0/pycityagent/cc/cc.py +0 -102
- pycityagent-1.0.0/pycityagent/cc/conve.py +0 -6
- pycityagent-1.0.0/pycityagent/cc/idle.py +0 -20
- pycityagent-1.0.0/pycityagent/cc/shop.py +0 -6
- pycityagent-1.0.0/pycityagent/cc/trip.py +0 -13
- pycityagent-1.0.0/pycityagent/cc/user.py +0 -13
- pycityagent-1.0.0/pycityagent/hubconnector/__init__.py +0 -3
- pycityagent-1.0.0/pycityagent/hubconnector/hubconnector.py +0 -137
- pycityagent-1.0.0/pycityagent/image/__init__.py +0 -3
- pycityagent-1.0.0/pycityagent/image/image.py +0 -158
- pycityagent-1.0.0/pycityagent/simulator.py +0 -161
- pycityagent-1.0.0/pycityagent/st/__init__.py +0 -4
- pycityagent-1.0.0/pycityagent/st/st.py +0 -96
- pycityagent-1.0.0/pycityagent/urbanllm/__init__.py +0 -3
- pycityagent-1.0.0/pycityagent/urbanllm/urbanllm.py +0 -132
- pycityagent-1.0.0/pycityagent.egg-info/PKG-INFO +0 -181
- pycityagent-1.0.0/pycityagent.egg-info/SOURCES.txt +0 -51
- pycityagent-1.0.0/pycityagent.egg-info/dependency_links.txt +0 -1
- pycityagent-1.0.0/pycityagent.egg-info/requires.txt +0 -10
- pycityagent-1.0.0/pycityagent.egg-info/top_level.txt +0 -1
- pycityagent-1.0.0/pyproject.toml +0 -43
- pycityagent-1.0.0/setup.cfg +0 -4
- /pycityagent-1.0.0/pycityagent/brain/persistence/__init__.py → /pycityagent-2.0.0a1/pycityagent/config.py +0 -0
- {pycityagent-1.0.0/pycityagent/brain/reason → pycityagent-2.0.0a1/pycityagent/environment/interact}/__init__.py +0 -0
- {pycityagent-1.0.0/pycityagent/brain/retrive → pycityagent-2.0.0a1/pycityagent/environment/message}/__init__.py +0 -0
@@ -0,0 +1,208 @@
|
|
1
|
+
Metadata-Version: 2.1
|
2
|
+
Name: pycityagent
|
3
|
+
Version: 2.0.0a1
|
4
|
+
Summary: LLM-based城市环境agent构建库
|
5
|
+
License: MIT
|
6
|
+
Author: Yuwei Yan
|
7
|
+
Author-email: pinkgranite86@gmail.com
|
8
|
+
Requires-Python: >=3.10,<4.0
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
10
|
+
Classifier: Operating System :: OS Independent
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
12
|
+
Classifier: Programming Language :: Python :: 3.10
|
13
|
+
Classifier: Programming Language :: Python :: 3.11
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
15
|
+
Classifier: Programming Language :: Python :: 3.13
|
16
|
+
Requires-Dist: Pillow (==11.0.0)
|
17
|
+
Requires-Dist: Requests (==2.32.3)
|
18
|
+
Requires-Dist: Shapely (==2.0.6)
|
19
|
+
Requires-Dist: aiohttp (==3.10.10)
|
20
|
+
Requires-Dist: citystreetview (==1.2.4)
|
21
|
+
Requires-Dist: dashscope (==1.14.1)
|
22
|
+
Requires-Dist: geojson (==3.1.0)
|
23
|
+
Requires-Dist: gradio (>=5.7.1,<6.0.0)
|
24
|
+
Requires-Dist: grpcio (==1.67.1)
|
25
|
+
Requires-Dist: matplotlib (==3.8.3)
|
26
|
+
Requires-Dist: mosstool (==1.0.24)
|
27
|
+
Requires-Dist: networkx (==3.2.1)
|
28
|
+
Requires-Dist: numpy (>=1.20.0,<2.0.0)
|
29
|
+
Requires-Dist: openai (==1.54.3)
|
30
|
+
Requires-Dist: poetry (>=1.2.2)
|
31
|
+
Requires-Dist: protobuf (<=4.24.0)
|
32
|
+
Requires-Dist: pycitydata (==1.0.0)
|
33
|
+
Requires-Dist: pycityproto (==2.0.7)
|
34
|
+
Requires-Dist: pyyaml (>=6.0.2,<7.0.0)
|
35
|
+
Requires-Dist: sidecar (==0.7.0)
|
36
|
+
Requires-Dist: zhipuai (>=2.1.5.20230904,<3.0.0.0)
|
37
|
+
Description-Content-Type: text/markdown
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
<div style="text-align: center; background-color: white; padding: 20px; border-radius: 30px;">
|
42
|
+
<img src="./static/cityagent_logo.png" alt="CityAgent Logo" width="200" style="display: block; margin: 0 auto;">
|
43
|
+
<h1 style="color: black; margin: 0; font-size: 3em;">CityAgent: LLM Agents in City</h1>
|
44
|
+
</div>
|
45
|
+
|
46
|
+
|
47
|
+
# 🚀 CityAgent
|
48
|
+
  
|
49
|
+
[](https://docs.fiblab.net/pycityagent)  
|
50
|
+
|
51
|
+
|
52
|
+
CityAgent is an advanced framework specifically designed for building intelligent agents in urban simulation environments. With CityAgent, you can easily create and manage agents, enabling complex urban scenarios to be modeled and simulated efficiently.
|
53
|
+
|
54
|
+
## 🌟 Features
|
55
|
+
- **Modular Design**: Plug-and-play components for agent behavior.
|
56
|
+
- **Urban Environment Simulation**: Built to simulate diverse urban scenarios.
|
57
|
+
- **LLM Integration**: Connects to language models for enriched agent behavior.
|
58
|
+
- **Flexible Configuration**: YAML-based configuration for easy customization.
|
59
|
+
|
60
|
+
## 📑 Table of Contents
|
61
|
+
|
62
|
+
1. [News](#news)
|
63
|
+
2. [Framework](#framework)
|
64
|
+
3. [Setup](#setup)
|
65
|
+
4. [QuickStart](#quickstart)
|
66
|
+
5. [Contributing](#contributing)
|
67
|
+
6. [License](#license)
|
68
|
+
|
69
|
+
<a id="news"></a>
|
70
|
+
## 📰 News
|
71
|
+
|
72
|
+
- 📢 **11.10** - Initial update is now live!
|
73
|
+
- 📢 **2.x version is not compatible* with 1.x version**
|
74
|
+
|
75
|
+
Stay tuned for upcoming updates!
|
76
|
+
|
77
|
+
<a id="framework"></a>
|
78
|
+
## 🛠️ Framework
|
79
|
+
|
80
|
+
CityAgent is built with a multi-layered architecture that allows users to create and manage intelligent agents for urban environments in a scalable and flexible manner. The framework is divided into several key layers, each responsible for different functionalities as depicted in the diagram below:
|
81
|
+
|
82
|
+
<img src="./static/framework.png" alt="CityAgent Framework Overview" width="600" style="display: block; margin: 20px auto;">
|
83
|
+
|
84
|
+
### Architectural Layers
|
85
|
+
- **Model Layer**: Handles agent configuration, task definitions, logging setup, and result aggregation.
|
86
|
+
- **Task Configuration**: Defines agent behaviors and objectives.
|
87
|
+
- **Unified Execution**: Centralized entry point for agent processes.
|
88
|
+
|
89
|
+
- **Agent Layer**: Implements multi-head workflows to manage agent actions.
|
90
|
+
- **Memory**: Stores agent-related information, such as location and motion.
|
91
|
+
- **Static Profiles**: Maintains unchanging agent attributes.
|
92
|
+
- **Custom Data Pool**: Functions as a working memory.
|
93
|
+
- **Multi-Head Workflow**: Supports both normal and event-driven modes.
|
94
|
+
- **Reason Block**: Utilizes LLMs to determine decisions based on context and tools.
|
95
|
+
- **Route Block**: Chooses the best path based on defined criteria using LLMs or rules.
|
96
|
+
- **Action Block**: Executes actions as per defined contexts and tools.
|
97
|
+
|
98
|
+
- **Message Layer**: Facilitates agent communication through peer-to-peer (P2P), peer-to-group (P2G), and group chats.
|
99
|
+
|
100
|
+
- **Environment Layer**: Manages interaction with the urban environment.
|
101
|
+
- **Environment Sensing**: Reads data from the environment.
|
102
|
+
- **Interaction Handling**: Writes or modifies environmental states.
|
103
|
+
- **Message Management**: Handles incoming and outgoing agent messages.
|
104
|
+
|
105
|
+
- **LLM Layer**: Provides configuration and integration for using LLMs in the agent's workflow.
|
106
|
+
- **Prompting & Execution**: Supports model invocation and monitoring.
|
107
|
+
- **Model Support**: Compatible with various LLMs, such as OpenAI, Qwen, Deepseek, etc.
|
108
|
+
|
109
|
+
- **Tool Layer**: Provides additional utilities to the agents.
|
110
|
+
- **String Processing**: Handles parsing and formatting.
|
111
|
+
- **Result Analysis**: Parses responses in formats like JSON or dictionaries.
|
112
|
+
- **Data Storage & Retrieval**: Includes ranking and search tools.
|
113
|
+
|
114
|
+
<a id="setup"></a>
|
115
|
+
## ⚙️ Setup
|
116
|
+
|
117
|
+
You can set up CityAgent in two different ways:
|
118
|
+
|
119
|
+
### 1. From Scratch
|
120
|
+
|
121
|
+
Follow these steps to set up CityAgent from scratch by cloning the repository. The project is built using Python and managed with Poetry.
|
122
|
+
|
123
|
+
1. **Clone the Repository**
|
124
|
+
```bash
|
125
|
+
git clone [This Repository]
|
126
|
+
```
|
127
|
+
2. **Navigate to the Project Directory**
|
128
|
+
```bash
|
129
|
+
cd pycityagent
|
130
|
+
```
|
131
|
+
3. **Install Poetry** (if not installed)
|
132
|
+
```bash
|
133
|
+
curl -sSL https://install.python-poetry.org | python3 -
|
134
|
+
```
|
135
|
+
4. **Install Dependencies**
|
136
|
+
```bash
|
137
|
+
poetry install
|
138
|
+
```
|
139
|
+
5. **Activate the Virtual Environment**
|
140
|
+
```bash
|
141
|
+
poetry shell
|
142
|
+
```
|
143
|
+
|
144
|
+
### 2. Install via pip
|
145
|
+
|
146
|
+
This method is not yet available. Stay tuned for future updates!
|
147
|
+
|
148
|
+
<a id="quickstart"></a>
|
149
|
+
## 🚀 QuickStart
|
150
|
+
|
151
|
+
Get started with CityAgent in just a few minutes!
|
152
|
+
|
153
|
+
### 1. Config Configuration
|
154
|
+
CityAgent uses a configuration file written in `.yaml` format to manage settings for various components. Below is a sample configuration file (`config_template.yaml`) that showcases the structure:
|
155
|
+
|
156
|
+
```yaml
|
157
|
+
llm_request:
|
158
|
+
text_request:
|
159
|
+
request_type: openai
|
160
|
+
api_key: <YOUR_API_KEY>
|
161
|
+
model: gpt-4o
|
162
|
+
img_understand_request:
|
163
|
+
request_type: none
|
164
|
+
api_key: none
|
165
|
+
model: none
|
166
|
+
img_generate_request:
|
167
|
+
request_type: none
|
168
|
+
api_key: none
|
169
|
+
model: none
|
170
|
+
|
171
|
+
simulator_request:
|
172
|
+
simulator:
|
173
|
+
server: https://api-opencity-2x.fiblab.net:58081
|
174
|
+
map_request:
|
175
|
+
mongo_uri: <MONGO_URI>
|
176
|
+
mongo_db: llmsim
|
177
|
+
mongo_coll: map_beijing5ring_withpoi_0424
|
178
|
+
cache_dir: ./cache
|
179
|
+
route_request:
|
180
|
+
server: http://api-opencity-2x.fiblab.net:58082
|
181
|
+
streetview_request:
|
182
|
+
engine: baidumap / googlemap
|
183
|
+
mapAK: baidumap api-key (if you use baidumap engine)
|
184
|
+
proxy: googlemap proxy (if you use googlemap engine)
|
185
|
+
```
|
186
|
+
|
187
|
+
### 2. Example Usage
|
188
|
+
To get started quickly, please refer to the `examples` folder in the repository. It contains sample scripts and configurations to help you understand how to create and use agents in an urban simulation environment.
|
189
|
+
|
190
|
+
<a id="contributing"></a>
|
191
|
+
## 🤝 Contributing
|
192
|
+
We welcome contributions from the community!.
|
193
|
+
|
194
|
+
<a id="license"></a>
|
195
|
+
## 📄 License
|
196
|
+
|
197
|
+
CityAgent is licensed under the MIT License. See the [LICENSE](LICENSE) file for more details.
|
198
|
+
|
199
|
+
---
|
200
|
+
|
201
|
+
Feel free to reach out if you have any questions, suggestions, or want to collaborate!
|
202
|
+
|
203
|
+
---
|
204
|
+
|
205
|
+
> **Follow us**: Stay updated with the latest news and features by watching the repository.
|
206
|
+
|
207
|
+
---
|
208
|
+
|
@@ -0,0 +1,169 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
<div style="text-align: center; background-color: white; padding: 20px; border-radius: 30px;">
|
4
|
+
<img src="./static/cityagent_logo.png" alt="CityAgent Logo" width="200" style="display: block; margin: 0 auto;">
|
5
|
+
<h1 style="color: black; margin: 0; font-size: 3em;">CityAgent: LLM Agents in City</h1>
|
6
|
+
</div>
|
7
|
+
|
8
|
+
|
9
|
+
# 🚀 CityAgent
|
10
|
+
  
|
11
|
+
[](https://docs.fiblab.net/pycityagent)  
|
12
|
+
|
13
|
+
|
14
|
+
CityAgent is an advanced framework specifically designed for building intelligent agents in urban simulation environments. With CityAgent, you can easily create and manage agents, enabling complex urban scenarios to be modeled and simulated efficiently.
|
15
|
+
|
16
|
+
## 🌟 Features
|
17
|
+
- **Modular Design**: Plug-and-play components for agent behavior.
|
18
|
+
- **Urban Environment Simulation**: Built to simulate diverse urban scenarios.
|
19
|
+
- **LLM Integration**: Connects to language models for enriched agent behavior.
|
20
|
+
- **Flexible Configuration**: YAML-based configuration for easy customization.
|
21
|
+
|
22
|
+
## 📑 Table of Contents
|
23
|
+
|
24
|
+
1. [News](#news)
|
25
|
+
2. [Framework](#framework)
|
26
|
+
3. [Setup](#setup)
|
27
|
+
4. [QuickStart](#quickstart)
|
28
|
+
5. [Contributing](#contributing)
|
29
|
+
6. [License](#license)
|
30
|
+
|
31
|
+
<a id="news"></a>
|
32
|
+
## 📰 News
|
33
|
+
|
34
|
+
- 📢 **11.10** - Initial update is now live!
|
35
|
+
- 📢 **2.x version is not compatible* with 1.x version**
|
36
|
+
|
37
|
+
Stay tuned for upcoming updates!
|
38
|
+
|
39
|
+
<a id="framework"></a>
|
40
|
+
## 🛠️ Framework
|
41
|
+
|
42
|
+
CityAgent is built with a multi-layered architecture that allows users to create and manage intelligent agents for urban environments in a scalable and flexible manner. The framework is divided into several key layers, each responsible for different functionalities as depicted in the diagram below:
|
43
|
+
|
44
|
+
<img src="./static/framework.png" alt="CityAgent Framework Overview" width="600" style="display: block; margin: 20px auto;">
|
45
|
+
|
46
|
+
### Architectural Layers
|
47
|
+
- **Model Layer**: Handles agent configuration, task definitions, logging setup, and result aggregation.
|
48
|
+
- **Task Configuration**: Defines agent behaviors and objectives.
|
49
|
+
- **Unified Execution**: Centralized entry point for agent processes.
|
50
|
+
|
51
|
+
- **Agent Layer**: Implements multi-head workflows to manage agent actions.
|
52
|
+
- **Memory**: Stores agent-related information, such as location and motion.
|
53
|
+
- **Static Profiles**: Maintains unchanging agent attributes.
|
54
|
+
- **Custom Data Pool**: Functions as a working memory.
|
55
|
+
- **Multi-Head Workflow**: Supports both normal and event-driven modes.
|
56
|
+
- **Reason Block**: Utilizes LLMs to determine decisions based on context and tools.
|
57
|
+
- **Route Block**: Chooses the best path based on defined criteria using LLMs or rules.
|
58
|
+
- **Action Block**: Executes actions as per defined contexts and tools.
|
59
|
+
|
60
|
+
- **Message Layer**: Facilitates agent communication through peer-to-peer (P2P), peer-to-group (P2G), and group chats.
|
61
|
+
|
62
|
+
- **Environment Layer**: Manages interaction with the urban environment.
|
63
|
+
- **Environment Sensing**: Reads data from the environment.
|
64
|
+
- **Interaction Handling**: Writes or modifies environmental states.
|
65
|
+
- **Message Management**: Handles incoming and outgoing agent messages.
|
66
|
+
|
67
|
+
- **LLM Layer**: Provides configuration and integration for using LLMs in the agent's workflow.
|
68
|
+
- **Prompting & Execution**: Supports model invocation and monitoring.
|
69
|
+
- **Model Support**: Compatible with various LLMs, such as OpenAI, Qwen, Deepseek, etc.
|
70
|
+
|
71
|
+
- **Tool Layer**: Provides additional utilities to the agents.
|
72
|
+
- **String Processing**: Handles parsing and formatting.
|
73
|
+
- **Result Analysis**: Parses responses in formats like JSON or dictionaries.
|
74
|
+
- **Data Storage & Retrieval**: Includes ranking and search tools.
|
75
|
+
|
76
|
+
<a id="setup"></a>
|
77
|
+
## ⚙️ Setup
|
78
|
+
|
79
|
+
You can set up CityAgent in two different ways:
|
80
|
+
|
81
|
+
### 1. From Scratch
|
82
|
+
|
83
|
+
Follow these steps to set up CityAgent from scratch by cloning the repository. The project is built using Python and managed with Poetry.
|
84
|
+
|
85
|
+
1. **Clone the Repository**
|
86
|
+
```bash
|
87
|
+
git clone [This Repository]
|
88
|
+
```
|
89
|
+
2. **Navigate to the Project Directory**
|
90
|
+
```bash
|
91
|
+
cd pycityagent
|
92
|
+
```
|
93
|
+
3. **Install Poetry** (if not installed)
|
94
|
+
```bash
|
95
|
+
curl -sSL https://install.python-poetry.org | python3 -
|
96
|
+
```
|
97
|
+
4. **Install Dependencies**
|
98
|
+
```bash
|
99
|
+
poetry install
|
100
|
+
```
|
101
|
+
5. **Activate the Virtual Environment**
|
102
|
+
```bash
|
103
|
+
poetry shell
|
104
|
+
```
|
105
|
+
|
106
|
+
### 2. Install via pip
|
107
|
+
|
108
|
+
This method is not yet available. Stay tuned for future updates!
|
109
|
+
|
110
|
+
<a id="quickstart"></a>
|
111
|
+
## 🚀 QuickStart
|
112
|
+
|
113
|
+
Get started with CityAgent in just a few minutes!
|
114
|
+
|
115
|
+
### 1. Config Configuration
|
116
|
+
CityAgent uses a configuration file written in `.yaml` format to manage settings for various components. Below is a sample configuration file (`config_template.yaml`) that showcases the structure:
|
117
|
+
|
118
|
+
```yaml
|
119
|
+
llm_request:
|
120
|
+
text_request:
|
121
|
+
request_type: openai
|
122
|
+
api_key: <YOUR_API_KEY>
|
123
|
+
model: gpt-4o
|
124
|
+
img_understand_request:
|
125
|
+
request_type: none
|
126
|
+
api_key: none
|
127
|
+
model: none
|
128
|
+
img_generate_request:
|
129
|
+
request_type: none
|
130
|
+
api_key: none
|
131
|
+
model: none
|
132
|
+
|
133
|
+
simulator_request:
|
134
|
+
simulator:
|
135
|
+
server: https://api-opencity-2x.fiblab.net:58081
|
136
|
+
map_request:
|
137
|
+
mongo_uri: <MONGO_URI>
|
138
|
+
mongo_db: llmsim
|
139
|
+
mongo_coll: map_beijing5ring_withpoi_0424
|
140
|
+
cache_dir: ./cache
|
141
|
+
route_request:
|
142
|
+
server: http://api-opencity-2x.fiblab.net:58082
|
143
|
+
streetview_request:
|
144
|
+
engine: baidumap / googlemap
|
145
|
+
mapAK: baidumap api-key (if you use baidumap engine)
|
146
|
+
proxy: googlemap proxy (if you use googlemap engine)
|
147
|
+
```
|
148
|
+
|
149
|
+
### 2. Example Usage
|
150
|
+
To get started quickly, please refer to the `examples` folder in the repository. It contains sample scripts and configurations to help you understand how to create and use agents in an urban simulation environment.
|
151
|
+
|
152
|
+
<a id="contributing"></a>
|
153
|
+
## 🤝 Contributing
|
154
|
+
We welcome contributions from the community!.
|
155
|
+
|
156
|
+
<a id="license"></a>
|
157
|
+
## 📄 License
|
158
|
+
|
159
|
+
CityAgent is licensed under the MIT License. See the [LICENSE](LICENSE) file for more details.
|
160
|
+
|
161
|
+
---
|
162
|
+
|
163
|
+
Feel free to reach out if you have any questions, suggestions, or want to collaborate!
|
164
|
+
|
165
|
+
---
|
166
|
+
|
167
|
+
> **Follow us**: Stay updated with the latest news and features by watching the repository.
|
168
|
+
|
169
|
+
---
|
@@ -0,0 +1,214 @@
|
|
1
|
+
"""智能体模板类及其定义"""
|
2
|
+
|
3
|
+
from abc import ABC, abstractmethod
|
4
|
+
from datetime import datetime
|
5
|
+
from enum import Enum
|
6
|
+
from typing import Dict, List, Optional
|
7
|
+
|
8
|
+
from .economy import EconomyClient
|
9
|
+
from .environment import Simulator
|
10
|
+
from .llm import LLM
|
11
|
+
from .memory import Memory
|
12
|
+
|
13
|
+
|
14
|
+
class AgentType(Enum):
|
15
|
+
"""
|
16
|
+
Agent类型
|
17
|
+
|
18
|
+
- Citizen, Citizen type agent
|
19
|
+
- Institution, Orgnization or institution type agent
|
20
|
+
"""
|
21
|
+
|
22
|
+
Unspecified = "Unspecified"
|
23
|
+
Citizen = "Citizen"
|
24
|
+
Institution = "Institution"
|
25
|
+
|
26
|
+
|
27
|
+
class Agent(ABC):
|
28
|
+
"""
|
29
|
+
Agent base class
|
30
|
+
"""
|
31
|
+
|
32
|
+
def __init__(
|
33
|
+
self,
|
34
|
+
name: str,
|
35
|
+
type: AgentType = AgentType.Unspecified,
|
36
|
+
llm_client: Optional[LLM] = None,
|
37
|
+
economy_client: Optional[EconomyClient] = None,
|
38
|
+
simulator: Optional[Simulator] = None,
|
39
|
+
memory: Optional[Memory] = None,
|
40
|
+
) -> None:
|
41
|
+
"""
|
42
|
+
Initialize the Agent.
|
43
|
+
|
44
|
+
Args:
|
45
|
+
name (str): The name of the agent.
|
46
|
+
type (AgentType): The type of the agent. Defaults to `AgentType.Unspecified`
|
47
|
+
llm_client (LLM): The language model client. Defaults to None.
|
48
|
+
economy_client (EconomyClient): The `EconomySim` client. Defaults to None.
|
49
|
+
simulator (Simulator, optional): The simulator object. Defaults to None.
|
50
|
+
memory (Memory, optional): The memory of the agent. Defaults to None.
|
51
|
+
"""
|
52
|
+
self._name = name
|
53
|
+
self._type = type
|
54
|
+
self._llm_client = llm_client
|
55
|
+
self._economy_client = economy_client
|
56
|
+
self._simulator = simulator
|
57
|
+
self._memory = memory
|
58
|
+
self._has_bound_to_simulator = False
|
59
|
+
self._interview_history: List[Dict] = [] # 存储采访历史
|
60
|
+
|
61
|
+
def set_memory(self, memory: Memory):
|
62
|
+
"""
|
63
|
+
Set the memory of the agent.
|
64
|
+
"""
|
65
|
+
self._memory = memory
|
66
|
+
|
67
|
+
def set_simulator(self, simulator: Simulator):
|
68
|
+
"""
|
69
|
+
Set the simulator of the agent.
|
70
|
+
"""
|
71
|
+
self._simulator = simulator
|
72
|
+
|
73
|
+
def set_economy_client(self, economy_client: EconomyClient):
|
74
|
+
"""
|
75
|
+
Set the economy_client of the agent.
|
76
|
+
"""
|
77
|
+
self._economy_client = economy_client
|
78
|
+
|
79
|
+
@property
|
80
|
+
def LLM(self):
|
81
|
+
"""The Agent's LLM"""
|
82
|
+
if self._llm_client is None:
|
83
|
+
raise RuntimeError(
|
84
|
+
f"LLM access before assignment, please `set_llm_client` first!"
|
85
|
+
)
|
86
|
+
return self._llm_client
|
87
|
+
|
88
|
+
@property
|
89
|
+
def economy_client(self):
|
90
|
+
"""The Agent's EconomyClient"""
|
91
|
+
if self._economy_client is None:
|
92
|
+
raise RuntimeError(
|
93
|
+
f"EconomyClient access before assignment, please `set_economy_client` first!"
|
94
|
+
)
|
95
|
+
return self._economy_client
|
96
|
+
|
97
|
+
@property
|
98
|
+
def memory(self):
|
99
|
+
"""The Agent's Memory"""
|
100
|
+
if self._memory is None:
|
101
|
+
raise RuntimeError(
|
102
|
+
f"Memory access before assignment, please `set_memory` first!"
|
103
|
+
)
|
104
|
+
return self._memory
|
105
|
+
|
106
|
+
@property
|
107
|
+
def simulator(self):
|
108
|
+
"""The Simulator"""
|
109
|
+
if self._simulator is None:
|
110
|
+
raise RuntimeError(
|
111
|
+
f"Simulator access before assignment, please `set_simulator` first!"
|
112
|
+
)
|
113
|
+
return self._simulator
|
114
|
+
|
115
|
+
async def generate_response(self, question: str) -> str:
|
116
|
+
"""生成回答
|
117
|
+
|
118
|
+
基于智能体的记忆和当前状态,生成对问题的回答。
|
119
|
+
|
120
|
+
Args:
|
121
|
+
question: 需要回答的问题
|
122
|
+
|
123
|
+
Returns:
|
124
|
+
str: 智能体的回答
|
125
|
+
"""
|
126
|
+
dialog = []
|
127
|
+
|
128
|
+
# 添加系统提示
|
129
|
+
system_prompt = f"你是一个名叫{self._name}的{self._type.value}。请以第一人称的方式回答问题,保持回答简洁明了。"
|
130
|
+
dialog.append({"role": "system", "content": system_prompt})
|
131
|
+
|
132
|
+
# 添加记忆上下文
|
133
|
+
if self._memory:
|
134
|
+
relevant_memories = await self._memory.search(question)
|
135
|
+
if relevant_memories:
|
136
|
+
dialog.append(
|
137
|
+
{
|
138
|
+
"role": "system",
|
139
|
+
"content": f"基于以下记忆回答问题:\n{relevant_memories}",
|
140
|
+
}
|
141
|
+
)
|
142
|
+
|
143
|
+
# 添加用户问题
|
144
|
+
dialog.append({"role": "user", "content": question})
|
145
|
+
|
146
|
+
# 使用LLM生成回答
|
147
|
+
if not self._llm_client:
|
148
|
+
return "抱歉,我现在无法回答问题。"
|
149
|
+
|
150
|
+
response = await self._llm_client.atext_request(dialog) # type:ignore
|
151
|
+
|
152
|
+
# 记录采访历史
|
153
|
+
self._interview_history.append(
|
154
|
+
{
|
155
|
+
"timestamp": datetime.now().isoformat(),
|
156
|
+
"question": question,
|
157
|
+
"response": response,
|
158
|
+
}
|
159
|
+
)
|
160
|
+
|
161
|
+
return response # type:ignore
|
162
|
+
|
163
|
+
def get_interview_history(self) -> List[Dict]:
|
164
|
+
"""获取采访历史记录"""
|
165
|
+
return self._interview_history
|
166
|
+
|
167
|
+
@abstractmethod
|
168
|
+
async def forward(self) -> None:
|
169
|
+
"""智能体行为逻辑"""
|
170
|
+
raise NotImplementedError
|
171
|
+
|
172
|
+
|
173
|
+
class CitizenAgent(Agent):
|
174
|
+
"""
|
175
|
+
CitizenAgent: 城市居民智能体类及其定义
|
176
|
+
"""
|
177
|
+
|
178
|
+
def __init__(
|
179
|
+
self,
|
180
|
+
name: str,
|
181
|
+
llm_client: Optional[LLM] = None,
|
182
|
+
simulator: Optional[Simulator] = None,
|
183
|
+
memory: Optional[Memory] = None,
|
184
|
+
) -> None:
|
185
|
+
super().__init__(
|
186
|
+
name,
|
187
|
+
AgentType.Citizen,
|
188
|
+
llm_client,
|
189
|
+
None,
|
190
|
+
simulator,
|
191
|
+
memory,
|
192
|
+
)
|
193
|
+
|
194
|
+
|
195
|
+
class InstitutionAgent(Agent):
|
196
|
+
"""
|
197
|
+
InstitutionAgent: 机构智能体类及其定义
|
198
|
+
"""
|
199
|
+
|
200
|
+
def __init__(
|
201
|
+
self,
|
202
|
+
name: str,
|
203
|
+
llm_client: Optional[LLM] = None,
|
204
|
+
simulator: Optional[Simulator] = None,
|
205
|
+
memory: Optional[Memory] = None,
|
206
|
+
) -> None:
|
207
|
+
super().__init__(
|
208
|
+
name,
|
209
|
+
AgentType.Institution,
|
210
|
+
llm_client,
|
211
|
+
None,
|
212
|
+
simulator,
|
213
|
+
memory,
|
214
|
+
)
|