- 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.
116 lines
2.0 KiB
Go
116 lines
2.0 KiB
Go
package llm
|
|
|
|
// Role represents the role of a message in a conversation.
|
|
type Role string
|
|
|
|
const (
|
|
RoleSystem Role = "system"
|
|
RoleUser Role = "user"
|
|
RoleAssistant Role = "assistant"
|
|
)
|
|
|
|
// Image represents an image that can be included in a message.
|
|
type Image struct {
|
|
Base64 string
|
|
ContentType string
|
|
Url string
|
|
}
|
|
|
|
func (i Image) toRaw() map[string]any {
|
|
res := map[string]any{
|
|
"base64": i.Base64,
|
|
"contenttype": i.ContentType,
|
|
"url": i.Url,
|
|
}
|
|
|
|
return res
|
|
}
|
|
|
|
func (i *Image) fromRaw(raw map[string]any) Image {
|
|
var res Image
|
|
|
|
res.Base64 = raw["base64"].(string)
|
|
res.ContentType = raw["contenttype"].(string)
|
|
res.Url = raw["url"].(string)
|
|
|
|
return res
|
|
}
|
|
|
|
// Message represents a message in a conversation.
|
|
type Message struct {
|
|
Role Role
|
|
Name string
|
|
Text string
|
|
Images []Image
|
|
}
|
|
|
|
func (m Message) toRaw() map[string]any {
|
|
res := map[string]any{
|
|
"role": m.Role,
|
|
"name": m.Name,
|
|
"text": m.Text,
|
|
}
|
|
|
|
images := make([]map[string]any, 0, len(m.Images))
|
|
for _, img := range m.Images {
|
|
images = append(images, img.toRaw())
|
|
}
|
|
|
|
res["images"] = images
|
|
|
|
return res
|
|
}
|
|
|
|
func (m *Message) fromRaw(raw map[string]any) Message {
|
|
var res Message
|
|
|
|
res.Role = Role(raw["role"].(string))
|
|
res.Name = raw["name"].(string)
|
|
res.Text = raw["text"].(string)
|
|
|
|
images := raw["images"].([]map[string]any)
|
|
for _, img := range images {
|
|
var i Image
|
|
|
|
res.Images = append(res.Images, i.fromRaw(img))
|
|
}
|
|
|
|
return res
|
|
}
|
|
|
|
// ToolCall represents a tool call made by an assistant.
|
|
type ToolCall struct {
|
|
ID string
|
|
FunctionCall FunctionCall
|
|
}
|
|
|
|
func (t ToolCall) toRaw() map[string]any {
|
|
res := map[string]any{
|
|
"id": t.ID,
|
|
}
|
|
|
|
res["function"] = t.FunctionCall.toRaw()
|
|
|
|
return res
|
|
}
|
|
|
|
// ToolCallResponse represents the response to a tool call.
|
|
type ToolCallResponse struct {
|
|
ID string
|
|
Result any
|
|
Error error
|
|
}
|
|
|
|
func (t ToolCallResponse) toRaw() map[string]any {
|
|
res := map[string]any{
|
|
"id": t.ID,
|
|
"result": t.Result,
|
|
}
|
|
|
|
if t.Error != nil {
|
|
res["error"] = t.Error.Error()
|
|
}
|
|
|
|
return res
|
|
}
|