<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged mistral</title>
<link>https://coderlegion.com/tag/mistral</link>
<description>Powered by Question2Answer</description>
<item>
<title>Build A Real-Time Voice Assistant with Mistral and FastRTC</title>
<link>https://coderlegion.com/4047/build-a-real-time-voice-assistant-with-mistral-and-fastrtc</link>
<description>&lt;p&gt;&lt;img src=&quot;https://coderlegion.com/?qa=blob&amp;amp;qa_blobid=7335495914467226737&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;In this post, I will demonstrate how to build a real-time voice assistant using Mistral AI and FastRTC.&lt;/p&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://mistral.ai/&quot;&gt;Mistral AI&lt;/a&gt; is one of the leading LLM providers, and they have made their LLM API easily accessible to developers.&lt;/p&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://fastrtc.org/&quot;&gt;FastRTC&lt;/a&gt;, on the other hand, is a real-time communication library for Python that enables you to quickly turn any Python function into real-time audio and video stream over WebRTC or WebSockets.&lt;/p&gt;
&lt;h3 id=&quot;building-a-real-time-voice-assistant&quot;&gt;&lt;strong&gt;Building A Real-time Voice Assistant&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;First, let's install the required libraries by running the code below in your terminal&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;pip install mistalai fastrtc
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Next, set an environment variable and import the libraries. Create a &lt;code&gt;.env&lt;/code&gt; file in your project and save your Mistral API key there&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;MISTRAL_API_KEY = &quot;&amp;lt;your-api-key&amp;gt;&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Import the libraries&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;from mistralai import Mistral
from fastrtc import (ReplyOnPause, Stream, get_stt_model, get_tts_model)
from dotenv import load_dotenv
import os

load_dotenv()
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To get your Mistral API key, you will need to create an account on their &lt;a rel=&quot;nofollow&quot; href=&quot;https://admin.mistral.ai/organization/api-keys&quot;&gt;website&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;In the above, we have imported Mistral and the specific methods that we need from FastRTC namely &lt;code&gt;ReplyOnPause()&lt;/code&gt;, &lt;code&gt;Stream()&lt;/code&gt;, &lt;code&gt;get_stt_model()&lt;/code&gt;, and &lt;code&gt;get_tts_model()&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;ReplyOnPause()&lt;/code&gt;: This method takes a Python audio function. It monitors the audio, and when it detects a pause, it takes it as a cue to give a reply.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;Stream()&lt;/code&gt;: This method streams the audio reply.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;get_stt_model()&lt;/code&gt;: This is used to access the speech-to-text model that is used to convert audio to text.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;get_tts_model()&lt;/code&gt;: This is used to access the text-to-speech model that is used to convert text back into audio.&lt;/p&gt;
&lt;p&gt;Now, let's activate the Mistral client with our API key stored in the &lt;code&gt;.env&lt;/code&gt; file&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;api_key = os.environ[&quot;MISTRAL_API_KEY&quot;]
model = &quot;mistral-large-latest&quot;

client = Mistral(api_key=api_key)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here we are using Mistral large model. However, you can try out other Mistral models too. Additionally, you can actually plug any LLM into FastRTC and receive real-time voice responses.&lt;/p&gt;
&lt;p&gt;We will now build the audio function that will take a prompt and return a response&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;stt_model = get_stt_model()
tts_model = get_tts_model()


def echo(audio):
    prompt = stt_model.stt(audio)
    chat_response = client.chat.complete(
    model = model,
    messages = [
        {
            &quot;role&quot;: &quot;user&quot;,
            &quot;content&quot;: f&quot;{prompt}&quot;
        },
      ]
    )

    for audio_chunk in tts_model.stream_tts_sync(chat_response.choices[0].message.content):
        yield audio_chunk
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Above, we wrote a function called &lt;code&gt;echo&lt;/code&gt;, and the function takes an audio input, then passes that to the speech-to-text method, which is converted to a user prompt and given to the LLM. The response from the LLM is then passed to a text-to-speech method and is streamed synchronously.&lt;/p&gt;
&lt;p&gt;Finally, we will run the application&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;stream = Stream(ReplyOnPause(echo), modality=&quot;audio&quot;, mode=&quot;send-receive&quot;)
stream.ui.launch() 
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This will launch the UI below at the URL: &lt;code&gt;http://127.0.0.1:7860/&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://coderlegion.com/?qa=blob&amp;amp;qa_blobid=3898391398304302750&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;Now, you can activate the microphone and say something to your assistant, who will give you a reply immediately.&lt;/p&gt;
&lt;h3 id=&quot;change-voice&quot;&gt;&lt;strong&gt;Change Voice&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;If you do not like the default voice, you can change that by passing an instance of &lt;code&gt;KokoroTTSOptions()&lt;/code&gt; to the text-to-speech method.&lt;/p&gt;
&lt;p&gt;First import &lt;code&gt;KokoroTTSOptions()&lt;/code&gt; from FastRTC by adding it to the import tuple&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;from fastrtc import (ReplyOnPause, Stream, get_stt_model, get_tts_model, KokoroTTSOptions)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Next, define the options&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;tts_model = get_tts_model(model=&quot;kokoro&quot;)

