Skip to content

Commit

Permalink
Added conversation mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
BadgerHobbs committed Nov 18, 2023
1 parent ccbbe46 commit e1b1cc1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -157,6 +158,9 @@
const systemMessage = getUrlParam(
"systemMessage",
);
const conversationMode = getUrlParam(
"conversationMode",
);

// Create and run assistant object
const assistant = new Assistant({
Expand All @@ -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
Expand All @@ -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:")

</script>
Expand Down
36 changes: 26 additions & 10 deletions js/assistant.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class Assistant {
chatModel;
textToSpeechModel;
systemMessage;
conversationMode;
conversation;
state;
mediaRecorder;
mediaStream;
Expand All @@ -60,19 +62,23 @@ 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,
voiceModel,
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;
}

Expand Down Expand Up @@ -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",
Expand All @@ -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,
}),
}
);
Expand Down

0 comments on commit e1b1cc1

Please sign in to comment.