- Migrate `compress_image.go` to `internal/imageutil` for better encapsulation. - Reorganize LLM provider implementations into distinct packages (`google`, `openai`, and `anthropic`). - Replace `go_llm` package name with `llm`. - Refactor internal APIs for improved clarity, including renaming `anthropic` to `anthropicImpl` and `google` to `googleImpl`. - Add helper methods and restructure message handling for better separation of concerns.
52 lines
1.6 KiB
Go
52 lines
1.6 KiB
Go
package llm
|
|
|
|
// Input is the interface for conversation inputs.
|
|
// Types that implement this interface can be part of a conversation:
|
|
// Message, ToolCall, ToolCallResponse, and ResponseChoice.
|
|
type Input interface {
|
|
// isInput is a marker method to ensure only valid types implement this interface.
|
|
isInput()
|
|
}
|
|
|
|
// Implement Input interface for all valid input types.
|
|
func (Message) isInput() {}
|
|
func (ToolCall) isInput() {}
|
|
func (ToolCallResponse) isInput() {}
|
|
func (ResponseChoice) isInput() {}
|
|
|
|
// Request represents a request to a language model.
|
|
type Request struct {
|
|
Conversation []Input
|
|
Messages []Message
|
|
Toolbox ToolBox
|
|
Temperature *float64
|
|
}
|
|
|
|
// NextRequest will take the current request's conversation, messages, the response, and any tool results, and
|
|
// return a new request with the conversation updated to include the response and tool results.
|
|
func (req Request) NextRequest(resp ResponseChoice, toolResults []ToolCallResponse) Request {
|
|
var res Request
|
|
|
|
res.Toolbox = req.Toolbox
|
|
res.Temperature = req.Temperature
|
|
|
|
res.Conversation = make([]Input, len(req.Conversation))
|
|
copy(res.Conversation, req.Conversation)
|
|
|
|
// now for every input message, convert those to an Input to add to the conversation
|
|
for _, msg := range req.Messages {
|
|
res.Conversation = append(res.Conversation, msg)
|
|
}
|
|
|
|
if resp.Content != "" || resp.Refusal != "" || len(resp.Calls) > 0 {
|
|
res.Conversation = append(res.Conversation, resp)
|
|
}
|
|
|
|
// if there are tool results, then we need to add those to the conversation
|
|
for _, result := range toolResults {
|
|
res.Conversation = append(res.Conversation, result)
|
|
}
|
|
|
|
return res
|
|
}
|