Skip to content

Instantly share code, notes, and snippets.

View razhangwei's full-sized avatar

Wei Zhang razhangwei

  • Facebook
  • Bay Area
View GitHub Profile
@razhangwei
razhangwei / 0 - structured_output.md
Last active September 7, 2024 06:14
#llm Structured Output and JSON mode
  • quite powerful as it simplies the writing the system prompt

  • often need an extra step of post processing, which can be done by LLM or traditional programs.

  • structured output vs json mode:

    • structured output: 100% gurantee schema; schema doesn't consume tokens
  • json: scheme usually goes to system prompts, costing tokens, doesn't guarantee json or scheme correctness.

@razhangwei
razhangwei / llm_cli.md
Created September 5, 2024 16:57
llm for cli
pipx install llm
llm install llm-gemini 
llm -m gemini-1.5-flash-latest
@razhangwei
razhangwei / visudo.sh
Created September 3, 2024 02:25
run script with sudo without (re-typing) password #Unix
## sudo visudo -f /etc/sudoers.d/custom_config
# /etc/sudoers.d/custom_config
# Extend the sudo ticket lifetime to 2 hours (7200 seconds)
Defaults timestamp_timeout=7200
# Allow your user to run the script without a password
wei ALL=(ALL) NOPASSWD: /Users/wei/start_nano_transcriber.sh
@razhangwei
razhangwei / keyboard.py
Created September 3, 2024 02:02
print feedback message to current text box via keyboard emulation #UI #keyboard #python
class FeedbackManager:
"""
Manage user feedback by printing a message and printing dots after it.
This class takes care of printing a message and then appending dots to it
until told to clear. It also handles clearing the feedback by deleting the
message and dots.
The printing of dots is done in a separate thread to avoid blocking.
"""
@razhangwei
razhangwei / audio_recorder.py
Created September 3, 2024 02:00
audio recorder #python #IO #audio #STT
class AudioRecorder:
def __init__(self):
self.is_recording = False
self.recording_queue = queue.Queue()
self.stop_recording = threading.Event()
self.recording_thread = None
def start_recording(self):
if not self.is_recording:
@razhangwei
razhangwei / chat_template.md
Last active September 2, 2024 18:25
chat template

Jinja

Go

Delimiters:

{{ and }} are used to enclose template actions.

Conditionals:

@razhangwei
razhangwei / nf4.py
Created September 2, 2024 17:41
NormalFloat 4 Quantization #pytorch #quantization
import torch
import torch.nn as nn
import torch.nn.functional as F
class NF4Quantizer(nn.Module):
def __init__(self):
super().__init__()
self.nf4_values = torch.tensor([
-1.0, -0.6961928009986877, -0.5250730514526367, -0.39491748809814453,
-0.28444138169288635, -0.18477343022823334, -0.09105003625154495, 0.0,
@razhangwei
razhangwei / cookiecutter
Created September 2, 2024 04:06
cookiecutter #template
1. install with pipx
2. ` pipx run cookiecutter gh:audreyfeldroy/cookiecutter-pypackage`
@razhangwei
razhangwei / pynput_demo.py
Last active August 31, 2024 18:18
pynput example #keyboard #macos #system
from pynput import keyboard
from pynput.keyboard import Key, Controller
# For simulating keyboard presses
keyboard_controller = Controller()
def on_press(key):
try:
print(f'Alphanumeric key pressed: {key.char}')
except AttributeError:
@razhangwei
razhangwei / rope.py
Last active January 2, 2025 00:36
#pytorch RoPE
import math
import torch
import torch.nn as nn
class RotaryPositionEmbedding(nn.Module):
"""
Implements Rotary Position Embedding (RoPE) as a PyTorch module.
Args:
dim (int): Dimension of the embedding (must be even)