From f35253c6efcb44a010b6214dcdca62950c15bd1f Mon Sep 17 00:00:00 2001 From: yuiseki Date: Sat, 9 Nov 2024 14:43:06 +0900 Subject: [PATCH] =?UTF-8?q?InputPredict=E3=81=A7=E3=81=AFpastMessage?= =?UTF-8?q?=E3=81=A7=E3=81=AF=E3=81=AA=E3=81=8FdialogueList=E3=82=92?= =?UTF-8?q?=E4=BD=BF=E3=81=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/api/ai/suggests/route.ts | 8 +------- src/app/page.tsx | 27 ++++++++++++--------------- src/components/InputPredict/index.tsx | 20 ++++++++++---------- src/types/DialogueElement.ts | 2 +- 4 files changed, 24 insertions(+), 33 deletions(-) diff --git a/src/app/api/ai/suggests/route.ts b/src/app/api/ai/suggests/route.ts index 57e2fa8e..d3084cdd 100644 --- a/src/app/api/ai/suggests/route.ts +++ b/src/app/api/ai/suggests/route.ts @@ -5,7 +5,6 @@ import { loadTridentSuggestChain, } from "@/utils/langchain/chains/suggest"; import { OpenAIEmbeddings } from "@langchain/openai"; -import { parsePastMessagesToLines } from "@/utils/trident/parsePastMessagesToLines"; import { VercelPostgres } from "@langchain/community/vectorstores/vercel_postgres"; import { createCheckDocumentExists, @@ -19,12 +18,7 @@ export async function POST(request: Request) { const reqJson = await request.json(); const lang = reqJson.lang; const location = reqJson.location; - const pastMessagesJsonString = reqJson.pastMessages; - - const chatHistoryLines = parsePastMessagesToLines( - pastMessagesJsonString, - true - ); + const chatHistoryLines = reqJson.dialogueList; let input = ""; diff --git a/src/app/page.tsx b/src/app/page.tsx index f82484d4..f4b73e42 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -398,21 +398,18 @@ export default function Home() { }} /> )} - {pastMessages && - pastMessages.length > 1 && - !responding && - !mapping && ( - { - scrollToBottom(); - }} - onSelect={(value: string) => { - setInputText(value); - onSubmit(value); - }} - /> - )} + {!responding && !mapping && dialogueList.length > 1 && ( + { + scrollToBottom(); + }} + onSelect={(value: string) => { + setInputText(value); + onSubmit(value); + }} + /> + )}
diff --git a/src/components/InputPredict/index.tsx b/src/components/InputPredict/index.tsx index e0890f17..e50dc9d8 100644 --- a/src/components/InputPredict/index.tsx +++ b/src/components/InputPredict/index.tsx @@ -3,23 +3,20 @@ import { nextPostJsonWithCache } from "@/utils/nextPostJson"; import { useContext, useEffect, useState } from "react"; import styles from "./styles.module.scss"; +import { DialogueElement } from "@/types/DialogueElement"; export const InputPredict: React.FC<{ - pastMessages?: any[]; + dialogueList: DialogueElement[]; onUpdateSuggestions?: () => void; onSelect?: (value: string) => void; -}> = ({ pastMessages, onUpdateSuggestions, onSelect }) => { +}> = ({ dialogueList, onUpdateSuggestions, onSelect }) => { const locationInfo = useContext(LocationContext); const [requesting, setRequesting] = useState(false); const [suggests, setSuggests] = useState(undefined); + console.log("pastMessages", dialogueList); + useEffect(() => { - if (!pastMessages) { - return; - } - if (pastMessages.length === 0) { - return; - } if (requesting) { return; } @@ -28,7 +25,10 @@ export const InputPredict: React.FC<{ const resJson = await nextPostJsonWithCache("/api/ai/suggests", { lang: window.navigator.language, location: locationInfo.location, - pastMessages: JSON.stringify(pastMessages), + dialogueList: dialogueList + .filter((d) => d.who === "user") + .map((d) => d.text) + .join("\n"), }); if (!resJson.suggests) { return; @@ -39,7 +39,7 @@ export const InputPredict: React.FC<{ setRequesting(false); }; thisEffect(); - }, [locationInfo, onUpdateSuggestions, pastMessages, requesting]); + }, [dialogueList, locationInfo, onUpdateSuggestions, requesting]); return ( <> diff --git a/src/types/DialogueElement.ts b/src/types/DialogueElement.ts index 501c57fe..93ee7a5b 100644 --- a/src/types/DialogueElement.ts +++ b/src/types/DialogueElement.ts @@ -1,5 +1,5 @@ export type DialogueElement = { - who: string; + who: "user" | "assistant"; text: string; textEnd?: string; };