Git Product home page Git Product logo

mistralai_client_dart's Introduction

Mistral AI client for Dart

.github/workflows/check.yaml style: very good analysis

Description

This is an unofficial Dart/Flutter client for the Mistral AI API.

The implementation is inspired by the official Mistral AI JS client.

Flutter Flow

For Flutter Flow integration please use the mistralai_client_dart_flutter_flow package.

It's a version of this package that is adapted to work with Flutter Flow.

Installation

dart pub add mistralai_client_dart

Usage

Create client

import 'package:mistralai_client_dart/mistralai_client_dart.dart';

final client = MistralAIClient(apiKey: 'your api key here');

List models

final modelsResult = await client.listModels();
final models = modelsResult.data.map((e) => e.id).toList();
print(models.join(', '));

Chat

final params = ChatParams(
  model: 'mistral-small-latest',
  messages: const [
    ChatMessage(role: 'user', content: 'Hello chat!'),
  ],
);
final chatCompletion = await client.chat(params);
final chatMessage = chatCompletion.choices[0].message;
print(chatMessage);

Chat stream

final stream = client.streamChat(params);
await for (final completionChunk in stream) {
  final chatMessage = completionChunk.choices[0].delta?.content;
  if (chatMessage != null) {
    print(chatMessage);
  }
}

Embeddings

final embeddings = await client.embeddings(
  const EmbeddingParams(
    model: 'mistral-embed',
    input: ['Hello chat!'],
  ),
);
for (final data in embeddings.data) {
  print(data.embedding);
}

Function calling

String retrievePaymentStatus(Map<String, String> data, String transactionId) =>
    '{"status": ${data[transactionId]}}';

final namesToFunctions = {
  'retrievePaymentStatus': (String transactionId) =>
      retrievePaymentStatus(paymentStatusData, transactionId),
};

final tools = [
  const ToolsFunction(
    name: 'retrievePaymentStatus',
    description: 'Get payment status of a transaction',
    parameters: [
      ToolsFunctionParameter(
        name: 'transactionId',
        type: 'string',
        description: 'The transaction ID',
        isRequired: true,
      ),
    ],
  ).toChatParamsFormat(),
];

var chatResponse = await client.chat(
    ChatParams(
        model: 'mistral-large-latest',
        messages: messages,
        tools: tools,
        toolChoice: 'auto',
    ),
);

final toolCall = chatResponse.choices[0].message.toolCalls?[0];
if (toolCall != null && toolCall.type == 'function') {
    final functionName = toolCall.function!.name;
    final functionParams = toolCall.function!.argumentsMap;

    print('calling functionName: $functionName');
    print('functionParams: $functionParams');

    final functionResult = namesToFunctions[functionName]!(
        functionParams['transactionId']! as String,
    );

    messages.add(
        ChatMessage(role: 'tool', content: functionResult, name: functionName),
    );

    chatResponse = await client.chat(
        ChatParams(
            model: model,
            messages: messages,
            tools: tools,
            toolChoice: 'auto',
        ),
    );

    print(chatResponse.choices[0].message.content);
}

Resources

You can check the official Mistral AI docs.

Contributing

For contributing guide please see CONTRIBUTING.md.

mistralai_client_dart's People

Contributors

gawi151 avatar kacperkluka avatar marcingruchala avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

mistralai_client_dart's Issues

[BUG] Package is incompatible with FlutterFlow v4.1

Describe the bug
mistralai_client_dart in version 0.1.0 is using dependencies incompatible with FlutterFlow v4.1:

  • Flutter SDK 3.1.3 used by FlutterFlow does not fit in the required version range >=3.2.0 <4.0.0
  • http dependency 0.13.0 used indirectly by FlutterFlow does not match http 1.2.0 used by mistralai_client_dart.

To Reproduce

  1. Create an empty new flutter flow project
  2. Add custom action depending on mistralai_client_dart
  3. Try to run the project either on the web or locally

Expected behavior
Version resolving completes and project runs

Additional context

Because test_project depends on mistralai_client_dart >=0.0.2 which requires SDK version >=3.2.0 <4.0.0, version solving failed.
Because no versions of mistralai_client_dart match >0.1.0 <0.2.0 and mistralai_client_dart 0.1.0 depends on http ^1.2.0, mistralai_client_dart ^0.1.0 requires http ^1.2.0.
And because google_fonts 4.0.3 depends on http ^0.13.0, mistralai_client_dart ^0.1.0 is incompatible with google_fonts 4.0.3.
So, because test_project depends on both google_fonts 4.0.3 and mistralai_client_dart ^0.1.0, version solving failed.

Support for FlutterFlow

Is your feature request related to a problem? Please describe.
Current version of FlutterFlow (4.1) is incompatible with our package.

Describe the solution you'd like
We want to create a separate special version of package that will allow FlutterFlow users to use it.

Describe alternatives you've considered
Downgrade all dependencies in the main package but that would introduce security vulnerability for existing users of package. So it's better to create a sibling package specially crafted for FlutterFlow.

Additional context
Issue created out of bug report #40

Things to do:

  • create seperate branch for FlutterFlow version of the package
  • downgrade all necessary package/sdk versions so it's compatible with FlutterFlow
  • test it with current version of FlutterFlow
  • update README about support for FlutterFlow
  • publish new package to pub.dev

Add examples for how to use the client library

We need to add example use cases of how to use our client library. Ideally, we should cover and use all public APIs of the client in the examples. The examples should be placed in a separate examples directory.

Document the public API

We need to document the public-facing API of our SDK in order to make it more user-friendly and usable.

At the time of writing this issue, this means that we need to document the methods in the mistralai_client_dart_base.dart file. We can use the official JS client API as a reference.

Refactor and clean up code

After the main features of the SDK are added we should structure and refactor the code to make it more readable and clean.

Can be worked on after the below issues are closed:

Setup lint

Setup lint and code style for the repository

Release 0.1.0

Prepare version 0.1.0 with Function Calling feature.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.