Skip to content

Commit

Permalink
✨ feat: support the auto create topic configuration (lobehub#490)
Browse files Browse the repository at this point in the history
* ✨ feat: support the auto create topic configuration

* 🐛 fix: fix the inbox session that not response on config

* ♻️ refactor: rename the agentConfig folder to agent

* 🎨 refactor: improve code

* 🌐 chore: add i18n
  • Loading branch information
arvinxx authored Nov 20, 2023
1 parent df48298 commit a7b7ef0
Show file tree
Hide file tree
Showing 34 changed files with 201 additions and 100 deletions.
10 changes: 9 additions & 1 deletion locales/en_US/setting.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,15 @@
"placeholder": "{{text}} will be replaced with real-time input information",
"title": "User Input Preprocessing"
},
"title": "Chat Settings"
"title": "Chat Settings",
"autoCreateTopicThreshold": {
"title": "Message Threshold",
"desc": "Automatically create a topic when the current message count exceeds this value"
},
"enableAutoCreateTopic": {
"title": "Enable Auto Topic Creation",
"desc": "Whether to automatically create a topic during the conversation, only effective in temporary topics"
}
},
"settingModel": {
"enableMaxTokens": {
Expand Down
10 changes: 9 additions & 1 deletion locales/ja_JP/setting.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,15 @@
"placeholder": "プリプロセステンプレート {{text}} はリアルタイムの入力情報に置き換えられます",
"title": "ユーザー入力のプリプロセス"
},
"title": "チャット設定"
"title": "チャット設定",
"autoCreateTopicThreshold": {
"desc": "現在のメッセージ数がこの値を超えると、トピックが自動的に作成されます",
"title": "メッセージ閾値"
},
"enableAutoCreateTopic": {
"desc": "会話中にトピックを自動的に作成するかどうか、一時的なトピックでのみ有効です",
"title": "トピックの自動作成"
}
},
"settingModel": {
"enableMaxTokens": {
Expand Down
10 changes: 9 additions & 1 deletion locales/ko_KR/setting.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,15 @@
"placeholder": "사전 처리 템플릿 {{text}}은(는) 실시간 입력 정보로 대체됩니다.",
"title": "사용자 입력 사전 처리"
},
"title": "채팅 설정"
"title": "채팅 설정",
"autoCreateTopicThreshold": {
"desc": "현재 메시지 수가이 값 이상이면 자동으로 주제를 만듭니다.",
"title": "메시지 임계값"
},
"enableAutoCreateTopic": {
"desc": "대화 중에 주제를 자동으로 만들지 여부를 설정합니다. 임시 주제에서만 작동합니다.",
"title": "주제 자동 생성"
}
},
"settingModel": {
"enableMaxTokens": {
Expand Down
10 changes: 9 additions & 1 deletion locales/ru_RU/setting.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,15 @@
"placeholder": "{{text}} будут заменены входной информацией, поступающей в режиме реального времени",
"title": "Предварительная обработка пользовательского ввода"
},
"title": "Настройки чата"
"title": "Настройки чата",
"autoCreateTopicThreshold": {
"desc": "Когда текущее количество сообщений превысит эту величину, будет автоматически создана тема",
"title": "Порог сообщений"
},
"enableAutoCreateTopic": {
"desc": "Автоматическое создание темы во время разговора, действует только во временной теме",
"title": "Включить автоматическое создание темы"
}
},
"settingModel": {
"enableMaxTokens": {
Expand Down
8 changes: 8 additions & 0 deletions locales/zh_CN/setting.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@
"title": "助手信息"
},
"settingChat": {
"autoCreateTopicThreshold": {
"desc": "当前消息数超过设定该值后,将自动创建话题",
"title": "消息阈值"
},
"chatStyleType": {
"title": "聊天窗口样式",
"type": {
Expand All @@ -121,6 +125,10 @@
"desc": "当未压缩的历史消息超过该值时,将进行压缩",
"title": "历史消息长度压缩阈值"
},
"enableAutoCreateTopic": {
"desc": "会话过程中是否自动创建话题,仅在临时话题中生效",
"title": "自动创建话题"
},
"enableCompressThreshold": {
"title": "是否开启历史消息长度压缩阈值"
},
Expand Down
10 changes: 9 additions & 1 deletion locales/zh_TW/setting.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,15 @@
"placeholder": "前置處理範本 {{text}} 將替換為即時輸入資訊",
"title": "使用者輸入前置處理"
},
"title": "聊天設定"
"title": "聊天設定",
"autoCreateTopicThreshold": {
"desc": "當前訊息數超過設定該值後,將自動創建話題",
"title": "訊息閾值"
},
"enableAutoCreateTopic": {
"desc": "會話過程中是否自動創建話題,僅在臨時話題中生效",
"title": "自動創建話題"
}
},
"settingModel": {
"enableMaxTokens": {
Expand Down
3 changes: 1 addition & 2 deletions src/app/chat/features/ChatHeader/ShareButton/Preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import ChatList from 'src/app/chat/features/Conversation/ChatList';

import pkg from '@/../package.json';
import { useSessionStore } from '@/store/session';
import { agentSelectors } from '@/store/session/slices/agentConfig';
import { sessionSelectors } from '@/store/session/slices/session/selectors';
import { agentSelectors, sessionSelectors } from '@/store/session/selectors';

import PluginTag from '../../ChatHeader/PluginTag';
import { useStyles } from './style';
Expand Down
2 changes: 1 addition & 1 deletion src/app/chat/features/ChatInput/ActionBar/FileUpload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Center } from 'react-layout-kit';

import { useFileStore } from '@/store/files';
import { useSessionStore } from '@/store/session';
import { agentSelectors } from '@/store/session/slices/agentConfig';
import { agentSelectors } from '@/store/session/selectors';

const FileUpload = memo(() => {
const { t } = useTranslation('chat');
Expand Down
3 changes: 1 addition & 2 deletions src/app/chat/features/ChatInput/ActionBar/ModelSwitch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ const ModelSwitch = memo(() => {
const { t } = useTranslation('setting');

const [model, updateAgentConfig] = useSessionStore((s) => {
const config = agentSelectors.currentAgentConfig(s);
return [config.model, s.updateAgentConfig];
return [agentSelectors.currentAgentModel(s), s.updateAgentConfig];
});

const modelList = useGlobalStore(settingsSelectors.modelList);
Expand Down
2 changes: 1 addition & 1 deletion src/app/chat/features/Conversation/usePluginsInit.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { usePluginStore } from '@/store/plugin';
import { useSessionStore } from '@/store/session';
import { agentSelectors } from '@/store/session/slices/agentConfig';
import { agentSelectors } from '@/store/session/selectors';

export const usePluginsInit = () => {
const [plugins] = useSessionStore((s) => [agentSelectors.currentAgentPlugins(s)]);
Expand Down
2 changes: 1 addition & 1 deletion src/app/chat/settings/features/SubmitAgentButton/Inner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { AGENTS_INDEX_GITHUB_ISSUE } from '@/const/url';
import AgentInfo from '@/features/AgentInfo';
import { useGlobalStore } from '@/store/global';
import { useSessionStore } from '@/store/session';
import { agentSelectors } from '@/store/session/slices/agentConfig';
import { agentSelectors } from '@/store/session/selectors';

const Inner = memo(() => {
const { t } = useTranslation('setting');
Expand Down
2 changes: 2 additions & 0 deletions src/const/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ export const DEFAUTT_AGENT_TTS_CONFIG: LobeAgentTTSConfig = {
export const VISION_MODEL_DEFAULT_MAX_TOKENS = 1000;

export const DEFAULT_AGENT_CONFIG: LobeAgentConfig = {
autoCreateTopicThreshold: 2,
displayMode: 'chat',
enableAutoCreateTopic: true,
historyCount: 1,
model: LanguageModel.GPT3_5,
params: {
Expand Down
18 changes: 17 additions & 1 deletion src/features/AgentSetting/AgentConfig/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,27 @@ const AgentConfig = memo(() => {
minWidth: undefined,
},
{
children: <Input placeholder={t('settingChat.inputTemplate.placeholder')} />,
children: <Input.TextArea placeholder={t('settingChat.inputTemplate.placeholder')} />,
desc: t('settingChat.inputTemplate.desc'),
label: t('settingChat.inputTemplate.title'),
name: 'inputTemplate',
},
{
children: <Switch />,
desc: t('settingChat.enableAutoCreateTopic.desc'),
label: t('settingChat.enableAutoCreateTopic.title'),
minWidth: undefined,
name: 'enableAutoCreateTopic',
valuePropName: 'checked',
},
{
children: <SliderWithInput max={8} min={0} />,
desc: t('settingChat.autoCreateTopicThreshold.desc'),
divider: false,
hidden: !config.enableAutoCreateTopic,
label: t('settingChat.autoCreateTopicThreshold.title'),
name: 'autoCreateTopicThreshold',
},
{
children: <Switch />,
label: t('settingChat.enableHistoryCount.title'),
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useSTT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { createHeaderWithOpenAI } from '@/services/_header';
import { OPENAI_URLS } from '@/services/_url';
import { settingsSelectors, useGlobalStore } from '@/store/global';
import { useSessionStore } from '@/store/session';
import { agentSelectors } from '@/store/session/slices/agentConfig';
import { agentSelectors } from '@/store/session/selectors';

interface STTConfig extends SWRConfiguration {
onTextChange: (value: string) => void;
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useTTS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { createHeaderWithOpenAI } from '@/services/_header';
import { OPENAI_URLS, TTS_URL } from '@/services/_url';
import { settingsSelectors, useGlobalStore } from '@/store/global';
import { useSessionStore } from '@/store/session';
import { agentSelectors } from '@/store/session/slices/agentConfig';
import { agentSelectors } from '@/store/session/selectors';
import { TTSServer } from '@/types/session';

interface TTSConfig extends SWRConfiguration {
Expand Down
8 changes: 8 additions & 0 deletions src/locales/default/setting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ export default {
title: '助手信息',
},
settingChat: {
autoCreateTopicThreshold: {
desc: '当前消息数超过设定该值后,将自动创建话题',
title: '消息阈值',
},
chatStyleType: {
title: '聊天窗口样式',
type: {
Expand All @@ -121,6 +125,10 @@ export default {
desc: '当未压缩的历史消息超过该值时,将进行压缩',
title: '历史消息长度压缩阈值',
},
enableAutoCreateTopic: {
desc: '会话过程中是否自动创建话题,仅在临时话题中生效',
title: '自动创建话题',
},
enableCompressThreshold: {
title: '是否开启历史消息长度压缩阈值',
},
Expand Down
6 changes: 3 additions & 3 deletions src/services/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { PluginRequestPayload, createHeadersWithPluginSettings } from '@lobehub/
import { merge } from 'lodash-es';

import { VISION_MODEL_WHITE_LIST } from '@/const/llm';
import { DEFAULT_AGENT_CONFIG } from '@/const/settings';
import { usePluginStore } from '@/store/plugin';
import { pluginSelectors } from '@/store/plugin/selectors';
import { initialLobeAgentConfig } from '@/store/session/initialState';
import type { OpenAIChatStreamPayload } from '@/types/openai/chat';
import { fetchAIFactory, getMessageError } from '@/utils/fetch';

Expand All @@ -22,9 +22,9 @@ class ChatService {
) => {
const payload = merge(
{
model: initialLobeAgentConfig.model,
model: DEFAULT_AGENT_CONFIG.model,
stream: true,
...initialLobeAgentConfig.params,
...DEFAULT_AGENT_CONFIG.params,
},
params,
);
Expand Down
60 changes: 60 additions & 0 deletions src/store/global/selectors/__snapshots__/settings.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`settingsSelectors > currentSettings > should merge DEFAULT_SETTINGS and s.settings correctly 1`] = `
{
"avatar": "avatar.jpg",
"defaultAgent": {
"config": {
"autoCreateTopicThreshold": 2,
"displayMode": "chat",
"enableAutoCreateTopic": true,
"historyCount": 1,
"model": "gpt-3.5-turbo",
"params": {
"frequency_penalty": 0,
"presence_penalty": 0,
"temperature": 0.6,
"top_p": 1,
},
"plugins": [],
"systemRole": "",
"tts": {
"showAllLocaleVoice": false,
"sttLocale": "auto",
"ttsService": "openai",
"voice": {
"openai": "alloy",
},
},
},
"meta": {
"avatar": "Default Agent",
"description": "Default agent for testing",
},
},
"fontSize": 14,
"language": "en-US",
"languageModel": {
"openAI": {
"OPENAI_API_KEY": "openai-api-key",
"customModelName": undefined,
"endpoint": "https://openai-endpoint.com",
"models": [
"gpt-3.5-turbo",
],
},
},
"neutralColor": "sand",
"password": "password123",
"primaryColor": "blue",
"themeMode": "light",
"tts": {
"openAI": {
"sttModel": "whisper-1",
"ttsModel": "tts-1",
},
"sttAutoStop": true,
"sttServer": "openai",
},
}
`;
52 changes: 1 addition & 51 deletions src/store/global/selectors/settings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,57 +54,7 @@ describe('settingsSelectors', () => {

const result = settingsSelectors.currentSettings(s);

expect(result).toEqual({
avatar: 'avatar.jpg',
fontSize: 14,
language: 'en-US',
neutralColor: 'sand',
password: 'password123',
primaryColor: 'blue',
themeMode: 'light',
defaultAgent: {
config: {
displayMode: 'chat',
historyCount: 1,
systemRole: '',
model: LanguageModel.GPT3_5,
params: {
frequency_penalty: 0,
presence_penalty: 0,
temperature: 0.6,
top_p: 1,
},
plugins: [],
tts: {
showAllLocaleVoice: false,
sttLocale: 'auto',
ttsService: 'openai',
voice: {
openai: 'alloy',
},
},
},
meta: {
avatar: 'Default Agent',
description: 'Default agent for testing',
},
},
tts: {
openAI: {
sttModel: 'whisper-1',
ttsModel: 'tts-1',
},
sttAutoStop: true,
sttServer: 'openai',
},
languageModel: {
openAI: {
OPENAI_API_KEY: 'openai-api-key',
endpoint: 'https://openai-endpoint.com',
models: ['gpt-3.5-turbo'],
},
},
});
expect(result).toMatchSnapshot();
});
});

Expand Down
Loading

0 comments on commit a7b7ef0

Please sign in to comment.