-
Notifications
You must be signed in to change notification settings - Fork 14
/
test-magone.py
140 lines (114 loc) · 5.08 KB
/
test-magone.py
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
# pip install azure-identity
import asyncio
import logging
import os
from autogen_core.application import SingleThreadedAgentRuntime
from autogen_core.application.logging import EVENT_LOGGER_NAME
from autogen_core.base import AgentId, AgentProxy
from autogen_core.components.code_executor import CodeBlock
from autogen_ext.code_executor.docker_executor import DockerCommandLineCodeExecutor
from autogen_magentic_one.agents.coder import Coder, Executor
from autogen_magentic_one.agents.file_surfer import FileSurfer
from autogen_magentic_one.agents.multimodal_web_surfer import MultimodalWebSurfer
from autogen_magentic_one.agents.orchestrator import LedgerOrchestrator
from autogen_magentic_one.agents.user_proxy import UserProxy
from autogen_magentic_one.messages import RequestReplyMessage, OrchestrationEvent
from autogen_magentic_one.messages import BroadcastMessage
from autogen_core.components.models import UserMessage
from autogen_magentic_one.utils import LogHandler
from dotenv import load_dotenv
load_dotenv()
# NOTE: Don't forget to 'playwright install --with-deps chromium'
async def confirm_code(code: CodeBlock) -> bool:
response = await asyncio.to_thread(
input,
f"Executor is about to execute code (lang: {code.language}):\n{code.code}\n\nDo you want to proceed? (yes/no): ",
)
return response.lower() == "yes"
async def main() -> None:
# Create the runtime.
runtime = SingleThreadedAgentRuntime()
# Create an appropriate client
#client = create_completion_client_from_env()
from autogen_ext.models import AzureOpenAIChatCompletionClient
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
# Create the token provider
token_provider = get_bearer_token_provider(
DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
)
client = AzureOpenAIChatCompletionClient(
model="gpt-4o",
api_version="2024-02-01",
azure_endpoint=os.getenv("AZURE_ENDPOINT"),
api_key=os.getenv("AZURE_OPENAI_API_KEY"),
#azure_ad_token_provider=token_provider,
model_capabilities={
"vision":True,
"function_calling":True,
"json_output":True,
}
)
async with DockerCommandLineCodeExecutor() as code_executor:
# Register agents.
await Coder.register(runtime, "Coder", lambda: Coder(model_client=client))
coder = AgentProxy(AgentId("Coder", "default"), runtime)
await Executor.register(
runtime,
"Executor",
lambda: Executor("A agent for executing code", executor=code_executor, confirm_execution=confirm_code),
)
executor = AgentProxy(AgentId("Executor", "default"), runtime)
# Register agents.
await MultimodalWebSurfer.register(runtime, "WebSurfer", MultimodalWebSurfer)
web_surfer = AgentProxy(AgentId("WebSurfer", "default"), runtime)
await FileSurfer.register(runtime, "file_surfer", lambda: FileSurfer(model_client=client))
file_surfer = AgentProxy(AgentId("file_surfer", "default"), runtime)
await UserProxy.register(
runtime,
"UserProxy",
lambda: UserProxy(description="The current user interacting with you."),
)
user_proxy = AgentProxy(AgentId("UserProxy", "default"), runtime)
await LedgerOrchestrator.register(
runtime,
"Orchestrator",
lambda: LedgerOrchestrator(
agents=[web_surfer, user_proxy, coder, executor, file_surfer],
model_client=client,
max_rounds=30,
max_time=25 * 60,
return_final_answer=True,
),
)
orchestrator = AgentProxy(AgentId("Orchestrator", "default"), runtime)
runtime.start()
actual_surfer = await runtime.try_get_underlying_agent_instance(web_surfer.id, type=MultimodalWebSurfer)
await actual_surfer.init(
model_client=client,
downloads_folder=os.getcwd(),
start_page="https://cve.mitre.org",#"https://github.com/microsoft/autogen",
browser_channel="chromium",
headless=True,
)
# await runtime.send_message(RequestReplyMessage(]), user_proxy.id) # USE this when you want interaction with the user
await runtime.send_message(
BroadcastMessage(
content=UserMessage(
# content="find a top article for Large Language Models in web and safe as 'article.pdf'", source="user"
content="generate three random words beging with 'A'", source="user"
)
),
recipient=orchestrator.id,
sender=user_proxy.id,
)
await runtime.stop_when_idle()
async def run_from_outside():
await main()
if __name__ == "__main__":
logger = logging.getLogger(EVENT_LOGGER_NAME)
logger.setLevel(logging.INFO)
log_handler = LogHandler()
log_handler.setStream
logger.handlers = [log_handler]
asyncio.run(main())
print("Done")