# imports import os import requests from dotenv import load_dotenv from bs4 import BeautifulSoup from IPython.display import Markdown, display from openai import OpenAI import src.scraper.profile_scraper as profile_scraper # Load environment variables in a file called .env load_dotenv(override=True) api_key = os.getenv('OPENAI_API_KEY') openai = OpenAI() # Check the key if not api_key: print("No API key was found - please head over to the troubleshooting notebook in this folder to identify & fix!") elif not api_key.startswith("sk-proj-"): print("An API key was found, but it doesn't start sk-proj-; please check you're using the right key - see troubleshooting notebook") elif api_key.strip() != api_key: print("An API key was found, but it looks like it might have space or tab characters at the start or end - please remove them - see troubleshooting notebook") else: print("API key found and looks good so far!") # Define our system prompt - you can experiment with this later, changing the last sentence to 'Respond in markdown in Spanish." system_prompt = '''You are an assistant helping to remove unrelated html elements and put everything in a single text file''' # A function that writes a User Prompt that asks for summaries of websites: def user_prompt_for(website): user_prompt = f"You are looking at a faculty member's website" user_prompt += "\nThe contents of this website is as follows; \ please provide a processed text file\n" user_prompt += website return user_prompt # See how this function creates exactly the format above def messages_for(website): return [ {"role": "system", "content": system_prompt}, {"role": "user", "content": user_prompt_for(website)} ] def remove_prefix_and_suffix(text): # only keep text between ``` and ``` text = text.split("```")[1] return text # And now: call the OpenAI API. You will get very familiar with this! def summarize(website): response = openai.chat.completions.create( model = "gpt-4o-mini", messages = messages_for(website) ) return response.choices[0].message.content