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)
This commit is contained in:
Matt Low 2024-03-17 21:59:42 +00:00
parent c2c61e2aaa
commit 46149e0b67
2 changed files with 12 additions and 3 deletions

View File

@ -68,7 +68,7 @@ func buildRequest(params model.RequestParameters, messages []model.Message) Requ
startIdx := 0 startIdx := 0
if len(messages) > 0 && messages[0].Role == model.MessageRoleSystem { if len(messages) > 0 && messages[0].Role == model.MessageRoleSystem {
requestBody.System = messages[0].Content requestBody.System = messages[0].Content
requestBody.Messages = requestBody.Messages[:len(messages)-1] requestBody.Messages = requestBody.Messages[1:]
startIdx = 1 startIdx = 1
} }

View File

@ -9,9 +9,10 @@ import (
"git.mlow.ca/mlow/lmcli/pkg/lmcli/model" "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: You may call them like this:
<function_calls> <function_calls>
<invoke> <invoke>
<tool_name>$TOOL_NAME</tool_name> <tool_name>$TOOL_NAME</tool_name>
@ -24,6 +25,14 @@ You may call them like this:
Here are the tools available:` 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 { type XMLTools struct {
XMLName struct{} `xml:"tools"` XMLName struct{} `xml:"tools"`
ToolDescriptions []XMLToolDescription `xml:"tool_description"` ToolDescriptions []XMLToolDescription `xml:"tool_description"`
@ -151,7 +160,7 @@ func buildToolsSystemPrompt(tools []model.Tool) string {
if err != nil { if err != nil {
panic("Could not serialize []model.Tool to XMLTools") 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) { func (x XMLTools) XMLString() (string, error) {