Gremllm

原始链接: https://github.com/awwaiid/gremllm

GREMLLM is a novel Python utility that uses LLMs to dynamically define object behavior. Instead of pre-defined methods, each call and attribute access triggers the LLM to generate and execute code. It supports OpenAI, Claude, Gemini, and local models, configurable via the `llm` library. Key features include: * **Dynamic Behavior:** Methods and properties are implemented on-the-fly using LLM reasoning. * **Wet Mode:** Method calls return `Gremllm` objects, enabling infinite chaining. * **Verbose Mode:** Displays the generated code for debugging. * **Multi-Model Support:** Works with various LLMs. * **Smart Error Handling:** Gracefully handles code failures. Examples include a basic counter and a shopping cart, showcasing dynamic attribute creation and method implementation. Wet mode allows for "living" objects that interact with each other, simulating emergent behavior. Verbose mode reveals the code generated by the LLM, aiding in understanding and debugging.

This Hacker News thread discusses Gremllm, a library using LLMs in playful ways, demonstrated by its creator with a D&D game example. A commenter mentions a similar interactive fiction class that treats LLM hallucinations as creative storytelling, using JSON to manage game state and integrating LLM-generated content by identifying and adding "hallucinated" objects to the game. Other commenters express both amusement and horror, likening it to Python's "fuckitpy" and XKCD's StackSort. One user suggests using it for mocks in testing. The author then jokes the name 'wet mode' is so named as 'if you get them wet they multiply'. There are suggestions for self-hosting and inspecting calling party code. Overall, the thread highlights the creative, albeit potentially chaotic, applications of LLMs, blurring the lines between coding and storytelling.
相关文章

原文

A slight upgrade to the Gremlins in your code, we hereby present GREMLLM. This utility class can be used for a variety of purposes. Uhm. Also please don't use this and if you do please tell me because WOW. Or maybe don't tell me. Or do.

from gremllm import Gremllm

# Be sure to tell your gremllm what sort of thing it is
counter = Gremllm('counter')
counter.value = 5
counter.increment()
print(counter.value)  # 6?
print(counter.to_roman_numerals()) # VI?

Every method call and attribute access goes through a gremllm to decide what code to execute.

  • Dynamic Behavior: Objects implement methods and properties on-the-fly using LLM reasoning
  • Wet Mode: Method calls return living gremllm objects instead of plain values for infinite chaining
  • Verbose Mode: See exactly what code the LLM generates with verbose=True
  • Multi-Model Support: Use OpenAI, Claude, Gemini, or local models via the llm library
  • Inheritance: Child objects automatically inherit wet and verbose settings
  • Smart Error Handling: Graceful fallbacks when libraries aren't available or code fails

Configure your preferred LLM using the llm library:

# For OpenAI (default)
llm keys set openai

# For Claude
pip install llm-claude-3
llm keys set claude

# For local models
pip install llm-ollama

You can also specify which model to use when creating a gremllm:

from gremllm import Gremllm

# Use default model (gpt-4o-mini)
counter = Gremllm('counter')

# Use specific OpenAI model
counter = Gremllm('counter', model='gpt-4o')

# Use Claude
counter = Gremllm('counter', model='claude-3-5-sonnet-20241022')

# Use local model via Ollama
counter = Gremllm('counter', model='llama2')

Basic counter (see example/counter.py):

from gremllm import Gremllm

counter = Gremllm('counter')
counter.value = 0
counter.increment()
counter.increment(5)
counter.add_seventeen()
print(counter.current_value)
print(counter.value_in_hex)
counter.reset()

Shopping cart (see example/cart.py):

from gremllm import Gremllm

# Remind me to not shop at your store
cart = Gremllm('shopping_cart')
cart.add_item('apple', 1.50)
cart.add_item('banana', 0.75)
total = cart.calculate_total()
print(f"Cart contents: {cart.contents_as_json()}")
print(f"Cart total: {total}")
cart.clear()

Wet mode creates an immersive experience where method calls return gremllm objects instead of plain values, allowing infinite chaining and interaction:

from gremllm import Gremllm

# Normal mode returns plain values
counter = Gremllm('counter')
result = counter.increment()  # Returns 1 (plain int)

# Wet mode returns living gremllm objects  
wet_counter = Gremllm('counter', wet=True)
result = wet_counter.increment()  # Returns a gremllm number object
doubled = result.double()  # Can call methods on the result!
squared = doubled.square()  # Keep chaining forever!

Swimming pool simulator demonstrating wet mode (see example/wet_pool.py):

from gremllm import Gremllm

# Everything stays "wet" and alive in wet mode!
pool = Gremllm('swimming_pool', wet=True)
splash = pool.cannonball()  # Returns a living splash object
ripples = splash.create_ripples()  # Splash creates living ripples
fish = ripples.scare_fish()  # Ripples interact with fish
# Infinite emergent behavior!

Debug and understand gremllm behavior by seeing the generated code:

from gremllm import Gremllm

# Enable verbose mode to see generated code
counter = Gremllm('counter', verbose=True)
result = counter.increment()

# Output shows:
# [GREMLLM counter.increment] Generated code:
# ==================================================
# _context['value'] = _context.get('value', 0) + 1
# result = _context['value']
# ==================================================

OMG THIS ACTUALLY WORKS

For background on the concept of "gremlins" in code, see: Gremlins Three Rules: An Evolutionary Analysis

联系我们 contact @ memedata.com