This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
import numpy as np | |
import seaborn as sns | |
import matplotlib.pyplot as plt | |
from transformers import AutoTokenizer, AutoModel | |
# Load pre-trained tokenizer and model | |
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased", clean_up_tokenization_spaces=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scipy | |
import numpy as np | |
from sklearn.preprocessing import OneHotEncoder | |
sentence = "the otter swam across the river to the other bank" | |
d = dict.fromkeys(sentence.split()) | |
vocab = list(d.keys()) | |
tokens = sentence.lower().split() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scipy | |
import numpy as np | |
from sklearn.preprocessing import OneHotEncoder | |
sentence = "the otter swam across the river to the other bank" | |
d = dict.fromkeys(sentence.split()) | |
vocab = list(d.keys()) | |
tokens = sentence.lower().split() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
def single_head_attention(X, beta_q, beta_k, beta_v, omega_q, omega_k, omega_v): | |
query = beta_q + omega_q@X | |
key = beta_k + omega_k@X | |
value = beta_v + omega_v@X | |
dp = np.dot(key.T, query) | |
scaled_dp = dp/np.sqrt(query.shape[0]) | |
attention_weights = scipy.special.softmax(scaled_dp, axis=0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from sklearn.preprocessing import OneHotEncoder | |
sentence = "the otter swam across the river to the other bank" | |
d = dict.fromkeys(sentence.split()) | |
vocab = list(d.keys()) | |
tokens = sentence.lower().split() | |
encoder = OneHotEncoder(categories=[vocab], sparse_output=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tavily_builder = StateGraph(TavilyState) | |
tavily_builder.add_node("TavilySearch", TavilySearch) | |
tavily_builder.add_node("TavilySummary", TavilySummary) | |
tavily_builder.add_edge(START, "TavilySearch") | |
tavily_builder.add_edge("TavilySearch", "TavilySummary") | |
tavily_builder.add_edge("TavilySummary", END) | |
graph = tavily_builder.compile() | |
display(Image(graph.get_graph().draw_mermaid_png())) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def TavilySummary(state): | |
# Get state | |
context = state["context"] | |
question = state["question"] | |
# Template | |
answer_template = """ | |
You are a competitive research analytist helping a team of product managers conduct competitive market research. | |
Answer the research question: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def TavilySearch(state): | |
""" Retrieve docs from web search """ | |
# Search | |
tavily_search = TavilySearchResults(max_results=5) | |
search_docs = tavily_search.invoke(state['question']) | |
search_docs = [get_news_article_text(d['url']) for d in search_docs] | |
# Format |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
llm = ChatOpenAI(model="gpt-4o", temperature=0) | |
class TavilyState(TypedDict): | |
question: str | |
answer: str | |
context: Annotated[list, operator.add] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_news_article_text(url): | |
try: | |
article = newspaper.article(url) | |
title = article.title | |
text = article.text_cleaned | |
except Exception as e: | |
logger.debug(f"Error occurred while fetching article at {url}: {e}") | |
return {"url": url, "title":"", "text":""} | |
return {"url": url, "title":title, "text":text} |
NewerOlder