-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add LangChainBot and AzureOpenAIAPIBot
close #268
- Loading branch information
1 parent
af2bcc3
commit c1c9386
Showing
13 changed files
with
738 additions
and
85 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import Bot from "@/bots/Bot"; | ||
import { | ||
HumanChatMessage, | ||
AIChatMessage, | ||
SystemChatMessage, | ||
} from "langchain/schema"; | ||
|
||
export default class LangChainBot extends Bot { | ||
static _brandId = "langChainBot"; | ||
static _chatModel = undefined; // ChatModel instance | ||
|
||
constructor() { | ||
super(); | ||
} | ||
|
||
async _sendPrompt(prompt, onUpdateResponse, callbackParam) { | ||
let messages = await this.getChatContext(); | ||
// Remove old messages if exceeding the pastRounds limit | ||
while (messages.length > this.getPastRounds() * 2) { | ||
messages.shift(); | ||
} | ||
|
||
// Convert the messages to the correct format | ||
messages = messages.map((item) => { | ||
if (item.type === "human") { | ||
return new HumanChatMessage(item.data.content); | ||
} else if (item.type === "ai") { | ||
return new AIChatMessage(item.data.content); | ||
} else if (item.type === "system") { | ||
return new SystemChatMessage(item.data.content); | ||
} else { | ||
return item; | ||
} | ||
}); | ||
|
||
// Add the prompt to the messages | ||
messages.push(new HumanChatMessage(prompt)); | ||
|
||
let res = ""; | ||
const model = this.constructor._chatModel; | ||
const callbacks = [ | ||
{ | ||
handleLLMNewToken(token) { | ||
if (token) { | ||
res += token; | ||
onUpdateResponse(callbackParam, { content: res, done: false }); | ||
} else { | ||
onUpdateResponse(callbackParam, { done: true }); | ||
} | ||
}, | ||
}, | ||
]; | ||
model.callbacks = callbacks; | ||
await model.call(messages); | ||
messages.push(new AIChatMessage(res)); | ||
this.setChatContext(messages); | ||
} | ||
|
||
async createChatContext() { | ||
return []; | ||
} | ||
|
||
getPastRounds() { | ||
throw new Error( | ||
"Abstract property 'pastRounds' must be implemented in the subclass.", | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import AzureOpenAIAPIBot from "./AzureOpenAIAPIBot"; | ||
|
||
export default class AzureOpenAIAPI35Bot extends AzureOpenAIAPIBot { | ||
static _className = "AzureOpenAIAPI35Bot"; | ||
static _logoFilename = "openai-35-azure-logo.png"; | ||
static _model = "gpt-3.5-turbo"; | ||
|
||
constructor() { | ||
super(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import LangChainBot from "@/bots/LangChainBot"; | ||
import store from "@/store"; | ||
import { ChatOpenAI } from "langchain/chat_models/openai"; | ||
|
||
export default class AzureOpenAIAPIBot extends LangChainBot { | ||
static _brandId = "azureOpenaiApi"; | ||
static _className = "AzureOpenAIAPIBot"; | ||
|
||
constructor() { | ||
super(); | ||
} | ||
|
||
async checkAvailability() { | ||
if ( | ||
!store.state.azureOpenaiApi.azureApiKey || | ||
!store.state.azureOpenaiApi.azureApiInstanceName || | ||
!store.state.azureOpenaiApi.azureOpenAIApiDeploymentName || | ||
!store.state.azureOpenaiApi.azureOpenAIApiVersion | ||
) { | ||
this.constructor._isAvailable = false; | ||
} else { | ||
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.constructor._isAvailable = true; | ||
} | ||
return this.isAvailable(); | ||
} | ||
|
||
getPastRounds() { | ||
return store.state.azureOpenaiApi.pastRounds; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<template> | ||
<v-list-subheader>{{ bot.getBrandName() }}</v-list-subheader> | ||
<v-list-item> | ||
<v-list-item-title>{{ $t("azureOpenaiApi.azureOpenAIApiKey") }}</v-list-item-title> | ||
<v-list-item-subtitle>{{ | ||
$t("settings.secretPrompt") | ||
}}</v-list-item-subtitle> | ||
<v-text-field | ||
v-model="azureOpenaiApi.azureApiKey" | ||
outlined | ||
dense | ||
placeholder="b40..." | ||
@update:model-value="setAzureOpenaiApi({ azureApiKey: $event })" | ||
></v-text-field> | ||
<v-list-item-title>{{ $t("azureOpenaiApi.azureApiInstanceName") }}</v-list-item-title> | ||
<v-list-item-subtitle>{{ | ||
$t("azureOpenaiApi.azureApiInstanceNamePrompt") | ||
}}</v-list-item-subtitle> | ||
<v-text-field | ||
v-model="azureOpenaiApi.azureApiInstanceName" | ||
outlined | ||
dense | ||
@update:model-value="setAzureOpenaiApi({ azureApiInstanceName: $event })" | ||
></v-text-field> | ||
<v-list-item-title>{{ $t("azureOpenaiApi.azureOpenAIApiDeploymentName") }}</v-list-item-title> | ||
<v-list-item-subtitle>{{ | ||
$t("azureOpenaiApi.azureOpenAIApiDeploymentNamePrompt") | ||
}}</v-list-item-subtitle> | ||
<v-text-field | ||
v-model="azureOpenaiApi.azureOpenAIApiDeploymentName" | ||
outlined | ||
dense | ||
@update:model-value="setAzureOpenaiApi({ azureOpenAIApiDeploymentName: $event })" | ||
></v-text-field> | ||
<v-list-item-title>{{ $t("azureOpenaiApi.azureOpenAIApiVersion") }}</v-list-item-title> | ||
<v-text-field | ||
v-model="azureOpenaiApi.azureOpenAIApiVersion" | ||
outlined | ||
dense | ||
placeholder="2023-03-15-preview" | ||
@update:model-value="setAzureOpenaiApi({ azureOpenAIApiVersion: $event })" | ||
></v-text-field> | ||
|
||
<v-list-item-title>{{ $t("azureOpenaiApi.temperature") }}</v-list-item-title> | ||
<v-list-item-subtitle>{{ | ||
$t("azureOpenaiApi.temperaturePrompt") | ||
}}</v-list-item-subtitle> | ||
<v-slider | ||
v-model="azureOpenaiApi.temperature" | ||
color="primary" | ||
:min="0" | ||
:max="2" | ||
:step="0.1" | ||
thumb-label | ||
show-ticks="always" | ||
:ticks="temperatureLabels" | ||
@update:model-value="setAzureOpenaiApi({ temperature: $event })" | ||
></v-slider> | ||
|
||
<v-list-item-title>{{ $t("bot.pastRounds") }}</v-list-item-title> | ||
<v-list-item-subtitle>{{ | ||
$t("bot.pastRoundsPrompt") | ||
}}</v-list-item-subtitle> | ||
<v-slider | ||
v-model="azureOpenaiApi.pastRounds" | ||
color="primary" | ||
:min="0" | ||
:max="10" | ||
:step="1" | ||
thumb-label | ||
show-ticks | ||
@update:model-value="setAzureOpenaiApi({ pastRounds: $event })" | ||
></v-slider> | ||
</v-list-item> | ||
</template> | ||
|
||
<script> | ||
import { mapState, mapMutations } from "vuex"; | ||
import Bot from "@/bots/microsoft/AzureOpenAIAPIBot"; | ||
import i18n from "@/i18n"; | ||
export default { | ||
data() { | ||
return { | ||
bot: Bot.getInstance(), | ||
temperatureLabels: { | ||
0: i18n.global.t("azureOpenaiApi.temperature0"), | ||
2: i18n.global.t("azureOpenaiApi.temperature2"), | ||
}, | ||
}; | ||
}, | ||
methods: { | ||
...mapMutations(["setAzureOpenaiApi"]), | ||
}, | ||
computed: { | ||
...mapState(["azureOpenaiApi"]), | ||
}, | ||
}; | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
c1c9386
There was a problem hiding this comment.
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-llm.vercel.app
chatall-sunner.vercel.app
chatall-git-main-sunner.vercel.app