Skip to content

Commit

Permalink
Save details about the example output to the file
Browse files Browse the repository at this point in the history
  • Loading branch information
scosman committed Sep 30, 2024
1 parent 6e94cb0 commit 50b7a1a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
21 changes: 20 additions & 1 deletion libs/core/kiln_ai/adapters/base_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class AdapterInfo:
adapter_name: str
model_name: str
model_provider: str
prompt_builder_name: str


class BaseAdapter(metaclass=ABCMeta):
Expand Down Expand Up @@ -121,12 +122,30 @@ def save_example(
example_output = ExampleOutput(
parent=example,
output=output_str,
# Synthetic since an adapter, not a human, is creating this
source=ExampleOutputSource.synthetic,
source_properties={"creator": Config.shared().user_id},
source_properties=self._properties_for_example_output(),
)
example_output.save_to_file()
return example

def _properties_for_example_output(self) -> Dict:
props = {}

# creator user id
creator = Config.shared().user_id
if creator and creator != "":
props["creator"] = creator

# adapter info
adapter_info = self.adapter_info()
props["adapter_name"] = adapter_info.adapter_name
props["model_name"] = adapter_info.model_name
props["model_provider"] = adapter_info.model_provider
props["prompt_builder_name"] = adapter_info.prompt_builder_name

return props


class BasePromptBuilder(metaclass=ABCMeta):
def __init__(self, task: Task, adapter: BaseAdapter | None = None):
Expand Down
1 change: 1 addition & 0 deletions libs/core/kiln_ai/adapters/langchain_adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ def adapter_info(self) -> AdapterInfo:
model_name=self.model_name,
model_provider=self.model_provider,
adapter_name="kiln_langchain_adapter",
prompt_builder_name=self.prompt_builder.prompt_builder_name(),
)

def _munge_response(self, response: Dict) -> Dict:
Expand Down
5 changes: 5 additions & 0 deletions libs/core/kiln_ai/adapters/test_prompt_adaptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,8 @@ async def run_simple_task(task: datamodel.Task, model_name: str, provider: str):
"You should answer the following question: four plus six times 10"
)
assert "64" in answer
model_info = adapter.adapter_info()
assert model_info.model_name == model_name
assert model_info.model_provider == provider
assert model_info.adapter_name == "kiln_langchain_adapter"
assert model_info.prompt_builder_name == "SimplePromptBuilder"
20 changes: 18 additions & 2 deletions libs/core/kiln_ai/adapters/test_saving_adapter_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def adapter_info(self) -> AdapterInfo:
adapter_name="mock_adapter",
model_name="mock_model",
model_provider="mock_provider",
prompt_builder_name="mock_prompt_builder",
)


Expand Down Expand Up @@ -51,7 +52,6 @@ def test_save_example_isolation(test_task):
assert saved_output.parent.id == example.id
assert saved_output.output == output_data
assert saved_output.source == ExampleOutputSource.synthetic
assert saved_output.source_properties == {"creator": Config.shared().user_id}
assert saved_output.requirement_ratings == {}

# Verify that the data can be read back from disk
Expand All @@ -68,8 +68,19 @@ def test_save_example_isolation(test_task):
assert reloaded_output.parent.id == reloaded_example.id
assert reloaded_output.output == output_data
assert reloaded_output.source == ExampleOutputSource.synthetic
assert reloaded_output.source_properties == {"creator": Config.shared().user_id}
assert reloaded_output.requirement_ratings == {}
assert reloaded_output.source_properties["adapter_name"] == "mock_adapter"
assert reloaded_output.source_properties["model_name"] == "mock_model"
assert reloaded_output.source_properties["model_provider"] == "mock_provider"
assert (
reloaded_output.source_properties["prompt_builder_name"]
== "mock_prompt_builder"
)
creator = Config.shared().user_id
if creator and creator != "":
assert reloaded_output.source_properties["creator"] == creator
else:
assert "creator" not in reloaded_output.source_properties

# Run again, with same input and different output. Should create a new ExampleOutput under the same Example.
example = adapter.save_example(input_data, ExampleSource.human, "Different output")
Expand Down Expand Up @@ -132,3 +143,8 @@ async def test_autosave_true(test_task):
assert examples[0].outputs()[0].output == "Test output"
assert examples[0].outputs()[0].source_properties["creator"] == "test_user"
assert examples[0].outputs()[0].source == ExampleOutputSource.synthetic
output = examples[0].outputs()[0]
assert output.source_properties["adapter_name"] == "mock_adapter"
assert output.source_properties["model_name"] == "mock_model"
assert output.source_properties["model_provider"] == "mock_provider"
assert output.source_properties["prompt_builder_name"] == "mock_prompt_builder"
6 changes: 6 additions & 0 deletions libs/core/kiln_ai/adapters/test_structured_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def adapter_info(self) -> AdapterInfo:
adapter_name="mock_adapter",
model_name="mock_model",
model_provider="mock_provider",
prompt_builder_name="mock_prompt_builder",
)


Expand Down Expand Up @@ -189,6 +190,11 @@ async def run_structured_input_test(tmp_path: Path, model_name: str, provider: s
assert response is not None
assert isinstance(response, str)
assert "[[equilateral]]" in response
adapter_info = a.adapter_info()
assert adapter_info.prompt_builder_name == "SimplePromptBuilder"
assert adapter_info.model_name == model_name
assert adapter_info.model_provider == provider
assert adapter_info.adapter_name == "kiln_langchain_adapter"


@pytest.mark.paid
Expand Down

0 comments on commit 50b7a1a

Please sign in to comment.