Web Crawl agent#
1# You may need to add your working directory to the Python path. To do so, uncomment the following lines of code
2# import sys
3# sys.path.append("/Path/to/directory/agentic-framework") # Replace with your directory path
4
5import logging
6
7from baf.core.agent import Agent
8from baf.core.session import Session
9from baf.exceptions.logger import logger
10from baf.nlp.llm.llm_openai_api import LLMOpenAI
11from baf.utils.web_crawl import crawl_website
12
13# Configure the logging module (optional)
14logger.setLevel(logging.INFO)
15
16# Create the agent
17agent = Agent('web_crawl_agent') # set persist_sessions=True to enable session persistence across restarts
18# Load agent properties stored in a dedicated file
19agent.load_properties('config.yaml')
20# Define the platform your agent will use
21# set authenticate_users=True to enable user authentication and previous history loading in the UI
22websocket_platform = agent.use_websocket_platform(use_ui=True)
23
24# Create the LLM
25gpt = LLMOpenAI(
26 agent=agent,
27 name='gpt-4o-mini',
28 parameters={},
29 num_previous_messages=10
30)
31
32webpages = crawl_website(
33 initial_url="https://besser-pearl.org/",
34 max_depth=3,
35 max_pages=20,
36 format="markdown",
37 base_url_prefix="https://besser-pearl.org/"
38)
39
40# STATES
41
42initial_state = agent.new_state('initial_state', initial=True)
43awaiting_state = agent.new_state('awaiting_state')
44answer_state = agent.new_state('answer_state')
45
46
47# STATES BODIES' DEFINITION + TRANSITIONS
48
49def initial_body(session: Session):
50 session.reply('I am here to answer any questions you may have about the BESSER project! Just say "hello" to get started.')
51
52
53initial_state.set_body(initial_body)
54initial_state.go_to(awaiting_state)
55
56awaiting_state.when_no_intent_matched().go_to(answer_state)
57
58
59def answer_body(session: Session):
60 question = session.event.message
61 system_message = \
62 f"You are a helpful assistant for answering questions about the content of a webpage. " \
63 f"For any message not related to that, inform about what the user can ask. " \
64 f"Use the following webpage content to answer the question as best as you can:\n{webpages}\n"
65 answer = gpt.predict(message=question, system_message=system_message)
66 session.reply(answer)
67answer_state.set_body(answer_body)
68answer_state.go_to(awaiting_state)
69
70
71# RUN APPLICATION
72
73if __name__ == '__main__':
74 agent.run()