From f4214f656ba50457dc039dd8dc61be92f2368ade Mon Sep 17 00:00:00 2001 From: Steve Dudenhoeffer Date: Sun, 6 Oct 2024 21:02:26 -0400 Subject: [PATCH] push of current changes --- pkg/anthropic.go => anthropic.go | 40 +++++++++++++++++++++-------- pkg/function.go => function.go | 2 +- go.mod | 25 +++---------------- go.sum | 43 -------------------------------- pkg/llm.go => llm.go | 14 ++++++----- pkg/openai.go => openai.go | 23 ++++++++++++++--- 6 files changed, 62 insertions(+), 85 deletions(-) rename pkg/anthropic.go => anthropic.go (75%) rename pkg/function.go => function.go (95%) rename pkg/llm.go => llm.go (74%) rename pkg/openai.go => openai.go (81%) diff --git a/pkg/anthropic.go b/anthropic.go similarity index 75% rename from pkg/anthropic.go rename to anthropic.go index b69b6ae..79c248f 100644 --- a/pkg/anthropic.go +++ b/anthropic.go @@ -1,9 +1,10 @@ -package llm +package go_llm import ( "context" "fmt" anth "github.com/liushuangls/go-anthropic/v2" + "log" ) type anthropic struct { @@ -45,15 +46,26 @@ func (a anthropic) requestToAnthropicRequest(req Request) anth.MessagesRequest { role = anth.RoleAssistant } - msgs = append(msgs, anth.Message{ - Role: role, - Content: []anth.MessageContent{ - { - Type: anth.MessagesContentTypeText, - Text: &msg.Text, - }, - }, - }) + m := anth.Message{ + Role: role, + Content: []anth.MessageContent{}, + } + + if msg.Text != "" { + m.Content = append(m.Content, anth.MessageContent{ + Type: anth.MessagesContentTypeText, + Text: &msg.Text, + }) + } + + if msg.ImageBase64 != "" { + m.Content = append(m.Content, anth.NewImageMessageContent(anth.MessageContentImageSource{ + Type: "base64", + MediaType: "image/png", + Data: msg.ImageBase64, + })) + } + msgs = append(msgs, m) } } @@ -67,6 +79,12 @@ func (a anthropic) requestToAnthropicRequest(req Request) anth.MessagesRequest { res.Messages = msgs + if req.Temperature != nil { + res.Temperature = req.Temperature + } + + log.Println("llm request to anthropic request", res) + return res } @@ -97,6 +115,8 @@ func (a anthropic) responseToLLMResponse(in anth.MessagesResponse) Response { res.Choices = append(res.Choices, choice) } + log.Println("anthropic response to llm response", res) + return res } diff --git a/pkg/function.go b/function.go similarity index 95% rename from pkg/function.go rename to function.go index 1e00d2c..5def8f5 100644 --- a/pkg/function.go +++ b/function.go @@ -1,4 +1,4 @@ -package llm +package go_llm type Function struct { Name string `json:"name"` diff --git a/go.mod b/go.mod index 03b8e42..60d8bd4 100644 --- a/go.mod +++ b/go.mod @@ -1,27 +1,8 @@ -module go-llm +module gitea.stevedudenhoeffer.com/steve/go-llm go 1.23.1 require ( - github.com/atotto/clipboard v0.1.4 // indirect - github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect - github.com/charmbracelet/bubbles v0.20.0 // indirect - github.com/charmbracelet/bubbletea v1.1.1 // indirect - github.com/charmbracelet/lipgloss v0.13.0 // indirect - github.com/charmbracelet/x/ansi v0.2.3 // indirect - github.com/charmbracelet/x/term v0.2.0 // indirect - github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect - github.com/liushuangls/go-anthropic/v2 v2.8.0 // indirect - github.com/lucasb-eyer/go-colorful v1.2.0 // indirect - github.com/mattn/go-isatty v0.0.20 // indirect - github.com/mattn/go-localereader v0.0.1 // indirect - github.com/mattn/go-runewidth v0.0.16 // indirect - github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect - github.com/muesli/cancelreader v0.2.2 // indirect - github.com/muesli/termenv v0.15.2 // indirect - github.com/rivo/uniseg v0.4.7 // indirect - github.com/sashabaranov/go-openai v1.31.0 // indirect - golang.org/x/sync v0.8.0 // indirect - golang.org/x/sys v0.24.0 // indirect - golang.org/x/text v0.3.8 // indirect + github.com/liushuangls/go-anthropic/v2 v2.8.0 + github.com/sashabaranov/go-openai v1.31.0 ) diff --git a/go.sum b/go.sum index f7d2d15..af2856f 100644 --- a/go.sum +++ b/go.sum @@ -1,47 +1,4 @@ -github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4= -github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI= -github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k= -github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8= -github.com/charmbracelet/bubbles v0.20.0 h1:jSZu6qD8cRQ6k9OMfR1WlM+ruM8fkPWkHvQWD9LIutE= -github.com/charmbracelet/bubbles v0.20.0/go.mod h1:39slydyswPy+uVOHZ5x/GjwVAFkCsV8IIVy+4MhzwwU= -github.com/charmbracelet/bubbletea v1.1.1 h1:KJ2/DnmpfqFtDNVTvYZ6zpPFL9iRCRr0qqKOCvppbPY= -github.com/charmbracelet/bubbletea v1.1.1/go.mod h1:9Ogk0HrdbHolIKHdjfFpyXJmiCzGwy+FesYkZr7hYU4= -github.com/charmbracelet/lipgloss v0.13.0 h1:4X3PPeoWEDCMvzDvGmTajSyYPcZM4+y8sCA/SsA3cjw= -github.com/charmbracelet/lipgloss v0.13.0/go.mod h1:nw4zy0SBX/F/eAO1cWdcvy6qnkDUxr8Lw7dvFrAIbbY= -github.com/charmbracelet/x/ansi v0.2.3 h1:VfFN0NUpcjBRd4DnKfRaIRo53KRgey/nhOoEqosGDEY= -github.com/charmbracelet/x/ansi v0.2.3/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw= -github.com/charmbracelet/x/term v0.2.0 h1:cNB9Ot9q8I711MyZ7myUR5HFWL/lc3OpU8jZ4hwm0x0= -github.com/charmbracelet/x/term v0.2.0/go.mod h1:GVxgxAbjUrmpvIINHIQnJJKpMlHiZ4cktEQCN6GWyF0= -github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4= -github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM= github.com/liushuangls/go-anthropic/v2 v2.8.0 h1:0zH2jDNycbrlszxnLrG+Gx8vVT0yJAPWU4s3ZTkWzgI= github.com/liushuangls/go-anthropic/v2 v2.8.0/go.mod h1:8BKv/fkeTaL5R9R9bGkaknYBueyw2WxY20o7bImbOek= -github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= -github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= -github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= -github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= -github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4= -github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88= -github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= -github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= -github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc= -github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= -github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D264iyp3TiX5OmNcI5cIARiQI= -github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo= -github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA= -github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo= -github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo= -github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8= -github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= -github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= -github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= github.com/sashabaranov/go-openai v1.31.0 h1:rGe77x7zUeCjtS2IS7NCY6Tp4bQviXNMhkQM6hz/UC4= github.com/sashabaranov/go-openai v1.31.0/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg= -golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= -golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= -golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg= -golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY= -golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= diff --git a/pkg/llm.go b/llm.go similarity index 74% rename from pkg/llm.go rename to llm.go index 6833e19..a161cf8 100644 --- a/pkg/llm.go +++ b/llm.go @@ -1,4 +1,4 @@ -package llm +package go_llm import ( "context" @@ -13,14 +13,16 @@ const ( ) type Message struct { - Role Role - Name string - Text string + Role Role + Name string + Text string + ImageBase64 string // ImageBase64 is the base64 string if the message contains an image, empty string otherwise. } type Request struct { - Messages []Message - Toolbox []Function + Messages []Message + Toolbox []Function + Temperature *float32 } type ToolCall struct { diff --git a/pkg/openai.go b/openai.go similarity index 81% rename from pkg/openai.go rename to openai.go index 2236238..484940f 100644 --- a/pkg/openai.go +++ b/openai.go @@ -1,4 +1,4 @@ -package llm +package go_llm import ( "context" @@ -19,11 +19,24 @@ func (o openai) requestToOpenAIRequest(request Request) oai.ChatCompletionReques } for _, msg := range request.Messages { - res.Messages = append(res.Messages, oai.ChatCompletionMessage{ + m := oai.ChatCompletionMessage{ Content: msg.Text, Role: string(msg.Role), Name: msg.Name, - }) + } + + if msg.ImageBase64 != "" { + part := oai.ChatMessagePart{ + Type: "image_url", + ImageURL: &oai.ChatMessageImageURL{ + URL: fmt.Sprintf("data:image/jpeg;base64,%s", msg.ImageBase64), + }, + } + + m.MultiContent = append(m.MultiContent, part) + } + + res.Messages = append(res.Messages, m) } for _, tool := range request.Toolbox { @@ -38,6 +51,10 @@ func (o openai) requestToOpenAIRequest(request Request) oai.ChatCompletionReques }) } + if request.Temperature != nil { + res.Temperature = *request.Temperature + } + return res }