Skip to content

Commit

Permalink
feat : add parameter -f in text command accept image file
Browse files Browse the repository at this point in the history
  • Loading branch information
LordPax committed Oct 13, 2024
1 parent 1a8049a commit 07ac8bc
Show file tree
Hide file tree
Showing 12 changed files with 210 additions and 32 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [Unreleased]

### Changed

* Parameter -f in text command accept image file (only for claude sdk)

## [0.4.0] - 2024-09-28

### Changed
Expand Down
13 changes: 9 additions & 4 deletions commands/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/LordPax/aicli/lang"
"github.com/LordPax/aicli/sdk"
"github.com/LordPax/aicli/service"
"github.com/LordPax/aicli/utils"

cli "github.com/urfave/cli/v2"
)
Expand Down Expand Up @@ -125,7 +126,6 @@ func textFlags() []cli.Flag {
Category: "text",
Action: func(c *cli.Context, files []string) error {
text := sdk.GetSdkText()
var fileContent []string

for _, file := range files {
f, err := os.ReadFile(file)
Expand All @@ -137,10 +137,15 @@ func textFlags() []cli.Flag {
return fmt.Errorf(l.Get("empty-file"), file)
}

fileContent = append(fileContent, string(f))
}
if fileType := utils.IsFileType(f, utils.IMAGE); fileType != "" {
if err := text.AppendImageHistory("system", "image/"+fileType, f); err != nil {
return err
}
continue
}

text.AppendHistory("system", fileContent...)
text.AppendHistory("system", string(f))
}

return nil
},
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/LordPax/aicli
go 1.23.1

