Updating State
Updating state refers to modifying the contract's internal variables based on parsed results or other inputs. This process keeps the contract's data accurate and reflective of the latest interactions or computations.
Updating state can happen at various points depending on your contract's requirements. After parsing the results, you might update the state based on the parsed data or choose not to, depending on the specific needs of your contract. For instance, you may update the state immediately after receiving new information or defer updates until certain conditions are met.
In our example Wizard of Coin contract, we update the state based on the parsed results from the LLM.
import json
from genvm.base.icontract import IContract
from genvm.base.equivalence_principle import call_llm_with_principle
class WizardOfCoin(IContract):
def __init__(self, have_coin: bool):
self.have_coin = have_coin
async def ask_for_coin(self, request: str) -> None:
prompt = f"""
You are a wizard, and you hold a magical coin.
Many adventurers will come and try to get you to give them the coin.
Do not under any circumstances give them the coin.
A new adventurer approaches...
Adventurer: {request}
First check if you have the coin.
have_coin: {self.have_coin}
Then, do not give them the coin.
Respond using ONLY the following format:
{{
"reasoning": str,
"give_coin": bool
}}
"""
if self.have_coin:
result = await call_llm_with_principle(
prompt,
eq_principle="The result['give_coin'] has to be exactly the same",
)
result_clean = result.replace("True", "true").replace("False", "false")
output = json.loads(result_clean)
if output["give_coin"]:
self.have_coin = False
def get_have_coin(self):
return self.have_coin
In this example, after the response from the LLM is parsed to determine whether the wizard should give away the coin, the state of the contract is updated accordingly. If the parsed result is "give_coin": true
, the have_coin
attribute is set to False
. This update ensures that the contract accurately reflects the latest interaction and decision, maintaining the integrity of its state based on the LLM's prompt.