Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add math prompt #72

Merged
merged 4 commits into from Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
83 changes: 83 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -118,6 +118,7 @@
"json5": "^2.2.3",
"langchain": "^0.1.25",
"lodash": "^4.17.21",
"mathjs": "^12.4.0",
"next": "^13.5.6",
"next-i18next": "15.2.0",
"next-transpile-modules": "10.0.1",
Expand Down
5 changes: 4 additions & 1 deletion src/client/ions/hooks/vector-store.ts
Expand Up @@ -22,7 +22,10 @@ export function useVectorStore(value: string) {

useEffect(() => {
if (query.trim()) {
window.ipc.send(buildKey([ID.VECTOR_STORE], { suffix: ":search" }), query);
window.ipc.send(buildKey([ID.VECTOR_STORE], { suffix: ":search" }), {
query,
options: { score_threshold: 0.2 },
});
} else {
setResults([]);
}
Expand Down
60 changes: 48 additions & 12 deletions src/client/pages/[locale]/prompt.tsx
Expand Up @@ -8,6 +8,7 @@ import ListItemContent from "@mui/joy/ListItemContent";
import ListItemDecorator from "@mui/joy/ListItemDecorator";
import Sheet from "@mui/joy/Sheet";
import Typography from "@mui/joy/Typography";
import { evaluate } from "mathjs";
import type { RefObject } from "react";
import { useEffect, useRef, useState } from "react";

Expand All @@ -21,13 +22,15 @@ import { useVectorStore } from "@/ions/hooks/vector-store";

export function useAutoFocusIPC<T extends HTMLElement>(reference: RefObject<T>) {
useEffect(() => {
const unsubscribe = window.ipc.on(buildKey([ID.WINDOW], { suffix: ":focus" }), () => {
function handleFocus() {
if (reference.current) {
reference.current.focus();
}
});
}

window.addEventListener("focus", handleFocus);
return () => {
unsubscribe();
window.removeEventListener("focus", handleFocus);
};
}, [reference]);
}
Expand All @@ -43,6 +46,7 @@ export default function Page() {
const frameReference = useRef<HTMLDivElement | null>(null);
const promptReference = useRef<HTMLInputElement | null>(null);
const [value, setValue] = useState("");
const [evaluationResult, setEvaluationResult] = useState("");
const suggestions = useVectorStore(value);

useAutoFocusIPC(promptReference);
Expand Down Expand Up @@ -102,14 +106,21 @@ export default function Page() {
}}
onChange={event => {
setValue(event.target.value);
window.ipc.send(
buildKey([ID.PROMPT], { suffix: ":query" }),
event.target.value
);
try {
const result = evaluate(event.target.value);
setEvaluationResult(result.toString());
} catch (error) {
console.error(error);
setEvaluationResult("");
}
}}
onKeyDown={event => {
onKeyDown={async event => {
if (event.key === "Enter" && !event.shiftKey) {
event.preventDefault();
if (evaluationResult) {
return;
}

const [suggestion] = suggestions;
if (suggestion) {
handleSuggestion(suggestion);
Expand All @@ -118,7 +129,7 @@ export default function Page() {
}}
/>
</Box>
{suggestions.length > 0 && (
{(suggestions.length > 0 || evaluationResult) && (
<Sheet
sx={{
my: "1em",
Expand All @@ -135,7 +146,34 @@ export default function Page() {
WebkitOverflowScrolling: "touch",
}}
>
{suggestions.map((suggestion, index) => {
{evaluationResult && (
<ListItem
sx={{
"--focus-outline-offset": "-2px",
}}
>
<ListItemButton
sx={{ height: 64 }}
onClick={async () => {
try {
await navigator.clipboard.writeText(evaluationResult);
} catch (error) {
console.log(error);
}
}}
>
<ListItemDecorator>
<Logo sx={{ color: "currentColor" }} />
</ListItemDecorator>
<ListItemContent>
<Typography level="h4" component="div">
{evaluationResult}
</Typography>
</ListItemContent>
</ListItemButton>
</ListItem>
)}
{suggestions.map(suggestion => {
let color: ChipProps["color"] = "red";
if (suggestion.score > 0.2) {
color = "orange";
Expand All @@ -157,8 +195,6 @@ export default function Page() {
}}
>
<ListItemButton
color={index === 0 ? "primary" : undefined}
variant={index === 0 ? "soft" : undefined}
sx={{ height: 64 }}
onClick={() => {
handleSuggestion(suggestion);
Expand Down
21 changes: 12 additions & 9 deletions src/electron/future/ipc/vector-store.ts
Expand Up @@ -3,7 +3,7 @@ import { ipcMain } from "electron";
import { buildKey } from "#/build-key";
import { VECTOR_STORE_COLLECTION } from "#/constants";
import { ID } from "#/enums";
import type { VectorStoreDocument } from "@/services/vector-store";
import type { SearchOptions, VectorStoreDocument } from "@/services/vector-store";
import { VectorStore } from "@/services/vector-store";

ipcMain.on(
Expand All @@ -21,14 +21,17 @@ ipcMain.on(
}
);

ipcMain.on(buildKey([ID.VECTOR_STORE], { suffix: ":search" }), async (event, query, options) => {
try {
const vectorStore = VectorStore.getInstance;
ipcMain.on(
buildKey([ID.VECTOR_STORE], { suffix: ":search" }),
async (event, { query, options }: { query: string; options?: SearchOptions }) => {
try {
const vectorStore = VectorStore.getInstance;

const result = await vectorStore.search(VECTOR_STORE_COLLECTION, query, options);
const result = await vectorStore.search(VECTOR_STORE_COLLECTION, query, options);

event.sender.send(buildKey([ID.VECTOR_STORE], { suffix: ":result" }), result);
} catch (error) {
event.sender.send(buildKey([ID.VECTOR_STORE], { suffix: ":error" }), error);
event.sender.send(buildKey([ID.VECTOR_STORE], { suffix: ":result" }), result);
} catch (error) {
event.sender.send(buildKey([ID.VECTOR_STORE], { suffix: ":error" }), error);
}
}
});
);