pycityagent 2.0.0a43__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 (81) hide show
  1. pycityagent/__init__.py +23 -0
  2. pycityagent/agent.py +833 -0
  3. pycityagent/cli/wrapper.py +44 -0
  4. pycityagent/economy/__init__.py +5 -0
  5. pycityagent/economy/econ_client.py +355 -0
  6. pycityagent/environment/__init__.py +7 -0
  7. pycityagent/environment/interact/__init__.py +0 -0
  8. pycityagent/environment/interact/interact.py +198 -0
  9. pycityagent/environment/message/__init__.py +0 -0
  10. pycityagent/environment/sence/__init__.py +0 -0
  11. pycityagent/environment/sence/static.py +416 -0
  12. pycityagent/environment/sidecar/__init__.py +8 -0
  13. pycityagent/environment/sidecar/sidecarv2.py +109 -0
  14. pycityagent/environment/sim/__init__.py +29 -0
  15. pycityagent/environment/sim/aoi_service.py +39 -0
  16. pycityagent/environment/sim/client.py +126 -0
  17. pycityagent/environment/sim/clock_service.py +44 -0
  18. pycityagent/environment/sim/economy_services.py +192 -0
  19. pycityagent/environment/sim/lane_service.py +111 -0
  20. pycityagent/environment/sim/light_service.py +122 -0
  21. pycityagent/environment/sim/person_service.py +295 -0
  22. pycityagent/environment/sim/road_service.py +39 -0
  23. pycityagent/environment/sim/sim_env.py +145 -0
  24. pycityagent/environment/sim/social_service.py +59 -0
  25. pycityagent/environment/simulator.py +331 -0
  26. pycityagent/environment/utils/__init__.py +14 -0
  27. pycityagent/environment/utils/base64.py +16 -0
  28. pycityagent/environment/utils/const.py +244 -0
  29. pycityagent/environment/utils/geojson.py +24 -0
  30. pycityagent/environment/utils/grpc.py +57 -0
  31. pycityagent/environment/utils/map_utils.py +157 -0
  32. pycityagent/environment/utils/port.py +11 -0
  33. pycityagent/environment/utils/protobuf.py +41 -0
  34. pycityagent/llm/__init__.py +11 -0
  35. pycityagent/llm/embeddings.py +231 -0
  36. pycityagent/llm/llm.py +377 -0
  37. pycityagent/llm/llmconfig.py +13 -0
  38. pycityagent/llm/utils.py +6 -0
  39. pycityagent/memory/__init__.py +13 -0
  40. pycityagent/memory/const.py +43 -0
  41. pycityagent/memory/faiss_query.py +302 -0
  42. pycityagent/memory/memory.py +448 -0
  43. pycityagent/memory/memory_base.py +170 -0
  44. pycityagent/memory/profile.py +165 -0
  45. pycityagent/memory/self_define.py +165 -0
  46. pycityagent/memory/state.py +173 -0
  47. pycityagent/memory/utils.py +28 -0
  48. pycityagent/message/__init__.py +3 -0
  49. pycityagent/message/messager.py +88 -0
  50. pycityagent/metrics/__init__.py +6 -0
  51. pycityagent/metrics/mlflow_client.py +147 -0
  52. pycityagent/metrics/utils/const.py +0 -0
  53. pycityagent/pycityagent-sim +0 -0
  54. pycityagent/pycityagent-ui +0 -0
  55. pycityagent/simulation/__init__.py +8 -0
  56. pycityagent/simulation/agentgroup.py +580 -0
  57. pycityagent/simulation/simulation.py +634 -0
  58. pycityagent/simulation/storage/pg.py +184 -0
  59. pycityagent/survey/__init__.py +4 -0
  60. pycityagent/survey/manager.py +54 -0
  61. pycityagent/survey/models.py +120 -0
  62. pycityagent/utils/__init__.py +11 -0
  63. pycityagent/utils/avro_schema.py +109 -0
  64. pycityagent/utils/decorators.py +99 -0
  65. pycityagent/utils/parsers/__init__.py +13 -0
  66. pycityagent/utils/parsers/code_block_parser.py +37 -0
  67. pycityagent/utils/parsers/json_parser.py +86 -0
  68. pycityagent/utils/parsers/parser_base.py +60 -0
  69. pycityagent/utils/pg_query.py +92 -0
  70. pycityagent/utils/survey_util.py +53 -0
  71. pycityagent/workflow/__init__.py +26 -0
  72. pycityagent/workflow/block.py +211 -0
  73. pycityagent/workflow/prompt.py +79 -0
  74. pycityagent/workflow/tool.py +240 -0
  75. pycityagent/workflow/trigger.py +163 -0
  76. pycityagent-2.0.0a43.dist-info/LICENSE +21 -0
  77. pycityagent-2.0.0a43.dist-info/METADATA +235 -0
  78. pycityagent-2.0.0a43.dist-info/RECORD +81 -0
  79. pycityagent-2.0.0a43.dist-info/WHEEL +5 -0
  80. pycityagent-2.0.0a43.dist-info/entry_points.txt +3 -0
  81. pycityagent-2.0.0a43.dist-info/top_level.txt +3 -0
