MadcowD / ell

A language model programming library.
http://docs.ell.so/
MIT License
5.35k stars 315 forks source link

Function `call_tools_and_collect_as_message().text` doesn't return the tool call result text #201

Closed devmao closed 2 months ago

devmao commented 2 months ago

Description

Running this "tool usage" example from the doc doesn't output the tool result as expected:

Sure, I can help you plan your trip to Paris! Here are a few things we can look into:

1. **Weather:** Checking the current weather in Paris to pack accordingly.
2. **Sights and Activities:** Recommendations for must-see attractions and activities.
3. **Accommodation:** Suggestions for places to stay according to your preferences.
4. **Transportation:** Guide on how to get around the city.

Let's start by checking the current weather in Paris. One moment please.
<tool_call>
Weather info: <tool_result>

Moreover, when a tool uses a Field to provide an argument description, the lexical closure fails.

Exception: Failed to capture the lexical closure of default parameter tools. Error: Failed to capture the lexical closure of default parameter None. Error: Failed to capture the lexical closure of default parameter location. Error: invalid syntax (<unknown>, line 3)
Recursion stack: travel_planner -> get_weather
Recursion stack: travel_planner
Recursion stack: travel_plannerCell Execution Error

Env

MadcowD commented 2 months ago

Thanks! Gonna take a look. Are you running this in a jupyter notebook or just normal py?

devmao commented 2 months ago

Thanks! Gonna take a look. Are you running this in a jupyter notebook or just normal py?

jupyter

tomviner commented 2 months ago

To get the text out you need the last line of the example to be:

- print("Weather info:", tool_results.text)
+ print("Weather info:", tool_results.tool_results[0].result[0].text)
devmao commented 2 months ago

To get the text out you need the last line of the example to be:

- print("Weather info:", tool_results.text)
+ print("Weather info:", tool_results.tool_results[0].result[0].text)

I know, but it seems to me not ideal based on the function call_tools_and_collect_as_message() intent.

MadcowD commented 2 months ago

Yeah I agree this is not great :)

MadcowD commented 2 months ago

Would this be better?

    print("Weather info:", message.tool_results[0].text)
MadcowD commented 2 months ago

Would you rather it look like:

│   assistant: Certainly, I'll file the claim draft for you with the information we have.
│              I'll use today's date and make some assumptions based on what you've told
│              me, but please note that you may need to provide more details later in the
│              process.
│              I'll go ahead and create the claim draft now:
│              ToolCall(tool=<function create_claim_draft at 0x106ecc4a0>,
│              tool_call_id='toolu_015R3MrU1RpnRLfLrpApZL4L',
│              params=create_claim_draft(claim_details='Car was smashed by someone else.
│              Further details pending investigation.', claim_type='Collision',
│              claim_amount=5000.0, claim_date='2023-05-24'))
│
│        user: ToolResult(tool_call_id='toolu_015R3MrU1RpnRLfLrpApZL4L',
│              result=[ContentBlock(text=claim_id-123234)])
│   assistant: Thank you for providing that information. I understand that your car was
│              smashed by someone else today, and you estimate the damage to be around
│              $5,000. Let's create a claim draft based on this information. I'll need
│              to use today's date for the claim.
│              I'm going to create a claim draft for you now using the information
│              you've provided. Here's what I'm going to do:
│              ToolCall(tool=<function create_claim_draft at 0x1056ac4a0>,
│              tool_call_id='toolu_01Vxzv7sS5zwokJFGTFk6cGE',
│              params=create_claim_draft(claim_details='Car was smashed by another
│              vehicle. Damage estimated at $5,000.', claim_type='Collision',
│              claim_amount=5000.0, claim_date='2023-05-30'))
│
│        user: claim_id-123234

for


@ell.tool()
def create_claim_draft(claim_details: str, claim_type: str, claim_amount: float, 
                       claim_date : str = Field(description="The date of the claim in the format YYYY-MM-DD.")):
    """Create a claim draft. Returns the claim id created."""
    print("Create claim draft", claim_details, claim_type, claim_amount, claim_date)
    return "claim_id-123234"

@ell.tool()
def approve_claim(claim_id : str):
    """Approve a claim"""
    return "approved"

@ell.complex(model="claude-3-5-sonnet-20240620", tools=[create_claim_draft, approve_claim], temperature=0.1, max_tokens=400)
def insurance_claim_chatbot(message_history: List[Message]) -> List[Message]:
    return [
        ell.system( """You are a an insurance adjuster AI. You are given a dialogue with a user and have access to various tools to effectuate the insurance claim adjustment process. Ask question until you have enough information to create a claim draft. Then ask for approval."""),
    ] + message_history

I feel like you'd miss that "claim_id-123234" is a tool result on first glance.

MadcowD commented 2 months ago

we could make the repr readable also

│   assistant: Thank you for providing that information. I understand that your car was
│              smashed by someone else today, and you estimate the damage to be around
│              $5,000. Let's create a claim draft based on this information.
│              First, I'll need to use today's date for the claim. I'll proceed with
│              creating a claim draft using the information you've provided.
│              ToolCall(create_claim_draft(claim_details='Car was smashed by another
│              party. Estimated damage: $5,000.' claim_type='Auto Collision'
│              claim_amount=5000.0 claim_date='2023-05-30'),
│              tool_call_id=toolu_01G76pfqEpBGgxQx7Gi3yL1z)
│
│        user: ToolResult(tool_call_id=toolu_01G76pfqEpBGgxQx7Gi3yL1z,
│              result=claim_id-123234)
MadcowD commented 2 months ago

See: https://github.com/MadcowD/ell/pull/239 for what I mean

MadcowD commented 2 months ago

fuckit we're goign to go with the readable repr for now since we're pre 0.1.0 and i'm fine for breaking changes here until we resolve this conversation cc @alex-dixon