Skip to content

Commit

Permalink
Add a mock runtime and a mock test for counter
Browse files Browse the repository at this point in the history
  • Loading branch information
klntsky committed Nov 23, 2024
1 parent 836defb commit 3db02ad
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
2 changes: 1 addition & 1 deletion python/src/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def extract_parameter_set(ast):
if ast["name"] != "MODEL":
res.use_var(ast["name"])
elif ast["type"] == "use":
for _, expr in ast["exprs"]:
for _, expr in ast["parameters"].items():
res = res.then(extract_parameter_set(expr))
elif ast["type"] == "assign":
if ast["required"]:
Expand Down
31 changes: 31 additions & 0 deletions python/src/runtimes/mock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from runtime import BaseRuntime
from provider import BaseLLMProvider
from parse_metaprompt import parse_metaprompt

from typing import AsyncGenerator, List
import os

class MockRuntime(BaseRuntime):
def __init__(self, modules):
# TODO: implement relative module loading in the mock
self.modules = modules

def set_status(self, status: str):
self.status = status

def load_module(self, module_name: str):
if module_name in self.modules:
return parse_metaprompt(self.modules[module_name])

raise ImportError(
f"Module {module_name} not found"
)

def print_chunk(self, chunk: str):
pass

def input(self, prompt):
pass # TODO: implement

def finalize(self):
pass
45 changes: 45 additions & 0 deletions python/tests/test_eval.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from metaprompt import metaprompt
from config import Config
from providers.mock import MockProvider
from runtimes.mock import MockRuntime

import pytest
import re
Expand Down Expand Up @@ -185,3 +186,47 @@ async def test_chat_history():
chat2: eat an apple
"""
assert result.strip() == expected.strip()


@pytest.mark.asyncio
async def test_counter():
prompt = """
counter is: [:number]
[:if [:number] is zero or less
:then done!
:else [:use ./counter
:number=[$ subtract one from [:number].
just give me the resulting number, no other output]]
]"""
result = await metaprompt(
prompt,
Config(
providers=MockProvider(
rules=[
({"chat": r"3 is zero"}, "false"),
({"chat": r"subtract one from 3"}, "2"),
({"chat": r"2 is zero"}, "false"),
({"chat": r"subtract one from 2"}, "1"),
({"chat": r"1 is zero"}, "false"),
({"chat": r"subtract one from 1"}, "0"),
({"chat": r"0 is zero"}, "true"),
]
),
parameters={"number": "3"},
),
runtime=MockRuntime(
modules={"./counter": prompt},
),
)

expected = """
counter is: 3
counter is: 2
counter is: 1
counter is: 0
done!
"""
assert result.strip() == expected.strip()

0 comments on commit 3db02ad

Please sign in to comment.