Merge pull request #679 from h4r5h4/fix/folder-name

remove space in the examples/o1_web_crawler folder name
This commit is contained in:
Eric Ciarla 2024-09-25 10:40:09 -04:00 committed by GitHub
commit abdc08edea
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 154 additions and 154 deletions

View File

@ -1,152 +1,152 @@
import os import os
from firecrawl import FirecrawlApp from firecrawl import FirecrawlApp
import json import json
from dotenv import load_dotenv from dotenv import load_dotenv
from openai import OpenAI from openai import OpenAI
# ANSI color codes # ANSI color codes
class Colors: class Colors:
CYAN = '\033[96m' CYAN = '\033[96m'
YELLOW = '\033[93m' YELLOW = '\033[93m'
GREEN = '\033[92m' GREEN = '\033[92m'
RED = '\033[91m' RED = '\033[91m'
MAGENTA = '\033[95m' MAGENTA = '\033[95m'
BLUE = '\033[94m' BLUE = '\033[94m'
RESET = '\033[0m' RESET = '\033[0m'
# Load environment variables # Load environment variables
load_dotenv() load_dotenv()
# Retrieve API keys from environment variables # Retrieve API keys from environment variables
firecrawl_api_key = os.getenv("FIRECRAWL_API_KEY") firecrawl_api_key = os.getenv("FIRECRAWL_API_KEY")
openai_api_key = os.getenv("OPENAI_API_KEY") openai_api_key = os.getenv("OPENAI_API_KEY")
# Initialize the FirecrawlApp and OpenAI client # Initialize the FirecrawlApp and OpenAI client
app = FirecrawlApp(api_key=firecrawl_api_key) app = FirecrawlApp(api_key=firecrawl_api_key)
client = OpenAI(api_key=openai_api_key) client = OpenAI(api_key=openai_api_key)
# Find the page that most likely contains the objective # Find the page that most likely contains the objective
def find_relevant_page_via_map(objective, url, app, client): def find_relevant_page_via_map(objective, url, app, client):
try: try:
print(f"{Colors.CYAN}Understood. The objective is: {objective}{Colors.RESET}") print(f"{Colors.CYAN}Understood. The objective is: {objective}{Colors.RESET}")
print(f"{Colors.CYAN}Initiating search on the website: {url}{Colors.RESET}") print(f"{Colors.CYAN}Initiating search on the website: {url}{Colors.RESET}")
map_prompt = f""" map_prompt = f"""
The map function generates a list of URLs from a website and it accepts a search parameter. Based on the objective of: {objective}, come up with a 1-2 word search parameter that will help us find the information we need. Only respond with 1-2 words nothing else. The map function generates a list of URLs from a website and it accepts a search parameter. Based on the objective of: {objective}, come up with a 1-2 word search parameter that will help us find the information we need. Only respond with 1-2 words nothing else.
""" """
print(f"{Colors.YELLOW}Analyzing objective to determine optimal search parameter...{Colors.RESET}") print(f"{Colors.YELLOW}Analyzing objective to determine optimal search parameter...{Colors.RESET}")
completion = client.chat.completions.create( completion = client.chat.completions.create(
model="o1-preview", model="o1-preview",
messages=[ messages=[
{ {
"role": "user", "role": "user",
"content": [ "content": [
{ {
"type": "text", "type": "text",
"text": map_prompt "text": map_prompt
} }
] ]
} }
] ]
) )
map_search_parameter = completion.choices[0].message.content map_search_parameter = completion.choices[0].message.content
print(f"{Colors.GREEN}Optimal search parameter identified: {map_search_parameter}{Colors.RESET}") print(f"{Colors.GREEN}Optimal search parameter identified: {map_search_parameter}{Colors.RESET}")
print(f"{Colors.YELLOW}Mapping website using the identified search parameter...{Colors.RESET}") print(f"{Colors.YELLOW}Mapping website using the identified search parameter...{Colors.RESET}")
map_website = app.map_url(url, params={"search": map_search_parameter}) map_website = app.map_url(url, params={"search": map_search_parameter})
print(f"{Colors.GREEN}Website mapping completed successfully.{Colors.RESET}") print(f"{Colors.GREEN}Website mapping completed successfully.{Colors.RESET}")
print(f"{Colors.GREEN}Located {len(map_website)} relevant links.{Colors.RESET}") print(f"{Colors.GREEN}Located {len(map_website)} relevant links.{Colors.RESET}")
return map_website return map_website
except Exception as e: except Exception as e:
print(f"{Colors.RED}Error encountered during relevant page identification: {str(e)}{Colors.RESET}") print(f"{Colors.RED}Error encountered during relevant page identification: {str(e)}{Colors.RESET}")
return None return None
# Scrape the top 3 pages and see if the objective is met, if so return in json format else return None # Scrape the top 3 pages and see if the objective is met, if so return in json format else return None
def find_objective_in_top_pages(map_website, objective, app, client): def find_objective_in_top_pages(map_website, objective, app, client):
try: try:
# Get top 3 links from the map result # Get top 3 links from the map result
top_links = map_website[:3] if isinstance(map_website, list) else [] top_links = map_website[:3] if isinstance(map_website, list) else []
print(f"{Colors.CYAN}Proceeding to analyze top {len(top_links)} links: {top_links}{Colors.RESET}") print(f"{Colors.CYAN}Proceeding to analyze top {len(top_links)} links: {top_links}{Colors.RESET}")
for link in top_links: for link in top_links:
print(f"{Colors.YELLOW}Initiating scrape of page: {link}{Colors.RESET}") print(f"{Colors.YELLOW}Initiating scrape of page: {link}{Colors.RESET}")
# Scrape the page # Scrape the page
scrape_result = app.scrape_url(link, params={'formats': ['markdown']}) scrape_result = app.scrape_url(link, params={'formats': ['markdown']})
print(f"{Colors.GREEN}Page scraping completed successfully.{Colors.RESET}") print(f"{Colors.GREEN}Page scraping completed successfully.{Colors.RESET}")
# Check if objective is met # Check if objective is met
check_prompt = f""" check_prompt = f"""
Given the following scraped content and objective, determine if the objective is met. Given the following scraped content and objective, determine if the objective is met.
If it is, extract the relevant information in a simple and concise JSON format. Use only the necessary fields and avoid nested structures if possible. If it is, extract the relevant information in a simple and concise JSON format. Use only the necessary fields and avoid nested structures if possible.
If the objective is not met with confidence, respond with 'Objective not met'. If the objective is not met with confidence, respond with 'Objective not met'.
Objective: {objective} Objective: {objective}
Scraped content: {scrape_result['markdown']} Scraped content: {scrape_result['markdown']}
Remember: Remember:
1. Only return JSON if you are confident the objective is fully met. 1. Only return JSON if you are confident the objective is fully met.
2. Keep the JSON structure as simple and flat as possible. 2. Keep the JSON structure as simple and flat as possible.
3. Do not include any explanations or markdown formatting in your response. 3. Do not include any explanations or markdown formatting in your response.
""" """
completion = client.chat.completions.create( completion = client.chat.completions.create(
model="o1-preview", model="o1-preview",
messages=[ messages=[
{ {
"role": "user", "role": "user",
"content": [ "content": [
{ {
"type": "text", "type": "text",
"text": check_prompt "text": check_prompt
} }
] ]
} }
] ]
) )
result = completion.choices[0].message.content result = completion.choices[0].message.content
if result != "Objective not met": if result != "Objective not met":
print(f"{Colors.GREEN}Objective potentially fulfilled. Relevant information identified.{Colors.RESET}") print(f"{Colors.GREEN}Objective potentially fulfilled. Relevant information identified.{Colors.RESET}")
try: try:
return json.loads(result) return json.loads(result)
except json.JSONDecodeError: except json.JSONDecodeError:
print(f"{Colors.RED}Error in parsing response. Proceeding to next page...{Colors.RESET}") print(f"{Colors.RED}Error in parsing response. Proceeding to next page...{Colors.RESET}")
else: else:
print(f"{Colors.YELLOW}Objective not met on this page. Proceeding to next link...{Colors.RESET}") print(f"{Colors.YELLOW}Objective not met on this page. Proceeding to next link...{Colors.RESET}")
print(f"{Colors.RED}All available pages analyzed. Objective not fulfilled in examined content.{Colors.RESET}") print(f"{Colors.RED}All available pages analyzed. Objective not fulfilled in examined content.{Colors.RESET}")
return None return None
except Exception as e: except Exception as e:
print(f"{Colors.RED}Error encountered during page analysis: {str(e)}{Colors.RESET}") print(f"{Colors.RED}Error encountered during page analysis: {str(e)}{Colors.RESET}")
return None return None
# Main function to execute the process # Main function to execute the process
def main(): def main():
# Get user input # Get user input
url = input(f"{Colors.BLUE}Enter the website to crawl: {Colors.RESET}") url = input(f"{Colors.BLUE}Enter the website to crawl: {Colors.RESET}")
objective = input(f"{Colors.BLUE}Enter your objective: {Colors.RESET}") objective = input(f"{Colors.BLUE}Enter your objective: {Colors.RESET}")
print(f"{Colors.YELLOW}Initiating web crawling process...{Colors.RESET}") print(f"{Colors.YELLOW}Initiating web crawling process...{Colors.RESET}")
# Find the relevant page # Find the relevant page
map_website = find_relevant_page_via_map(objective, url, app, client) map_website = find_relevant_page_via_map(objective, url, app, client)
if map_website: if map_website:
print(f"{Colors.GREEN}Relevant pages identified. Proceeding with detailed analysis...{Colors.RESET}") print(f"{Colors.GREEN}Relevant pages identified. Proceeding with detailed analysis...{Colors.RESET}")
# Find objective in top pages # Find objective in top pages
result = find_objective_in_top_pages(map_website, objective, app, client) result = find_objective_in_top_pages(map_website, objective, app, client)
if result: if result:
print(f"{Colors.GREEN}Objective successfully fulfilled. Extracted information:{Colors.RESET}") print(f"{Colors.GREEN}Objective successfully fulfilled. Extracted information:{Colors.RESET}")
print(f"{Colors.MAGENTA}{json.dumps(result, indent=2)}{Colors.RESET}") print(f"{Colors.MAGENTA}{json.dumps(result, indent=2)}{Colors.RESET}")
else: else:
print(f"{Colors.RED}Unable to fulfill the objective with the available content.{Colors.RESET}") print(f"{Colors.RED}Unable to fulfill the objective with the available content.{Colors.RESET}")
else: else:
print(f"{Colors.RED}No relevant pages identified. Consider refining the search parameters or trying a different website.{Colors.RESET}") print(f"{Colors.RED}No relevant pages identified. Consider refining the search parameters or trying a different website.{Colors.RESET}")
if __name__ == "__main__": if __name__ == "__main__":
main() main()

View File

@ -1,3 +1,3 @@
firecrawl-py firecrawl-py
python-dotenv python-dotenv
openai openai