ollama use agent and tools error
func main() {
llm, err := ollama.New(
ollama.WithModel("qwen2.5:14b"),
ollama.WithFormat("json"),
ollama.WithHTTPClient(httpClient),
)
if err != nil {
panic(err)
}
content := []llms.MessageContent{
llms.TextParts(llms.ChatMessageTypeHuman, "What is the weather like in Chicago?"),
}
generateContent, err := llm.GenerateContent(context.Background(), content, llms.WithTools(llmTools))
if err != nil {
panic(err)
}
println(generateContent.Choices[0].Content)
AgentsTest(llm)
}
func AgentsTest(llm llms.Model) {
agent := agents.NewOneShotAgent(llm, agentTools, agents.WithMaxIterations(1))
executor := agents.NewExecutor(agent)
input := "这段话的中文意思是什么:hello world"
answer, err := chains.Run(context.Background(), executor, input)
if err != nil {
panic(err)
}
fmt.Println(answer)
}
var agentTools = []tools.Tool{
&TranslateTools{},
}
var llmTools = []llms.Tool{
{
Type: "function",
Function: &llms.FunctionDefinition{
Name: "getCurrentWeather",
Description: "Get the current weather in a given location",
Parameters: map[string]any{
"type": "object",
"properties": map[string]any{
"location": map[string]any{
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
},
"required": []string{"location"},
},
},
},
}
var _ tools.Tool = (*TranslateTools)(nil)
type TranslateTools struct {
}
func (t TranslateTools) Name() string {
return "translation_content"
}
func (t TranslateTools) Description() string {
return "翻译用户输入的content从source_language到target_language"
}
func (t TranslateTools) Call(ctx context.Context, input string) (string, error) {
fmt.Println("input:", input)
return input, nil
}
response error
{
panic: unable to parse agent output:
goroutine 1 [running]:
main.AgentsTest({0xee41a8?, 0xc0000fec00?})
D:/code/golang/langchaingo_demo/main.go:60 +0x11f
main.main()
D:/code/golang/langchaingo_demo/main.go:51 +0x3a5
exit status 2
Can anyone tell me why?
I also encountered this problem. It is obviously a bug in the prompt word inside the framework.
It looks like it's potentially this line https://github.com/tmc/langchaingo/blob/d3e43b6321761adc8139cd277b2d28f3b412cc10/agents/mrkl.go#L149, happening because Action Input is not included in the output when using Ollama.
Example output from my testing:
panic: unable to parse agent output: Question: What is 3 times 3
Thought: To calculate this, I need to multiply 3 by 3.
Action: Perform multiplication
Result: 3 * 3 = 9
Answer: 9
I also ran into this issue this past weekend. I had a tool with No input required in the description. once I changed it to "The input should be the empty string" it started working.
Would it be reasonable to change this behavior to set the empty string as the ToolInput when none is found in the message?
Hi, Have you found a workaround ?