Skip to content

Commit

Permalink
Refactor adapter instructions into abstract method
Browse files Browse the repository at this point in the history
  • Loading branch information
scosman committed Sep 9, 2024
1 parent 9e9cca9 commit e60fbcc
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
7 changes: 3 additions & 4 deletions libs/core/kiln_ai/adapters/base_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ async def invoke(self, input: str) -> Dict | str:
async def _run(self, input: str) -> Dict | str:
pass

# override for provider specific instructions (e.g. json format instead of tool call)
# adapter specific instructions (e.g. tool calling, json format, etc)
@abstractmethod
def adapter_specific_instructions(self) -> str | None:
if self._is_structured:
return "Always respond with a tool call. Never respond with a human readable message."
return None
pass


class BasePromptBuilder(metaclass=ABCMeta):
Expand Down
6 changes: 6 additions & 0 deletions libs/core/kiln_ai/adapters/langchain_adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ def __init__(
prompt_builder.adapter = self
self.prompt_builder = prompt_builder

def adapter_specific_instructions(self) -> str | None:
# TODO: would be better to explicitly use bind_tools:tool_choice="task_response" here
if self._is_structured:
return "Always respond with a tool call. Never respond with a human readable message."
return None

async def _run(self, input: str) -> Dict | str:
# TODO cleanup
prompt = self.prompt_builder.build_prompt(input)
Expand Down
3 changes: 3 additions & 0 deletions libs/core/kiln_ai/adapters/test_structured_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ def __init__(self, kiln_task: models.Task, response: Dict | str | None):
async def _run(self, input: str) -> Dict | str:
return self.response

def adapter_specific_instructions(self) -> str | None:
return None


async def test_mock_unstructred_response(tmp_path):
task = build_structured_output_test_task(tmp_path)
Expand Down

0 comments on commit e60fbcc

Please sign in to comment.