Skip to content

Commit

Permalink
Fix: Setting changes in any BotSettings derived from CommonBotSetting…
Browse files Browse the repository at this point in the history
…s do not take effect on the fly (#686)

* fix: Setting changes in any BotSettings derived from CommonBotSettings do not take effect on the fly

[BUG]
#683

* Fix: Hoist setupModel to LangChainBot

[BUG]
#683

* Refactor setupModel method in GeminiBot, LangChainBot, WenxinQianfanBot, AzureOpenAIAPIBot, and OpenAIAPIBot

---------

Co-authored-by: Sunner Sun <sunner@gmail.com>
  • Loading branch information
awesdroid and sunner authored Jan 16, 2024
1 parent b333f0b commit fcb2e0f
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 40 deletions.
22 changes: 13 additions & 9 deletions src/bots/GeminiBot.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,7 @@ export default class GeminiBot extends LangChainBot {
let available = false;

if (store.state.gemini.apiKey) {
const chatModel = new ChatGoogleGenerativeAI({
apiKey: store.state.gemini.apiKey,
modelName: this.constructor._model ? this.constructor._model : "",
temperature: store.state.gemini.temperature,
streaming: true,
topK: store.state.gemini.topK,
topP: store.state.gemini.topP,
});
this.constructor._chatModel = chatModel;
this.setupModel();
available = true;
}
return available;
Expand All @@ -33,4 +25,16 @@ export default class GeminiBot extends LangChainBot {
getPastRounds() {
return store.state.gemini.pastRounds;
}

_setupModel() {
const chatModel = new ChatGoogleGenerativeAI({
apiKey: store.state.gemini.apiKey,
modelName: this.constructor._model ? this.constructor._model : "",
temperature: store.state.gemini.temperature,
streaming: true,
topK: store.state.gemini.topK,
topP: store.state.gemini.topP,
});
return chatModel;
}
}
10 changes: 10 additions & 0 deletions src/bots/LangChainBot.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ export default class LangChainBot extends Bot {
return [];
}

setupModel() {
this.constructor._chatModel = this._setupModel();
}

_setupModel() {
throw new Error(
"Abstract property '_setupModel' must be implemented in the subclass.",
);
}

getPastRounds() {
throw new Error(
"Abstract property 'pastRounds' must be implemented in the subclass.",
Expand Down
19 changes: 12 additions & 7 deletions src/bots/baidu/WenxinQianfanBot.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,23 @@ export default class WenxinQianfanBot extends LangChainBot {
let available = false;
const { apiKey, secretKey } = store.state.wenxinQianfan;
if (apiKey && secretKey) {
const chatModel = new ChatBaiduWenxin({
modelName: this.constructor._model,
baiduApiKey: apiKey,
baiduSecretKey: secretKey,
streaming: true,
});
this.constructor._chatModel = chatModel;
this.setupModel();
available = true;
}
return available;
}

_setupModel() {
const { apiKey, secretKey } = store.state.wenxinQianfan;
const chatModel = new ChatBaiduWenxin({
modelName: this.constructor._model,
baiduApiKey: apiKey,
baiduSecretKey: secretKey,
streaming: true,
});
return chatModel;
}

getPastRounds() {
return store.state.wenxinQianfan.pastRounds;
}
Expand Down
26 changes: 15 additions & 11 deletions src/bots/microsoft/AzureOpenAIAPIBot.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,26 @@ export default class AzureOpenAIAPIBot extends LangChainBot {
store.state.azureOpenaiApi.azureOpenAIApiDeploymentName &&
store.state.azureOpenaiApi.azureOpenAIApiVersion
) {
const chatModel = new ChatOpenAI({
azureOpenAIApiKey: store.state.azureOpenaiApi.azureApiKey,
azureOpenAIApiInstanceName:
store.state.azureOpenaiApi.azureApiInstanceName,
azureOpenAIApiDeploymentName:
store.state.azureOpenaiApi.azureOpenAIApiDeploymentName,
azureOpenAIApiVersion: store.state.azureOpenaiApi.azureOpenAIApiVersion,
temperature: store.state.azureOpenaiApi.temperature,
streaming: true,
});
this.constructor._chatModel = chatModel;
this.setupModel();
available = true;
}
return available;
}

_setupModel() {
const chatModel = new ChatOpenAI({
azureOpenAIApiKey: store.state.azureOpenaiApi.azureApiKey,
azureOpenAIApiInstanceName:
store.state.azureOpenaiApi.azureApiInstanceName,
azureOpenAIApiDeploymentName:
store.state.azureOpenaiApi.azureOpenAIApiDeploymentName,
azureOpenAIApiVersion: store.state.azureOpenaiApi.azureOpenAIApiVersion,
temperature: store.state.azureOpenaiApi.temperature,
streaming: true,
});
return chatModel;
}

getPastRounds() {
return store.state.azureOpenaiApi.pastRounds;
}
Expand Down
28 changes: 16 additions & 12 deletions src/bots/openai/OpenAIAPIBot.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,27 @@ export default class OpenAIAPIBot extends LangChainBot {
let available = false;

if (store.state.openaiApi.apiKey) {
const chatModel = new ChatOpenAI({
configuration: {
basePath: store.state.openaiApi.alterUrl
? store.state.openaiApi.alterUrl
: "",
},
openAIApiKey: store.state.openaiApi.apiKey,
modelName: this.constructor._model ? this.constructor._model : "",
temperature: store.state.openaiApi.temperature,
streaming: true,
});
this.constructor._chatModel = chatModel;
this.setupModel();
available = true;
}
return available;
}

_setupModel() {
const chatModel = new ChatOpenAI({
configuration: {
basePath: store.state.openaiApi.alterUrl
? store.state.openaiApi.alterUrl
: "",
},
openAIApiKey: store.state.openaiApi.apiKey,
modelName: this.constructor._model ? this.constructor._model : "",
temperature: store.state.openaiApi.temperature,
streaming: true,
});
return chatModel;
}

getPastRounds() {
return store.state.openaiApi.pastRounds;
}
Expand Down
6 changes: 6 additions & 0 deletions src/components/BotSettings/AzureOpenAIAPIBotSettings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
:settings="settings"
:brand-id="brandId"
mutation-type="setAzureOpenaiApi"
:watcher="watcher"
></CommonBotSettings>
</template>

Expand Down Expand Up @@ -73,5 +74,10 @@ export default {
brandId: Bot._brandId,
};
},
methods: {
watcher() {
Bot.getInstance().setupModel();
},
},
};
</script>
16 changes: 15 additions & 1 deletion src/components/BotSettings/CommonBotSettings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
</template>

<script setup>
import { computed, onMounted, ref } from "vue";
import { computed, onMounted, ref, watch } from "vue";
import { useStore } from "vuex";
import { Type } from "./settings.const";
const store = useStore();
Expand All @@ -101,8 +101,22 @@ const props = defineProps({
type: String,
required: true,
},
watcher: {
type: Function,
default: undefined,
},
});
if (props.watcher) {
watch(
() => settingState.value,
(newValue) => {
console.log(`${props.brandId}: ${JSON.stringify(newValue)}`);
props.watcher(newValue);
},
);
}
onMounted(() => {
for (const setting of props.settings) {
if (setting.type !== Type.Slider) {
Expand Down
6 changes: 6 additions & 0 deletions src/components/BotSettings/GeminiBotSettings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
:settings="settings"
:brand-id="brandId"
mutation-type="setGemini"
:watcher="watcher"
></CommonBotSettings>
</template>

Expand Down Expand Up @@ -71,5 +72,10 @@ export default {
brandId: Bot._brandId,
};
},
methods: {
watcher() {
Bot.getInstance().setupModel();
},
},
};
</script>
9 changes: 9 additions & 0 deletions src/components/BotSettings/OpenAIAPIBotSettings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
:settings="settings"
:brand-id="brandId"
mutation-type="setOpenaiApi"
:watcher="watcher"
></CommonBotSettings>
</template>

<script>
import _bots from "@/bots";
import Bot from "@/bots/openai/OpenAIAPIBot";
import CommonBotSettings from "@/components/BotSettings/CommonBotSettings.vue";
import i18n from "@/i18n";
Expand Down Expand Up @@ -60,5 +62,12 @@ export default {
brandId: Bot._brandId,
};
},
methods: {
watcher() {
_bots.all
.filter((bot) => bot instanceof Bot)
.map((bot) => bot.setupModel());
},
},
};
</script>

1 comment on commit fcb2e0f

@vercel
Copy link

@vercel vercel bot commented on fcb2e0f Jan 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

chatall – ./

chatall-sunner.vercel.app
chatall-llm.vercel.app
chatall-git-main-sunner.vercel.app

Please sign in to comment.