require (
github.com/h2non/filetype v1.1.3
github.com/urfave/cli/v2 v2.27.3
golang.org/x/term v0.24.0
gopkg.in/ini.v1 v1.67.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lV
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/h2non/filetype v1.1.3 h1:FKkx9QbD7HR/zjK1Ia5XiBsq9zdLi5Kf3zGyFTAFkGg=
github.com/h2non/filetype v1.1.3/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
Expand Down
3 changes: 2 additions & 1 deletion lang/en.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var EN_STRINGS = LangString{
"silent": "Disable printing log to stdout",
"no-args": "No arguments provided",
"no-command": "No command provided",
"not-implemented": "Feature \"%s\" is not implemented",
"unknown-sdk": "Unknown sdk \"%s\"",
"sdk-model-usage": "Select a model",
"inerte-usage": "Do not make API call",
Expand All @@ -18,7 +19,7 @@ var EN_STRINGS = LangString{
"text-system-usage": "Instruction to enter as context (use \"-\" for stdin)",
"text-history-usage": "Select a history",
"text-clear-usage": "Clear history",
"text-file-usage": "Text file to use",
"text-file-usage": "Text or image file to use",
"text-input": "(\"exit\" to quit) " + utils.Blue + "user> " + utils.Reset,
"translate-input": "(\"exit\" to quit) " + utils.Blue + "> " + utils.Reset,
"text-list-history-usage": "List history",
Expand Down
3 changes: 2 additions & 1 deletion lang/fr.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var FR_STRINGS = LangString{
"silent": "Désactiver l'impression du journal sur stdout",
"no-args": "Aucun argument fourni",
"no-command": "Aucune commande fournie",
"not-implemented": "La fonctionnalité \"%s\" n'est pas implémentée",
"unknown-sdk": "Sdk inconnu \"%s\"",
"sdk-model-usage": "Sélectionner un modèle",
"inerte-usage": "N'effectue pas d'appel à l'API",
Expand All @@ -18,7 +19,7 @@ var FR_STRINGS = LangString{
"text-system-usage": "Instruction à entrer comme context (utilisez \"-\" pour stdin)",
"text-history-usage": "Sélectionner un historique",
"text-clear-usage": "Effacer l'historique",
"text-file-usage": "Fichier texte à utiliser",
"text-file-usage": "Fichier texte ou image à utiliser",
"text-input": "(\"exit\" pour quitter) " + utils.Blue + "user> " + utils.Reset,
"translate-input": "(\"exit\" pour quitter) " + utils.Blue + "> " + utils.Reset,
"text-list-history-usage": "Lister l'historique",
Expand Down
25 changes: 22 additions & 3 deletions sdk/claude.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sdk

import (
"encoding/base64"
"encoding/json"
"errors"
"io"
Expand All @@ -10,8 +11,8 @@ import (
)

type ClaudeResponse struct {
Role string `json:"role"`
Content []Content `json:"content"`
Role string `json:"role"`
Content []ContentText `json:"content"`
}

func (c *ClaudeResponse) GetContent() string {
Expand Down Expand Up @@ -137,7 +138,7 @@ func (c *ClaudeText) AppendHistory(role string, text ...string) Message {

// If the last message is from the same role, append the new text to the last message
if lastMessage != nil && lastMessage.Role == role {
return c.AppendMessage(idLastMsg, text...)
return c.AppendTextMessage(idLastMsg, text...)
}

message := Message{
Expand All @@ -148,3 +149,21 @@ func (c *ClaudeText) AppendHistory(role string, text ...string) Message {

return message
}

func (c *ClaudeText) AppendImageHistory(role, fileType string, file []byte) error {
name := c.SelectedHistory

if role == "system" {
role = "user"
}

str := base64.StdEncoding.EncodeToString(file)

message := Message{
Role: role,
Content: []IContent{NewContentImage(str, fileType)},
}
c.History[name] = append(c.History[name], message)

return nil
}
43 changes: 33 additions & 10 deletions sdk/history.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package sdk

import (
"encoding/base64"
"encoding/json"
"os"
"path"
"strings"

"github.com/LordPax/aicli/config"
"github.com/LordPax/aicli/utils"
Expand All @@ -19,7 +19,8 @@ type ITextHistory interface {
GetSelectedHistory() string
ClearHistory()
GetMessage(index int) *Message
AppendMessage(index int, text ...string) Message
AppendTextMessage(index int, text ...string) Message
AppendImageHistory(role, fileType string, file []byte) error
GetHistoryNames() []string
}

Expand Down Expand Up @@ -72,6 +73,20 @@ func (t *TextHistory) AppendHistory(role string, text ...string) Message {
return message
}

func (t *TextHistory) AppendImageHistory(role, fileType string, file []byte) error {
name := t.SelectedHistory

str := base64.StdEncoding.EncodeToString(file)

message := Message{
Role: role,
Content: []IContent{NewContentImage(str, fileType)},
}
t.History[name] = append(t.History[name], message)

return nil
}

func (t *TextHistory) SaveHistory() error {
f, err := os.Create(t.HistoryFile)
if err != nil {
Expand Down Expand Up @@ -101,10 +116,21 @@ func (t *TextHistory) LoadHistory() error {
return nil
}

if err := json.Unmarshal(f, &t.History); err != nil {
var tempHistory map[string][]json.RawMessage
if err := json.Unmarshal(f, &tempHistory); err != nil {
return err
}

t.History = make(map[string][]Message)
for key, rawMessages := range tempHistory {
t.History[key] = make([]Message, len(rawMessages))
for i, rawMessage := range rawMessages {
if err := json.Unmarshal(rawMessage, &t.History[key][i]); err != nil {
return err
}
}
}

return nil
}

Expand All @@ -126,7 +152,7 @@ func (t *TextHistory) GetMessage(index int) *Message {
return &t.History[name][index]
}

func (t *TextHistory) AppendMessage(index int, text ...string) Message {
func (t *TextHistory) AppendTextMessage(index int, text ...string) Message {
name := t.SelectedHistory
message := t.GetMessage(index)

Expand All @@ -148,14 +174,11 @@ func (t *TextHistory) GetHistoryNames() []string {
return names
}

func textContent(text ...string) []Content {
var content []Content
func textContent(text ...string) []IContent {
var content []IContent

for _, t := range text {
content = append(content, Content{
Type: "text",
Text: strings.TrimSpace(t),
})
content = append(content, NewContentText(t))
}

return content
Expand Down
7 changes: 7 additions & 0 deletions sdk/mistral.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package sdk
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"

"github.com/LordPax/aicli/lang"
"github.com/LordPax/aicli/utils"
)

Expand Down Expand Up @@ -112,3 +114,8 @@ func (m *MistralText) SendRequest(text string) (Message, error) {

return respMessage, nil
}

func (t *MistralText) AppendImageHistory(role, fileType string, file []byte) error {
l := lang.GetLocalize()
return fmt.Errorf(l.Get("not-implemented"), "AppendImageHistory")
}
7 changes: 7 additions & 0 deletions sdk/openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package sdk
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"

"github.com/LordPax/aicli/lang"
"github.com/LordPax/aicli/utils"
)

Expand Down Expand Up @@ -133,3 +135,8 @@ func (o *OpenaiText) SendRequest(text string) (Message, error) {

return respMessage, nil
}

func (t *OpenaiText) AppendImageHistory(role, fileType string, file []byte) error {
l := lang.GetLocalize()
return fmt.Errorf(l.Get("not-implemented"), "AppendImageHistory")
}
Loading

0 comments on commit 07ac8bc

Please sign in to comment.