-
Notifications
You must be signed in to change notification settings - Fork 732
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
Improve function call (#279) #393
Conversation
…ove_function_call
…ove_function_call fixed some erros in "improve function call"
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.
Awesome! It makes the feature much easier to uses. Thanks a lot. Left some minor suggestions to improve readability of code.
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.
Sorry for the late review. Your code looks awesome and let's wait for @lightaime to have a look. During waiting, could you add an example to show how to add a custom function? I believe it will be useful.
Tests failed because |
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.
the test function is good and comprehensive.
""" | ||
return a * b | ||
|
||
expect_res = json.loads("""{"type": "function", |
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.
JSON could be formatted here.
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.
Good point
Thanks @forever-ly for this very helpful PR!! This is awesome. Also thanks everyone for the review. I will go ahead and merge it. Since it is a huge and helpful PR, please feel free to review again and let me know is there any other issue. |
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.
High-quality code!
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.
clean code and its structure.
Hello, @forever-ly. I seem to have encountered a bug and I'm curious if Then I found the output of But for I suspect that this issue might be due to OpenAI not accepting the "prefixItems" key, which is produced by the get_openai_tool_schema function. I'm not entirely sure if my analysis is correct, and I would greatly appreciate your insights on this matter. |
Description
pydantic
likeinstructor
(close [Feature Request] Improve function calling implementation #279 )Enum
. For example, theget_weather_data
tool may not work as expected since the input arguments liketime_units
should be enum types: #334. The agent may hallucinate an invalid input if enum types are not supported.(close [Feature Request] SupportEnum
type in function calling #360)functions
parameter inclient.chat.completions.create
has been deprecated and replaced bytool_calls
since openai version >= 1.2.3)Motivation and Context
The openai function/tool call feature requires obtaining the
json schema
description of the function/tool. The current implementation in camel involves usingcamel.utils.commons.parse_doc
to parse Google-style docstrings. There are the following issues:functions
parameter inclient.chat.completions.create
has been deprecated and replaced bytool_calls
since openai version >= 1.2.3)The solution is to use
docstring_parser
, which is a library that provides parsing for various docstring styles, including ReST, Google, Numpydoc-style, and Epydoc docstringsparse_doc
gets the type information of parameters by parsing the docstring, so we have to use thejson schema
datatype instead of thepython
datatype when writing docstring, and in addition, data types such asEnum
cannot be supported this way. For example the current docstring must be written asIf we write it in the following form, the parsed types will be
int
andstr
, which is inconsistent with the type of the `json schema that the openai function call acceptsTherefore, after referring to the corresponding implementation in the
llama_index
andinstructor
library, theinspect
module is used to extract the type information from the function signature instead of the docstring, and thenpydantic
is used to complete the conversion ofpython
types tojson schema
types. More complex data types can also be supported in this implementation, such asEnum
, `datatimeIn conclusion, I defined
get_openai_function_schema
andget_openai_tool_schema
to replace theparse_doc
function to:python
parameter types tojson schema
.For example, for the following function
The result of
parse_doc
is:The result of
get_openai_function_schema
is:The result of
get_openai_tool_schema
is:In addition,the function call module needs to provide:
Therefore, I have modified
class OpenFunction
to provide the above functionality:validate_openai_tool_schema
: Validates the format of the tool schema against the json schema specificationget_openai_tool_scheme
;set_openai_tool_schema
get_openai_function_schema
;set_openai_function_schema
get_function_name
;set_function_name
get_function_description
;set_function_description
get_paramter_description
;set_paramter_description
get_parameter
;set_parameter
parameters
Types of changes
What types of changes does your code introduce? Put an
x
in all the boxes that apply:Checklist
Go over all the following points, and put an
x
in all the boxes that apply.If you are unsure about any of these, don't hesitate to ask. We are here to help!