Parsing Results

Parsing results refers to the process of interpreting and extracting useful information from the responses received after each call, whether it be from an LLM or web data. This step is essential to understand and structure the data received, which might be used for further processing, validation, or decision-making.

ℹ️

Parsing results is something you'll do after every call to an LLM or a web data source. After parsing, what you do with the data is up to you—it could involve updating the contract state, performing additional calculations, or making decisions based on the parsed information.

In the Wizard of Coin contract, the LLM provides a JSON response indicating whether the wizard should give the coin to the adventurer. The response is then parsed to extract the decision.

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

In the code above:

  • The result_clean variable ensures the response is in proper JSON format by replacing Python's True and False with their lowercase JSON equivalents.
  • The json.loads(result_clean) function converts the JSON string into a Python dictionary, making it easy to access specific fields within the response.

By parsing the results in this manner, the contract can effectively interpret the LLM's decision and take appropriate actions based on the parsed data.