MCPcopy Create free account
hub / github.com/microsoft/TypeChat / evaluate_expression

Function evaluate_expression

python/examples/math/program.py:59–91  ·  view source on GitHub ↗
(expr: JsonValue)

Source from the content-addressed store, hash-verified

57 return asyncio.gather(*[evaluate_expression(e) for e in array])
58
59 async def evaluate_expression(expr: JsonValue) -> JsonValue:
60 match expr:
61 case bool() | int() | float() | str() | None:
62 return expr
63
64 case { "@ref": int(index) } if not isinstance(index, bool):
65 if 0 <= index < len(results):
66 return results[index]
67
68 raise ValueError(f"Index {index} is out of range [0, {len(results)})")
69
70 case { "@ref": ref_value }:
71 raise ValueError(f"'ref' value must be an integer, but was ${ref_value}")
72
73 case { "@func": str(function_name) }:
74 args: list[JsonValue]
75 match expr:
76 case { "@args": None }:
77 args = []
78 case { "@args": list() }:
79 args = cast(list[JsonValue], expr["@args"]) # TODO
80 case { "@args": _ }:
81 raise ValueError("Given an invalid value for '@args'.")
82 case _:
83 args = []
84
85 return await onCall(function_name, await evaluate_array(args))
86
87 case list(array_expression_elements):
88 return await evaluate_array(array_expression_elements)
89
90 case _:
91 raise ValueError("This condition should never hit")
92
93 for step in program["@steps"]:
94 results.append(await evaluate_expression(cast(JsonValue, step)))

Callers 2

evaluate_arrayFunction · 0.85
evaluate_json_programFunction · 0.85

Calls 1

evaluate_arrayFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…