pycityagent 2.0.0a65__cp311-cp311-macosx_11_0_arm64.whl → 2.0.0a67__cp311-cp311-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.
Files changed (87) hide show
  1. pycityagent/agent/agent.py +157 -57
  2. pycityagent/agent/agent_base.py +316 -43
  3. pycityagent/cityagent/bankagent.py +49 -9
  4. pycityagent/cityagent/blocks/__init__.py +1 -2
  5. pycityagent/cityagent/blocks/cognition_block.py +54 -31
  6. pycityagent/cityagent/blocks/dispatcher.py +22 -17
  7. pycityagent/cityagent/blocks/economy_block.py +46 -32
  8. pycityagent/cityagent/blocks/mobility_block.py +209 -105
  9. pycityagent/cityagent/blocks/needs_block.py +101 -54
  10. pycityagent/cityagent/blocks/other_block.py +42 -33
  11. pycityagent/cityagent/blocks/plan_block.py +59 -42
  12. pycityagent/cityagent/blocks/social_block.py +167 -126
  13. pycityagent/cityagent/blocks/utils.py +13 -6
  14. pycityagent/cityagent/firmagent.py +17 -35
  15. pycityagent/cityagent/governmentagent.py +3 -3
  16. pycityagent/cityagent/initial.py +79 -49
  17. pycityagent/cityagent/memory_config.py +123 -94
  18. pycityagent/cityagent/message_intercept.py +0 -4
  19. pycityagent/cityagent/metrics.py +41 -0
  20. pycityagent/cityagent/nbsagent.py +24 -36
  21. pycityagent/cityagent/societyagent.py +9 -4
  22. pycityagent/cli/wrapper.py +2 -2
  23. pycityagent/economy/econ_client.py +407 -81
  24. pycityagent/environment/__init__.py +0 -3
  25. pycityagent/environment/sim/__init__.py +0 -3
  26. pycityagent/environment/sim/aoi_service.py +2 -2
  27. pycityagent/environment/sim/client.py +3 -31
  28. pycityagent/environment/sim/clock_service.py +2 -2
  29. pycityagent/environment/sim/lane_service.py +8 -8
  30. pycityagent/environment/sim/light_service.py +8 -8
  31. pycityagent/environment/sim/pause_service.py +9 -10
  32. pycityagent/environment/sim/person_service.py +20 -20
  33. pycityagent/environment/sim/road_service.py +2 -2
  34. pycityagent/environment/sim/sim_env.py +21 -5
  35. pycityagent/environment/sim/social_service.py +4 -4
  36. pycityagent/environment/simulator.py +249 -27
  37. pycityagent/environment/utils/__init__.py +2 -2
  38. pycityagent/environment/utils/geojson.py +2 -2
  39. pycityagent/environment/utils/grpc.py +4 -4
  40. pycityagent/environment/utils/map_utils.py +2 -2
  41. pycityagent/llm/embeddings.py +147 -28
  42. pycityagent/llm/llm.py +178 -111
  43. pycityagent/llm/llmconfig.py +5 -0
  44. pycityagent/llm/utils.py +4 -0
  45. pycityagent/memory/__init__.py +0 -4
  46. pycityagent/memory/const.py +2 -2
  47. pycityagent/memory/faiss_query.py +140 -61
  48. pycityagent/memory/memory.py +394 -91
  49. pycityagent/memory/memory_base.py +140 -34
  50. pycityagent/memory/profile.py +13 -13
  51. pycityagent/memory/self_define.py +13 -13
  52. pycityagent/memory/state.py +14 -14
  53. pycityagent/message/message_interceptor.py +253 -3
  54. pycityagent/message/messager.py +133 -6
  55. pycityagent/metrics/mlflow_client.py +47 -4
  56. pycityagent/pycityagent-sim +0 -0
  57. pycityagent/pycityagent-ui +0 -0
  58. pycityagent/simulation/__init__.py +3 -2
  59. pycityagent/simulation/agentgroup.py +150 -54
  60. pycityagent/simulation/simulation.py +276 -66
  61. pycityagent/survey/manager.py +45 -3
  62. pycityagent/survey/models.py +42 -2
  63. pycityagent/tools/__init__.py +1 -2
  64. pycityagent/tools/tool.py +93 -69
  65. pycityagent/utils/avro_schema.py +2 -2
  66. pycityagent/utils/parsers/code_block_parser.py +1 -1
  67. pycityagent/utils/parsers/json_parser.py +2 -2
  68. pycityagent/utils/parsers/parser_base.py +2 -2
  69. pycityagent/workflow/block.py +64 -13
  70. pycityagent/workflow/prompt.py +31 -23
  71. pycityagent/workflow/trigger.py +91 -24
  72. {pycityagent-2.0.0a65.dist-info → pycityagent-2.0.0a67.dist-info}/METADATA +2 -2
  73. pycityagent-2.0.0a67.dist-info/RECORD +97 -0
  74. pycityagent/environment/interact/__init__.py +0 -0
  75. pycityagent/environment/interact/interact.py +0 -198
  76. pycityagent/environment/message/__init__.py +0 -0
  77. pycityagent/environment/sence/__init__.py +0 -0
  78. pycityagent/environment/sence/static.py +0 -416
  79. pycityagent/environment/sidecar/__init__.py +0 -8
  80. pycityagent/environment/sidecar/sidecarv2.py +0 -109
  81. pycityagent/environment/sim/economy_services.py +0 -192
  82. pycityagent/metrics/utils/const.py +0 -0
  83. pycityagent-2.0.0a65.dist-info/RECORD +0 -105
  84. {pycityagent-2.0.0a65.dist-info → pycityagent-2.0.0a67.dist-info}/LICENSE +0 -0
  85. {pycityagent-2.0.0a65.dist-info → pycityagent-2.0.0a67.dist-info}/WHEEL +0 -0
  86. {pycityagent-2.0.0a65.dist-info → pycityagent-2.0.0a67.dist-info}/entry_points.txt +0 -0
  87. {pycityagent-2.0.0a65.dist-info → pycityagent-2.0.0a67.dist-info}/top_level.txt +0 -0
