From 81cf388d13b058051099ef2ed04bbf2ed465968c Mon Sep 17 00:00:00 2001 From: yuiseki Date: Mon, 8 Jan 2024 00:19:05 +0900 Subject: [PATCH] refactor --- src/components/InputPredict/index.tsx | 0 .../SuggestByCurrentLocation/index.tsx | 2 +- .../styles.module.scss | 4 +-- .../SuggestByTrend/styles.module.scss | 1 - src/utils/langchain/chains/suggest/prompt.ts | 18 ++++++---- .../trident/convertChatHistoryToLines.ts | 17 ++++++++++ .../trident/parsePastMessagesToChatHistory.ts | 33 +++++++++++++++++++ src/utils/trident/parsePastMessagesToLines.ts | 28 ++++++++++++++++ 8 files changed, 93 insertions(+), 10 deletions(-) create mode 100644 src/components/InputPredict/index.tsx create mode 100644 src/utils/trident/convertChatHistoryToLines.ts create mode 100644 src/utils/trident/parsePastMessagesToChatHistory.ts create mode 100644 src/utils/trident/parsePastMessagesToLines.ts diff --git a/src/components/InputPredict/index.tsx b/src/components/InputPredict/index.tsx new file mode 100644 index 00000000..e69de29b diff --git a/src/components/InputSuggest/SuggestByCurrentLocation/index.tsx b/src/components/InputSuggest/SuggestByCurrentLocation/index.tsx index 41afbba4..0ce6b499 100644 --- a/src/components/InputSuggest/SuggestByCurrentLocation/index.tsx +++ b/src/components/InputSuggest/SuggestByCurrentLocation/index.tsx @@ -27,7 +27,7 @@ export const SuggestByCurrentLocation: React.FC<{ 10 ); console.log("address", address); - const name = address.name; + const name = address.display_name; setAddress(name); setSuggests([]); }; diff --git a/src/components/InputSuggest/SuggestByCurrentLocation/styles.module.scss b/src/components/InputSuggest/SuggestByCurrentLocation/styles.module.scss index 33f7a4fa..363599eb 100644 --- a/src/components/InputSuggest/SuggestByCurrentLocation/styles.module.scss +++ b/src/components/InputSuggest/SuggestByCurrentLocation/styles.module.scss @@ -26,12 +26,12 @@ .suggestListWrap { display: flex; - flex-wrap: wrap; + flex-direction: column; + flex-wrap: no-wrap; gap: 2%; } .suggestListItem { - flex: 46%; font-size: 0.8em; margin-top: 12px; padding: 6px; diff --git a/src/components/InputSuggest/SuggestByTrend/styles.module.scss b/src/components/InputSuggest/SuggestByTrend/styles.module.scss index b2358781..477af98d 100644 --- a/src/components/InputSuggest/SuggestByTrend/styles.module.scss +++ b/src/components/InputSuggest/SuggestByTrend/styles.module.scss @@ -28,7 +28,6 @@ } .suggestListItem { - flex: 46%; font-size: 0.8em; margin-top: 12px; padding: 6px; diff --git a/src/utils/langchain/chains/suggest/prompt.ts b/src/utils/langchain/chains/suggest/prompt.ts index 78284dcb..08af1a34 100644 --- a/src/utils/langchain/chains/suggest/prompt.ts +++ b/src/utils/langchain/chains/suggest/prompt.ts @@ -11,11 +11,17 @@ export const tridentSuggestExampleList: Array<{ output: string; }> = [ { - input: "台東区", - output: `台東区のラーメン屋を表示して -台東区の駅を表示して -台東区の公園を表示して -台東区の病院を表示して`, + input: "台東区, 東京都, 日本", + output: `台東区の地図を表示して +東京都の地図を表示して +日本の地図を表示して`, + }, + { + input: "台東区の地図を表示して", + output: `ラーメン屋を表示して +駅を表示して +公園を表示して +病院を表示して`, }, { input: "New York", @@ -32,7 +38,7 @@ You will always output according to the following rules: - You MUST ALWAYS output IN THE LANGUAGE WHICH INPUT IS WRITING. - You MUST NOT output in any language other than the language written by the input. - You output with the most accurate grammar possible. -- You SHOULD output 4 lines of text. +- You SHOULD output max 4 lines of text. - Your output MUST be the list of suggestions of the maps based on the input. ### Examples: ###`; diff --git a/src/utils/trident/convertChatHistoryToLines.ts b/src/utils/trident/convertChatHistoryToLines.ts new file mode 100644 index 00000000..2a5b31f2 --- /dev/null +++ b/src/utils/trident/convertChatHistoryToLines.ts @@ -0,0 +1,17 @@ +import { ChatMessageHistory } from "langchain/memory"; + +export const convertChatHistoryToLines = async ( + chatHistory?: ChatMessageHistory, + excludeAI?: boolean +) => { + let chatHistoryLines = ""; + if (!chatHistory) { + return chatHistoryLines; + } + const messages = await chatHistory.getMessages(); + messages.forEach((message) => { + console.log(message); + chatHistoryLines += message.lc_kwargs.content + "\n"; + }); + return chatHistoryLines; +}; diff --git a/src/utils/trident/parsePastMessagesToChatHistory.ts b/src/utils/trident/parsePastMessagesToChatHistory.ts new file mode 100644 index 00000000..46d8ec61 --- /dev/null +++ b/src/utils/trident/parsePastMessagesToChatHistory.ts @@ -0,0 +1,33 @@ +import { ChatMessageHistory } from "langchain/memory"; +import { AIMessage, HumanMessage } from "langchain/schema"; + +export const parsePastMessagesToChatHistory = ( + pastMessagesJsonString: string +) => { + let chatHistory = undefined; + + if (pastMessagesJsonString && pastMessagesJsonString !== "undefined") { + const pastMessages: Array<{ + type: string; + id: string[]; + kwargs: { content: string }; + }> = JSON.parse(pastMessagesJsonString); + + const chatHistoryMessages = pastMessages.map((message) => { + if (message.kwargs.content) { + switch (message.id[2]) { + case "HumanMessage": + return new HumanMessage(message.kwargs.content); + case "AIMessage": + return new AIMessage(message.kwargs.content); + default: + return new HumanMessage(""); + } + } else { + return new HumanMessage(""); + } + }); + chatHistory = new ChatMessageHistory(chatHistoryMessages); + } + return chatHistory; +}; diff --git a/src/utils/trident/parsePastMessagesToLines.ts b/src/utils/trident/parsePastMessagesToLines.ts new file mode 100644 index 00000000..5774cbe4 --- /dev/null +++ b/src/utils/trident/parsePastMessagesToLines.ts @@ -0,0 +1,28 @@ +import { ChatMessageHistory } from "langchain/memory"; + +export const parsePastMessagesToLines = (pastMessagesJsonString: string) => { + let chatHistory: Array = []; + if (pastMessagesJsonString && pastMessagesJsonString !== "undefined") { + const pastMessages: Array<{ + type: string; + id: string[]; + kwargs: { content: string }; + }> = JSON.parse(pastMessagesJsonString); + + chatHistory = + pastMessages && + pastMessages + .map((message) => { + switch (message.id[2]) { + case "HumanMessage": + return `Human: ${message.kwargs.content}`; + case "AIMessage": + return `AI: ${message.kwargs.content}`; + default: + return null; + } + }) + .filter((v) => v); + } + return chatHistory; +};