class
CrystalMistral::Client
- CrystalMistral::Client
- CrystalMistral::ClientBuilder
- Reference
- Object
Defined in:
crystal-mistral/client.crConstructors
Instance Method Summary
- #api_key : String | Nil
- #api_key=(api_key : String | Nil)
-
#chat(model : String, messages : Array(Messages), temperature : Float32 = 1) : ChatResponse
Sends a chat completion request to the Mistral API.
-
#code(model : String, prompt : String, suffix : String = "", temperature : Float32 = 1) : ChatResponse
Sends a code completion (FIM - Fill-in-the-Middle) request to the Mistral API.
-
#embeddings(model : String, input : Array(String)) : EmbeddingResponse
Sends an embeddings request to the Mistral API.
- #headers
Constructor Detail
Instance Method Detail
Sends a chat completion request to the Mistral API.
Arguments:
- model : String — the model name (e.g., "mistral-large-latest")
- messages : Array(Messages) — conversation history, each message should have a role and content
- temperature : Float32 = 1 — sampling temperature for randomness
Returns:
- ChatResponse — structured response from Mistral with choices and message content
Raises:
- ArgumentError if model is empty
- RuntimeError with details if the API returns an error
Example:
require "crystal-mistral"
messages = [
Message.new(
role: Role::User,
content: "Hello, Mistral!"
),
]
client = CrystalMistral::Client.new
response = client.chat(
model: "mistral-large-latest",
messages: messages,
temperature: 0.2_f32
)
puts response.choices[0].message.content
def code(model : String, prompt : String, suffix : String = "", temperature : Float32 = 1) : ChatResponse
#
Sends a code completion (FIM - Fill-in-the-Middle) request to the Mistral API.
Arguments:
- model : String — the model name for code completion (e.g., "codestral-2405")
- prompt : String — the prefix (before cursor) code snippet
- suffix : String = "" — the suffix (after cursor) code snippet
- temperature : Float32 = 1.0 — controls randomness; lower is more deterministic
Returns:
- ChatResponse — includes the generated code completion
Raises:
- ArgumentError if model or prompt is empty
- RuntimeError if API returns bad request code or unexpected error
Example:
client = CrystalMistral::Client.new
response = client.code(
model: "codestral-2405",
prompt: "def hello(name : String)",
suffix: "",
temperature: 0.7
)
puts "Generated code:\n#{puts resp.choices[0].message.content}"
def embeddings(model : String, input : Array(String)) : EmbeddingResponse
#
Sends an embeddings request to the Mistral API.
Arguments:
- model : String — the model name for embedding (e.g., "mistral-embed")
- input : Array(String) — list of texts to be embedded
Returns:
- EmbeddingResponse — includes vectors for each input
Raises:
- ArgumentError if model is empty
- RuntimeError if API returns bad request code or unexpected error
Example:
input = [
"Crystal is a fast language.",
"Mistral provides language models.",
]
client = CrystalMistral::Client.new
response = client.embeddings(
model: "mistral-embed",
input: input
)
response.data.each_with_index do |embedding, i|
puts "Embedding for input ##{i}: #{embedding.embedding[0..4]}..."
end