pycityagent/llm/utils.py CHANGED
@@ -1,5 +1,9 @@
1
1
  import base64
2
2
 
3
+ __all__ = [
4
+ "encode_image",
5
+ ]
6
+
3
7
 
4
8
  def encode_image(image_path):
5
9
  with open(image_path, "rb") as image_file:
@@ -2,10 +2,6 @@
2
2
 
3
3
  from .faiss_query import FaissQuery
4
4
  from .memory import Memory
5
- from .memory_base import MemoryBase, MemoryUnit
6
- from .profile import ProfileMemory, ProfileMemoryUnit
7
- from .self_define import DynamicMemory
8
- from .state import StateMemory
9
5
 
10
6
  __all__ = [
11
7
  "Memory",
@@ -8,9 +8,9 @@ PROFILE_ATTRIBUTES = {
8
8
  "skill": str(),
9
9
  "occupation": str(),
10
10
  "family_consumption": str(),
11
- "consumption": str(),
11
+ "consumption": float(),
12
12
  "personality": str(),
13
- "income": str(),
13
+ "income": float(),
14
14
  "currency": float(),
15
15
  "residence": str(),
16
16
  "race": str(),
@@ -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
- Return content most similar to the given query.
97
-
98
- Args:
99
- query (str): The text to look up documents similar to.
100
- agent_id (int): The identifier of the agent to filter specific documents. Only documents associated with this agent will be considered.
101
- k (int, optional): The number of top similar contents to return. Defaults to 4.
102
- fetch_k (int, optional): The number of documents to fetch before applying any filters. Defaults to 20.
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
- Returns:
111
- Union[list[tuple[str,dict]], list[tuple[str, float,dict]]]:
112
- Depending on the `return_score_type` parameter, returns either a list of strings representing the top-k similar contents,
113
- or a list of tuples where each tuple contains a string and a floating-point score.
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 = await self.vectors_store.asimilarity_search_with_relevance_scores(
140
- query=query,
141
- k=k,
142
- filter=_filter,
143
- fetch_k=fetch_k,
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
- Return content most similar to the given query.
161
-
162
- Args:
163
- embedding (list[float]): The vector to look up documents similar to.
164
- agent_id (int): The identifier of the agent to filter specific documents. Only documents associated with this agent will be considered.
165
- k (int, optional): The number of top similar contents to return. Defaults to 4.
166
- fetch_k (int, optional): The number of documents to fetch before applying any filters. Defaults to 20.
167
- return_score_type (Union[Literal["none"], Literal["similarity_score"], Literal["L2-distance"]], optional):
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
- Depending on the `return_score_type` parameter, returns either a list of strings representing the top-k similar contents,
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
- Return contents selected using the maximal marginal relevance asynchronously.
215
-
216
- Args:
217
- query (str): The text to look up documents similar to.
218
- agent_id (int): The identifier of the agent to filter specific documents. Only documents associated with this agent will be considered.
219
- k (int, optional): The number of top similar contents to return. Defaults to 4.
220
- fetch_k (int, optional): The number of documents to fetch before applying any filters. Defaults to 20.
221
- lambda_mult (float): Number between 0 and 1 that determines the degree of diversity among the results with 0 corresponding to maximum diversity and 1 to minimum diversity. Defaults to 0.5.
222
- return_score_type (Literal["none"].,optional):
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]]: the result contents.
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
- Return contents selected using the maximal marginal relevance asynchronously.
261
-
262
- Args:
263
- embedding (list[float]): The vector to look up documents similar to.
264
- agent_id (int): The identifier of the agent to filter specific documents. Only documents associated with this agent will be considered.
265
- k (int, optional): The number of top similar contents to return. Defaults to 4.
266
- fetch_k (int, optional): The number of documents to fetch before applying any filters. Defaults to 20.
267
- lambda_mult (float): Number between 0 and 1 that determines the degree of diversity among the results with 0 corresponding to maximum diversity and 1 to minimum diversity. Defaults to 0.5.
268
- return_score_type (Union[Literal["none"], Literal["similarity_score"]], optional):
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
- Depending on the `return_score_type` parameter, returns either a list of strings representing the top-k similar contents,
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 = {