LlmHelloWorld Contract
The LlmHelloWorld contract is the simplest example of calling an LLM from a Python Intelligent Contract. The leader validator asks an LLM to generate a short salute, and the other validators use the non-comparative Equivalence Principle to judge whether the leader's response actually is a salute — without each validator generating its own. This is how GenLayer reaches consensus on open-ended LLM output that would never match word-for-word across validators.
# { "Depends": "py-genlayer:1jb45aa8ynh2a9c9xn3b7qqh8sm5q93hwfp7jqmwsfhh8jpz09h6" }
from genlayer import *
import typing
class LlmHelloWorld(gl.Contract):
message: str
def __init__(self):
self.message = ""
@gl.public.write
def set_message(self) -> typing.Any:
def get_input() -> str:
return "A new developer just deployed their first Intelligent Contract on GenLayer"
self.message = gl.eq_principle.prompt_non_comparative(
get_input,
task="Write a short, friendly salute of at most one sentence celebrating this moment",
criteria="""
The response is a salute or greeting
It is friendly and positive
It is at most one sentence
""",
)
@gl.public.view
def get_message(self) -> str:
return self.messageCode Explanation
- Initialization: The
LlmHelloWorldclass initializes with an empty string in themessagevariable. - Write Method:
set_message()callsgl.eq_principle.prompt_non_comparative()with three parameters:- An input function (
get_input) providing the context the LLM works from - A
taskdescribing what the leader's LLM should generate - The
criteriavalidators use to judge the leader's output
- An input function (
- The leader runs the LLM with the input and task and proposes the salute it generated.
- Each validator does not generate its own salute; it evaluates the leader's response against the criteria and votes to accept or reject.
- Read Method:
get_message()returns the stored salute.
Key Components
- Open-ended LLM output: Every LLM run produces a differently worded salute, so validators could never agree on an exact string.
- Criteria-based consensus:
gl.eq_principle.prompt_non_comparative()lets validators agree on whether the output satisfies the criteria ("is this a friendly one-sentence salute?") instead of requiring identical text. - State Management: The contract stores the accepted salute in a single string state variable.
Deploying the Contract
To deploy the LlmHelloWorld contract:
- Deploy the Contract: No initial parameters are needed for deployment.
- The contract will initialize with an empty message.
Checking the Contract State
After deployment, you can:
- Use
get_message()to view the currently stored salute. - Initially, this will return an empty string.
Executing Transactions
To interact with the deployed contract:
- Call
set_message()to trigger the LLM interaction. - The function will:
- Have the leader's LLM generate a one-sentence salute
- Have validators judge the salute against the criteria
- Store the salute once validators accept it
Understanding the Consensus Flow
This contract demonstrates several important concepts:
- Leader proposes, validators judge: Only the leader generates the salute; validators evaluate it, which is cheaper and tolerant of wording differences.
- Subjective agreement: Validators reach consensus on a subjective question ("is this a salute?") rather than on exact bytes.
- State Updates: LLM-generated content becomes blockchain state only after validators accept it.
Handling Different Scenarios
- Initial State: The message starts empty.
- After set_message(): The message contains a salute, worded differently on every run.
- Multiple Calls: Each call to
set_message()stores a new, differently worded salute. - Validator rejection: If the leader's LLM returns something that is not a salute (or violates the criteria), validators reject it and consensus handles the disagreement.
Important Notes
- This is a minimal example of LLM integration in an Intelligent Contract; the prompt and criteria are intentionally simple.
- Never use
gl.eq_principle.strict_eq()for LLM calls — LLM output is non-deterministic, so exact-match consensus fails. Use criteria-based validation like this example, or a custom validator function for full control. - For a variant where validators check the response against task criteria with explicit parameters, see LlmHelloWorldNonComparative.
You can monitor the contract's behavior through transaction logs, which will show the LLM responses and state updates as they occur.