Skip to content

Instantly share code, notes, and snippets.

View chand1012's full-sized avatar

Chandler chand1012

View GitHub Profile
@chand1012
chand1012 / random_n8n.js
Created March 26, 2024 18:50
Get a random item from an input N8N array. MIT https://chand1012.mit-license.org
function getRandomElement(arr) {
if (!Array.isArray(arr) || arr.length === 0) {
throw new Error('The input must be a non-empty array.');
}
const randomIndex = Math.floor(Math.random() * arr.length);
return arr[randomIndex];
}
const items = $input.all();
from typing import Any, List, Mapping, Optional
# pip install httpx langchain
import httpx
from langchain.callbacks.manager import CallbackManagerForLLMRun
from langchain_core.language_models.llms import LLM
class LlamaCppServer(LLM):
'''Use Llama.cpp's builtin server to remotely host an LLM for use with Langchain'''
@chand1012
chand1012 / modal_sdxl_api.py
Last active November 3, 2023 20:51
Basically ripped right out of their docs with a few modifications. https://modal.com/docs/examples/stable_diffusion_xl
from pathlib import Path
from modal import Image, Mount, Stub, asgi_app, gpu, method
from pydantic import BaseModel
def download_models():
from huggingface_hub import snapshot_download
ignore = ["*.bin", "*.onnx_data", "*/diffusion_pytorch_model.safetensors"]
@chand1012
chand1012 / big_squash.sh
Created May 4, 2023 17:53
This script squashes a git repo into a single commit with a specified commit message. MIT License https://chand1012.mit-license.org
#!/bin/bash
# This script squashes a git repo into a single commit with a specified commit message
# Check if git is installed
if ! git --version 1>/dev/null 2>&1; then
echo "Error: git is not installed. Please install git and try again."
exit 1
fi
@chand1012
chand1012 / fetchWithTimeout.ts
Last active April 14, 2023 17:57
Deno fetch with optional timeout parameter. If the request takes longer than 10 seconds (or whatever you set it to), throws an error.
// LICENSE: https://chand1012.mit-license.org
const fetchWithTimeout = (url: string, options: RequestInit, timeout: number = 10000): Promise<any> => {
return new Promise(async (resolve, reject) => {
const controller = new AbortController();
const signal = controller.signal;
const timer = setTimeout(() => {
controller.abort();
reject(new Error("Request timed out"));
}, timeout);
@chand1012
chand1012 / workerskv-deno.ts
Created March 5, 2023 14:59
Use Cloudflare Workers with Deno
const endpoint = (key: string) => {
const accountID = Deno.env.get("WORKERS_KV_ACCOUNT_ID");
const namespaceID = Deno.env.get("WORKERS_KV_NAMESPACE_ID");
return `https://api.cloudflare.com/client/v4/accounts/${accountID}/storage/kv/namespaces/${namespaceID}/values/${key}`
};
export const set = async (key: string, value: string, ttl: number = 0, expiration: number = 0) => {
const { success, result, errors } = await fetch(`https://api.cloudflare.com/client/v4/accounts/${Deno.env.get("WORKERS_KV_ACCOUNT_ID")}/storage/kv/namespaces/${Deno.env.get("WORKERS_KV_NAMESPACE_ID")}/bulk`, {
method: "PUT",
@chand1012
chand1012 / workerskv.py
Created March 5, 2023 14:57
Script to use Cloudflare Workers with Python via Requests
import os
import requests
def endpoint(key):
accountID = os.getenv("WORKERS_KV_ACCOUNT_ID")
namespaceID = os.getenv("WORKERS_KV_NAMESPACE_ID")
return f"https://api.cloudflare.com/client/v4/accounts/{accountID}/storage/kv/namespaces/{namespaceID}/values/{key}"
def set(key, value, ttl=0, expiration=0):
headers = {
@chand1012
chand1012 / workerskv.ts
Created March 5, 2023 14:37
Use cloudflare workers anywhere you can use Fetch
// info https://giuseppegurgone.com/vercel-cloudflare-kv
// api docs https://developers.cloudflare.com/api/operations/workers-kv-namespace-write-multiple-key-value-pairs
const endpoint = (key: string) => {
const accountID = process.env.WORKERS_KV_ACCOUNT_ID;
const namespaceID = process.env.WORKERS_KV_NAMESPACE_ID;
return `https://api.cloudflare.com/client/v4/accounts/${accountID}/storage/kv/namespaces/${namespaceID}/values/${key}`
};
export const set = async (key: string, value: string, ttl: number = 0, expiration: number = 0) => {
@chand1012
chand1012 / CONTRIBUTING.md
Created December 23, 2022 04:26
The CONTRIBUTING.md file that I use.

Contributing

Please note we have a code of conduct, please follow it in all your interactions with the project.

Branches, Issues, & Pull Requests

Branch Names

Branch names should be

import os
import json
from pymongo import MongoClient
import pymysql
from dotenv import load_dotenv
# Load environment variables
load_dotenv()