From 46149e0b677bb34cfd68ea6f0b4c945fd0fa6969 Mon Sep 17 00:00:00 2001 From: Matt Low Date: Sun, 17 Mar 2024 21:59:42 +0000 Subject: [PATCH] Attempt to fix anthropic tool calling Models have been way too eager to use tools when the task does not require it (for example, reading the filesystem in order to show an code example) --- pkg/lmcli/provider/anthropic/anthropic.go | 2 +- pkg/lmcli/provider/anthropic/tools.go | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/pkg/lmcli/provider/anthropic/anthropic.go b/pkg/lmcli/provider/anthropic/anthropic.go index 3908258..cff568c 100644 --- a/pkg/lmcli/provider/anthropic/anthropic.go +++ b/pkg/lmcli/provider/anthropic/anthropic.go @@ -68,7 +68,7 @@ func buildRequest(params model.RequestParameters, messages []model.Message) Requ startIdx := 0 if len(messages) > 0 && messages[0].Role == model.MessageRoleSystem { requestBody.System = messages[0].Content - requestBody.Messages = requestBody.Messages[:len(messages)-1] + requestBody.Messages = requestBody.Messages[1:] startIdx = 1 } diff --git a/pkg/lmcli/provider/anthropic/tools.go b/pkg/lmcli/provider/anthropic/tools.go index 7c52b1e..2d91504 100644 --- a/pkg/lmcli/provider/anthropic/tools.go +++ b/pkg/lmcli/provider/anthropic/tools.go @@ -9,9 +9,10 @@ import ( "git.mlow.ca/mlow/lmcli/pkg/lmcli/model" ) -const TOOL_PREAMBLE = `In this environment you have access to a set of tools which may assist you in fulfilling user requests. +const TOOL_PREAMBLE = `You have access to the following tools when replying. You may call them like this: + $TOOL_NAME @@ -24,6 +25,14 @@ You may call them like this: Here are the tools available:` +const TOOL_PREAMBLE_FOOTER = `Recognize the utility of these tools in a broad range of different applications, and the power they give you to solve a wide range of different problems. However, ensure that the tools are used judiciously and only when clearly relevant to the user's request. Specifically: + +1. Only use a tool if the user has explicitly requested or provided information that warrants its use. Do not make assumptions about files or data existing without the user mentioning them. + +2. If there is ambiguity about whether using a tool is appropriate, ask a clarifying question to the user before proceeding. Confirm your understanding of their request and intent. + +3. Prioritize providing direct responses and explanations based on your own knowledge and understanding. Use tools to supplement and enhance your responses when clearly applicable, but not as a default action.` + type XMLTools struct { XMLName struct{} `xml:"tools"` ToolDescriptions []XMLToolDescription `xml:"tool_description"` @@ -151,7 +160,7 @@ func buildToolsSystemPrompt(tools []model.Tool) string { if err != nil { panic("Could not serialize []model.Tool to XMLTools") } - return TOOL_PREAMBLE + "\n" + xmlToolsString + "\n" + return TOOL_PREAMBLE + "\n\n" + xmlToolsString + "\n\n" + TOOL_PREAMBLE_FOOTER } func (x XMLTools) XMLString() (string, error) {