Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
yuiseki committed Jan 7, 2024
1 parent 072102c commit 81cf388
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 10 deletions.
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -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([]);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
}

.suggestListItem {
flex: 46%;
font-size: 0.8em;
margin-top: 12px;
padding: 6px;
Expand Down
18 changes: 12 additions & 6 deletions src/utils/langchain/chains/suggest/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@ export const tridentSuggestExampleList: Array<{
output: string;
}> = [
{
input: "台東区",
output: `台東区のラーメン屋を表示して
台東区の駅を表示して
台東区の公園を表示して
台東区の病院を表示して`,
input: "台東区, 東京都, 日本",
output: `台東区の地図を表示して
東京都の地図を表示して
日本の地図を表示して`,
},
{
input: "台東区の地図を表示して",
output: `ラーメン屋を表示して
駅を表示して
公園を表示して
病院を表示して`,
},
{
input: "New York",
Expand All @@ -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: ###`;
Expand Down
17 changes: 17 additions & 0 deletions src/utils/trident/convertChatHistoryToLines.ts
Original file line number Diff line number Diff line change
@@ -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;
};
33 changes: 33 additions & 0 deletions src/utils/trident/parsePastMessagesToChatHistory.ts
Original file line number Diff line number Diff line change
@@ -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;
};
28 changes: 28 additions & 0 deletions src/utils/trident/parsePastMessagesToLines.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ChatMessageHistory } from "langchain/memory";

export const parsePastMessagesToLines = (pastMessagesJsonString: string) => {
let chatHistory: Array<string | null> = [];
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;
};

1 comment on commit 81cf388

@vercel
Copy link

@vercel vercel bot commented on 81cf388 Jan 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.