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

Handle functions with *args and **kwargs #106

Merged
merged 7 commits into from
Feb 18, 2024

Conversation

jackmpcollins
Copy link
Owner

@jackmpcollins jackmpcollins commented Feb 14, 2024

Handle functions with *args and **kwargs by serializing these as normal params (list/array for args, dict/object for kwargs), and using the function signature to parse these correctly into the FunctionCall when returned by the LLM.

Fixes #102

@jackmpcollins
Copy link
Owner Author

It takes a little bit of convincing but this appears to work as expected.

from magentic import prompt_chain


def do_something(x: str, y: str, **kwargs: int) -> str:
    print(x)
    print(y)
    print(kwargs)
    return f"you provided {x=}, {y=}, and {kwargs=}!"


@prompt_chain(
    "Call the do_something function passing 'hello' and 'world' for x and y, and also additional kwargs {{a: 1 b: 2 c:3}}",
    functions=[do_something],
)
def chain() -> str:
   ...


out = chain()
print("---")
print(out)

output:

hello
world
{'a': 1, 'b': 2, 'c': 3}
---
The function `do_something` has been called with the arguments x='hello', y='world', and kwargs={'a': 1, 'b': 2, 'c': 3} successfully.

@jackmpcollins jackmpcollins marked this pull request as ready for review February 18, 2024 04:27
@jackmpcollins jackmpcollins merged commit ac6c60b into main Feb 18, 2024
1 check passed
@jackmpcollins jackmpcollins deleted the fix-functions-with-args-kwargs branch February 18, 2024 04:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Issues with **kwargs keywords in functions used as tools
1 participant