Skip to content

Instantly share code, notes, and snippets.

View jgoodie's full-sized avatar

John Goodman jgoodie

  • San Diego
View GitHub Profile
@jgoodie
jgoodie / visualize_attention_weights.py
Created January 19, 2025 19:42
visualize the attention weights
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)
@jgoodie
jgoodie / mhsa.py
Created January 19, 2025 19:09
multiheaded self-attention
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()
@jgoodie
jgoodie / single_headed_attention_exp.py
Created January 19, 2025 17:24
Example of single headed attention mechanism
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()
@jgoodie
jgoodie / single_head_attention.py
Last active February 2, 2025 22:44
single head attention
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)
@jgoodie
jgoodie / one_hot_encoding.py
Created January 19, 2025 05:36
sentence one-hot-encoding
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)
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()))
@jgoodie
jgoodie / TavilySummary.py
Created November 4, 2024 04:40
Function to summarize Tavily search results
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:
@jgoodie
jgoodie / TavilySearch.py
Created November 4, 2024 04:39
Function to call Tavily search API
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
@jgoodie
jgoodie / llmandstateclass.py
Created November 4, 2024 03:33
Define the llm and the agent state class
llm = ChatOpenAI(model="gpt-4o", temperature=0)
class TavilyState(TypedDict):
question: str
answer: str
context: Annotated[list, operator.add]
@jgoodie
jgoodie / QandDNewspaper4k.py
Created November 4, 2024 02:04
Super quick and dirty web scraper using Newspaper4k
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}