Skip to content

Commit fb1b93a

Browse files
Update function calling section of README
Co-authored-by: Paul Shippy <paul.shippy@strongmind.com>
1 parent f8a4482 commit fb1b93a

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,9 +338,10 @@ You can stream it as well!
338338

339339
### Functions
340340

341-
You can describe and pass in functions and the model will intelligently choose to output a JSON object containing arguments to call those them. For example, if you want the model to use your method `get_current_weather` to get the current weather in a given location:
341+
You can describe and pass in functions and the model will intelligently choose to output a JSON object containing arguments to call those them. For example, if you want the model to use your method `get_current_weather` to get the current weather in a given location, see the example below. Note that tool_choice is optional, but if you exclude it, the model will choose whether to use the function or not ([see this for more details](https://github.com/openai/openai-cookbook/blob/main/examples/How_to_call_functions_with_chat_models.ipynb)).
342342

343343
```ruby
344+
344345
def get_current_weather(location:, unit: "fahrenheit")
345346
# use a weather api to fetch weather
346347
end
@@ -378,16 +379,22 @@ response =
378379
},
379380
}
380381
],
382+
tool_choice: {
383+
type: "function",
384+
function: {
385+
name: "get_current_weather"
386+
}
387+
}
381388
},
382389
)
383390

384391
message = response.dig("choices", 0, "message")
385392

386393
if message["role"] == "assistant" && message["tool_calls"]
387-
function_name = message.dig("tool_calls", "function", "name")
394+
function_name = message.dig("tool_calls", 0, "function", "name")
388395
args =
389396
JSON.parse(
390-
message.dig("tool_calls", "function", "arguments"),
397+
message.dig("tool_calls", 0, "function", "arguments"),
391398
{ symbolize_names: true },
392399
)
393400

0 commit comments

Comments
 (0)