diff --git a/README.md b/README.md index 3b8ecb2..af23aaa 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,11 @@ Below are the list of availible configuration options (as of 2023/11/08): - `You are a helpful voice assistant.` (default) - `custom` +- Conversation Mode (URL Param 'conversationMode'): + + - `true` (default) + - `false` + _Note: These configuration options are additionally displayed within the console log for convenient reference._ ## Docker @@ -83,9 +88,8 @@ docker run -d \ dave ``` -## Future Features +## Future Enhancements -- [ ] Support conversations of more than one message/question. - [ ] Automatically cut off recording after n seconds of silence. ## Acknowledgments diff --git a/index.html b/index.html index afaa7bb..b7de2b0 100644 --- a/index.html +++ b/index.html @@ -137,6 +137,7 @@ console.info("Voice Models (URL Param 'voiceModel'):", VoiceModel); console.info("Chat Models (URL Param 'chatModel'):", ChatModel); console.info("Text-to-Speech Models (URL Param 'textToSpeechModel'):", TextToSpeechModel); + console.info("Conversation Mode (URL Param 'conversationMode'): true/false"); console.groupEnd("Configurable Assistant Settings:") // Get parameters from url @@ -157,6 +158,9 @@ const systemMessage = getUrlParam( "systemMessage", ); + const conversationMode = getUrlParam( + "conversationMode", + ); // Create and run assistant object const assistant = new Assistant({ @@ -165,6 +169,7 @@ chatModel: chatModel || ChatModel.GPT_3_5_TURBO, textToSpeechModel: textToSpeechModel || TextToSpeechModel.TTS_1, systemMessage: systemMessage || "You are a helpful and concise voice assistant.", + conversationMode: (conversationMode ?? "").toLowerCase() !== "false", }); // Display assistant settings @@ -174,6 +179,7 @@ console.info("Chat Model:", assistant.chatModel); console.info("Text-to-Speech Model:", assistant.textToSpeechModel); console.info("System Message:", assistant.systemMessage); + console.info("Conversation Mode:", assistant.conversationMode); console.groupEnd("Configured Assistant Settings:") diff --git a/js/assistant.js b/js/assistant.js index 1f72050..3ebee32 100644 --- a/js/assistant.js +++ b/js/assistant.js @@ -49,6 +49,8 @@ class Assistant { chatModel; textToSpeechModel; systemMessage; + conversationMode; + conversation; state; mediaRecorder; mediaStream; @@ -60,6 +62,7 @@ class Assistant { * @param {string} chatModel Selected OpenAI chat model. * @param {string} textToSpeechModel Selected OpenAI text-to-speech model. * @param {string} systemMessage System message provided alongside prompt to OpenAI chat model. + * @param {boolean} conversationMode Conversation mode enabled. */ constructor({ apiKey, @@ -67,12 +70,15 @@ class Assistant { chatModel, textToSpeechModel, systemMessage, + conversationMode, }) { this.apiKey = apiKey; this.voiceModel = voiceModel; this.chatModel = chatModel; this.textToSpeechModel = textToSpeechModel; this.systemMessage = systemMessage; + this.conversationMode = conversationMode; + this.conversation = []; this.state = State.WAITING; } @@ -197,6 +203,25 @@ class Assistant { ); try { + + // Reset conversation if not in conversation mode + if (!this.conversationMode) { + this.conversation = []; + } + + // Add system message if configured and conversation empty + if (this.systemMessage & this.conversation.length === 0) { + conversation.push({ + role: "system", + content: this.systemMessage, + }); + } + + this.conversation.push({ + role: "user", + content: transcription, + }); + // Send POST request to chat API to get answer to transcription const response = await fetch( "https://api.openai.com/v1/chat/completions", @@ -208,16 +233,7 @@ class Assistant { }, body: JSON.stringify({ model: this.chatModel, - messages: [ - { - role: "system", - content: this.systemMessage, - }, - { - role: "user", - content: transcription, - }, - ], + messages: this.conversation, }), } );