options = KokoroTTSOptions(
    voice=&quot;af_bella&quot;,
    speed=1.0,
    lang=&quot;en-us&quot;
)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then pass the options to the text-to-speech method in your audio function&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;for audio_chunk in tts_model.stream_tts_sync(chat_response.choices[0].message.content, options = options)
        yield audio_chunk
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For more voice options, you can check out &lt;a rel=&quot;nofollow&quot; href=&quot;https://huggingface.co/hexgrad/Kokoro-82M/blob/main/VOICES.md&quot;&gt;KokoroTTS documentation&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&quot;complete-project-code&quot;&gt;&lt;strong&gt;Complete Project Code&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Here is the complete code that we have used to create the real-time voice assistant&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;import os
from fastrtc import (ReplyOnPause, Stream, get_stt_model, get_tts_model, KokoroTTSOptions)
from dotenv import load_dotenv
from mistralai import Mistral

load_dotenv()

api_key = os.environ[&quot;MISTRAL_API_KEY&quot;]
model = &quot;mistral-large-latest&quot;

client = Mistral(api_key=api_key)

options = KokoroTTSOptions(
    voice=&quot;af_bella&quot;,
    speed=1.0,
    lang=&quot;en-us&quot;
)

stt_model = get_stt_model()
tts_model = get_tts_model(model=&quot;kokoro&quot;)

def echo(audio):
    prompt = stt_model.stt(audio)
    chat_response = client.chat.complete(
    model = model,
    messages = [

        {
            &quot;role&quot;: &quot;user&quot;,
            &quot;content&quot;: f&quot;{prompt}&quot;
        },
      ]
    )

    for audio_chunk in tts_model.stream_tts_sync(chat_response.choices[0].message.content, options=options):
        yield audio_chunk

if__name__==&quot;__main__&quot;:
       stream = Stream(ReplyOnPause(echo), modality=&quot;audio&quot;, mode=&quot;send-receive&quot;)
       stream.ui.launch()
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, instead of typing a prompt, you can give voice commands to an LLM and have it speak its response, just like a natural human conversation.&lt;/p&gt;
&lt;p&gt;I hope you found this post useful. If you did, please share it with others; it might benefit them, too. Thanks for reading!&lt;/p&gt;
&lt;p&gt;You can follow me on the following platforms:&lt;/p&gt;
&lt;p&gt;GitHub: &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/Ifeanyi55&quot;&gt;Ifeanyi55&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;X: &lt;a rel=&quot;nofollow&quot; href=&quot;https://x.com/Ifeanyidiaye&quot;&gt;iCode2&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;LinkedIn: &lt;a rel=&quot;nofollow&quot; href=&quot;https://www.linkedin.com/in/ifeanyi-idiaye-09523abb/&quot;&gt;Ifeanyi Idiaye&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;You can also visit my website: &lt;a rel=&quot;nofollow&quot; href=&quot;https://ifeanyi-idiaye.netlify.app/&quot;&gt;https://ifeanyi-idiaye.netlify.app/&lt;/a&gt;&lt;/p&gt;
</description>
<category>Automation</category>
<guid isPermaLink="true">https://coderlegion.com/4047/build-a-real-time-voice-assistant-with-mistral-and-fastrtc</guid>
<pubDate>Tue, 15 Jul 2025 19:34:36 +0000</pubDate>
</item>
</channel>
</rss>