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 project described as an "AI cousin" to the Python error steamroller "fuckitpy," suggesting it might leverage AI to playfully and perhaps unpredictably handle code. The original poster finds the project both appalling and delightful. Andreabergia shares a code snippet demonstrating Gremllm's usage, creating a "counter" object and showcasing its potential to increment and convert values to Roman numerals, expressing enthusiastic approval. SoftTalker jokingly suggests it could be used as an alternative to writing mocks for testing purposes, highlighting the potentially unpredictable and humorous nature of the library. Overall, the thread conveys amusement and intrigue towards Gremllm's seemingly unconventional approach to coding tasks.
相关文章

原文

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