from flask import Flask, request from flask_cors import CORS # Necessary for cross-domain requests from groq import Groq app = Flask(__name__) CORS(app) # This is used to allow cross-domain requests during development # Initialize Groq client client = Groq( api_key="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", ) INIT_MESG = [ { "role": "user", "content": """ You are a voice chatbot that responds to human user's speech input. The speech input texts are sometimes broken or hard stop due to the listening mechanism. If the message you read is not complete, please ask the user to repeat or complete politely and concisely. Remember you are speaking not writing, so please use oral expression in plain language. """, }, { "role": "assistant", "content": "OK, I understood.", }, ] history_messages = INIT_MESG.copy() @app.route('/process-speech', methods=['POST']) def process_speech(): data = request.json user_text = data['text'] history_messages.append({"role": "user", "content": user_text}) completion = client.chat.completions.create( model="mixtral-8x7b-32768", messages=history_messages, ) ai_response = completion.choices[0].message.content history_messages.append({"role": "assistant", "content": ai_response}) return {'response': ai_response} @app.route('/start-speech', methods=['POST']) def start_speech(): global history_messages history_messages = INIT_MESG.copy() return {'response': 'OK'} if __name__ == '__main__': app.run(debug=True)