Skip to content

Commit

Permalink
fix: Dont create assistants per user
Browse files Browse the repository at this point in the history
  • Loading branch information
bracesproul committed Oct 24, 2024
1 parent 83a1470 commit 67880b7
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 38 deletions.
1 change: 0 additions & 1 deletion frontend/app/components/ThreadHistory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ interface ThreadHistoryProps {
userId: string | undefined;
createThread: (id: string) => Promise<any>;
clearMessages: () => void;
assistantId: string | undefined;
switchSelectedThread: (thread: ThreadActual) => void;
getUserThreads: (id: string) => Promise<void>;
deleteThread: (id: string) => Promise<void>;
Expand Down
34 changes: 3 additions & 31 deletions frontend/app/hooks/useGraph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { getCookie, setCookie } from "../utils/cookies";
import { ThreadActual, useThreads } from "./useThreads";
import { ModelOptions } from "../types";
import { useRuns } from "./useRuns";
import { ASSISTANT_ID_COOKIE_NAME } from "../utils/constants";
import { USER_ID_COOKIE_NAME } from "../utils/constants";

export const createClient = () => {
const apiUrl = process.env.NEXT_PUBLIC_API_URL ?? "http://localhost:3000/api";
Expand Down Expand Up @@ -61,32 +61,12 @@ interface UseGraphInput {

export function useGraph(inputArgs: UseGraphInput) {
const { toast } = useToast();
const { getThreadById, setThreadId } = useThreads(inputArgs.userId);
const { setThreadId } = useThreads(inputArgs.userId);
const { shareRun } = useRuns();
const [messages, setMessages] = useState<BaseMessage[]>([]);
const [assistantId, setAssistantId] = useState<string>();
const [selectedModel, setSelectedModel] =
useState<ModelOptions>("openai/gpt-4o-mini");

useEffect(() => {
if (assistantId || typeof window === "undefined") return;
getOrCreateAssistant();
}, []);

const getOrCreateAssistant = async () => {
const assistantIdCookie = getCookie(ASSISTANT_ID_COOKIE_NAME);
if (assistantIdCookie) {
setAssistantId(assistantIdCookie);
return;
}
const client = createClient();
const assistant = await client.assistants.create({
graphId: "chat",
});
setAssistantId(assistant.assistant_id);
setCookie(ASSISTANT_ID_COOKIE_NAME, assistant.assistant_id);
};

const streamMessage = async (params: GraphInput) => {
if (!inputArgs.threadId) {
toast({
Expand All @@ -95,13 +75,6 @@ export function useGraph(inputArgs: UseGraphInput) {
});
return undefined;
}
if (!assistantId) {
toast({
title: "Error",
description: "Assistant ID not found",
});
return undefined;
}

const client = createClient();

Expand All @@ -122,7 +95,7 @@ export function useGraph(inputArgs: UseGraphInput) {
}),
};

const stream = client.runs.stream(inputArgs.threadId, assistantId, {
const stream = client.runs.stream(inputArgs.threadId, "chat", {
input,
streamMode: "events",
config: {
Expand Down Expand Up @@ -682,7 +655,6 @@ export function useGraph(inputArgs: UseGraphInput) {

return {
messages,
assistantId,
selectedModel,
setSelectedModel,
setMessages,
Expand Down
6 changes: 1 addition & 5 deletions frontend/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export default function ContentComposerChatInterface(): React.ReactElement {
messages,
setMessages,
streamMessage,
assistantId,
switchSelectedThread,
selectedModel,
setSelectedModel,
Expand All @@ -47,15 +46,13 @@ export default function ContentComposerChatInterface(): React.ReactElement {
});
const [isRunning, setIsRunning] = useState(false);

const isSubmitDisabled = !userId || !assistantId || !currentThread;
const isSubmitDisabled = !userId || !currentThread;

async function onNew(message: AppendMessage): Promise<void> {
if (isSubmitDisabled) {
let description = "";
if (!userId) {
description = "Unable to find user ID. Please try again later.";
} else if (!assistantId) {
description = "Unable to find assistant ID. Please try again later.";
} else if (!currentThread) {
description =
"Unable to find current thread ID. Please try again later.";
Expand Down Expand Up @@ -121,7 +118,6 @@ export default function ContentComposerChatInterface(): React.ReactElement {
userThreads={userThreads}
userId={userId}
createThread={createThread}
assistantId={assistantId}
deleteThread={(id) => deleteThread(id, () => setMessages([]))}
clearMessages={() => setMessages([])}
/>
Expand Down
1 change: 0 additions & 1 deletion frontend/app/utils/constants.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export const RESPONSE_FEEDBACK_KEY = "user_score";
export const SOURCE_CLICK_KEY = "user_click";
export const ASSISTANT_ID_COOKIE_NAME = "clc_assistant_id_v3";
export const THREAD_ID_COOKIE_NAME = "clc_thread_id_v3";
export const USER_ID_COOKIE_NAME = "clc_user_id_v3";

0 comments on commit 67880b7

Please sign in to comment.