Created
March 4, 2025 20:12
-
-
Save TimelessP/33f9813a702df703fa7e9de074e83619 to your computer and use it in GitHub Desktop.
lmsfim.py - using LM Studio tools calling and fimputerr computer emulator
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 lmstudio as lms | |
import time | |
import random | |
import math | |
# https://github.com/TimelessP/fimputerr | |
from fimputerr import run_fimputerr_code # Import unchanged Fimputerr interpreter | |
# ----------------- Utility Functions ----------------- | |
def get_time() -> str: | |
"""Returns the current time in YYYY-MM-DD HH:MM:SS format.""" | |
return time.strftime("%Y-%m-%d %H:%M:%S") | |
def convert_units(value: float, unit: str) -> str: | |
""" | |
Converts between common units. | |
Supported conversions: | |
- "km" → Converts kilometers to miles (default assumption). | |
- "miles" → Converts miles to kilometers (default assumption). | |
- "km_to_miles" → Explicitly converts kilometers to miles. | |
- "miles_to_km" → Explicitly converts miles to kilometers. | |
- "c_to_f" → Converts Celsius to Fahrenheit. | |
Example usage: | |
>>> convert_units(100, "km") | |
"100 km to miles = 62.14" | |
>>> convert_units(50, "miles") | |
"50 miles to km = 80.47" | |
""" | |
conversions = { | |
"c_to_f": lambda c: c * 9/5 + 32, | |
"km_to_miles": lambda km: km * 0.621371, | |
"miles_to_km": lambda miles: miles / 0.621371 | |
} | |
unit = unit.lower().replace(" ", "_").replace("-", "_") | |
if unit == "km": | |
unit = "km_to_miles" | |
elif unit == "miles": | |
unit = "miles_to_km" | |
if unit in conversions: | |
return f"{value} {unit.replace('_', ' ')} = {conversions[unit](value):.2f}" | |
return f"Unsupported unit conversion: {unit}" | |
def is_prime(n: int) -> bool: | |
"""Returns True if a number is prime.""" | |
if n < 2: | |
return False | |
for i in range(2, int(math.sqrt(n)) + 1): | |
if n % i == 0: | |
return False | |
return True | |
def random_number(start: int, end: int) -> int: | |
"""Returns a random number between start and end (inclusive).""" | |
return random.randint(start, end) | |
# ----------------- Fimputerr Shim Function ----------------- | |
def run_fimputerr(fimputerr_code: str) -> str: | |
""" | |
Executes Fimputerr assembly code and returns the output logs. | |
Fimputerr Assembly Overview: | |
- **ERR MOV X** → Moves X into the register. | |
- **ERR ADD X** → Adds X to the register. | |
- **ERR SUB X** → Subtracts X from the register. | |
- **ERR MUL X** → Multiplies the register by X. | |
- **ERR DIV X** → Divides the register by X (resets to 0 if X=0). | |
- **ERR CMP X** → Compares, always causes an "error" in Fimputerr. | |
- **ERR JMP X** → Jumps forward/backward X instructions. | |
- **ERR NOP** → No operation. | |
- **ERR RND** → Assigns a random value to the register. | |
- **ERR HLT** → Halts execution. | |
Example Input: | |
``` | |
ERR MOV 10 | |
ERR ADD 5 | |
ERR MUL 2 | |
ERR HLT | |
``` | |
Example Output: | |
``` | |
ERR MOV 10 -> register=10, errors=0 | |
ERR ADD 5 -> register=15, errors=0 | |
ERR MUL 2 -> register=30, errors=0 | |
ERR HLT -> register=30, errors=0 | |
``` | |
The AI can generate valid Fimputerr assembly and use this function to execute it. | |
""" | |
output = run_fimputerr_code(fimputerr_code) | |
return "\n".join(output) | |
# ----------------- Load LM Studio Model ----------------- | |
model = lms.llm("phi-4@q3_k_l") | |
# ----------------- AI-Generated Fimputerr Code Execution ----------------- | |
model.act( | |
"""Write a simple Fimputerr assembly program that calculates (10 + 5) * 2 | |
and executes it using the provided function.""", | |
tools=[run_fimputerr], # AI can now write and execute Fimputerr code | |
on_message=print, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment