Skip to content

Commit

Permalink
feat : add sdk mistral
Browse files Browse the repository at this point in the history
* add parametre -L
* improve the way we add message to history
* improve input function for interactive mode
  • Loading branch information
LordPax committed Sep 26, 2024
1 parent cb1e51e commit 2862c2a
Show file tree
Hide file tree
Showing 12 changed files with 259 additions and 103 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Changelog

## [Unreleased]

### Added

* Add parameter -L for text command to list all history name
* Add sdk mistral for text command

### Changed

* Improve the way we add message to history
* Improve input function for interactive mode

## [0.1.0] - 2024-09-22

### Added
Expand Down
28 changes: 22 additions & 6 deletions commands/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,10 @@ func textFlags() []cli.Flag {
},
},
&cli.BoolFlag{
Name: "clear",
Aliases: []string{"c"},
Usage: l.Get("text-clear-usage"),
Name: "clear",
Aliases: []string{"c"},
Usage: l.Get("text-clear-usage"),
DisableDefaultText: true,
Action: func(c *cli.Context, value bool) error {
text := sdk.GetSdkText()
text.ClearHistory()
Expand All @@ -142,9 +143,10 @@ func textFlags() []cli.Flag {
},
},
&cli.BoolFlag{
Name: "list-history",
Aliases: []string{"l"},
Usage: l.Get("text-list-history-usage"),
Name: "list-history",
Aliases: []string{"l"},
Usage: l.Get("text-list-history-usage"),
DisableDefaultText: true,
Action: func(c *cli.Context, value bool) error {
if err := service.ListHistory(true); err != nil {
return err
Expand All @@ -153,6 +155,20 @@ func textFlags() []cli.Flag {
return nil
},
},
&cli.BoolFlag{
Name: "list-history-name",
Aliases: []string{"L"},
Usage: l.Get("text-list-history-name-usage"),
DisableDefaultText: true,
Action: func(c *cli.Context, value bool) error {
text := sdk.GetSdkText()
for _, name := range text.GetHistoryNames() {
fmt.Println(name)
}
os.Exit(0)
return nil
},
},
}
}

Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.23.1

require (
github.com/urfave/cli/v2 v2.27.3
golang.org/x/term v0.24.0
gopkg.in/ini.v1 v1.67.0
)

Expand All @@ -12,4 +13,5 @@ require (
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
golang.org/x/sys v0.25.0 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ github.com/urfave/cli/v2 v2.27.3 h1:/POWahRmdh7uztQ3CYnaDddk0Rm90PyOgIxgW2rr41M=
github.com/urfave/cli/v2 v2.27.3/go.mod h1:m4QzxcD2qpra4z7WhzEGn74WZLViBnMpb1ToCAKdGRQ=
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4=
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.24.0 h1:Mh5cbb+Zk2hqqXNO7S1iTjEphVL+jb8ZWaqh/g+JWkM=
golang.org/x/term v0.24.0/go.mod h1:lOBK/LVxemqiMij05LGJ0tzNr8xlmwBRJ81PX6wVLH8=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
53 changes: 27 additions & 26 deletions lang/en.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,31 @@ package lang
import "github.com/LordPax/aicli/utils"

var EN_STRINGS = LangString{
"usage": "CLI toot to use ai model",
"output-desc": "Output directory",
"output-dir-empty": "Output directory is empty",
"silent": "Disable printing log to stdout",
"no-args": "No arguments provided",
"no-command": "No command provided",
"unknown-sdk": "Unknown sdk \"%s\"",
"sdk-model-usage": "Select a model",
"text-usage": "Generate text from a prompt",
"sdk-usage": "Select a sdk",
"text-temp-usage": "Set temperature",
"text-system-usage": "Instruction with role system (use \"-\" for stdin)",
"text-history-usage": "Select a history",
"text-clear-usage": "Clear history",
"text-file-usage": "Text 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",
"type-required": "Type is required",
"apiKey-required": "API key is required",
"empty-file": "File \"%s\" is empty",
"empty-history": "History \"%s\" is empty\n",
"translate-usage": "Translate a text",
"translate-source-usage": "Source language",
"translate-target-usage": "Target language",
"translate-target-required": "Target language is required",
"usage": "CLI toot to use ai model",
"output-desc": "Output directory",
"output-dir-empty": "Output directory is empty",
"silent": "Disable printing log to stdout",
"no-args": "No arguments provided",
"no-command": "No command provided",
"unknown-sdk": "Unknown sdk \"%s\"",
"sdk-model-usage": "Select a model",
"text-usage": "Generate text from a prompt",
"sdk-usage": "Select a sdk",
"text-temp-usage": "Set temperature",
"text-system-usage": "Instruction with role system (use \"-\" for stdin)",
"text-history-usage": "Select a history",
"text-clear-usage": "Clear history",
"text-file-usage": "Text 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",
"text-list-history-name-usage": "List history names",
"type-required": "Type is required",
"api-key-required": "API key is required",
"empty-file": "File \"%s\" is empty",
"empty-history": "History \"%s\" is empty\n",
"translate-usage": "Translate a text",
"translate-source-usage": "Source language",
"translate-target-usage": "Target language",
"translate-target-required": "Target language is required",
}
53 changes: 27 additions & 26 deletions lang/fr.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,31 @@ package lang
import "github.com/LordPax/aicli/utils"

var FR_STRINGS = LangString{
"usage": "CLI pour utiliser des modèles d'IA",
"output-desc": "Répertoire de sortie",
"output-dir-empty": "Le répertoire de sortie est vide",
"silent": "Désactiver l'impression du journal sur stdout",
"no-args": "Aucun argument fourni",
"no-command": "Aucune commande fournie",
"unknown-sdk": "Sdk inconnu \"%s\"",
"sdk-model-usage": "Sélectionner un modèle",
"text-usage": "Générer du texte à partir d'un prompt",
"sdk-usage": "Sélectionner un sdk",
"text-temp-usage": "Définir la température",
"text-system-usage": "Instruction avec rôle système (utilisez \"-\" pour stdin)",
"text-history-usage": "Sélectionner un historique",
"text-clear-usage": "Effacer l'historique",
"text-file-usage": "Fichier texte à 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",
"type-required": "Le type est requis",
"apiKey-required": "La clé API est requise",
"empty-file": "Le fichier \"%s\" est vide",
"empty-history": "L'historique \"%s\" est vide\n",
"translate-usage": "Traduire un texte",
"translate-source-usage": "Langue source",
"translate-target-usage": "Langue cible",
"translate-target-required": "La langue cible est requise",
"usage": "CLI pour utiliser des modèles d'IA",
"output-desc": "Répertoire de sortie",
"output-dir-empty": "Le répertoire de sortie est vide",
"silent": "Désactiver l'impression du journal sur stdout",
"no-args": "Aucun argument fourni",
"no-command": "Aucune commande fournie",
"unknown-sdk": "Sdk inconnu \"%s\"",
"sdk-model-usage": "Sélectionner un modèle",
"text-usage": "Générer du texte à partir d'un prompt",
"sdk-usage": "Sélectionner un sdk",
"text-temp-usage": "Définir la température",
"text-system-usage": "Instruction avec rôle système (utilisez \"-\" pour stdin)",
"text-history-usage": "Sélectionner un historique",
"text-clear-usage": "Effacer l'historique",
"text-file-usage": "Fichier texte à 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",
"text-list-history-name-usage": "Lister les noms d'historique",
"type-required": "Le type est requis",
"api-key-required": "La clé API est requise",
"empty-file": "Le fichier \"%s\" est vide",
"empty-history": "L'historique \"%s\" est vide\n",
"translate-usage": "Traduire un texte",
"translate-source-usage": "Langue source",
"translate-target-usage": "Langue cible",
"translate-target-required": "La langue cible est requise",
}
25 changes: 10 additions & 15 deletions sdk/claude.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,14 @@ func NewClaudeText(apiKey, model string, temp float64) (*ClaudeText, error) {
func (c *ClaudeText) SendRequest(text string) (Message, error) {
var textResponse ClaudeResponse

idLastMsg := len(c.GetHistory()) - 1
lastMessage := c.GetMessage(idLastMsg)
c.AppendHistory("user", text)

if lastMessage != nil && lastMessage.Role == "user" {
c.AppendMessage(idLastMsg, text)
} else {
c.AppendHistory("user", text)
}
test := c.GetHistory()

jsonBody, err := json.Marshal(ClaudeBody{
Model: c.Model,
MaxTokens: 1024,
Messages: c.GetHistory(),
Messages: test,
})
if err != nil {
return Message{}, err
Expand Down Expand Up @@ -123,23 +118,23 @@ func (c *ClaudeText) SendRequest(text string) (Message, error) {
}

func (c *ClaudeText) AppendHistory(role string, text ...string) Message {
var content []Content
name := c.SelectedHistory

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

for _, t := range text {
content = append(content, Content{
Type: "text",
Text: t,
})
idLastMsg := len(c.GetHistory()) - 1
lastMessage := c.GetMessage(idLastMsg)

// 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...)
}

message := Message{
Role: role,
Content: content,
Content: textContent(text...),
}
c.History[name] = append(c.History[name], message)

Expand Down
43 changes: 29 additions & 14 deletions sdk/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"os"
"path"
"strings"

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

type TextHistory struct {
Expand Down Expand Up @@ -59,19 +61,11 @@ func (t *TextHistory) GetSelectedHistory() string {
}

func (t *TextHistory) AppendHistory(role string, text ...string) Message {
var content []Content
name := t.SelectedHistory

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

message := Message{
Role: role,
Content: content,
Content: textContent(text...),
}
t.History[name] = append(t.History[name], message)

Expand Down Expand Up @@ -132,16 +126,37 @@ func (t *TextHistory) GetMessage(index int) *Message {
return &t.History[name][index]
}

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

content := textContent(text...)
message.Content = append(message.Content, content...)

t.History[name][index] = *message

return *message
}

func (t *TextHistory) GetHistoryNames() []string {
var names []string

for k := range t.History {
names = append(names, k)
}

return names
}

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

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

t.History[name][index] = *message
return content
}
Loading

0 comments on commit 2862c2a

Please sign in to comment.