File size: 2,153 Bytes
db7ec76
 
 
 
 
 
 
 
df2f2e8
db7ec76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df2f2e8
db7ec76
 
 
 
df2f2e8
db7ec76
df2f2e8
 
db7ec76
 
 
 
 
 
 
 
 
 
df2f2e8
 
 
 
 
db7ec76
 
df2f2e8
db7ec76
 
 
 
df2f2e8
 
 
 
 
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
# 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