import openai import requests import json import os from bs4 import BeautifulSoup from IPython.display import Markdown api_key= os.getenv("OPENAI_API_KEY") # OpenAI API Key (Load from environment variable for security) openai.api_key = api_key # Replace with your API key or load from env # User-Agent Headers HEADERS = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36" } class Website: """A utility class to scrape and process a website.""" def __init__(self, url): self.url = url response = requests.get(url, headers=HEADERS) self.body = response.content soup = BeautifulSoup(self.body, 'html.parser') self.title = soup.title.string if soup.title else "No title found" if soup.body: for irrelevant in soup.body(["script", "style", "img", "input"]): irrelevant.decompose() self.text = soup.body.get_text(separator="\n", strip=True) else: self.text = "" links = [link.get('href') for link in soup.find_all('a')] self.links = [link for link in links if link] def get_contents(self): return f"Webpage Title:\n{self.title}\nWebpage Contents:\n{self.text}\n\n" link_system_prompt = """ You are provided with a list of links found on a webpage. You will decide which links are present trading opportunities. Respond in JSON format like this: { "links": [ {"type": "news article", "url": "https://example.com/news1"}, {"type": "news article", "url": "https://example.com/news2"} ] } """ def get_links_user_prompt(website): """Generate a prompt for filtering news article links.""" user_prompt = f"Here are the links found on {website.url}. Extract only news articles:\n" user_prompt += "\n".join(website.links) return user_prompt def get_links(url): """Fetch and filter news article links from a webpage.""" website = Website(url) response = openai.chat.completions.create( model='gpt-4o-mini', messages=[ {"role": "system", "content": link_system_prompt}, {"role": "user", "content": get_links_user_prompt(website)} ], response_format={"type": "json_object"} ) result = response.choices[0].message.content return json.loads(result) def get_all_details(url): """Retrieve content from the main page and its news article links.""" result = f"Landing page:\n{Website(url).get_contents()}\n" links = get_links(url) for link in links["links"]: result += f"\n\n{link['type']}\n" result += Website(link["url"]).get_contents() return result system_prompt = "You are great at analyzing overall market sentiment." def get_market_sentiment_prompt(url): """Generate a prompt for summarizing recent news articles.""" user_prompt = '''You are able to gauge the overall market sentiment.for example is overall market is risk off,'the market sentiment is generally riskoff'. here comes the news articles below\n''' user_prompt += "Here are the page contents:\n" user_prompt += get_all_details(url) return user_prompt def get_sentiment(url): """Generate a markdown-formatted news summary.""" response = openai.chat.completions.create( model='gpt-4o-mini', messages=[ {"role": "system", "content": system_prompt}, {"role": "user", "content": get_market_sentiment_prompt(url)} ] ) result = response.choices[0].message.content return result # Return markdown text # Function to call externally def fetch_market_sentiment(url): """Fetch a summarized news report from a given URL.""" return get_sentiment(url)