-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
Add decorator for function calling #1018
Add decorator for function calling #1018
Conversation
@microsoft-github-policy-service agree company="AIRT Technologies" |
@microsoft-github-policy-service agree company="AIRT Technologies" |
@davorrunje In the "Why are these changes needed?" section, can you add a description of the expected benefits of this PR? Will it make certain code more readable or easier to write? Also, how much do you anticipate this will increase the learning curve for repo users unfamiliar with python decorators? |
@rickyloynd-microsoft I tried to summarize the discussion we had on Discord (https://discord.com/channels/1153072414184452236/1185807538801873016) in the "Why are these changes needed?" section. Please let me know if more info is needed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This decorator addition is very appealing and the documentation looks good to me now. Thanks @davorrunje!
Thank you :) This is one of my favorite projects and I am glad if I can help. I am not happy with the documentation, i will try to write a standalone page with more examples in a separate PR. |
@davorrunje thank you so much for your contribution! |
Is this feature in 0.2.2 or in the next version? I tried the example in the notebook and it's not working. I am on 0.2.2. Error I'm getting is: Any suggestions? Thanks! |
It will be in the next pip version, not 0.2.2. |
@ekzhu it was a pleasure :) |
* raise content_filter error * import error handling
* add function decorator to converasble agent * polishing * polishing * added function decorator to the notebook with async function calls * added support for return type hint and JSON encoding of returned value if needed * polishing * polishing * refactored async case * Python 3.8 support added * polishing * polishing * missing docs added * refacotring and changes as requested * getLogger * documentation added * test fix * test fix * added testing of agentchat_function_call_currency_calculator.ipynb to test_notebook.py * added support for Pydantic parameters in function decorator * polishing * Update website/docs/Use-Cases/agent_chat.md Co-authored-by: Li Jiang <[email protected]> * Update website/docs/Use-Cases/agent_chat.md Co-authored-by: Li Jiang <[email protected]> * fixes problem with logprob parameter in openai.types.chat.chat_completion.Choice added by openai version 1.5.0 * get 100% code coverage on code added * updated docs * default values added to JSON schema * serialization using json.dump() add for values not string or BaseModel * added limit to openai version because of breaking changes in 1.5.0 * added line-by-line comments in docs to explain the process * polishing --------- Co-authored-by: Eric Zhu <[email protected]> Co-authored-by: Li Jiang <[email protected]>
Why are these changes needed?
Inspired by modern Python frameworks, an alternative method of registering functions using typing annotations and a function decorator could be added to the framework.
The principle can be illustrated in the following modified example in
agentchat_function_call.ipynb
notebook:The
@agent.register_for_llm(...)
function decorator reads the signature of the function and generates the function dictionary and calls agent.update_function_signature()
. The@agent.register_for_execution(...)
function decorator wraps the function so it uses Pydantic.BaseModel.model_dump_json() to serialize output to string if needed and then callsagent.register_function()
with the wrapped function.The main advantage of this approach is the automatic generation of JSON schema from type hints in the signature of a decorated function. It is easy to use and results in much shorter and more understandable code. Alternatively, manually written JSON schemas are prone to errors and difficult to debug. If there is an error in the JSON schema, OpenAI API will silently ignore it and potentially produce incorrect parameters. Such mistakes are easy to make, a developer must remember to use 'string', 'integer', 'number', 'null' instead of appropriate Python base types. Encoding more complex JSON schemas involving tuples, lists, unions etc. is even more difficult. On the other hand, proposed decorators immediately raise an exception with an explanation of why the generation of the JSON schema failed.
Developers today are well versed in using decorators and type hints because of FastAPI and similar frameworks. Moreover, there is a strong preference for such a style as evidenced by the rising popularity of FastAPI and the decline of older Flask. Implementing decorators in the framework requires a somewhat deeper knowledge of Python, but using them feels natural and has proven easy to adopt by a wide range of developers.
Related issue number
Checks