langchaingo icon indicating copy to clipboard operation
langchaingo copied to clipboard

ollama use agent and tools error

Open pdxrlj opened this issue 1 year ago • 5 comments

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

pdxrlj avatar Oct 09 '24 10:10 pdxrlj

Can anyone tell me why?

pdxrlj avatar Oct 09 '24 10:10 pdxrlj

I also encountered this problem. It is obviously a bug in the prompt word inside the framework.

lyr-2000 avatar Jan 12 '25 05:01 lyr-2000

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

rayterrill avatar Mar 31 '25 02:03 rayterrill

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?

LucasRoesler avatar Jun 30 '25 07:06 LucasRoesler

Hi, Have you found a workaround ?

disaster37 avatar Sep 18 '25 07:09 disaster37