From 649923634ff7302f2e7c58c505cbe5cb3a431c68 Mon Sep 17 00:00:00 2001 From: Bam4d Date: Sat, 9 Dec 2023 08:44:36 +0000 Subject: [PATCH] nicer examples for chat streaming --- examples/chat_no_streaming.js | 4 +--- examples/chat_with_streaming.js | 8 ++++++-- src/client.js | 1 - 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/examples/chat_no_streaming.js b/examples/chat_no_streaming.js index 6f70592..161df4d 100644 --- a/examples/chat_no_streaming.js +++ b/examples/chat_no_streaming.js @@ -9,6 +9,4 @@ const chatResponse = await client.chat({ messages: [{role: 'user', content: 'What is the best French cheese?'}], }); -console.log('Chat:', chatResponse); - -console.log('Chat:', chatResponse.choices[0].message); +console.log('Chat:', chatResponse.choices[0].message.content); diff --git a/examples/chat_with_streaming.js b/examples/chat_with_streaming.js index 4ba5000..191381a 100644 --- a/examples/chat_with_streaming.js +++ b/examples/chat_with_streaming.js @@ -5,10 +5,14 @@ const apiKey = process.env.MISTRAL_API_KEY; const client = new MistralClient(apiKey); const chatStreamResponse = await client.chatStream({ - model: 'mistral-medium', + model: 'mistral-tiny', messages: [{role: 'user', content: 'What is the best French cheese?'}], }); +console.log('Chat Stream:'); for await (const chunk of chatStreamResponse) { - console.log('Chat Stream:', JSON.stringify(chunk)); + if (chunk.choices[0].delta.content !== undefined) { + const streamText = chunk.choices[0].delta.content; + process.stdout.write(streamText); + } } diff --git a/src/client.js b/src/client.js index 1b6456c..6140c04 100644 --- a/src/client.js +++ b/src/client.js @@ -163,7 +163,6 @@ class MistralClient { true, safeMode, ); - console.log(request); const response = await this._request( 'post', 'v1/chat/completions', request, );