Skip to content

Instantly share code, notes, and snippets.

View robert-mcdermott's full-sized avatar

Robert McDermott robert-mcdermott

View GitHub Profile
@robert-mcdermott
robert-mcdermott / ollama-emb-cosine-dot.py
Created February 22, 2024 07:47
ollama embeddings cosine similarity and dot product example
# pip install scikit-learn numpy ollama
import ollama
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
text1 = ollama.embeddings(model='nomic-embed-text', prompt='The sky is blue because of rayleigh scattering')
text2 = ollama.embeddings(model='nomic-embed-text', prompt='The sky is cloudy and grey today')
vec1 = np.array(text1['embedding']).reshape(1, -1)
vec2 = np.array(text2['embedding']).reshape(1, -1)
@robert-mcdermott
robert-mcdermott / ollama-web-qa.py
Last active September 12, 2024 10:23
Use Ollama with a local LLM to query websites
import argparse
import requests
from langchain.llms import Ollama
from langchain.document_loaders import WebBaseLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.embeddings import GPT4AllEmbeddings
from langchain.vectorstores import Chroma
from langchain.chains import RetrievalQA
def main(args):
@robert-mcdermott
robert-mcdermott / ollama-doc-qa.py
Created January 4, 2024 18:53
Query web pages with local LLMs using Ollama and Langchain
# pip install chromadb==0.4.15 # need to pin to this version for current langchain version
from langchain.llms import Ollama
from langchain.document_loaders import WebBaseLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.embeddings import GPT4AllEmbeddings
from langchain.vectorstores import Chroma
from langchain.chains import RetrievalQA
ollama = Ollama(base_url='http://localhost:11434', model='zephyr:latest')
@robert-mcdermott
robert-mcdermott / word_freq_plot.py
Created January 1, 2024 01:20
Plot Word Frequencies minus Stop Words - Plotly and Matplotlib
import plotly.express as px
import matplotlib.pyplot as plt
from collections import Counter
import re
import sys
import nltk
from nltk.corpus import stopwords
def plot_word_frequencies_matplot(file_path, top):
# Load stop words
@robert-mcdermott
robert-mcdermott / website-content-scrape.py
Last active November 8, 2023 19:59
Recursive website content scrape
import os
import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin, urlparse
def is_valid(url, base_url):
parsed = urlparse(url)
return bool(parsed.netloc) and parsed.netloc == urlparse(base_url).netloc
def is_binary(url):
@robert-mcdermott
robert-mcdermott / chat-ollama-langchain.py
Created October 8, 2023 23:12
chatting with local ollama using langchain
from langchain.chat_models import ChatOllama
from langchain.callbacks.manager import CallbackManager
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
from langchain.schema import HumanMessage
chat_model = ChatOllama(model="mistral", base_url = "http://localhost:11434", callback_manager = CallbackManager([StreamingStdOutCallbackHandler()]))
#chat_model = ChatOllama(model="mistral")
messages = [
HumanMessage(content="Why is the sky blue?")
@robert-mcdermott
robert-mcdermott / print-embeddings.py
Created October 3, 2023 06:46
Print sentence vector embeddings
from sentence_transformers import SentenceTransformer
model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
embeddings = model.encode("A fat tuxedo cat")
print(embeddings)
@robert-mcdermott
robert-mcdermott / sentence-similarity.py
Last active October 3, 2023 05:27
sentence-similarity.py
from sentence_transformers import SentenceTransformer, util
model = SentenceTransformer('all-MiniLM-L6-v2')
# Two lists of sentences
sentences1 = ['The cat sits outside',
'A man is playing guitar',
'The new movie is awesome',
'Jim can run very fast',
'My goldfish is hungry']
import boto3
def main():
organizations_client = boto3.client("organizations")
list_accounts_object = organizations_client.list_accounts()
all_accounts = []
accounts = list_accounts_object["Accounts"]
while "NextToken" in list_accounts_object:
#!/usr/bin/python
import os, socket, sys, time
socket.setdefaulttimeout(10)
start = time.time()
subnets = ['140.107.42','140.107.43','140.107.88','140.107.89','140.107.134',\
'140.107.135','140.107.52','140.107.53','140.107.152','140.107.153',\
'140.107.170','140.107.171']
scanips = []; hosts = []; vhosts = []; free = []; vfree = []; reclaim = []; squatters = []