autogen/test/test_pydantic.py
Davor Runje 4b5ec5a52f
Add decorator for function calling (#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>
2023-12-25 16:07:20 +00:00

42 lines
1.5 KiB
Python

from typing import Dict, List, Optional, Tuple, Union
from pydantic import BaseModel, Field
from typing_extensions import Annotated
from autogen._pydantic import model_dump, model_dump_json, type2schema
def test_type2schema() -> None:
assert type2schema(str) == {"type": "string"}
assert type2schema(int) == {"type": "integer"}
assert type2schema(float) == {"type": "number"}
assert type2schema(bool) == {"type": "boolean"}
assert type2schema(None) == {"type": "null"}
assert type2schema(Optional[int]) == {"anyOf": [{"type": "integer"}, {"type": "null"}]}
assert type2schema(List[int]) == {"items": {"type": "integer"}, "type": "array"}
assert type2schema(Tuple[int, float, str]) == {
"maxItems": 3,
"minItems": 3,
"prefixItems": [{"type": "integer"}, {"type": "number"}, {"type": "string"}],
"type": "array",
}
assert type2schema(Dict[str, int]) == {"additionalProperties": {"type": "integer"}, "type": "object"}
assert type2schema(Annotated[str, "some text"]) == {"type": "string"}
assert type2schema(Union[int, float]) == {"anyOf": [{"type": "integer"}, {"type": "number"}]}
def test_model_dump() -> None:
class A(BaseModel):
a: str
b: int = 2
assert model_dump(A(a="aaa")) == {"a": "aaa", "b": 2}
def test_model_dump_json() -> None:
class A(BaseModel):
a: str
b: int = 2
assert model_dump_json(A(a="aaa")).replace(" ", "") == '{"a":"aaa","b":2}'