X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/522d7fd117285e4e4ce3ea5ec9b5a1722521b681..59948b44c0fe2165b90e3aa0d18646f6c3d74af3:/model.go?ds=inline diff --git a/model.go b/model.go index 507fbb1..cc77f9a 100644 --- a/model.go +++ b/model.go @@ -41,9 +41,9 @@ const ( var errModelCallLimit = errors.New("call limit reached") // modelEndpoint returns the URL and the authorizer of the endpoint in config. -// The ChatGPT backend uses the token file in dir. The OpenAI API uses the key in -// OPENAI_API_KEY, and there is no fallback from one to the other. All clients -// share the authorizer, so one refresh runs at a time. +// The ChatGPT backend uses the token file in dir. The OpenAI API uses the key +// in OPENAI_API_KEY, and there is no fallback from one to the other. All +// clients share the authorizer, so one refresh runs at a time. func modelEndpoint(config Config, dir string) (string, ModelAuthorizer, error) { if config.Endpoint == configEndpointAPI { key := os.Getenv("OPENAI_API_KEY") @@ -73,22 +73,22 @@ func newModelClient(url string, auth ModelAuthorizer, cacheKey, usagePath string // modelUserMessage returns a user input item with one text part for each text. func modelUserMessage(texts ...string) any { - type part struct { - Type string `json:"type"` - Text string `json:"text"` - } - - content := make([]part, len(texts)) + content := make([]ModelInputPart, len(texts)) for n, text := range texts { - content[n] = part{ + content[n] = ModelInputPart{ Type: "input_text", Text: text, } } + return modelUserParts(content) +} + +// modelUserParts keeps text and image parts in one user message. +func modelUserParts(content []ModelInputPart) any { return struct { - Role string `json:"role"` - Content []part `json:"content"` + Role string `json:"role"` + Content []ModelInputPart `json:"content"` }{ Role: "user", Content: content, @@ -321,6 +321,13 @@ type ModelOutputItem struct { } `json:"content"` } +// ModelInputPart is a text or image part in a user message or tool result. +type ModelInputPart struct { + Type string `json:"type"` + Text string `json:"text,omitempty"` + ImageURL string `json:"image_url,omitempty"` +} + // ModelRequest is one model call. Input holds the input items in order, such as // modelUserMessage values or output items of an earlier response. Node names // the node that a compaction builds, or is nil. Turn names the request of a