Skip to content
This repository was archived by the owner on Sep 11, 2025. It is now read-only.

Commit 452b9be

Browse files
.
1 parent 8c00244 commit 452b9be

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

sdk/assemblyscript/examples/textgeneration/assembly/toolcalling.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,20 +89,24 @@ export function generateTextWithTools(prompt: string): string {
8989
// or we can end the conversation by returning error directly or throwing an exception.
9090
//
9191
// NOTE: A future release of Modus may simplify this process.
92+
let toolMsg: ToolMessage<string>;
9293
const fnName = tc.function.name;
9394
if (fnName === "getCurrentTime") {
9495
const args = JSON.parse<Map<string, string>>(tc.function.arguments);
9596
const result = getCurrentTime(args.get("tz"));
96-
input.messages.push(new ToolMessage(result, tc.id));
97+
toolMsg = new ToolMessage(result, tc.id);
9798
} else if (fnName === "getUserTimeZone") {
9899
const timeZone = getUserTimeZone();
99-
input.messages.push(new ToolMessage(timeZone, tc.id));
100+
toolMsg = new ToolMessage(timeZone, tc.id);
100101
} else if (fnName === "getCurrentTimeInUserTimeZone") {
101102
const result = getCurrentTimeInUserTimeZone();
102-
input.messages.push(new ToolMessage(result, tc.id));
103+
toolMsg = new ToolMessage(result, tc.id);
103104
} else {
104105
throw new Error(`Unknown tool call: ${tc.function.name}`);
105106
}
107+
108+
// Add the tool's response to the conversation.
109+
input.messages.push(toolMsg);
106110
}
107111
} else if (msg.content != "") {
108112
// return the model's final response to the user.

sdk/go/examples/textgeneration/toolcalling.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,30 +89,34 @@ func GenerateTextWithTools(prompt string) (string, error) {
8989
// or we can end the conversation by returning the error directly.
9090
//
9191
// NOTE: A future release of Modus may simplify this process.
92+
var toolMsg *openai.ToolMessage[string]
9293
switch tc.Function.Name {
9394

9495
case "getCurrentTime":
9596
tz := gjson.Get(tc.Function.Arguments, "tz").Str
9697
if result, err := getCurrentTime(tz); err == nil {
97-
input.Messages = append(input.Messages, openai.NewToolMessage(result, tc.Id))
98+
toolMsg = openai.NewToolMessage(result, tc.Id)
9899
} else {
99-
input.Messages = append(input.Messages, openai.NewToolMessage(err, tc.Id))
100+
toolMsg = openai.NewToolMessage(err, tc.Id)
100101
}
101102

102103
case "getUserTimeZone":
103104
timeZone := getUserTimeZone()
104-
input.Messages = append(input.Messages, openai.NewToolMessage(timeZone, tc.Id))
105+
toolMsg = openai.NewToolMessage(timeZone, tc.Id)
105106

106107
case "getCurrentTimeInUserTimeZone":
107108
if result, err := getCurrentTimeInUserTimeZone(); err == nil {
108-
input.Messages = append(input.Messages, openai.NewToolMessage(result, tc.Id))
109+
toolMsg = openai.NewToolMessage(result, tc.Id)
109110
} else {
110-
input.Messages = append(input.Messages, openai.NewToolMessage(err, tc.Id))
111+
toolMsg = openai.NewToolMessage(err, tc.Id)
111112
}
112113

113114
default:
114115
return "", fmt.Errorf("Unknown tool call: %s", tc.Function.Name)
115116
}
117+
118+
// Add the tool's response to the conversation.
119+
input.Messages = append(input.Messages, toolMsg)
116120
}
117121

118122
} else if msg.Content != "" {

0 commit comments

Comments
 (0)