forked from microsoft/autogen
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add decorator for function calling (microsoft#1018)
* 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 <bnujli@gmail.com> * Update website/docs/Use-Cases/agent_chat.md Co-authored-by: Li Jiang <bnujli@gmail.com> * 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 <ekzhu@users.noreply.github.com> Co-authored-by: Li Jiang <bnujli@gmail.com>
- Loading branch information
1 parent
b1adac5
commit 4b5ec5a
Showing
19 changed files
with
2,164 additions
and
203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
from typing import Any, Dict, Optional, Tuple, Type, Union, get_args | ||
|
||
from pydantic import BaseModel | ||
from pydantic.version import VERSION as PYDANTIC_VERSION | ||
from typing_extensions import get_origin | ||
|
||
__all__ = ("JsonSchemaValue", "model_dump", "model_dump_json", "type2schema") | ||
|
||
PYDANTIC_V1 = PYDANTIC_VERSION.startswith("1.") | ||
|
||
if not PYDANTIC_V1: | ||
from pydantic import TypeAdapter | ||
from pydantic._internal._typing_extra import eval_type_lenient as evaluate_forwardref | ||
from pydantic.json_schema import JsonSchemaValue | ||
|
||
def type2schema(t: Optional[Type]) -> JsonSchemaValue: | ||
"""Convert a type to a JSON schema | ||
Args: | ||
t (Type): The type to convert | ||
Returns: | ||
JsonSchemaValue: The JSON schema | ||
""" | ||
return TypeAdapter(t).json_schema() | ||
|
||
def model_dump(model: BaseModel) -> Dict[str, Any]: | ||
"""Convert a pydantic model to a dict | ||
Args: | ||
model (BaseModel): The model to convert | ||
Returns: | ||
Dict[str, Any]: The dict representation of the model | ||
""" | ||
return model.model_dump() | ||
|
||
def model_dump_json(model: BaseModel) -> str: | ||
"""Convert a pydantic model to a JSON string | ||
Args: | ||
model (BaseModel): The model to convert | ||
Returns: | ||
str: The JSON string representation of the model | ||
""" | ||
return model.model_dump_json() | ||
|
||
|
||
# Remove this once we drop support for pydantic 1.x | ||
else: # pragma: no cover | ||
from pydantic import schema_of | ||
from pydantic.typing import evaluate_forwardref as evaluate_forwardref | ||
|
||
JsonSchemaValue = Dict[str, Any] | ||
|
||
def type2schema(t: Optional[Type]) -> JsonSchemaValue: | ||
"""Convert a type to a JSON schema | ||
Args: | ||
t (Type): The type to convert | ||
Returns: | ||
JsonSchemaValue: The JSON schema | ||
""" | ||
if PYDANTIC_V1: | ||
if t is None: | ||
return {"type": "null"} | ||
elif get_origin(t) is Union: | ||
return {"anyOf": [type2schema(tt) for tt in get_args(t)]} | ||
elif get_origin(t) in [Tuple, tuple]: | ||
prefixItems = [type2schema(tt) for tt in get_args(t)] | ||
return { | ||
"maxItems": len(prefixItems), | ||
"minItems": len(prefixItems), | ||
"prefixItems": prefixItems, | ||
"type": "array", | ||
} | ||
|
||
d = schema_of(t) | ||
if "title" in d: | ||
d.pop("title") | ||
if "description" in d: | ||
d.pop("description") | ||
|
||
return d | ||
|
||
def model_dump(model: BaseModel) -> Dict[str, Any]: | ||
"""Convert a pydantic model to a dict | ||
Args: | ||
model (BaseModel): The model to convert | ||
Returns: | ||
Dict[str, Any]: The dict representation of the model | ||
""" | ||
return model.dict() | ||
|
||
def model_dump_json(model: BaseModel) -> str: | ||
"""Convert a pydantic model to a JSON string | ||
Args: | ||
model (BaseModel): The model to convert | ||
Returns: | ||
str: The JSON string representation of the model | ||
""" | ||
return model.json() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.