MadcowD / ell

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

@ell.tool() failed to capture the lexical closure #255

Open femto opened 2 days ago

femto commented 2 days ago
During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/femtozheng/python-project/ell/src/ell/lmp/_track.py", line 131, in tracked_func
    ell.util.closure.lexically_closured_source(func_to_track, forced_dependencies)
  File "/Users/femtozheng/python-project/ell/src/ell/util/closure.py", line 405, in lexically_closured_source
    _, fnclosure, uses = lexical_closure(func, initial_call=True, recursion_stack=[], forced_dependencies=forced_dependencies)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/femtozheng/python-project/ell/src/ell/util/closure.py", line 94, in lexical_closure
    _process_signature_dependency(v, dependencies, already_closed, recursion_stack, uses, k)
  File "/Users/femtozheng/python-project/ell/src/ell/util/closure.py", line 191, in _process_signature_dependency
    _raise_error(f"Failed to capture the lexical closure of default parameter {name}", e, recursion_stack)
  File "/Users/femtozheng/python-project/ell/src/ell/util/closure.py", line 311, in _raise_error
    raise Exception(error_msg)
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_planner

while trying out:

from typing import List

from openai import OpenAI

import ell
from pydantic import BaseModel, Field
ell.init(store='./logdir', autocommit=True, verbose=True)
@ell.tool()
def get_weather(location: str = Field(description="The full name of a city and country, e.g. San Francisco, CA, USA")):
    """Get the current weather for a given location."""
    # Simulated weather API call
    return f"The weather in {location} is sunny."

@ell.complex(model="gpt-4-turbo", tools=[get_weather])
def travel_planner(destination: str):
    """Plan a trip based on the destination and current weather."""
    return [
        ell.system("You are a travel planner. Use the weather tool to provide relevant advice."),
        ell.user(f"Plan a trip to {destination}")
    ]

result = travel_planner("Paris")
print(result.text)  # Prints travel advice
if result.tool_calls:
    # This is done so that we can pass the tool calls to the language model
    result_message = result.call_tools_and_collect_as_message()
    print("Weather info:", result_message.tool_results[0].text) # Raw text of the tool call.
    print("Message to be sent to the LLM:", result_message.text) # Representation of the message to be sent to the LLM.

It seemed when I comment out ell.init(store='./logdir', autocommit=True, verbose=True) it is ok, but if ell.init is present then the error is thrown

MadcowD commented 1 day ago

Okay great find! Still refactoring and cleaning this code should have a fix for you tomorrow or something