Skip to content

Commit

Permalink
feat(translate) : add deepl sdk for translate
Browse files Browse the repository at this point in the history
  • Loading branch information
LordPax committed Sep 22, 2024
1 parent 2cd6d87 commit 2ca3a65
Show file tree
Hide file tree
Showing 13 changed files with 476 additions and 137 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Added

* Add interactive mode for translate command
* Add sdk deepl for translate command
* Add interactive mode for text command
* Add parameter for text command
* `sdk` : Select the sdk to use
Expand Down
6 changes: 3 additions & 3 deletions commands/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ func TextCommand() *cli.Command {
ArgsUsage: "[prompt|-]",
Aliases: []string{"t"},
Action: textAction,
Flags: TextFlags(),
Flags: textFlags(),
}
}

func TextFlags() []cli.Flag {
func textFlags() []cli.Flag {
l := lang.GetLocalize()
textSdk := sdk.GetSdkText()

return []cli.Flag{
&cli.StringFlag{
Name: "sdk",
Aliases: []string{"S"},
Usage: l.Get("text-sdk-usage"),
Usage: l.Get("sdk-usage"),
DefaultText: textSdk.GetName(),
Action: func(c *cli.Context, value string) error {
if err := sdk.InitSdkText(value); err != nil {
Expand Down
70 changes: 70 additions & 0 deletions commands/translate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package commands

import (
"github.com/LordPax/aicli/lang"
"github.com/LordPax/aicli/sdk"
"github.com/LordPax/aicli/service"
cli "github.com/urfave/cli/v2"
)

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

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(),
Action: func(c *cli.Context, value string) error {
if err := sdk.InitSdkTranslate(value); err != nil {
return err
}
return nil
},
},
&cli.StringFlag{
Name: "source",
Aliases: []string{"s"},
Usage: l.Get("translate-source-usage"),
Action: func(c *cli.Context, value string) error {
sdkTranslate.SetSourceLang(value)
return nil
},
},
&cli.StringFlag{
Name: "target",
Aliases: []string{"t"},
Usage: l.Get("translate-target-usage"),
Action: func(c *cli.Context, value string) error {
sdkTranslate.SetTargetLang(value)
return nil
},
},
}
}

func translateAction(c *cli.Context) error {
text := c.Args().First()

if c.NArg() == 0 {
if err := service.TranslateInteractiveMode(); err != nil {
return err
}
return nil
}

return service.TranslateText(text)
}
47 changes: 26 additions & 21 deletions lang/en.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,30 @@ 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",
"text-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,
"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",
"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",
}
47 changes: 26 additions & 21 deletions lang/fr.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,30 @@ 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",
"text-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,
"text-list-history-usage": "Lister l'historique",
"type-required": "Le type est requis",
"apiKey-required": "La clé API est requise",
"empty-file": "Le fichier est vide",
"empty-history": "L'historique \"%s\" est vide\n",
"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",
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func main() {
app.Flags = commands.MainFlags()
app.Commands = []*cli.Command{
commands.TextCommand(),
commands.TranslateCommand(),
// TODO : add command for image, audio and translate
}

Expand Down
4 changes: 2 additions & 2 deletions sdk/claude.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ func NewClaudeText(apiKey, model string, temp float64) (*ClaudeText, error) {
Name: "claude",
ApiUrl: "https://api.anthropic.com/v1/messages",
ApiKey: apiKey,
Model: "claude-3-5-sonnet-20240620",
},
SdkText: SdkText{
Temp: 0.7,
Model: "claude-3-5-sonnet-20240620",
Temp: 0.7,
},
TextHistory: *history,
}
Expand Down
87 changes: 87 additions & 0 deletions sdk/deepl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package sdk

import (
"encoding/json"
"errors"
"io"
"net/http"

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

type DeepLBody struct {
Text []string `json:"text"`
TargetLang string `json:"target_lang"`
SourceLang string `json:"source_lang"`
}

type DeepLResponse struct {
Translations []struct {
DetectedSourceLanguage string `json:"detected_source_language"`
Text string `json:"text"`
} `json:"translations"`
}

type DeepLError struct {
Message string `json:"message"`
}

type DeepL struct {
Sdk
SdkTranslate
}

func NewDeepL(apiKey string) (*DeepL, error) {
return &DeepL{
Sdk: Sdk{
Name: "deepl",
ApiUrl: "https://api-free.deepl.com/v2/translate",
ApiKey: apiKey,
},
SdkTranslate: SdkTranslate{
SourceLang: "",
TargetLang: "",
},
}, nil
}

func (d *DeepL) SendRequest(text string) (string, error) {
var deeplResponse DeepLResponse

jsonBody, err := json.Marshal(DeepLBody{
Text: []string{text},
TargetLang: d.GetTargetLang(),
SourceLang: d.GetSourceLang(),
})
if err != nil {
return "", err
}

resp, err := utils.PostRequest(d.ApiUrl, jsonBody, map[string]string{
"Content-Type": "application/json",
"Authorization": "DeepL-Auth-Key " + d.ApiKey,
})
if err != nil {
return "", err
}
defer resp.Body.Close()

respBody, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}

if resp.StatusCode != http.StatusOK {
var deepLError DeepLError
if err := json.Unmarshal(respBody, &deepLError); err != nil {
return "", err
}
return "", errors.New(deepLError.Message)
}

if err := json.Unmarshal(respBody, &deeplResponse); err != nil {
return "", err
}

return deeplResponse.Translations[0].Text, nil
}
4 changes: 2 additions & 2 deletions sdk/openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ func NewOpenaiText(apiKey, model string, temp float64) (*OpenaiText, error) {
Name: "openai",
ApiUrl: "https://api.openai.com/v1/chat/completions",
ApiKey: apiKey,
Model: "gpt-4",
},
SdkText: SdkText{
Temp: 0.7,
Model: "gpt-4",
Temp: 0.7,
},
TextHistory: *history,
}
Expand Down
Loading

0 comments on commit 2ca3a65

Please sign in to comment.