Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
tbrandaws committed Oct 23, 2023
1 parent cb7cd0d commit d4e00ad
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 133 deletions.
3 changes: 2 additions & 1 deletion packages/web/src/components/InputChatContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Props = {
content: string;
disabled?: boolean;
placeholder?: string;
resetDisabled?: boolean;
onChangeContent: (content: string) => void;
onSend: () => void;
onReset: () => void;
Expand Down Expand Up @@ -63,7 +64,7 @@ const InputChatContent: React.FC<Props> = (props) => {
loading={loading}
onClick={props.onSend}
/>
{!isEmpty && (
{!isEmpty && !props.resetDisabled && (
<Button
className="absolute -top-14 right-0 p-2 text-sm"
outlined
Expand Down
19 changes: 6 additions & 13 deletions packages/web/src/hooks/useChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { v4 as uuid } from 'uuid';
import useChatApi from './useChatApi';
import useConversation from './useConversation';
import { KeyedMutator } from 'swr';
import { getPromptGeneratorById } from '../prompts';
import { getSystemContextById } from '../prompts';

const useChatState = create<{
chats: {
Expand Down Expand Up @@ -78,19 +78,17 @@ const useChatState = create<{
};

const initChatWithSystemContext = (id: string) => {
const promptGenerator = getPromptGeneratorById(id);
initChat(
id,
[{ role: 'system', content: promptGenerator.systemContext() }],
undefined
);
const systemContext = getSystemContextById(id);
initChat(id, [{ role: 'system', content: systemContext }], undefined);
};

const setTitle = (id: string, title: string) => {
set((state) => {
return {
chats: produce(state.chats, (draft) => {
draft[id].chat!.title = title;
if (draft[id].chat) {
draft[id].chat!.title = title;
}
}),
};
});
Expand Down Expand Up @@ -382,10 +380,6 @@ const useChat = (id: string, chatId?: string) => {
return chats[id]?.messages.filter((chat) => chat.role !== 'system') ?? [];
}, [chats, id]);

const promptGenerator = useMemo(() => {
return getPromptGeneratorById(id);
}, [id]);

return {
loading: loading[id] ?? false,
setLoading: (newLoading: boolean) => {
Expand All @@ -411,7 +405,6 @@ const useChat = (id: string, chatId?: string) => {
sendFeedback: async (createdDate: string, feedback: string) => {
await sendFeedback(id, createdDate, feedback);
},
promptGenerator,
};
};

Expand Down
7 changes: 3 additions & 4 deletions packages/web/src/hooks/useRag.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import useChat from './useChat';
import useChatApi from './useChatApi';
import useRagApi from './useRagApi';
import { RagPrompt } from '../prompts';
import { ragPrompt } from '../prompts';

const useRag = (id: string) => {
const {
Expand All @@ -14,7 +14,6 @@ const useRag = (id: string) => {
popMessage,
pushMessage,
isEmpty,
promptGenerator,
} = useChat(id);

const { retrieve } = useRagApi();
Expand All @@ -35,7 +34,7 @@ const useRag = (id: string) => {
messages: [
{
role: 'user',
content: (promptGenerator as RagPrompt).generatePrompt({
content: ragPrompt({
promptType: 'RETRIEVE',
retrieveQueries: [content],
}),
Expand All @@ -46,7 +45,7 @@ const useRag = (id: string) => {
// Kendra から 参考ドキュメントを Retrieve してシステムコンテキストとして設定する
const items = await retrieve(query);
updateSystemContext(
(promptGenerator as RagPrompt).generatePrompt({
ragPrompt({
promptType: 'SYSTEM_CONTEXT',
referenceItems: items.data.ResultItems ?? [],
})
Expand Down
3 changes: 2 additions & 1 deletion packages/web/src/pages/ChatPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const ChatPage: React.FC = () => {
clear();
setContent('');
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
}, [clear]);

useEffect(() => {
if (messages.length > 0) {
Expand Down Expand Up @@ -94,6 +94,7 @@ const ChatPage: React.FC = () => {
content={content}
disabled={loading}
onChangeContent={setContent}
resetDisabled={!!chatId}
onSend={() => {
onSend();
}}
Expand Down
12 changes: 3 additions & 9 deletions packages/web/src/pages/EditorialPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { create } from 'zustand';
import Texteditor from '../components/TextEditor';
import { DocumentComment } from 'generative-ai-use-cases-jp';
import debounce from 'lodash.debounce';
import { EditorialPrompt } from '../prompts';
import { editorialPrompt } from '../prompts';

const REGEX_BRACKET = /\{(?:[^{}])*\}/g;
const REGEX_ZENKAKU =
Expand Down Expand Up @@ -77,13 +77,7 @@ const EditorialPage: React.FC = () => {

const { state } = useLocation();
const { pathname } = useLocation();
const {
loading,
messages,
postChat,
promptGenerator,
clear: clearChat,
} = useChat(pathname);
const { loading, messages, postChat, clear: clearChat } = useChat(pathname);

// Memo 変数
const filterComment = (
Expand Down Expand Up @@ -206,7 +200,7 @@ const EditorialPage: React.FC = () => {
const getAnnotation = (sentence: string, context: string) => {
setCommentState({});
postChat(
(promptGenerator as EditorialPrompt).generatePrompt({
editorialPrompt({
sentence,
context: context === '' ? undefined : context,
}),
Expand Down
12 changes: 3 additions & 9 deletions packages/web/src/pages/GenerateTextPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Markdown from '../components/Markdown';
import ButtonCopy from '../components/ButtonCopy';
import useChat from '../hooks/useChat';
import { create } from 'zustand';
import { GenerateTextPrompt } from '../prompts';
import { generateTextPrompt } from '../prompts';

type StateType = {
information: string;
Expand Down Expand Up @@ -59,13 +59,7 @@ const GenerateTextPage: React.FC = () => {
clear,
} = useGenerateTextPageState();
const { state, pathname } = useLocation();
const {
loading,
messages,
postChat,
promptGenerator,
clear: clearChat,
} = useChat(pathname);
const { loading, messages, postChat, clear: clearChat } = useChat(pathname);

const disabledExec = useMemo(() => {
return information === '' || loading;
Expand All @@ -81,7 +75,7 @@ const GenerateTextPage: React.FC = () => {

const getGeneratedText = (information: string, context: string) => {
postChat(
(promptGenerator as GenerateTextPrompt).generatePrompt({
generateTextPrompt({
information,
context,
}),
Expand Down
12 changes: 3 additions & 9 deletions packages/web/src/pages/SummarizePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Markdown from '../components/Markdown';
import ButtonCopy from '../components/ButtonCopy';
import useChat from '../hooks/useChat';
import { create } from 'zustand';
import { SummarizePrompt } from '../prompts';
import { summarizePrompt } from '../prompts';

type StateType = {
sentence: string;
Expand Down Expand Up @@ -60,13 +60,7 @@ const SummarizePage: React.FC = () => {
clear,
} = useSummarizePageState();
const { state, pathname } = useLocation();
const {
loading,
messages,
postChat,
promptGenerator,
clear: clearChat,
} = useChat(pathname);
const { loading, messages, postChat, clear: clearChat } = useChat(pathname);

const disabledExec = useMemo(() => {
return sentence === '' || loading;
Expand All @@ -82,7 +76,7 @@ const SummarizePage: React.FC = () => {

const getSummary = (sentence: string, context: string) => {
postChat(
(promptGenerator as SummarizePrompt).generatePrompt({
summarizePrompt({
sentence,
context,
}),
Expand Down
12 changes: 3 additions & 9 deletions packages/web/src/pages/TranslatePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import useChat from '../hooks/useChat';
import { create } from 'zustand';
import debounce from 'lodash.debounce';
import { PiCaretDown } from 'react-icons/pi';
import { TranslatePrompt } from '../prompts';
import { translatePrompt } from '../prompts';

const languages = [
{ label: '英語' },
Expand Down Expand Up @@ -86,13 +86,7 @@ const TranslatePage: React.FC = () => {

const { state } = useLocation();
const { pathname } = useLocation();
const {
loading,
messages,
postChat,
promptGenerator,
clear: clearChat,
} = useChat(pathname);
const { loading, messages, postChat, clear: clearChat } = useChat(pathname);

// Memo 変数
const disabledExec = useMemo(() => {
Expand Down Expand Up @@ -155,7 +149,7 @@ const TranslatePage: React.FC = () => {
context: string
) => {
postChat(
(promptGenerator as TranslatePrompt).generatePrompt({
translatePrompt({
sentence,
language,
context: context === '' ? undefined : context,
Expand Down
Loading

0 comments on commit d4e00ad

Please sign in to comment.