-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent.py
92 lines (68 loc) · 2.51 KB
/
agent.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
from langchain_google_genai import ChatGoogleGenerativeAI
from browser_use import Agent
from browser_use.browser.browser import Browser, BrowserConfig
from browser_use.controller.service import Controller
import asyncio
from dotenv import load_dotenv
load_dotenv()
import os
import streamlit as st
from typing import List
from pydantic import BaseModel
class News(BaseModel):
headline: str
url: str
class NewsList(BaseModel):
news: List[News]
def get_llm():
"""
Returns an instance of the Google Generative AI LLM.
The LLM is initialized with the "gemini-1.5-flash" model and the API key
set in the GEMINI_API_KEY environment variable.
Returns:
llm (ChatGoogleGenerativeAI): An instance of the Google Generative AI LLM.
"""
return ChatGoogleGenerativeAI(model="gemini-1.5-flash", api_key = os.getenv("GEMINI_API_KEY"))
def initialize_agent(query):
"""
Initializes a new instance of the Agent class with the given query.
This function also initializes a new instance of the Controller and Browser classes
and returns them as a tuple.
Args:
query (str): The query to use for the Agent.
Returns:
tuple: A tuple containing the initialized Agent and Browser.
"""
llm = get_llm()
controller = Controller(output_model=News)
browser = Browser(config=BrowserConfig())
return Agent(
task = query,
llm = llm,
controller = controller,
browser = browser,
use_vision=True,
max_actions_per_step=1
), browser
st.title("Browser AI Agent 🤖 powered by Google Gemini")
query = st.text_input("Enter your query here:")
if st.button("Run Agent"):
st.write("Initlializing Agent...")
agent, browser = initialize_agent(query)
# Create a placeholder to update results dynamically
output_area = st.empty()
async def run_agent():
with st.spinner("Running Agent..."):
history = await agent.run(max_steps=25)
result = history.final_result()
output_area.write("### Results from Agent:")
print(result)
if result:
parsed: NewsList = NewsList.model_validate_json(result)
for news in parsed.news:
output_area.write(f"Title: {news.headline}")
output_area.write(f"URL: {news.url}")
else:
output_area.write("No results found")
st.success("Agent completed Task successfully!")
asyncio.run(run_agent())