Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
vineel opened this issue Nov 1, 2024 · 2 comments

Comments

@vineel
Copy link

vineel commented Nov 1, 2024

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
Copy link
Collaborator

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

@vineel
Copy link
Author

vineel commented Nov 4, 2024

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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants