forked from agent0ai/agent-zero
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinitialize.py
More file actions
169 lines (150 loc) · 6.81 KB
/
initialize.py
File metadata and controls
169 lines (150 loc) · 6.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
from agent import AgentConfig
import models
from python.helpers import runtime, settings, defer
from python.helpers.print_style import PrintStyle
def initialize_agent(override_settings: dict | None = None):
current_settings = settings.get_settings()
if override_settings:
current_settings = settings.merge_settings(current_settings, override_settings)
def _normalize_model_kwargs(kwargs: dict) -> dict:
# convert string values that represent valid Python numbers to numeric types
result = {}
for key, value in kwargs.items():
if isinstance(value, str):
# try to convert string to number if it's a valid Python number
try:
# try int first, then float
result[key] = int(value)
except ValueError:
try:
result[key] = float(value)
except ValueError:
result[key] = value
else:
result[key] = value
return result
# chat model from user settings
chat_llm = models.ModelConfig(
type=models.ModelType.CHAT,
provider=current_settings["chat_model_provider"],
name=current_settings["chat_model_name"],
api_base=current_settings["chat_model_api_base"],
ctx_length=current_settings["chat_model_ctx_length"],
vision=current_settings["chat_model_vision"],
limit_requests=current_settings["chat_model_rl_requests"],
limit_input=current_settings["chat_model_rl_input"],
limit_output=current_settings["chat_model_rl_output"],
kwargs=_normalize_model_kwargs(current_settings["chat_model_kwargs"]),
)
# utility model from user settings
utility_llm = models.ModelConfig(
type=models.ModelType.CHAT,
provider=current_settings["util_model_provider"],
name=current_settings["util_model_name"],
api_base=current_settings["util_model_api_base"],
ctx_length=current_settings["util_model_ctx_length"],
limit_requests=current_settings["util_model_rl_requests"],
limit_input=current_settings["util_model_rl_input"],
limit_output=current_settings["util_model_rl_output"],
kwargs=_normalize_model_kwargs(current_settings["util_model_kwargs"]),
)
# embedding model from user settings
embedding_llm = models.ModelConfig(
type=models.ModelType.EMBEDDING,
provider=current_settings["embed_model_provider"],
name=current_settings["embed_model_name"],
api_base=current_settings["embed_model_api_base"],
limit_requests=current_settings["embed_model_rl_requests"],
kwargs=_normalize_model_kwargs(current_settings["embed_model_kwargs"]),
)
# browser model from user settings
browser_llm = models.ModelConfig(
type=models.ModelType.CHAT,
provider=current_settings["browser_model_provider"],
name=current_settings["browser_model_name"],
api_base=current_settings["browser_model_api_base"],
vision=current_settings["browser_model_vision"],
kwargs=_normalize_model_kwargs(current_settings["browser_model_kwargs"]),
)
# agent configuration
config = AgentConfig(
chat_model=chat_llm,
utility_model=utility_llm,
embeddings_model=embedding_llm,
browser_model=browser_llm,
profile=current_settings["agent_profile"],
memory_subdir=current_settings["agent_memory_subdir"],
knowledge_subdirs=[current_settings["agent_knowledge_subdir"], "default"],
mcp_servers=current_settings["mcp_servers"],
browser_http_headers=current_settings["browser_http_headers"],
# code_exec params get initialized in _set_runtime_config
# additional = {},
)
# update SSH and docker settings
_set_runtime_config(config, current_settings)
# update config with runtime args
_args_override(config)
# initialize MCP in deferred task to prevent blocking the main thread
# async def initialize_mcp_async(mcp_servers_config: str):
# return initialize_mcp(mcp_servers_config)
# defer.DeferredTask(thread_name="mcp-initializer").start_task(initialize_mcp_async, config.mcp_servers)
# initialize_mcp(config.mcp_servers)
# import python.helpers.mcp_handler as mcp_helper
# import agent as agent_helper
# import python.helpers.print_style as print_style_helper
# if not mcp_helper.MCPConfig.get_instance().is_initialized():
# try:
# mcp_helper.MCPConfig.update(config.mcp_servers)
# except Exception as e:
# first_context = agent_helper.AgentContext.first()
# if first_context:
# (
# first_context.log
# .log(type="warning", content=f"Failed to update MCP settings: {e}", temp=False)
# )
# (
# print_style_helper.PrintStyle(background_color="black", font_color="red", padding=True)
# .print(f"Failed to update MCP settings: {e}")
# )
# return config object
return config
def initialize_chats():
from python.helpers import persist_chat
async def initialize_chats_async():
persist_chat.load_tmp_chats()
return defer.DeferredTask().start_task(initialize_chats_async)
def initialize_mcp():
set = settings.get_settings()
async def initialize_mcp_async():
from python.helpers.mcp_handler import initialize_mcp as _initialize_mcp
return _initialize_mcp(set["mcp_servers"])
return defer.DeferredTask().start_task(initialize_mcp_async)
def initialize_job_loop():
from python.helpers.job_loop import run_loop
return defer.DeferredTask("JobLoop").start_task(run_loop)
def initialize_preload():
import preload
return defer.DeferredTask().start_task(preload.preload)
def _args_override(config):
# update config with runtime args
for key, value in runtime.args.items():
if hasattr(config, key):
# conversion based on type of config[key]
if isinstance(getattr(config, key), bool):
value = value.lower().strip() == "true"
elif isinstance(getattr(config, key), int):
value = int(value)
elif isinstance(getattr(config, key), float):
value = float(value)
elif isinstance(getattr(config, key), str):
value = str(value)
else:
raise Exception(
f"Unsupported argument type of '{key}': {type(getattr(config, key))}"
)
setattr(config, key, value)
def _set_runtime_config(config: AgentConfig, set: settings.Settings):
ssh_conf = settings.get_runtime_config(set)
for key, value in ssh_conf.items():
if hasattr(config, key):
setattr(config, key, value)