@@ -77,7 +77,7 @@ Will output:
7777}
7878```
7979
80- for claude, you should pass 2nd argument as SchemaFormat.claude or ` claude ` :
80+ For claude, you should pass 2nd argument as SchemaFormat.claude or ` claude ` :
8181
8282``` python
8383from function_schema import get_function_schema
@@ -90,114 +90,35 @@ Please refer to the [Claude tool use](https://docs.anthropic.com/claude/docs/too
9090You can use any type hinting supported by python for the first argument of ` Annotated ` . including:
9191` typing.Literal ` , ` typing.Optional ` , ` typing.Union ` , and ` T | None ` for python 3.10+.
9292` Doc ` class or plain string in ` Annotated ` is used for describe the parameter.
93- ` Doc ` metadata is the [ PEP propose] ( https://peps.python.org/pep-0727/ ) for standardizing the metadata in type hints.
94- currently, implemented in ` typing-extensions ` module. Also ` function_schema.Doc ` is provided for compatibility.
9593
9694Enumeratable candidates can be defined with ` enum.Enum ` in the argument of ` Annotated ` .
95+ In shorthand, you can use ` typing.Literal ` as the type will do the same thing:
9796
9897``` python
99- import enum
100-
101- class AnimalType (enum .Enum ):
102- dog = enum.auto()
103- cat = enum.auto()
104-
105- def get_animal (
106- animal : Annotated[str , Doc(" The animal to get" ), AnimalType],
107- ) -> str :
108- """ Returns the animal."""
109- return f " Animal is { animal.value} "
110- ```
111- In this example, each name of ` AnimalType ` enums(` dog ` , ` cat ` ) is used as an enum schema.
112- In shorthand, you can use ` typing.Literal ` as the type will do the same thing.
98+ from typing import Annotated, Literal
11399
114- ``` python
115100def get_animal (
116101 animal : Annotated[Literal[" dog" , " cat" ], Doc(" The animal to get" )],
117102) -> str :
118103 """ Returns the animal."""
119104 return f " Animal is { animal} "
120105```
121106
122-
123- ### Plain String in Annotated
124-
125- The string value of ` Annotated ` is used as a description for convenience.
126-
127- ``` python
128- def get_weather (
129- city : Annotated[str , " The city to get the weather for" ], # <- string value of Annotated is used as a description
130- unit : Annotated[Optional[str ], " The unit to return the temperature in" ] = " celcius" ,
131- ) -> str :
132- """ Returns the weather for the given city."""
133- return f " Weather for { city} is 20°C "
134- ```
135-
136- But this would create a predefined meaning for any plain string inside of ` Annotated ` ,
137- and any tool that was using plain strings in them for any other purpose, which is currently allowed, would now be invalid.
138- Please refer to the [ PEP 0727, Plain String in Annotated] ( https://peps.python.org/pep-0727/#plain-string-in-annotated ) for more information.
139-
140- ### Usage with OpenAI API
141-
142- You can use this schema to make a function call in OpenAI API:
143- ``` python
144- import openai
145- openai.api_key = " sk-..."
146-
147- # Create an assistant with the function
148- assistant = client.beta.assistants.create(
149- instructions = " You are a weather bot. Use the provided functions to answer questions." ,
150- model = " gpt-4-turbo-preview" ,
151- tools = [{
152- " type" : " function" ,
153- " function" : get_function_schema(get_weather),
154- }]
155- )
156-
157- run = client.beta.messages.create(
158- assistant_id = assistant.id,
159- messages = [
160- {" role" : " user" , " content" : " What's the weather like in Seoul?" }
161- ]
162- )
163-
164- # or with chat completion
165-
166- result = openai.chat.completion.create(
167- model = " gpt-3.5-turbo" ,
168- messages = [
169- {" role" : " user" , " content" : " What's the weather like in Seoul?" }
170- ],
171- tools = [{
172- " type" : " function" ,
173- " function" : get_function_schema(get_weather)
174- }],
175- tool_call = " auto" ,
176- )
177- ```
178-
179- ### Usage with Anthropic Claude
180-
181- ``` python
182- import anthropic
183-
184- client = anthropic.Client()
185-
186- response = client.beta.tools.messages.create(
187- model = " claude-3-opus-20240229" ,
188- max_tokens = 4096 ,
189- tools = [get_function_schema(get_weather, " claude" )],
190- messages = [
191- {" role" : " user" , " content" : " What's the weather like in Seoul?" }
192- ]
193- )
194- ```
195-
196107### CLI usage
197108
198109``` sh
199110function_schema mymodule.py my_function | jq
200111```
201112
113+ ### More Examples
114+
115+ For comprehensive usage examples with different AI platforms, see the [ examples directory] ( ./examples/ ) :
116+
117+ - ** [ Basic Usage] ( ./examples/basic_usage.py ) ** - Core features and function definition patterns
118+ - ** [ OpenAI Integration] ( ./examples/openai_example.py ) ** - Assistant API and Chat Completion examples
119+ - ** [ Claude Integration] ( ./examples/claude_example.py ) ** - Anthropic Claude tool calling examples
120+ - ** [ MCP Integration] ( ./examples/mcp_example.py ) ** - Model Context Protocol examples
121+ - ** [ CLI Usage] ( ./examples/cli_example.py ) ** - Command-line interface examples
122+
202123## License
203124MIT License
0 commit comments