This file contains hidden or 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
class RESPObject: | |
""" | |
Base class for RESP data types. | |
Each RESP object maintains both its logical value and metadata about its | |
wire format representation, enabling efficient parsing and serialization. | |
""" | |
type: RESPObjectType | |
value: Any | |
bytes_length: int # Length of actual data in bytes |
This file contains hidden or 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 generate( | |
self, | |
prompt_tokens: List[List[int]], | |
max_gen_len: int, | |
temperature: float = 0.6, | |
top_p: float = 0.9, | |
logprobs: bool = False, | |
echo: bool = False, | |
) -> Tuple[List[List[int]], Optional[List[List[float]]]]: | |
# .... |
This file contains hidden or 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 matplotlib.pyplot as plt | |
import numpy as np | |
def plot_temperature_effects(logits, temperatures): | |
""" | |
Visualize how different temperatures affect probability distribution | |
""" | |
plt.figure(figsize=(12, 6)) | |
x = np.arange(len(logits)) | |
This file contains hidden or 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 sample_next_token(logits, temperature=1.0): | |
"""Sample next token from language model logits""" | |
# Apply temperature | |
scaled_logits = [l/temperature for l in logits] | |
# Convert to probabilities with softmax | |
max_logit = max(scaled_logits) | |
exp_logits = [math.exp(l - max_logit) for l in scaled_logits] | |
total = sum(exp_logits) | |
probs = [e/total for e in exp_logits] |
This file contains hidden or 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
# For most things, Python's built-in tools work great | |
import random | |
random.seed(42) | |
random.random() # Gets you a random decimal |
This file contains hidden or 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
# Different parts of your program should get their own random numbers | |
game_rng = MersenneTwister(seed1) # For game stuff | |
ai_rng = MersenneTwister(seed2) # For AI stuff |
This file contains hidden or 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
# Same starting number = same "random" sequence | |
rng1 = MersenneTwister(42) | |
rng2 = MersenneTwister(42) | |
assert rng1.extract_number() == rng2.extract_number() |
This file contains hidden or 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
# Using operating system's secure random source | |
import os | |
true_random_bytes = os.urandom(16) | |
# On Linux systems | |
with open("/dev/random", "rb") as f: | |
hardware_random = f.read(16) |
This file contains hidden or 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 sample_next_token(rng, logits, temperature=1.0): | |
"""Sample next token from language model logits""" | |
# Apply temperature | |
scaled_logits = [l/temperature for l in logits] | |
# Convert to probabilities with softmax | |
max_logit = max(scaled_logits) | |
exp_logits = [math.exp(l - max_logit) for l in scaled_logits] | |
total = sum(exp_logits) | |
probs = [e/total for e in exp_logits] |
This file contains hidden or 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 estimate_pi(rng, n=1000000): | |
inside = 0 | |
for _ in range(n): | |
x = uniform(rng, -1, 1) | |
y = uniform(rng, -1, 1) | |
if x*x + y*y <= 1: | |
inside += 1 | |
return 4 * inside / n | |
mt = MersenneTwister(42) |