Skip to content

Commit

Permalink
improve : multiple improvement
Browse files Browse the repository at this point in the history
* add parameter -i for text command to not make api call
* improve the way we initialize the sdk
* rename `system` parameter to `context` for text command (breaking change)
  • Loading branch information
LordPax committed Sep 28, 2024
1 parent 973d271 commit f989620
Show file tree
Hide file tree
Showing 14 changed files with 146 additions and 48 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Changelog

## [Unreleased]

### Added

* Add parameter -i for text command to not make api call

### Changed

* Improve the way we initialize the sdk
* Rename `system` parameter to `context` for text command (breaking change)

## [0.2.0] - 2024-09-27

### Added
Expand Down
7 changes: 4 additions & 3 deletions commands/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ func MainFlags() []cli.Flag {
l := lang.GetLocalize()
return []cli.Flag{
&cli.BoolFlag{
Name: "silent",
Aliases: []string{"s"},
Usage: l.Get("silent"),
Name: "silent",
Aliases: []string{"s"},
Usage: l.Get("silent"),
DisableDefaultText: true,
Action: func(c *cli.Context, value bool) error {
log, err := utils.GetLog()
if err != nil {
Expand Down
46 changes: 37 additions & 9 deletions commands/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,21 @@ import (
cli "github.com/urfave/cli/v2"
)

func TextCommand() *cli.Command {
func TextCommand() (*cli.Command, error) {
l := lang.GetLocalize()

if err := sdk.InitSdkText(""); err != nil {
return nil, err
}

return &cli.Command{
Name: "text",
Usage: l.Get("text-usage"),
ArgsUsage: "[prompt|-]",
Aliases: []string{"t"},
Action: textAction,
Flags: textFlags(),
}
}, nil
}

func textFlags() []cli.Flag {
Expand All @@ -35,18 +40,32 @@ func textFlags() []cli.Flag {
Aliases: []string{"S"},
Usage: l.Get("sdk-usage"),
DefaultText: textSdk.GetName(),
Category: "global",
Action: func(c *cli.Context, value string) error {
if err := sdk.InitSdkText(value); err != nil {
return err
}
return nil
},
},
&cli.BoolFlag{
Name: "inerte",
Aliases: []string{"i"},
Usage: l.Get("inerte-usage"),
DisableDefaultText: true,
Category: "global",
Action: func(c *cli.Context, value bool) error {
text := sdk.GetSdkText()
text.SetInerte(value)
return nil
},
},
&cli.StringFlag{
Name: "history",
Aliases: []string{"H"},
Usage: l.Get("text-history-usage"),
DefaultText: textSdk.GetSelectedHistory(),
Category: "history",
Action: func(c *cli.Context, value string) error {
text := sdk.GetSdkText()
text.SetSelectedHistory(value)
Expand All @@ -58,6 +77,7 @@ func textFlags() []cli.Flag {
Aliases: []string{"m"},
Usage: l.Get("sdk-model-usage"),
DefaultText: textSdk.GetModel(),
Category: "text",
Action: func(c *cli.Context, value string) error {
text := sdk.GetSdkText()
text.SetModel(value)
Expand All @@ -69,16 +89,18 @@ func textFlags() []cli.Flag {
Aliases: []string{"t"},
Usage: l.Get("text-temp-usage"),
DefaultText: strconv.FormatFloat(textSdk.GetTemp(), 'f', -1, 64),
Category: "text",
Action: func(c *cli.Context, value float64) error {
text := sdk.GetSdkText()
text.SetTemp(value)
return nil
},
},
&cli.StringSliceFlag{
Name: "system",
Aliases: []string{"s"},
Usage: l.Get("text-system-usage"),
Name: "context",
Aliases: []string{"s"},
Usage: l.Get("text-system-usage"),
Category: "text",
Action: func(c *cli.Context, values []string) error {
text := sdk.GetSdkText()
var content []string
Expand All @@ -102,9 +124,10 @@ func textFlags() []cli.Flag {
},
},
&cli.StringSliceFlag{
Name: "file",
Aliases: []string{"f"},
Usage: l.Get("text-file-usage"),
Name: "file",
Aliases: []string{"f"},
Usage: l.Get("text-file-usage"),
Category: "text",
Action: func(c *cli.Context, files []string) error {
text := sdk.GetSdkText()
var fileContent []string
Expand Down Expand Up @@ -132,6 +155,7 @@ func textFlags() []cli.Flag {
Aliases: []string{"c"},
Usage: l.Get("text-clear-usage"),
DisableDefaultText: true,
Category: "history",
Action: func(c *cli.Context, value bool) error {
text := sdk.GetSdkText()
text.ClearHistory()
Expand All @@ -147,6 +171,7 @@ func textFlags() []cli.Flag {
Aliases: []string{"l"},
Usage: l.Get("text-list-history-usage"),
DisableDefaultText: true,
Category: "history",
Action: func(c *cli.Context, value bool) error {
if err := service.ListHistory(true); err != nil {
return err
Expand All @@ -160,6 +185,7 @@ func textFlags() []cli.Flag {
Aliases: []string{"L"},
Usage: l.Get("text-list-history-name-usage"),
DisableDefaultText: true,
Category: "history",
Action: func(c *cli.Context, value bool) error {
text := sdk.GetSdkText()
for _, name := range text.GetHistoryNames() {
Expand All @@ -173,7 +199,9 @@ func textFlags() []cli.Flag {
}

func textAction(c *cli.Context) error {
if c.NArg() == 0 {
textSdk := sdk.GetSdkText()

if c.NArg() == 0 && !textSdk.GetInerte() {
if err := service.InteractiveMode(); err != nil {
return err
}
Expand Down
27 changes: 19 additions & 8 deletions commands/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,34 @@ import (
cli "github.com/urfave/cli/v2"
)

func TranslateCommand() *cli.Command {
func TranslateCommand() (*cli.Command, error) {
l := lang.GetLocalize()

if err := sdk.InitSdkTranslate(""); err != nil {
return nil, err
}

return &cli.Command{
Name: "translate",
Usage: l.Get("translate-usage"),
ArgsUsage: "[text|-]",
Aliases: []string{"tr"},
Action: translateAction,
Flags: translateFlags(),
}
}, nil
}

func translateFlags() []cli.Flag {
l := lang.GetLocalize()
sdkTranslate := sdk.GetSdkTranslate()

return []cli.Flag{
&cli.StringFlag{
Name: "sdk",
Aliases: []string{"S"},
Usage: l.Get("sdk-usage"),
DefaultText: sdkTranslate.GetName(),
Category: "global",
Action: func(c *cli.Context, value string) error {
if err := sdk.InitSdkTranslate(value); err != nil {
return err
Expand All @@ -36,19 +43,23 @@ func translateFlags() []cli.Flag {
},
},
&cli.StringFlag{
Name: "source",
Aliases: []string{"s"},
Usage: l.Get("translate-source-usage"),
Name: "source",
Aliases: []string{"s"},
Usage: l.Get("translate-source-usage"),
Category: "translate",
Action: func(c *cli.Context, value string) error {
sdkTranslate := sdk.GetSdkTranslate()
sdkTranslate.SetSourceLang(value)
return nil
},
},
&cli.StringFlag{
Name: "target",
Aliases: []string{"t"},
Usage: l.Get("translate-target-usage"),
Name: "target",
Aliases: []string{"t"},
Usage: l.Get("translate-target-usage"),
Category: "translate",
Action: func(c *cli.Context, value string) error {
sdkTranslate := sdk.GetSdkTranslate()
sdkTranslate.SetTargetLang(value)
return nil
},
Expand Down
3 changes: 2 additions & 1 deletion lang/en.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ var EN_STRINGS = LangString{
"no-command": "No command provided",
"unknown-sdk": "Unknown sdk \"%s\"",
"sdk-model-usage": "Select a model",
"inerte-usage": "Do not make API call",
"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-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",
Expand Down
3 changes: 2 additions & 1 deletion lang/fr.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ var FR_STRINGS = LangString{
"no-command": "Aucune commande fournie",
"unknown-sdk": "Sdk inconnu \"%s\"",
"sdk-model-usage": "Sélectionner un modèle",
"inerte-usage": "N'effectue pas d'appel à l'API",
"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-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",
Expand Down
23 changes: 15 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/LordPax/aicli/commands"
"github.com/LordPax/aicli/config"
"github.com/LordPax/aicli/lang"
"github.com/LordPax/aicli/sdk"
"github.com/LordPax/aicli/utils"

cli "github.com/urfave/cli/v2"
Expand Down Expand Up @@ -38,20 +37,28 @@ func main() {
l.AddStrings(&lang.EN_STRINGS, "en_US.UTF-8", "en_GB.UTF-8", "en")
l.AddStrings(&lang.FR_STRINGS, "fr_FR.UTF-8", "fr_CA.UTF-8", "fr")

if err := sdk.InitSdk(); err != nil {
log.PrintfErr("%v\n", err)
os.Exit(1)
}

app := cli.NewApp()
app.Name = config.NAME
app.Usage = l.Get("usage")
app.Version = config.VERSION
app.Action = commands.MainAction
app.Flags = commands.MainFlags()

textCmd, err := commands.TextCommand()
if err != nil {
log.PrintfErr("%v\n", err)
os.Exit(1)
}

translateCmd, err := commands.TranslateCommand()
if err != nil {
log.PrintfErr("%v\n", err)
os.Exit(1)
}

app.Commands = []*cli.Command{
commands.TextCommand(),
commands.TranslateCommand(),
textCmd,
translateCmd,
// TODO : add command for image, audio and translate
}

Expand Down
14 changes: 11 additions & 3 deletions sdk/claude.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func NewClaudeText(apiKey, model string, temp float64) (*ClaudeText, error) {
Name: "claude",
ApiUrl: "https://api.anthropic.com/v1/messages",
ApiKey: apiKey,
Inerte: false,
},
SdkText: SdkText{
Model: "claude-3-5-sonnet-20240620",
Expand All @@ -68,14 +69,21 @@ func NewClaudeText(apiKey, model string, temp float64) (*ClaudeText, error) {
func (c *ClaudeText) SendRequest(text string) (Message, error) {
var textResponse ClaudeResponse

c.AppendHistory("user", text)
if text != "" {
c.AppendHistory("user", text)
}

test := c.GetHistory()
if c.GetInerte() {
if err := c.SaveHistory(); err != nil {
return Message{}, err
}
return Message{}, nil
}

jsonBody, err := json.Marshal(ClaudeBody{
Model: c.Model,
MaxTokens: 1024,
Messages: test,
Messages: c.GetHistory(),
})
if err != nil {
return Message{}, err
Expand Down
1 change: 1 addition & 0 deletions sdk/deepl.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func NewDeepL(apiKey string) (*DeepL, error) {
Name: "deepl",
ApiUrl: "https://api-free.deepl.com/v2/translate",
ApiKey: apiKey,
Inerte: false,
},
SdkTranslate: SdkTranslate{
SourceLang: "",
Expand Down
12 changes: 11 additions & 1 deletion sdk/mistral.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func NewMistralText(apiKey, model string, temp float64) (*MistralText, error) {
Name: "mistral",
ApiUrl: "https://api.mistral.ai/v1/chat/completions",
ApiKey: apiKey,
Inerte: false,
},
SdkText: SdkText{
Model: "mistral-medium",
Expand All @@ -56,7 +57,16 @@ func NewMistralText(apiKey, model string, temp float64) (*MistralText, error) {
func (m *MistralText) SendRequest(text string) (Message, error) {
var textResponse OpenaiResponse

m.AppendHistory("user", text)
if text != "" {
m.AppendHistory("user", text)
}

if m.GetInerte() {
if err := m.SaveHistory(); err != nil {
return Message{}, err
}
return Message{}, nil
}

jsonBody, err := json.Marshal(OpenaiBody{
Model: m.Model,
Expand Down
12 changes: 11 additions & 1 deletion sdk/openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func NewOpenaiText(apiKey, model string, temp float64) (*OpenaiText, error) {
Name: "openai",
ApiUrl: "https://api.openai.com/v1/chat/completions",
ApiKey: apiKey,
Inerte: false,
},
SdkText: SdkText{
Model: "gpt-4",
Expand All @@ -77,7 +78,16 @@ func NewOpenaiText(apiKey, model string, temp float64) (*OpenaiText, error) {
func (o *OpenaiText) SendRequest(text string) (Message, error) {
var textResponse OpenaiResponse

o.AppendHistory("user", text)
if text != "" {
o.AppendHistory("user", text)
}

if o.GetInerte() {
if err := o.SaveHistory(); err != nil {
return Message{}, err
}
return Message{}, nil
}

jsonBody, err := json.Marshal(OpenaiBody{
Model: o.Model,
Expand Down
Loading

0 comments on commit f989620

Please sign in to comment.