|
7 | 7 | This is a small utility to generate JSON schemas for python functions. |
8 | 8 | With power of type annotations, it is possible to generate a schema for a function without describing it twice. |
9 | 9 |
|
10 | | -At this moment, extracting schema from a function is only useful for [OpenAI API function-call](https://platform.openai.com/docs/guides/gpt/function-calling) feature. |
11 | | -But it can be used for other purposes for example to generate documentation in the future. |
| 10 | +At this moment, extracting schema from a function is useful for [OpenAI Assistant Toll Calling](https://platform.openai.com/docs/assistants/tools/function-calling), [OpenAI API function-call](https://platform.openai.com/docs/guides/function-calling), and [Anthropic Claude Toll calling](https://docs.anthropic.com/claude/docs/tool-use) feature. |
| 11 | +And it can be used for other purposes for example to generate documentation in the future. |
12 | 12 |
|
13 | 13 | ## Installation |
14 | 14 |
|
@@ -79,20 +79,64 @@ Will output: |
79 | 79 | } |
80 | 80 | ``` |
81 | 81 |
|
| 82 | +for claude, you should pass 2nd argument as SchemaFormat.claude or `claude`: |
| 83 | + |
| 84 | +```python |
| 85 | +from function_schema import get_function_schema |
| 86 | + |
| 87 | +schema = get_function_schema(get_weather, "claude") |
| 88 | +``` |
| 89 | + |
| 90 | +Please refer to the [Claude tool use](https://docs.anthropic.com/claude/docs/tool-use) documentation for more information. |
| 91 | + |
82 | 92 | You can use this schema to make a function call in OpenAI API: |
83 | 93 | ```python |
84 | 94 | import openai |
85 | 95 | openai.api_key = "sk-..." |
86 | 96 |
|
87 | | -result = openai.ChatCompletion.create( |
| 97 | +# Create an assistant with the function |
| 98 | +assistant = client.beta.assistants.create( |
| 99 | + instructions="You are a weather bot. Use the provided functions to answer questions.", |
| 100 | + model="gpt-4-turbo-preview", |
| 101 | + tools=[{ |
| 102 | + "type": "function", |
| 103 | + "function": get_function_schema(get_weather), |
| 104 | + }] |
| 105 | +) |
| 106 | + |
| 107 | +run = client.beta.messages.create( |
| 108 | + assistant_id=assistant.id, |
| 109 | + messages=[ |
| 110 | + {"role": "user", "content": "What's the weather like in Seoul?"} |
| 111 | + ] |
| 112 | +) |
| 113 | + |
| 114 | +# or with chat completion |
| 115 | + |
| 116 | +result = openai.chat.completion.create( |
88 | 117 | model="gpt-3.5-turbo", |
89 | 118 | messages=[ |
90 | | - {"role": "user", "content": "What's the weather like in Seoul?"} |
91 | | - ], |
92 | | - functions=[ |
93 | | - get_function_schema(get_weather) |
| 119 | + {"role": "user", "content": "What's the weather like in Seoul?"} |
94 | 120 | ], |
95 | | - function_call="auto", |
| 121 | + tools=[get_function_schema(get_weather)], |
| 122 | + tool_call="auto", |
| 123 | +) |
| 124 | +``` |
| 125 | + |
| 126 | +In claude api, |
| 127 | + |
| 128 | +```python |
| 129 | +import anthropic |
| 130 | + |
| 131 | +client = anthropic.Client() |
| 132 | + |
| 133 | +response = client.beta.tools.messages.create( |
| 134 | + model="claude-3-opus-20240229", |
| 135 | + max_tokens=4096, |
| 136 | + tools=[get_function_schema(get_weather, "claude")], |
| 137 | + messages=[ |
| 138 | + {"role": "user", "content": "What's the weather like in Seoul?"} |
| 139 | + ] |
96 | 140 | ) |
97 | 141 | ``` |
98 | 142 |
|
|
0 commit comments