Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjohnsonpint committed Jan 16, 2025
1 parent 8c00244 commit 452b9be
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,24 @@ export function generateTextWithTools(prompt: string): string {
// or we can end the conversation by returning error directly or throwing an exception.
//
// NOTE: A future release of Modus may simplify this process.
let toolMsg: ToolMessage<string>;
const fnName = tc.function.name;
if (fnName === "getCurrentTime") {
const args = JSON.parse<Map<string, string>>(tc.function.arguments);
const result = getCurrentTime(args.get("tz"));
input.messages.push(new ToolMessage(result, tc.id));
toolMsg = new ToolMessage(result, tc.id);
} else if (fnName === "getUserTimeZone") {
const timeZone = getUserTimeZone();
input.messages.push(new ToolMessage(timeZone, tc.id));
toolMsg = new ToolMessage(timeZone, tc.id);
} else if (fnName === "getCurrentTimeInUserTimeZone") {
const result = getCurrentTimeInUserTimeZone();
input.messages.push(new ToolMessage(result, tc.id));
toolMsg = new ToolMessage(result, tc.id);
} else {
throw new Error(`Unknown tool call: ${tc.function.name}`);
}

// Add the tool's response to the conversation.
input.messages.push(toolMsg);
}
} else if (msg.content != "") {
// return the model's final response to the user.
Expand Down
14 changes: 9 additions & 5 deletions sdk/go/examples/textgeneration/toolcalling.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,30 +89,34 @@ func GenerateTextWithTools(prompt string) (string, error) {
// or we can end the conversation by returning the error directly.
//
// NOTE: A future release of Modus may simplify this process.
var toolMsg *openai.ToolMessage[string]
switch tc.Function.Name {

case "getCurrentTime":
tz := gjson.Get(tc.Function.Arguments, "tz").Str
if result, err := getCurrentTime(tz); err == nil {
input.Messages = append(input.Messages, openai.NewToolMessage(result, tc.Id))
toolMsg = openai.NewToolMessage(result, tc.Id)
} else {
input.Messages = append(input.Messages, openai.NewToolMessage(err, tc.Id))
toolMsg = openai.NewToolMessage(err, tc.Id)
}

case "getUserTimeZone":
timeZone := getUserTimeZone()
input.Messages = append(input.Messages, openai.NewToolMessage(timeZone, tc.Id))
toolMsg = openai.NewToolMessage(timeZone, tc.Id)

case "getCurrentTimeInUserTimeZone":
if result, err := getCurrentTimeInUserTimeZone(); err == nil {
input.Messages = append(input.Messages, openai.NewToolMessage(result, tc.Id))
toolMsg = openai.NewToolMessage(result, tc.Id)
} else {
input.Messages = append(input.Messages, openai.NewToolMessage(err, tc.Id))
toolMsg = openai.NewToolMessage(err, tc.Id)
}

default:
return "", fmt.Errorf("Unknown tool call: %s", tc.Function.Name)
}

// Add the tool's response to the conversation.
input.Messages = append(input.Messages, toolMsg)
}

} else if msg.Content != "" {
Expand Down

0 comments on commit 452b9be

Please sign in to comment.