|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + // Packages |
| 8 | + |
| 9 | + "github.com/djthorpe/go-tablewriter" |
| 10 | + "github.com/mutablelogic/go-client" |
| 11 | + "github.com/mutablelogic/go-client/pkg/mistral" |
| 12 | + "github.com/mutablelogic/go-client/pkg/openai/schema" |
| 13 | +) |
| 14 | + |
| 15 | +/////////////////////////////////////////////////////////////////////////////// |
| 16 | +// GLOBALS |
| 17 | + |
| 18 | +var ( |
| 19 | + mistralName = "mistral" |
| 20 | + mistralClient *mistral.Client |
| 21 | + mistralModel string |
| 22 | + mistralEncodingFormat string |
| 23 | + mistralTemperature *float64 |
| 24 | + mistralMaxTokens *uint64 |
| 25 | + mistralStream *bool |
| 26 | + mistralSafePrompt bool |
| 27 | + mistralSeed *uint64 |
| 28 | + mistralSystemPrompt string |
| 29 | +) |
| 30 | + |
| 31 | +/////////////////////////////////////////////////////////////////////////////// |
| 32 | +// LIFECYCLE |
| 33 | + |
| 34 | +func mistralRegister(flags *Flags) { |
| 35 | + // Register flags required |
| 36 | + flags.String(mistralName, "mistral-api-key", "${MISTRAL_API_KEY}", "API Key") |
| 37 | + flags.String(mistralName, "model", "", "Model to use") |
| 38 | + flags.String(mistralName, "encoding-format", "", "The format of the output data") |
| 39 | + flags.String(mistralName, "system", "", "Provide a system prompt to the model") |
| 40 | + flags.Float(mistralName, "temperature", 0, "Sampling temperature to use, between 0.0 and 1.0") |
| 41 | + flags.Unsigned(mistralName, "max-tokens", 0, "Maximum number of tokens to generate") |
| 42 | + flags.Bool(mistralName, "stream", false, "Stream output") |
| 43 | + flags.Bool(mistralName, "safe-prompt", false, "Inject a safety prompt before all conversations.") |
| 44 | + flags.Unsigned(mistralName, "seed", 0, "Set random seed") |
| 45 | + |
| 46 | + flags.Register(Cmd{ |
| 47 | + Name: mistralName, |
| 48 | + Description: "Interact with Mistral models, from https://docs.mistral.ai/api/", |
| 49 | + Parse: mistralParse, |
| 50 | + Fn: []Fn{ |
| 51 | + {Name: "models", Call: mistralModels, Description: "Gets a list of available models"}, |
| 52 | + {Name: "embeddings", Call: mistralEmbeddings, Description: "Create embeddings from text", MinArgs: 1, Syntax: "<text>..."}, |
| 53 | + {Name: "chat", Call: mistralChat, Description: "Create a chat completion", MinArgs: 1, Syntax: "<text>..."}, |
| 54 | + }, |
| 55 | + }) |
| 56 | +} |
| 57 | + |
| 58 | +func mistralParse(flags *Flags, opts ...client.ClientOpt) error { |
| 59 | + apiKey := flags.GetString("mistral-api-key") |
| 60 | + if apiKey == "" { |
| 61 | + return fmt.Errorf("missing -mistral-api-key flag") |
| 62 | + } else if client, err := mistral.New(apiKey, opts...); err != nil { |
| 63 | + return err |
| 64 | + } else { |
| 65 | + mistralClient = client |
| 66 | + } |
| 67 | + |
| 68 | + // Get the command-line parameters |
| 69 | + mistralModel = flags.GetString("model") |
| 70 | + mistralEncodingFormat = flags.GetString("encoding-format") |
| 71 | + mistralSafePrompt = flags.GetBool("safe-prompt") |
| 72 | + mistralSystemPrompt = flags.GetString("system") |
| 73 | + if temp, err := flags.GetValue("temperature"); err == nil { |
| 74 | + t := temp.(float64) |
| 75 | + mistralTemperature = &t |
| 76 | + } |
| 77 | + if maxtokens, err := flags.GetValue("max-tokens"); err == nil { |
| 78 | + t := maxtokens.(uint64) |
| 79 | + mistralMaxTokens = &t |
| 80 | + } |
| 81 | + if stream, err := flags.GetValue("stream"); err == nil { |
| 82 | + t := stream.(bool) |
| 83 | + mistralStream = &t |
| 84 | + } |
| 85 | + if seed, err := flags.GetValue("seed"); err == nil { |
| 86 | + t := seed.(uint64) |
| 87 | + mistralSeed = &t |
| 88 | + } |
| 89 | + |
| 90 | + // Return success |
| 91 | + return nil |
| 92 | +} |
| 93 | + |
| 94 | +/////////////////////////////////////////////////////////////////////////////// |
| 95 | +// METHODS |
| 96 | + |
| 97 | +func mistralModels(ctx context.Context, writer *tablewriter.Writer, args []string) error { |
| 98 | + // Get models |
| 99 | + models, err := mistralClient.ListModels() |
| 100 | + if err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + |
| 104 | + return writer.Write(models) |
| 105 | +} |
| 106 | + |
| 107 | +func mistralEmbeddings(ctx context.Context, writer *tablewriter.Writer, args []string) error { |
| 108 | + // Set options |
| 109 | + opts := []mistral.Opt{} |
| 110 | + if mistralModel != "" { |
| 111 | + opts = append(opts, mistral.OptModel(mistralModel)) |
| 112 | + } |
| 113 | + if mistralEncodingFormat != "" { |
| 114 | + opts = append(opts, mistral.OptEncodingFormat(mistralEncodingFormat)) |
| 115 | + } |
| 116 | + |
| 117 | + // Get embeddings |
| 118 | + embeddings, err := mistralClient.CreateEmbedding(args, opts...) |
| 119 | + if err != nil { |
| 120 | + return err |
| 121 | + } |
| 122 | + return writer.Write(embeddings) |
| 123 | +} |
| 124 | + |
| 125 | +func mistralChat(ctx context.Context, w *tablewriter.Writer, args []string) error { |
| 126 | + var messages []*schema.Message |
| 127 | + |
| 128 | + // Set options |
| 129 | + opts := []mistral.Opt{} |
| 130 | + if mistralModel != "" { |
| 131 | + opts = append(opts, mistral.OptModel(mistralModel)) |
| 132 | + } |
| 133 | + if mistralTemperature != nil { |
| 134 | + opts = append(opts, mistral.OptTemperature(*mistralTemperature)) |
| 135 | + } |
| 136 | + if mistralMaxTokens != nil { |
| 137 | + opts = append(opts, mistral.OptMaxTokens(int(*mistralMaxTokens))) |
| 138 | + } |
| 139 | + if mistralStream != nil { |
| 140 | + opts = append(opts, mistral.OptStream(func() { |
| 141 | + fmt.Println("STREAM") |
| 142 | + })) |
| 143 | + } |
| 144 | + if mistralSafePrompt { |
| 145 | + opts = append(opts, mistral.OptSafePrompt()) |
| 146 | + } |
| 147 | + if mistralSeed != nil { |
| 148 | + opts = append(opts, mistral.OptSeed(int(*mistralSeed))) |
| 149 | + } |
| 150 | + if mistralSystemPrompt != "" { |
| 151 | + messages = append(messages, schema.NewMessage("system").Add(schema.Text(mistralSystemPrompt))) |
| 152 | + } |
| 153 | + |
| 154 | + // Append user message |
| 155 | + message := schema.NewMessage("user") |
| 156 | + for _, arg := range args { |
| 157 | + message.Add(schema.Text(arg)) |
| 158 | + } |
| 159 | + messages = append(messages, message) |
| 160 | + |
| 161 | + // Request -> Response |
| 162 | + responses, err := mistralClient.Chat(ctx, messages, opts...) |
| 163 | + if err != nil { |
| 164 | + return err |
| 165 | + } |
| 166 | + |
| 167 | + // Write table |
| 168 | + return w.Write(responses) |
| 169 | +} |
0 commit comments