[GH-ISSUE #9461] Can somebody update the comments on the API part of go? #6167

Closed
opened 2026-04-12 17:32:14 -05:00 by GiteaMirror · 0 comments
Owner

Originally created by @lvyonghuan on GitHub (Mar 2, 2025).
Original GitHub issue: https://github.com/ollama/ollama/issues/9461

I want to use the function call function, but I don't understand how to make function calls in the go language API. The examples found on the Internet are all from python, but I tried it several times but couldn't figure out how to use go to function calls.

I tried to view the message structure, but I didn't understand what its members mean. Such as ToolCalls. I just don't know what it is. Then is the ToolCallFunction, what's the meaning of each member?


This is my current code.The function can be called, but when I try to do a quadratic generation as the tool (as shown in the code), nothing is generated. I don't know how to do it well, because there is a lack of an example code and no explicit comments. Some parts of the code are done by copilot.

        req := &api.ChatRequest{
		Model:    "qwen2.5:32b",
		Messages: ms,
		Tools:    returnToolList(),
	}

	res := make(chan api.ChatResponse, 1)//Pass data to the caller
	var respFunc api.ChatResponseFunc
	respFunc = func(resp api.ChatResponse) error {
		if len(resp.Message.ToolCalls) > 0 {
			for _, toolCall := range resp.Message.ToolCalls {
				if toolCall.Function.Name == "search" {
					query := toolCall.Function.Arguments["query"].(string)
					log.Println("search query:", query)
					result, err := googleSearch(query)
					if err != nil {
						log.Println(err)
						continue
					}

					var searchResult string
					for _, item := range result.Items {
						searchResult += item.Title + "\n" + item.Link + "\n" + item.Snippet + "\n\n"
					}

					ms = append(ms, api.Message{Content: searchResult, Role: tool})

					req.Messages = ms
				}
			}

			go startChat(client, ctx, req, res, respFunc)
		} else {
			res <- resp
		}
		return nil
	}

        go startChat(client, ctx, req, res, respFunc)

	return res, nil
Originally created by @lvyonghuan on GitHub (Mar 2, 2025). Original GitHub issue: https://github.com/ollama/ollama/issues/9461 I want to use the function call function, but I don't understand how to make function calls in the go language API. The examples found on the Internet are all from python, but I tried it several times but couldn't figure out how to use go to function calls. I tried to view the message structure, but I didn't understand what its members mean. Such as [`ToolCalls`](https://github.com/ollama/ollama/blob/af68d60a58c7ade657a1f4641bf301a29d977174/api/types.go#L131). I just don't know what it is. Then is the [`ToolCallFunction`](https://github.com/ollama/ollama/blob/af68d60a58c7ade657a1f4641bf301a29d977174/api/types.go#L150), what's the meaning of each member? --- This is my current code.The function can be called, but when I try to do a quadratic generation as the tool (as shown in the code), nothing is generated. I don't know how to do it well, because there is a lack of an example code and no explicit comments. Some parts of the code are done by copilot. ```go req := &api.ChatRequest{ Model: "qwen2.5:32b", Messages: ms, Tools: returnToolList(), } res := make(chan api.ChatResponse, 1)//Pass data to the caller var respFunc api.ChatResponseFunc respFunc = func(resp api.ChatResponse) error { if len(resp.Message.ToolCalls) > 0 { for _, toolCall := range resp.Message.ToolCalls { if toolCall.Function.Name == "search" { query := toolCall.Function.Arguments["query"].(string) log.Println("search query:", query) result, err := googleSearch(query) if err != nil { log.Println(err) continue } var searchResult string for _, item := range result.Items { searchResult += item.Title + "\n" + item.Link + "\n" + item.Snippet + "\n\n" } ms = append(ms, api.Message{Content: searchResult, Role: tool}) req.Messages = ms } } go startChat(client, ctx, req, res, respFunc) } else { res <- resp } return nil } go startChat(client, ctx, req, res, respFunc) return res, nil ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#6167