pycityagent 2.0.0a66__cp310-cp310-macosx_11_0_arm64.whl → 2.0.0a67__cp310-cp310-macosx_11_0_arm64.whl
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/agent/agent.py +157 -57
- pycityagent/agent/agent_base.py +316 -43
- pycityagent/cityagent/bankagent.py +49 -9
- pycityagent/cityagent/blocks/__init__.py +1 -2
- pycityagent/cityagent/blocks/cognition_block.py +54 -31
- pycityagent/cityagent/blocks/dispatcher.py +22 -17
- pycityagent/cityagent/blocks/economy_block.py +46 -32
- pycityagent/cityagent/blocks/mobility_block.py +130 -100
- pycityagent/cityagent/blocks/needs_block.py +101 -44
- pycityagent/cityagent/blocks/other_block.py +42 -33
- pycityagent/cityagent/blocks/plan_block.py +59 -42
- pycityagent/cityagent/blocks/social_block.py +167 -116
- pycityagent/cityagent/blocks/utils.py +13 -6
- pycityagent/cityagent/firmagent.py +17 -35
- pycityagent/cityagent/governmentagent.py +3 -3
- pycityagent/cityagent/initial.py +79 -44
- pycityagent/cityagent/memory_config.py +108 -88
- pycityagent/cityagent/message_intercept.py +0 -4
- pycityagent/cityagent/metrics.py +41 -0
- pycityagent/cityagent/nbsagent.py +24 -36
- pycityagent/cityagent/societyagent.py +7 -3
- pycityagent/cli/wrapper.py +2 -2
- pycityagent/economy/econ_client.py +407 -81
- pycityagent/environment/__init__.py +0 -3
- pycityagent/environment/sim/__init__.py +0 -3
- pycityagent/environment/sim/aoi_service.py +2 -2
- pycityagent/environment/sim/client.py +3 -31
- pycityagent/environment/sim/clock_service.py +2 -2
- pycityagent/environment/sim/lane_service.py +8 -8
- pycityagent/environment/sim/light_service.py +8 -8
- pycityagent/environment/sim/pause_service.py +9 -10
- pycityagent/environment/sim/person_service.py +20 -20
- pycityagent/environment/sim/road_service.py +2 -2
- pycityagent/environment/sim/sim_env.py +21 -5
- pycityagent/environment/sim/social_service.py +4 -4
- pycityagent/environment/simulator.py +249 -27
- pycityagent/environment/utils/__init__.py +2 -2
- pycityagent/environment/utils/geojson.py +2 -2
- pycityagent/environment/utils/grpc.py +4 -4
- pycityagent/environment/utils/map_utils.py +2 -2
- pycityagent/llm/embeddings.py +147 -28
- pycityagent/llm/llm.py +122 -77
- pycityagent/llm/llmconfig.py +5 -0
- pycityagent/llm/utils.py +4 -0
- pycityagent/memory/__init__.py +0 -4
- pycityagent/memory/const.py +2 -2
- pycityagent/memory/faiss_query.py +140 -61
- pycityagent/memory/memory.py +393 -90
- pycityagent/memory/memory_base.py +140 -34
- pycityagent/memory/profile.py +13 -13
- pycityagent/memory/self_define.py +13 -13
- pycityagent/memory/state.py +14 -14
- pycityagent/message/message_interceptor.py +253 -3
- pycityagent/message/messager.py +133 -6
- pycityagent/metrics/mlflow_client.py +47 -4
- pycityagent/pycityagent-sim +0 -0
- pycityagent/pycityagent-ui +0 -0
- pycityagent/simulation/__init__.py +3 -2
- pycityagent/simulation/agentgroup.py +145 -52
- pycityagent/simulation/simulation.py +257 -62
- pycityagent/survey/manager.py +45 -3
- pycityagent/survey/models.py +42 -2
- pycityagent/tools/__init__.py +1 -2
- pycityagent/tools/tool.py +93 -69
- pycityagent/utils/avro_schema.py +2 -2
- pycityagent/utils/parsers/code_block_parser.py +1 -1
- pycityagent/utils/parsers/json_parser.py +2 -2
- pycityagent/utils/parsers/parser_base.py +2 -2
- pycityagent/workflow/block.py +64 -13
- pycityagent/workflow/prompt.py +31 -23
- pycityagent/workflow/trigger.py +91 -24
- {pycityagent-2.0.0a66.dist-info → pycityagent-2.0.0a67.dist-info}/METADATA +2 -2
- pycityagent-2.0.0a67.dist-info/RECORD +97 -0
- pycityagent/environment/interact/__init__.py +0 -0
- pycityagent/environment/interact/interact.py +0 -198
- pycityagent/environment/message/__init__.py +0 -0
- pycityagent/environment/sence/__init__.py +0 -0
- pycityagent/environment/sence/static.py +0 -416
- pycityagent/environment/sidecar/__init__.py +0 -8
- pycityagent/environment/sidecar/sidecarv2.py +0 -109
- pycityagent/environment/sim/economy_services.py +0 -192
- pycityagent/metrics/utils/const.py +0 -0
- pycityagent-2.0.0a66.dist-info/RECORD +0 -105
- {pycityagent-2.0.0a66.dist-info → pycityagent-2.0.0a67.dist-info}/LICENSE +0 -0
- {pycityagent-2.0.0a66.dist-info → pycityagent-2.0.0a67.dist-info}/WHEEL +0 -0
- {pycityagent-2.0.0a66.dist-info → pycityagent-2.0.0a67.dist-info}/entry_points.txt +0 -0
- {pycityagent-2.0.0a66.dist-info → pycityagent-2.0.0a67.dist-info}/top_level.txt +0 -0
@@ -14,12 +14,31 @@ from ..utils.decorators import lock_decorator
|
|
14
14
|
|
15
15
|
|
16
16
|
class FaissQuery:
|
17
|
+
"""
|
18
|
+
A class for handling similarity searches and document management using FAISS.
|
19
|
+
|
20
|
+
- **Description**:
|
21
|
+
- This class provides functionalities to manage embeddings and perform similarity searches over a set of documents.
|
22
|
+
- It allows adding, deleting, and querying documents based on their semantic content through embeddings.
|
23
|
+
- The class initializes with an optional embedding model and index type, setting up the environment for vector operations.
|
24
|
+
- If no embedding model is provided during initialization, certain methods will not be available until one is set.
|
25
|
+
"""
|
26
|
+
|
17
27
|
def __init__(
|
18
28
|
self,
|
19
29
|
embeddings: Optional[Embeddings] = None,
|
20
30
|
index_type: Any = faiss.IndexFlatL2,
|
21
31
|
dimension: Optional[int] = None,
|
22
32
|
) -> None:
|
33
|
+
"""
|
34
|
+
Initialize the FaissQuery instance.
|
35
|
+
|
36
|
+
- **Parameters**:
|
37
|
+
- `embeddings` (Optional[Embeddings], optional): An embedding function that converts text into vectors. Defaults to None.
|
38
|
+
- `index_type` (Any, optional): The type of FAISS index to use for vector storage and retrieval. Defaults to faiss.IndexFlatL2.
|
39
|
+
- `dimension` (Optional[int], optional): The dimensionality of the vectors produced by the embedding function. Defaults to None.
|
40
|
+
If not specified, it's inferred from the embedding of a sample text ("hello world").
|
41
|
+
"""
|
23
42
|
self._embeddings = embeddings
|
24
43
|
self._lock = asyncio.Lock()
|
25
44
|
if embeddings is None:
|
@@ -40,6 +59,16 @@ class FaissQuery:
|
|
40
59
|
def embeddings(
|
41
60
|
self,
|
42
61
|
) -> Embeddings:
|
62
|
+
"""
|
63
|
+
Access the current embedding model.
|
64
|
+
|
65
|
+
- **Description**:
|
66
|
+
- Getter for the embedding model used to convert text into vector representations.
|
67
|
+
- If no embedding model has been set, a RuntimeError is raised prompting the user to set one first.
|
68
|
+
|
69
|
+
- **Returns**:
|
70
|
+
- `Embeddings`: The current embedding model.
|
71
|
+
"""
|
43
72
|
if self._embeddings is None:
|
44
73
|
raise RuntimeError(f"No embedding set, please `set_embeddings` first!")
|
45
74
|
return self._embeddings
|
@@ -48,6 +77,16 @@ class FaissQuery:
|
|
48
77
|
def vectors_store(
|
49
78
|
self,
|
50
79
|
) -> FAISS:
|
80
|
+
"""
|
81
|
+
Access the current vector store.
|
82
|
+
|
83
|
+
- **Description**:
|
84
|
+
- Getter for the FAISS vector store which holds the indexed documents and allows for similarity searches.
|
85
|
+
- If the vector store hasn't been initialized due to a missing embedding model, a RuntimeError is raised.
|
86
|
+
|
87
|
+
- **Returns**:
|
88
|
+
- `FAISS`: The current vector store instance.
|
89
|
+
"""
|
51
90
|
if self._vectors_store is None:
|
52
91
|
raise RuntimeError(f"No embedding set, thus no vector stores initialized!")
|
53
92
|
return self._vectors_store
|
@@ -59,6 +98,21 @@ class FaissQuery:
|
|
59
98
|
documents: Union[str, Sequence[str]],
|
60
99
|
extra_tags: Optional[dict] = None,
|
61
100
|
) -> list[str]:
|
101
|
+
"""
|
102
|
+
Add documents to the vector store with metadata.
|
103
|
+
|
104
|
+
- **Description**:
|
105
|
+
- Asynchronously adds one or more documents to the vector store, associating them with an agent ID and optional extra tags.
|
106
|
+
- Each document is converted into a vector using the embedding model before being added to the index.
|
107
|
+
|
108
|
+
- **Args**:
|
109
|
+
- `agent_id` (int): Identifier of the agent to associate with the documents.
|
110
|
+
- `documents` (Union[str, Sequence[str]]): A single document string or a sequence of document strings to add.
|
111
|
+
- `extra_tags` (Optional[dict], optional): Additional metadata tags to associate with the documents. Defaults to None.
|
112
|
+
|
113
|
+
- **Returns**:
|
114
|
+
- `list[str]`: List of document IDs that were added to the vector store.
|
115
|
+
"""
|
62
116
|
if isinstance(documents, str):
|
63
117
|
documents = [documents]
|
64
118
|
_metadata = {"_id": agent_id}
|
@@ -76,6 +130,15 @@ class FaissQuery:
|
|
76
130
|
self,
|
77
131
|
to_delete_ids: list[str],
|
78
132
|
):
|
133
|
+
"""
|
134
|
+
Delete documents from the vector store by IDs.
|
135
|
+
|
136
|
+
- **Description**:
|
137
|
+
- Asynchronously deletes documents from the vector store based on provided document IDs.
|
138
|
+
|
139
|
+
- **Args**:
|
140
|
+
- `to_delete_ids` (list[str]): List of document IDs to delete from the vector store.
|
141
|
+
"""
|
79
142
|
await self.vectors_store.adelete(
|
80
143
|
ids=to_delete_ids,
|
81
144
|
)
|
@@ -93,24 +156,26 @@ class FaissQuery:
|
|
93
156
|
filter: Optional[dict] = None,
|
94
157
|
) -> Union[list[tuple[str, dict]], list[tuple[str, float, dict]]]:
|
95
158
|
"""
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
return_score_type (Union[Literal["none"], Literal["similarity_score"], Literal["L2-distance"]], optional):
|
104
|
-
Specifies whether and how to return similarity scores with the results:
|
105
|
-
- "none": Do not return scores; only return the contents (default).
|
106
|
-
- "similarity_score": Return a tuple of content and its similarity score.
|
107
|
-
- "L2-distance": Return a tuple of content and its L2 distance from the query.
|
108
|
-
filter (dict, optional): The filter dict for metadata.
|
159
|
+
Perform a similarity search for documents related to the given query.
|
160
|
+
|
161
|
+
- **Description**:
|
162
|
+
- Conducts an asynchronous search for the top-k documents most similar to the query text.
|
163
|
+
- The search can be customized by specifying how many documents to fetch (`fetch_k`), how many to return (`k`),
|
164
|
+
and whether to include scores in the results (`return_score_type`).
|
165
|
+
- Filters can be applied to narrow down the search results based on metadata.
|
109
166
|
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
167
|
+
- **Args**:
|
168
|
+
- `query` (str): The text to look up documents similar to.
|
169
|
+
- `agent_id` (int): The identifier of the agent to filter specific documents.
|
170
|
+
- `k` (int, optional): The number of top similar contents to return. Defaults to 4.
|
171
|
+
- `fetch_k` (int, optional): The number of documents to fetch before applying any filters. Defaults to 20.
|
172
|
+
- `return_score_type` (Union[Literal["none"], Literal["similarity_score"], Literal["L2-distance"]], optional):
|
173
|
+
Specifies whether and how to return similarity scores with the results.
|
174
|
+
- `filter` (Optional[dict], optional): The filter dict for metadata.
|
175
|
+
|
176
|
+
- **Returns**:
|
177
|
+
- `Union[list[tuple[str, dict]], list[tuple[str, float, dict]]]`: Depending on the `return_score_type` parameter,
|
178
|
+
returns either a list of tuples containing the content and its associated metadata, or also including a floating-point score.
|
114
179
|
"""
|
115
180
|
_filter = {
|
116
181
|
"_id": agent_id,
|
@@ -136,11 +201,13 @@ class FaissQuery:
|
|
136
201
|
)
|
137
202
|
return [(r.page_content, r.metadata) for r in _result]
|
138
203
|
elif return_score_type == "similarity_score":
|
139
|
-
_result =
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
204
|
+
_result = (
|
205
|
+
await self.vectors_store.asimilarity_search_with_relevance_scores(
|
206
|
+
query=query,
|
207
|
+
k=k,
|
208
|
+
filter=_filter,
|
209
|
+
fetch_k=fetch_k,
|
210
|
+
)
|
144
211
|
)
|
145
212
|
return [(r.page_content, s, r.metadata) for r, s in _result]
|
146
213
|
else:
|
@@ -157,23 +224,28 @@ class FaissQuery:
|
|
157
224
|
filter: Optional[dict] = None,
|
158
225
|
) -> Union[list[tuple[str, dict]], list[tuple[str, float, dict]]]:
|
159
226
|
"""
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
227
|
+
Perform a similarity search for documents related to the given vector.
|
228
|
+
|
229
|
+
- **Description**:
|
230
|
+
- Conducts an asynchronous search for the top-k documents most similar to the provided embedding vector.
|
231
|
+
- The search can be customized by specifying how many documents to fetch (`fetch_k`), how many to return (`k`),
|
232
|
+
and whether to include L2 distances in the results (`return_score_type`).
|
233
|
+
- Filters can be applied to narrow down the search results based on metadata.
|
234
|
+
|
235
|
+
- **Args**:
|
236
|
+
- `embedding` (list[float]): The vector to look up documents similar to.
|
237
|
+
- `agent_id` (int): The identifier of the agent to filter specific documents.
|
238
|
+
- `k` (int, optional): The number of top similar contents to return. Defaults to 4.
|
239
|
+
- `fetch_k` (int, optional): The number of documents to fetch before applying any filters. Defaults to 20.
|
240
|
+
- `return_score_type` (Union[Literal["none"], Literal["L2-distance"]], optional):
|
168
241
|
Specifies whether and how to return similarity scores with the results:
|
169
242
|
- "none": Do not return scores; only return the contents (default).
|
170
243
|
- "L2-distance": Return a tuple of content and its L2 distance from the query.
|
171
|
-
filter (dict, optional): The filter dict for metadata.
|
244
|
+
- `filter` (Optional[dict], optional): The filter dict for metadata.
|
172
245
|
|
173
|
-
Returns
|
174
|
-
Union[list[tuple[str,dict]], list[tuple[str, float,dict]]]
|
175
|
-
|
176
|
-
or a list of tuples where each tuple contains a string and a floating-point score.
|
246
|
+
- **Returns**:
|
247
|
+
- `Union[list[tuple[str, dict]], list[tuple[str, float, dict]]]`: Depending on the `return_score_type` parameter,
|
248
|
+
returns either a list of tuples containing the content and its associated metadata, or also including a floating-point score.
|
177
249
|
"""
|
178
250
|
_filter = {
|
179
251
|
"_id": agent_id,
|
@@ -211,21 +283,25 @@ class FaissQuery:
|
|
211
283
|
filter: Optional[dict] = None,
|
212
284
|
) -> list[tuple[str, dict]]:
|
213
285
|
"""
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
286
|
+
Select contents using maximal marginal relevance asynchronously.
|
287
|
+
|
288
|
+
- **Description**:
|
289
|
+
- Asynchronously selects a set of documents that are relevant to the query while ensuring diversity among the results.
|
290
|
+
- The selection process balances between relevance to the query and diversity, controlled by `lambda_mult`.
|
291
|
+
|
292
|
+
- **Args**:
|
293
|
+
- `query` (str): The text to look up documents similar to.
|
294
|
+
- `agent_id` (int): The identifier of the agent to filter specific documents.
|
295
|
+
- `k` (int, optional): The number of top similar contents to return. Defaults to 4.
|
296
|
+
- `fetch_k` (int, optional): The number of documents to fetch before applying any filters. Defaults to 20.
|
297
|
+
- `lambda_mult` (float, optional): Number between 0 and 1 that determines the degree of diversity among the results. Defaults to 0.5.
|
298
|
+
- `return_score_type` (Literal["none"], optional):
|
223
299
|
Specifies whether and how to return similarity scores with the results:
|
224
300
|
- "none": Do not return scores; only return the contents (default).
|
225
|
-
filter (dict, optional): The filter dict for metadata.
|
301
|
+
- `filter` (Optional[dict], optional): The filter dict for metadata.
|
226
302
|
|
227
|
-
Returns
|
228
|
-
list[tuple[str,dict]]
|
303
|
+
- **Returns**:
|
304
|
+
- `list[tuple[str, dict]]`: A list of tuples containing the content and its associated metadata.
|
229
305
|
"""
|
230
306
|
_filter = {
|
231
307
|
"_id": agent_id,
|
@@ -257,24 +333,27 @@ class FaissQuery:
|
|
257
333
|
filter: Optional[dict] = None,
|
258
334
|
) -> Union[list[tuple[str, dict]], list[tuple[str, float, dict]]]:
|
259
335
|
"""
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
336
|
+
Select contents using maximal marginal relevance asynchronously based on embedding.
|
337
|
+
|
338
|
+
- **Description**:
|
339
|
+
- Asynchronously selects a set of documents that are relevant to the provided embedding vector while ensuring diversity among the results.
|
340
|
+
- The selection process balances between relevance to the embedding and diversity, controlled by `lambda_mult`.
|
341
|
+
|
342
|
+
- **Args**:
|
343
|
+
- `embedding` (list[float]): The vector to look up documents similar to.
|
344
|
+
- `agent_id` (int): The identifier of the agent to filter specific documents.
|
345
|
+
- `k` (int, optional): The number of top similar contents to return. Defaults to 4.
|
346
|
+
- `fetch_k` (int, optional): The number of documents to fetch before applying any filters. Defaults to 20.
|
347
|
+
- `lambda_mult` (float, optional): Number between 0 and 1 that determines the degree of diversity among the results. Defaults to 0.5.
|
348
|
+
- `return_score_type` (Union[Literal["none"], Literal["similarity_score"]], optional):
|
269
349
|
Specifies whether and how to return similarity scores with the results:
|
270
350
|
- "none": Do not return scores; only return the contents (default).
|
271
351
|
- "similarity_score": Return a tuple of content and its similarity score.
|
272
|
-
filter (dict, optional): The filter dict for metadata.
|
352
|
+
- `filter` (Optional[dict], optional): The filter dict for metadata.
|
273
353
|
|
274
|
-
Returns
|
275
|
-
Union[list[tuple[str,dict]], list[tuple[str, float,dict]]]
|
276
|
-
|
277
|
-
or a list of tuples where each tuple contains a string and a floating-point score.
|
354
|
+
- **Returns**:
|
355
|
+
- `Union[list[tuple[str, dict]], list[tuple[str, float, dict]]]`: Depending on the `return_score_type` parameter,
|
356
|
+
returns either a list of tuples containing the content and its associated metadata, or also including a floating-point score.
|
278
357
|
"""
|
279
358
|
|
280
359
|
_filter = {
|