@@ -0,0 +1,147 @@
1
+ import asyncio
2
+ import logging
3
+ import os
4
+ import uuid
5
+ from collections.abc import Sequence
6
+ from typing import Any, Optional, Union
7
+
8
+ import mlflow
9
+ from mlflow.entities import (Dataset, DatasetInput, Document, Experiment,
10
+ ExperimentTag, FileInfo, InputTag, LifecycleStage,
11
+ LiveSpan, Metric, NoOpSpan, Param, Run, RunData,
12
+ RunInfo, RunInputs, RunStatus, RunTag, SourceType,
13
+ Span, SpanEvent, SpanStatus, SpanStatusCode,
14
+ SpanType, Trace, TraceData, TraceInfo, ViewType)
15
+
16
+ from ..utils.decorators import lock_decorator
17
+
18
+ logger = logging.getLogger("mlflow")
19
+
20
+
21
+ def init_mlflow_connection(
22
+ config: dict,
23
+ mlflow_run_name: Optional[str] = None,
24
+ experiment_name: Optional[str] = None,
25
+ experiment_description: Optional[str] = None,
26
+ experiment_tags: Optional[dict[str, Any]] = None,
27
+ ) -> tuple[str, tuple[str, mlflow.MlflowClient, Run, str]]:
28
+
29
+ os.environ["MLFLOW_TRACKING_USERNAME"] = config.get("username", None)
30
+ os.environ["MLFLOW_TRACKING_PASSWORD"] = config.get("password", None)
31
+
32
+ run_uuid = str(uuid.uuid4())
33
+ # run name
34
+ if mlflow_run_name is None:
35
+ mlflow_run_name = f"exp_{run_uuid}"
36
+
37
+ # exp name
38
+ if experiment_name is None:
39
+ experiment_name = f"run_{run_uuid}"
40
+
41
+ # tags
42
+ if experiment_tags is None:
43
+ experiment_tags = {}
44
+ if experiment_description is not None:
45
+ experiment_tags["mlflow.note.content"] = experiment_description
46
+
47
+ uri = config["mlflow_uri"]
48
+ client = mlflow.MlflowClient(tracking_uri=uri)
49
+
50
+ # experiment
51
+ try:
52
+ experiment_id = client.create_experiment(
53
+ name=experiment_name,
54
+ tags=experiment_tags,
55
+ )
56
+ except Exception as e:
57
+ experiment = client.get_experiment_by_name(experiment_name)
58
+ if experiment is None:
59
+ raise e
60
+ experiment_id = experiment.experiment_id
61
+
62
+ # run
63
+ run = client.create_run(experiment_id=experiment_id, run_name=mlflow_run_name)
64
+
65
+ run_id = run.info.run_id
66
+
67
+ return run_id, (uri, client, run, run_uuid)
68
+
69
+
70
+ class MlflowClient:
71
+ """
72
+ - Mlflow client
73
+ """
74
+
75
+ def __init__(
76
+ self,
77
+ config: dict,
78
+ mlflow_run_name: Optional[str] = None,
79
+ experiment_name: Optional[str] = None,
80
+ experiment_description: Optional[str] = None,
81
+ experiment_tags: Optional[dict[str, Any]] = None,
82
+ run_id: Optional[str] = None,
83
+ ) -> None:
84
+ if run_id is None:
85
+ self._run_id, (
86
+ self._mlflow_uri,
87
+ self._client,
88
+ self._run,
89
+ self._run_uuid,
90
+ ) = init_mlflow_connection(
91
+ config=config,
92
+ mlflow_run_name=mlflow_run_name,
93
+ experiment_name=experiment_name,
94
+ experiment_description=experiment_description,
95
+ experiment_tags=experiment_tags,
96
+ )
97
+ else:
98
+ self._mlflow_uri = uri = config["mlflow_uri"]
99
+ os.environ["MLFLOW_TRACKING_USERNAME"] = config.get("username", None)
100
+ os.environ["MLFLOW_TRACKING_PASSWORD"] = config.get("password", None)
101
+ self._client = client = mlflow.MlflowClient(tracking_uri=uri)
102
+ self._run = client.get_run(run_id=run_id)
103
+ self._run_id = run_id
104
+ self._run_uuid = run_uuid = str(uuid.uuid4())
105
+ self._lock = asyncio.Lock()
106
+
107
+ @property
108
+ def client(
109
+ self,
110
+ ) -> mlflow.MlflowClient:
111
+ return self._client
112
+
113
+ @property
114
+ def run_id(
115
+ self,
116
+ ) -> str:
117
+ assert self._run_id is not None
118
+ return self._run_id
119
+
120
+ @lock_decorator
121
+ async def log_batch(
122
+ self,
123
+ metrics: Sequence[Metric] = (),
124
+ params: Sequence[Param] = (),
125
+ tags: Sequence[RunTag] = (),
126
+ ):
127
+ self.client.log_batch(
128
+ run_id=self.run_id, metrics=metrics, params=params, tags=tags
129
+ )
130
+
131
+ @lock_decorator
132
+ async def log_metric(
133
+ self,
134
+ key: str,
135
+ value: float,
136
+ step: Optional[int] = None,
137
+ timestamp: Optional[int] = None,
138
+ ):
139
+ if timestamp is not None:
140
+ timestamp = int(timestamp)
141
+ self.client.log_metric(
142
+ run_id=self.run_id,
143
+ key=key,
144
+ value=value,
145
+ timestamp=timestamp,
146
+ step=step,
147
+ )
File without changes
Binary file
Binary file
@@ -0,0 +1,8 @@
1
+ """
2
+ 城市智能体模拟器模块
3
+ """
4
+
5
+ from .simulation import AgentSimulation
6
+ from .storage.pg import PgWriter, create_pg_tables
7
+
8
+ __all__ = ["AgentSimulation", "PgWriter", "create_pg_tables"]