Skip to content

Commit

Permalink
feat: use same clickhouse and postgres message ids
Browse files Browse the repository at this point in the history
  • Loading branch information
drew-harris authored and densumesh committed Oct 8, 2024
1 parent 7e8c31c commit 5fc7799
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 6 deletions.
3 changes: 2 additions & 1 deletion frontends/chat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@solid-aria/focus": "^0.1.4",
"@solid-aria/switch": "^0.1.2",
"@solid-aria/visually-hidden": "^0.1.3",
"@solid-primitives/storage": "^4.2.1",
"@solidjs/router": "^0.10.1",
"rehype-sanitize": "^6.0.0",
"remark-breaks": "3.0.3",
Expand All @@ -34,11 +35,11 @@
"trieve-ts-sdk": "file:../../clients/ts-sdk"
},
"devDependencies": {
"config": "*",
"@types/sanitize-html": "^2.9.5",
"@typescript-eslint/eslint-plugin": "^6.14.0",
"@typescript-eslint/parser": "7.14.1",
"autoprefixer": "^10.4.16",
"config": "*",
"eslint": "^8.13.0",
"eslint-config-prettier": "^9.1.0",
"eslint-config-semistandard": "^17.0.0",
Expand Down
6 changes: 5 additions & 1 deletion frontends/chat/src/components/Atoms/AfMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ import { SolidMarkdown } from "solid-markdown";
import remarkGfm from "remark-gfm";
import remarkBreaks from "remark-breaks";
import rehypeSanitize from "rehype-sanitize";

import { MessageVoting } from "./MessageVoting";
export interface AfMessageProps {
queryId?: string;
normalChat: boolean;
role: "user" | "assistant" | "system";
content: string;
Expand Down Expand Up @@ -234,6 +235,9 @@ export const AfMessage = (props: AfMessageProps) => {
<Show when={copied()}>
<FiCheck class="text-green-500" />
</Show>
<Show when={props.queryId}>
{(id) => <MessageVoting queryId={id()} />}
</Show>
</div>
)}
</div>
Expand Down
114 changes: 114 additions & 0 deletions frontends/chat/src/components/Atoms/MessageVoting.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { makePersisted } from "@solid-primitives/storage";
import { IconTypes } from "solid-icons";
import {
BsHandThumbsDown,
BsHandThumbsDownFill,
BsHandThumbsUp,
BsHandThumbsUpFill,
} from "solid-icons/bs";
import { Show, useContext } from "solid-js";
import { createStore } from "solid-js/store";
import { UserContext } from "../contexts/UserContext";

interface MessageVotingProps {
queryId: string;
}

type MessageVoteStore = {
[key: string]: number;
};

const apiHost = import.meta.env.VITE_API_HOST as unknown as string;

// Create store exepcted to be directly destructured, this is fine
// eslint-disable-next-line @typescript-eslint/no-unused-vars, solid/reactivity
const [store, setStore] = makePersisted(createStore<MessageVoteStore>({}), {
name: "vote-cache",
});

export const MessageVoting = (props: MessageVotingProps) => {
const userContext = useContext(UserContext);

const updateVote = async (queryId: string, vote: number) => {
const prevVote = store[queryId];
setStore(queryId, vote);
const dataset = userContext.currentDataset?.();
if (dataset?.dataset.id) {
const response = await fetch(apiHost + "/analytics/rag", {
method: "PUT",
headers: {
"Content-Type": "application/json",
"TR-Dataset": dataset.dataset.id,
},
credentials: "include",
body: JSON.stringify({
query_id: queryId,
rating: vote,
}),
});

if (response.status !== 204) {
console.error("Failed to update vote", response);
// Rollback the vote
setStore(queryId, prevVote);
}
}
};

const VoteIcon = (props: {
icon: IconTypes;
onClick: (e: MouseEvent) => void;
}) => {
return (
<div onClick={(e) => props.onClick(e)} class="p-2">
{props.icon({})}
</div>
);
};

return (
<div class="flex flex-col">
<Show
fallback={
<VoteIcon
icon={BsHandThumbsUp}
onClick={(e) => {
e.preventDefault();
updateVote(props.queryId, 1);
}}
/>
}
when={store[props.queryId] === 1}
>
<VoteIcon
icon={BsHandThumbsUpFill}
onClick={(e) => {
e.preventDefault();
updateVote(props.queryId, 0);
}}
/>
</Show>

<Show
fallback={
<VoteIcon
icon={BsHandThumbsDown}
onClick={(e) => {
e.preventDefault();
updateVote(props.queryId, -1);
}}
/>
}
when={store[props.queryId] === -1}
>
<VoteIcon
icon={BsHandThumbsDownFill}
onClick={(e) => {
e.preventDefault();
updateVote(props.queryId, 0);
}}
/>
</Show>
</div>
);
};
4 changes: 3 additions & 1 deletion frontends/chat/src/components/Layouts/MainLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ const MainLayout = (props: LayoutProps) => {

const newMessage = {
content: lastMessage.content + newText,
id: lastMessage.id,
};
return [...prev.slice(0, prev.length - 1), newMessage];
});
Expand Down Expand Up @@ -177,7 +178,7 @@ const MainLayout = (props: LayoutProps) => {
if (regenerateLastMessage) {
requestMethod = "PATCH";
setMessages((prev): Message[] => {
const newMessages = [{ content: "" }];
const newMessages = [{ content: "", id: prev[prev.length - 1].id }];
return [...prev.slice(0, -1), ...newMessages];
});
} else {
Expand Down Expand Up @@ -297,6 +298,7 @@ const MainLayout = (props: LayoutProps) => {
{(message, idx) => {
return (
<AfMessage
queryId={message.id}
normalChat={false}
role={messageRoleFromIndex(idx())}
content={message.content}
Expand Down
1 change: 1 addition & 0 deletions frontends/chat/src/types/messages.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface Message {
id?: string | undefined;
content: string;
}

Expand Down
3 changes: 2 additions & 1 deletion server/src/data/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,10 @@ impl Message {
prompt_tokens: Option<i32>,
completion_tokens: Option<i32>,
dataset_id: T,
message_id: uuid::Uuid,
) -> Self {
Message {
id: uuid::Uuid::new_v4(),
id: message_id,
topic_id: topic_id.into(),
sort_order,
content: content.into(),
Expand Down
1 change: 1 addition & 0 deletions server/src/handlers/message_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ pub async fn create_message(
None,
None,
dataset_org_plan_sub.dataset.id,
uuid::Uuid::new_v4(),
);

// get the previous messages
Expand Down
5 changes: 4 additions & 1 deletion server/src/operators/message_operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ pub async fn create_generic_system_message(
Some(0),
Some(0),
dataset_id,
uuid::Uuid::new_v4(),
);

Ok(system_message)
Expand Down Expand Up @@ -598,10 +599,11 @@ pub async fn stream_response(
.expect("usize to i32 conversion should always succeed"),
),
dataset.id,
query_id,
);

let clickhouse_rag_event = RagQueryEventClickhouse {
id: uuid::Uuid::new_v4(),
id: query_id,
created_at: time::OffsetDateTime::now_utc(),
dataset_id: dataset.id,
search_id: uuid::Uuid::nil(),
Expand Down Expand Up @@ -661,6 +663,7 @@ pub async fn stream_response(
None,
Some(chunk_v.len().try_into().unwrap()),
dataset.id,
query_id_arb,
);

let clickhouse_rag_event = RagQueryEventClickhouse {
Expand Down
14 changes: 13 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,13 @@
dependencies:
"@solid-primitives/utils" "^3.0.2"

"@solid-primitives/storage@^4.2.1":
version "4.2.1"
resolved "https://registry.yarnpkg.com/@solid-primitives/storage/-/storage-4.2.1.tgz#8d6f5bebf81d5fd3d852e9eebe9445f95e90ed42"
integrity sha512-1XUJeaSlizH9Eam/+IbIpslHEnggJMNZXzfsr06AlbG6tJtQENMu0+94ZIvooxt4Cyw46wPzcnHYbSK7LzoQAA==
dependencies:
"@solid-primitives/utils" "^6.2.3"

"@solid-primitives/utils@^2.1.0":
version "2.2.1"
resolved "https://registry.yarnpkg.com/@solid-primitives/utils/-/utils-2.2.1.tgz#7df54fb4802ea5988cc881f6ee3c4d27bff0c6f6"
Expand All @@ -1130,6 +1137,11 @@
resolved "https://registry.yarnpkg.com/@solid-primitives/utils/-/utils-3.1.0.tgz#52edf36dabe62eba94f8356c3b9b788234d088a8"
integrity sha512-/rerChcwgFtHEgVCCBY7BXGHh7a83HcIAzR8QhXJ789geIVbBs2YxHF4UUZlG7ec00NKSfvO3+sQquN/xKQLMw==

"@solid-primitives/utils@^6.2.3":
version "6.2.3"
resolved "https://registry.yarnpkg.com/@solid-primitives/utils/-/utils-6.2.3.tgz#1abed4c74a2696e08bd2e49cf2b86fc8b87a32bd"
integrity sha512-CqAwKb2T5Vi72+rhebSsqNZ9o67buYRdEJrIFzRXz3U59QqezuuxPsyzTSVCacwS5Pf109VRsgCJQoxKRoECZQ==

"@solidjs/router@^0.10.1":
version "0.10.10"
resolved "https://registry.yarnpkg.com/@solidjs/router/-/router-0.10.10.tgz#cd236ef438e9aa50864e7361db710af26b152ad5"
Expand Down Expand Up @@ -7586,7 +7598,7 @@ totalist@^3.0.0:
integrity sha512-sf4i37nQ2LBx4m3wB74y+ubopq6W/dIzXg0FDGjsYnZHVa1Da8FH853wlL2gtUhg+xJXjfk3kUZS3BRoQeoQBQ==

"trieve-ts-sdk@file:clients/ts-sdk":
version "0.0.9"
version "0.0.12"

trim-lines@^3.0.0:
version "3.0.1"
Expand Down

0 comments on commit 5fc7799

Please sign in to comment.