module
CrystalMistral::Methods::Chat
Direct including types
Defined in:
crystal-mistral/methods/chat.crInstance Method Summary
-
#chat(model : String, messages : Array(Messages) | String, temperature : Float32 = 1) : ChatResponse
Sends a chat completion request to the Mistral API.
Instance Method Detail
def chat(model : String, messages : Array(Messages) | String, temperature : Float32 = 1) : ChatResponse
#
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 via
handle_error
if the response indicates an error
Example:
require "crystal-mistral"
# Use custom Messages type
messages = [
Messages.new(
role: Role::User,
content: "Hello, Mistral!"
),
]
# or String type
messages = %([{"role": "user", "content": "Best place in Norilsk"}])
# if you just pass the string
messages = “AAA WHAT TIME IS IT?!”
# string convert to -> [Messages.new(role: "user", content: messages)]
client = CrystalMistral::Client.new
response = client.chat(
model: "mistral-large-latest",
messages: messages,
temperature: 0.2_f32
)
puts response.choices[0].message.content