func()

in gollm/ollama.go [132:189]


func (c *OllamaChat) Send(ctx context.Context, contents ...any) (ChatResponse, error) {
	log := klog.FromContext(ctx)
	for _, content := range contents {
		switch v := content.(type) {
		case string:
			message := api.Message{
				Role:    "user",
				Content: v,
			}
			c.history = append(c.history, message)
		case FunctionCallResult:
			message := api.Message{
				Role:    "user",
				Content: fmt.Sprintf("Function call result: %s", v.Result),
			}
			c.history = append(c.history, message)
		default:
			return nil, fmt.Errorf("unsupported content type: %T", v)
		}
	}

	req := &api.ChatRequest{
		Model:    c.model,
		Messages: c.history,
		// set streaming to false
		Stream: new(bool),
		Tools:  c.tools,
	}

	var ollamaResponse *OllamaChatResponse

	respFunc := func(resp api.ChatResponse) error {
		log.Info("recieved response from ollama", "resp", resp)
		ollamaResponse = &OllamaChatResponse{
			ollamaResponse: resp,
			candidates: []*OllamaCandidate{
				{
					parts: []OllamaPart{
						{
							text:      resp.Message.Content,
							toolCalls: resp.Message.ToolCalls,
						},
					},
				},
			},
		}
		c.history = append(c.history, resp.Message)
		return nil
	}

	err := c.client.Chat(ctx, req, respFunc)
	if err != nil {
		return nil, err
	}

	log.Info("ollama response", "parsed_response", ollamaResponse)
	return ollamaResponse, nil
}