Skip to content

Commit 482f667

Browse files
Copilotcomfuture
andcommitted
Restore basic usage instructions to README while maintaining examples organization
Co-authored-by: comfuture <[email protected]>
1 parent a32fbc7 commit 482f667

File tree

1 file changed

+86
-33
lines changed

1 file changed

+86
-33
lines changed

README.md

Lines changed: 86 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,54 +18,107 @@ pip install function-schema
1818

1919
## Usage
2020

21-
See the [examples directory](./examples/) for comprehensive usage examples with different AI platforms:
22-
23-
- **[Basic Usage](./examples/basic_usage.py)** - Core features and function definition patterns
24-
- **[OpenAI Integration](./examples/openai_example.py)** - Assistant API and Chat Completion examples
25-
- **[Claude Integration](./examples/claude_example.py)** - Anthropic Claude tool calling examples
26-
- **[MCP Integration](./examples/mcp_example.py)** - Model Context Protocol examples
27-
- **[CLI Usage](./examples/cli_example.py)** - Command-line interface examples
28-
29-
### Quick Start
30-
3121
```python
32-
from typing import Annotated
33-
from function_schema import Doc, get_function_schema
34-
35-
def get_weather(city: Annotated[str, Doc("The city to get the weather for")]) -> str:
22+
from typing import Annotated, Optional
23+
from function_schema import Doc
24+
import enum
25+
26+
def get_weather(
27+
city: Annotated[str, Doc("The city to get the weather for")],
28+
unit: Annotated[
29+
Optional[str],
30+
Doc("The unit to return the temperature in"),
31+
enum.Enum("Unit", "celcius fahrenheit")
32+
] = "celcius",
33+
) -> str:
3634
"""Returns the weather for the given city."""
3735
return f"Weather for {city} is 20°C"
36+
```
37+
38+
Function description is taken from the docstring.
39+
Type hinting with `typing.Annotated` for annotate additional information about the parameters and return type.
40+
41+
Then you can generate a schema for this function:
42+
```python
43+
import json
44+
from function_schema import get_function_schema
3845

39-
# Generate schema
4046
schema = get_function_schema(get_weather)
47+
print(json.dumps(schema, indent=2))
48+
```
49+
50+
Will output:
51+
52+
```json
53+
{
54+
"name": "get_weather",
55+
"description": "Returns the weather for the given city.",
56+
"parameters": {
57+
"type": "object",
58+
"properties": {
59+
"city": {
60+
"type": "string",
61+
"description": "The city to get the weather for"
62+
},
63+
"unit": {
64+
"type": "string",
65+
"description": "The unit to return the temperature in",
66+
"enum": [
67+
"celcius",
68+
"fahrenheit"
69+
],
70+
"default": "celcius"
71+
}
72+
},
73+
}
74+
"required": [
75+
"city"
76+
]
77+
}
4178
```
4279

43-
### Key Features
80+
For claude, you should pass 2nd argument as SchemaFormat.claude or `claude`:
4481

45-
- **Type Annotations**: Use `typing.Annotated` with `Doc` metadata for parameter descriptions
46-
- **Multiple Formats**: Support for OpenAI (`"openai"`) and Claude (`"claude"`) schema formats
47-
- **Enum Support**: Use `enum.Enum` or `typing.Literal` for parameter constraints
48-
- **CLI Tool**: Generate schemas from command line using `function_schema`
82+
```python
83+
from function_schema import get_function_schema
84+
85+
schema = get_function_schema(get_weather, "claude")
86+
```
87+
88+
Please refer to the [Claude tool use](https://docs.anthropic.com/claude/docs/tool-use) documentation for more information.
4989

50-
For detailed examples and advanced usage patterns, see the [examples directory](./examples/).
90+
You can use any type hinting supported by python for the first argument of `Annotated`. including:
91+
`typing.Literal`, `typing.Optional`, `typing.Union`, and `T | None` for python 3.10+.
92+
`Doc` class or plain string in `Annotated` is used for describe the parameter.
5193

52-
### Platform Integration
94+
Enumeratable 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:
5396

54-
#### OpenAI API
55-
For detailed OpenAI integration examples including Assistant API and Chat Completion, see [examples/openai_example.py](./examples/openai_example.py).
97+
```python
98+
from typing import Annotated, Literal
5699

57-
#### Anthropic Claude
58-
For Claude tool calling examples and multi-turn conversations, see [examples/claude_example.py](./examples/claude_example.py).
100+
def get_animal(
101+
animal: Annotated[Literal["dog", "cat"], Doc("The animal to get")],
102+
) -> str:
103+
"""Returns the animal."""
104+
return f"Animal is {animal}"
105+
```
59106

60-
#### Model Context Protocol (MCP)
61-
For MCP server and tool integration examples, see [examples/mcp_example.py](./examples/mcp_example.py).
107+
### CLI usage
62108

63-
#### CLI Usage
64-
Generate schemas from command line:
65-
```bash
66-
function_schema examples/cli_example.py get_weather | jq
109+
```sh
110+
function_schema mymodule.py my_function | jq
67111
```
68-
For more CLI examples, see [examples/cli_example.py](./examples/cli_example.py).
112+
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
69122

70123
## License
71124
MIT License

0 commit comments

Comments
 (0)