Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with classifying complex labels #276

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/controlflow/tasks/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,21 +535,26 @@ def create_success_tool(self) -> Tool:
result_schema = None

# if the result_type is a tuple of options, then we want the LLM to provide
# a single integer index instead of writing out the entire option
# a single integer index instead of writing out the entire option. Therefore
# we create a tool that describes a series of options and accepts the index
# as a result.
if isinstance(self.result_type, tuple):
result_schema = int
options = {}
serialized_options = {}
for i, option in enumerate(self.result_type):
options[i] = option
try:
serialized = TypeAdapter(type(option)).dump_python(option)
except PydanticSchemaGenerationError:
serialized = repr(option)
options[i] = serialized
serialized_options[i] = serialized
options_str = "\n\n".join(
f"Option {i}: {option}" for i, option in options.items()
f"Option {i}: {option}" for i, option in serialized_options.items()
)
instructions = f"""
Provide a single integer as the result, corresponding to the index
of your chosen option. You options are: {options_str}
of your chosen option. Your options are: {options_str}
"""

# otherwise try to load the schema for the result type
Expand Down
20 changes: 20 additions & 0 deletions tests/tasks/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,26 @@ def test_success_tool_with_list_of_options_requires_int(self):
with pytest.raises(ValueError):
tool.run(input=dict(result="good"))

def test_tuple_of_ints_result(self):
task = Task("choose 5", result_type=(4, 5, 6))
tool = task.create_success_tool()
tool.run(input=dict(result=1))
assert task.result == 5

def test_tuple_of_pydantic_models_result(self):
class Person(BaseModel):
name: str
age: int

task = Task(
"Who is the oldest?",
result_type=(Person(name="Alice", age=30), Person(name="Bob", age=35)),
)
tool = task.create_success_tool()
tool.run(input=dict(result=1))
assert task.result == Person(name="Bob", age=35)
assert isinstance(task.result, Person)


class TestRun:
@pytest.mark.parametrize(
Expand Down