mistralai / client-ts

TS Client library for Mistral AI platform
Apache License 2.0
30 stars 10 forks source link

Is there an example of streaming a response from multiple statements (like in a chat) #39

Open vineel opened 3 weeks ago

vineel commented 3 weeks ago

Hello! I haven't been able to navigate the type constraints around using mistral.chat.stream . The example in the docs works with 1 message specified in a literal, but when trying to use multiple messages, inputted by the user, in a multi-statement chat... I just can't figure out the types. I have looked at the source/type defs and it seems so complex that I feel I must be missing something simple. Is there an example anywhere that shows how to use it?

For example, this is a very simple extrapolation of the given sample:

import { ChatCompletionStreamRequest, AssistantMessage, UserMessage, UserMessageContent } from '@mistralai/mistralai/models/components';

    const msg: UserMessage = {
        content: "Who is the best French painter? Answer in one short sentence.",
        role: "user",
    };

    let myval: ChatCompletionStreamRequest = {
        model: "mistral-small-latest",
        messages: [msg]
    };

    const result = await mistral.chat.stream(myval);

But Typescript complains...

Type 'UserMessage' is not assignable to type '(SystemMessage & { role: "system"; }) | (UserMessage & { role: "user"; }) | (AssistantMessage & { role: "assistant"; }) | (ToolMessage & { ...; })'.
  Type 'UserMessage' is not assignable to type 'UserMessage & { role: "user"; }'.
    Type 'UserMessage' is not assignable to type '{ role: "user"; }'.
      Types of property 'role' are incompatible.
        Type '"user" | undefined' is not assignable to type '"user"'.
          Type 'undefined' is not assignable to type '"user"'.

In my app, I need to retrieve a chat from the db, add the latest prompt, and stream an answer back to the browser. I was able to get it working in the old JS client, but I ran into some fetch compatibility problems, so I'm trying to upgrade to the TS client.

Thank you!

GaspardBT commented 3 weeks ago

Hey 👋 Thanks for reporting this issue could you tell us which version of the SDKs your are using?

vineel commented 3 weeks ago

Sure. From my package-lock.json...

        "@mistralai/mistralai": "^1.1.0",

For now I'm using a workaround "any" type. From my sample above:

  const msg: UserMessage = {
    content: "Who is the best French painter? Answer in one short sentence.",
    role: "user",
  };
  let messageArr: any = [msg];

  let myval: ChatCompletionStreamRequest = {
    model: "mistral-small-latest",
    messages: messageArr
  };

  const result = await mistral.chat.stream(myval);