Skip to content

Commit e97ce15

Browse files
authored
Re-use the original messages
1 parent 2735bd5 commit e97ce15

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

README.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -405,16 +405,18 @@ def get_current_weather(location:, unit: "fahrenheit")
405405
# use a weather api to fetch weather
406406
end
407407

408+
messages = [
409+
{
410+
"role": "user",
411+
"content": "What is the weather like in San Francisco?",
412+
},
413+
]
414+
408415
response =
409416
client.chat(
410417
parameters: {
411418
model: "gpt-4o",
412-
messages: [
413-
{
414-
"role": "user",
415-
"content": "What is the weather like in San Francisco?",
416-
},
417-
],
419+
messages: messages, # Defined above because we'll use it again
418420
tools: [
419421
{
420422
type: "function",
@@ -444,7 +446,6 @@ response =
444446
)
445447

446448
message = response.dig("choices", 0, "message")
447-
function_messages = []
448449

449450
if message["role"] == "assistant" && message["tool_calls"]
450451
messages["tool_calls"].each do |tool_calls|
@@ -461,18 +462,18 @@ if message["role"] == "assistant" && message["tool_calls"]
461462
# decide how to handle
462463
end
463464

464-
function_messages << [{
465+
messages << [{
465466
tool_call_id: tool_call_id,
466467
role: "tool",
467468
name: function_name,
468469
content: function_response
469-
}]
470+
}] # Extend the conversation with the results of the functions
470471
end
471472

472473
second_response = client.chat(
473474
parameters: {
474475
model: "gpt-4o",
475-
messages: function_messages
476+
messages: messages
476477
})
477478

478479
# At this point, the model has decided to call functions, you've called the functions

0 commit comments

Comments
 (0)