LlmHelloWorldNonComparative Contract
The LlmHelloWorldNonComparative contract is a Python Intelligent Contract example that stores an LLM response validated with the non-comparative Equivalence Principle. It demonstrates integrating AI capabilities without requiring all validators to execute the full task; validators only evaluate the leader's response against specified criteria. This is done by using the non-comparative equivalence principle.
# { "Depends": "py-genlayer:1jb45aa8ynh2a9c9xn3b7qqh8sm5q93hwfp7jqmwsfhh8jpz09h6" }
from genlayer import *
import typing
class LlmHelloWorldNonComparative(gl.Contract):
message: str
def __init__(self):
self.message = ""
@gl.public.write
def set_message(self) -> typing.Any:
def get_input() -> str:
return "There is no context, I just want you to answer with truthy value in python (for example: 'yes', 'True', 1)"
self.message = gl.eq_principle.prompt_non_comparative(
get_input,
task="Answer with truthy value in python (for example: 'yes', 'True', 1)",
criteria="Answer should be a truthy value in python"
)
@gl.public.view
def get_message(self) -> str:
return self.messageCode Explanation
- Initialization: The
LlmHelloWorldNonComparativeclass initializes with an empty string in themessagevariable. - Write Method:
set_message()uses AI functionality to generate and store a message.- Uses
gl.eq_principle.prompt_non_comparative()with three parameters:- An input function (
get_input) providing the prompt - A task description
- Validation criteria for the response
- An input function (
- Read Method:
get_message()returns the stored message.
Key Components
- AI Integration: The contract uses non-comparative equivalence principle to interact with an AI model.
- Deterministic Execution:
gl.eq_principle.prompt_non_comparative()ensures that all nodes in the network accept responses that meet the specified criteria. - State Management: The contract maintains a single string state variable that stores the AI response.
Deploying the Contract
To deploy the LlmHelloWorldNonComparative 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 message. - Initially, this will return an empty string.
Executing Transactions
To interact with the deployed contract:
- Call
set_message()to trigger the AI interaction. - The function will:
- Execute the AI prompt requesting a truthy Python value
- Validate the response against the specified criteria
- Store the result if it meets the criteria
Understanding AI Integration
This contract demonstrates several important concepts:
- AI Prompting: Shows how to formulate prompts with specific validation criteria.
- Non-comparative Validation: Uses criteria-based validation instead of exact matching.
- State Updates: Demonstrates how validated AI-generated content can be stored in blockchain state.
Handling Different Scenarios
- Initial State: The message starts empty.
- After set_message(): The message will contain any valid truthy Python value (e.g., "yes", "True", or "1").
- Multiple Calls: Each call to
set_message()may result in different valid responses. - Network Consensus: All nodes will accept any response that meets the validation criteria.
Important Notes
- This example demonstrates the non-comparative approach to AI response validation.
- The validation criteria ensure semantic correctness rather than exact matching.
- Different nodes may accept different responses as long as they meet the specified criteria.
You can monitor the contract's behavior through transaction logs, which will show the AI responses and state updates as they occur.