mistralai_dart 0.0.3+4 copy "mistralai_dart: ^0.0.3+4" to clipboard
mistralai_dart: ^0.0.3+4 copied to clipboard

Dart Client for the Mistral AI API (Mistral-7B, Mixtral 8x7B, embeddings, etc.).

example/mistralai_dart_example.dart

// ignore_for_file: avoid_print
import 'dart:io';

import 'package:mistralai_dart/mistralai_dart.dart';

Future<void> main() async {
  final client = MistralAIClient(
    apiKey: Platform.environment['MISTRAL_API_KEY'],
  );

  // Chat Completions
  await _createChatCompletion(client);
  await _createChatCompletionStream(client);

  // Embeddings
  await _createEmbedding(client);

  // Models
  await _listModels(client);

  client.endSession();
}

Future<void> _createChatCompletion(final MistralAIClient client) async {
  final res = await client.createChatCompletion(
    request: const ChatCompletionRequest(
      model: ChatCompletionModel.model(ChatCompletionModels.mistralMedium),
      temperature: 0,
      messages: [
        ChatCompletionMessage(
          role: ChatCompletionMessageRole.user,
          content: 'Why is the sky blue?',
        ),
      ],
    ),
  );
  print(res.choices.first.message?.content);
  // The sky appears blue due to a phenomenon called Rayleigh scattering...
}

Future<void> _createChatCompletionStream(final MistralAIClient client) async {
  final stream = client.createChatCompletionStream(
    request: const ChatCompletionRequest(
      model: ChatCompletionModel.model(ChatCompletionModels.mistralMedium),
      temperature: 0,
      messages: [
        ChatCompletionMessage(
          role: ChatCompletionMessageRole.user,
          content: 'Why is the sky blue?',
        ),
      ],
    ),
  );
  var text = '';
  await for (final res in stream) {
    text += res.choices.first.delta.content?.trim() ?? '';
  }
  print(text);
  // The sky appears blue due to a phenomenon called Rayleigh scattering...
}

Future<void> _createEmbedding(final MistralAIClient client) async {
  final generated = await client.createEmbedding(
    request: const EmbeddingRequest(
      model: EmbeddingModel.model(EmbeddingModels.mistralEmbed),
      input: ['Why is the sky blue?'],
    ),
  );
  print(generated.data.first.embedding);
  // [-0.0182342529296875, 0.03594970703125, 0.0286102294921875, ...]
}

Future<void> _listModels(final MistralAIClient client) async {
  final res = await client.listModels();
  print(res.data);
  // [Model(id: mistral-medium, object: model, created: 1702396611, ownedBy: mistralai), ...]
}
6
likes
160
points
10
downloads

Publisher

verified publisherlangchaindart.dev

Weekly Downloads

Dart Client for the Mistral AI API (Mistral-7B, Mixtral 8x7B, embeddings, etc.).

Homepage
Repository (GitHub)
View/report issues
Contributing

Topics

#ai #nlp #llms #mistral

Documentation

Documentation
API reference

License

MIT (license)

Dependencies

fetch_client, freezed_annotation, http, json_annotation, meta

More

Packages that depend on mistralai_dart