generated from LordPax/cli-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
369 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,26 @@ | ||
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-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", | ||
"type-required": "Type is required", | ||
"apiKey-required": "API key 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", | ||
"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", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,26 @@ | ||
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-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", | ||
"type-required": "Le type est requis", | ||
"apiKey-required": "La clé API 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", | ||
"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", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package sdk | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/LordPax/aicli/utils" | ||
) | ||
|
||
type ClaudeResponse struct { | ||
Role string `json:"role"` | ||
Content []Content `json:"content"` | ||
} | ||
|
||
type ClaudeText struct { | ||
Sdk | ||
SdkText | ||
} | ||
|
||
// Initialize ClaudeText struct, inheriting from Sdk and SdkText | ||
func NewClaudeText(apiKey, model string, temp float64) (*ClaudeText, error) { | ||
sdkService := &ClaudeText{ | ||
Sdk: Sdk{ | ||
ApiUrl: "https://api.anthropic.com/v1/messages", | ||
ApiKey: apiKey, | ||
Model: "claude-3-5-sonnet-20240620", | ||
}, | ||
SdkText: SdkText{ | ||
History: make(map[string][]Message), | ||
SelectedHistory: "default", | ||
Temp: 0.7, | ||
}, | ||
} | ||
|
||
if model != "" { | ||
sdkService.Model = model | ||
} | ||
|
||
if temp != 0 { | ||
sdkService.Temp = temp | ||
} | ||
|
||
if err := sdkService.LoadHistory(); err != nil { | ||
return nil, err | ||
} | ||
|
||
return sdkService, nil | ||
} | ||
|
||
func (c *ClaudeText) SendRequest(text string) (Message, error) { | ||
var textResponse ClaudeResponse | ||
|
||
c.AppendHistory("user", text) | ||
|
||
jsonBody, err := json.Marshal(TextBody{ | ||
Model: c.Model, | ||
// MaxTokens: 1024, | ||
Messages: c.GetHistory(), | ||
}) | ||
if err != nil { | ||
return Message{}, err | ||
} | ||
|
||
resp, err := utils.PostRequest(c.ApiUrl, jsonBody, map[string]string{ | ||
"Content-Type": "application/json", | ||
"anthropic-version": "2023-06-01", | ||
"x-api-key": c.ApiKey, | ||
}) | ||
if err != nil { | ||
return Message{}, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
respBody, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return Message{}, err | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
var errorMsg ErrorMsg | ||
if err := json.Unmarshal(respBody, &errorMsg); err != nil { | ||
return Message{}, err | ||
} | ||
return Message{}, errors.New(errorMsg.Error.Message) | ||
} | ||
|
||
if err := json.Unmarshal(respBody, &textResponse); err != nil { | ||
return Message{}, err | ||
} | ||
|
||
respMessage := c.AppendHistory(textResponse.Role, textResponse.Content[0].Text) | ||
|
||
if err := c.SaveHistory(); err != nil { | ||
return Message{}, err | ||
} | ||
|
||
return respMessage, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.