Skip to content

Commit

Permalink
InputPredictではpastMessageではなくdialogueListを使う
Browse files Browse the repository at this point in the history
  • Loading branch information
yuiseki committed Nov 9, 2024
1 parent 50838cc commit f35253c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 33 deletions.
8 changes: 1 addition & 7 deletions src/app/api/ai/suggests/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 = "";

Expand Down
27 changes: 12 additions & 15 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -398,21 +398,18 @@ export default function Home() {
}}
/>
)}
{pastMessages &&
pastMessages.length > 1 &&
!responding &&
!mapping && (
<InputPredict
pastMessages={pastMessages}
onUpdateSuggestions={() => {
scrollToBottom();
}}
onSelect={(value: string) => {
setInputText(value);
onSubmit(value);
}}
/>
)}
{!responding && !mapping && dialogueList.length > 1 && (
<InputPredict
dialogueList={dialogueList}
onUpdateSuggestions={() => {
scrollToBottom();
}}
onSelect={(value: string) => {
setInputText(value);
onSubmit(value);
}}
/>
)}
</LocationProvider>
<div style={{ height: "1px" }} ref={dialogueEndRef} />
</div>
Expand Down
20 changes: 10 additions & 10 deletions src/components/InputPredict/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[] | undefined>(undefined);

console.log("pastMessages", dialogueList);

useEffect(() => {
if (!pastMessages) {
return;
}
if (pastMessages.length === 0) {
return;
}
if (requesting) {
return;
}
Expand All @@ -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;
Expand All @@ -39,7 +39,7 @@ export const InputPredict: React.FC<{
setRequesting(false);
};
thisEffect();
}, [locationInfo, onUpdateSuggestions, pastMessages, requesting]);
}, [dialogueList, locationInfo, onUpdateSuggestions, requesting]);

return (
<>
Expand Down
2 changes: 1 addition & 1 deletion src/types/DialogueElement.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export type DialogueElement = {
who: string;
who: "user" | "assistant";
text: string;
textEnd?: string;
};

0 comments on commit f35253c

Please sign in to comment.