Skip to content

Commit 494a39b

Browse files
authored
Merge pull request #453 from StrongMind/main
Update function calling section of README
2 parents 7035674 + fb1b93a commit 494a39b

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
@@ -371,9 +371,10 @@ You can stream it as well!
371371

372372
### Functions
373373

374-
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:
374+
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)).
375375

376376
```ruby
377+
377378
def get_current_weather(location:, unit: "fahrenheit")
378379
# use a weather api to fetch weather
379380
end
@@ -411,16 +412,22 @@ response =
411412
},
412413
}
413414
],
415+
tool_choice: {
416+
type: "function",
417+
function: {
418+
name: "get_current_weather"
419+
}
420+
}
414421
},
415422
)
416423

417424
message = response.dig("choices", 0, "message")
418425

419426
if message["role"] == "assistant" && message["tool_calls"]
420-
function_name = message.dig("tool_calls", "function", "name")
427+
function_name = message.dig("tool_calls", 0, "function", "name")
421428
args =
422429
JSON.parse(
423-
message.dig("tool_calls", "function", "arguments"),
430+
message.dig("tool_calls", 0, "function", "arguments"),
424431
{ symbolize_names: true },
425432
)
426433

0 commit comments

Comments
 (0)