Skip to content

Commit

Permalink
Add some error handling when OPENAI_API_KEY is not in the env
Browse files Browse the repository at this point in the history
  • Loading branch information
foot committed Feb 9, 2024
1 parent aad7162 commit bd76aa9
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 16 deletions.
16 changes: 15 additions & 1 deletion src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { EmbeddingsComboBox } from "./EmbeddingsComboBox";
import { DotPlot, PlotData } from "./Plot";
import { toLatLng, toUnitSphere } from "../helpers/layout";
import { cosineDistance } from "../helpers/embeddings";
import { QueriesDisabledModal } from "./QueriesDisabledModal";

function App() {
const [selectedQuery, setSelectedQuery] = useState<ExampleQuery>(
Expand All @@ -23,7 +24,11 @@ function App() {
const [embeddingsData, setEmbeddingsData] = useState<EmbeddingsData>([]);
const [layoutData, setLayoutData] = useState<LayoutData>([]);
const [animate, setAnimate] = useState<boolean>(false);
const activeEmbedding = useQueryEmbedding(selectedQuery.query);
const [queryError, setQueryError] = useState<string>("");
const onQueryError = useCallback((err: string) => {
setQueryError(err);
}, []);
const activeEmbedding = useQueryEmbedding(selectedQuery.query, onQueryError);

const [displacement, setDisplacement] = useState<number>(1);
const [exponent, setExponent] = useState<number>(4);
Expand Down Expand Up @@ -91,12 +96,21 @@ function App() {
setSelectedLayoutPoint(index);
}, []);

const closeQueriesDisabledModal = useCallback(() => {
setQueryError("");
}, []);

if (!data) {
return "Loading...";
}

return (
<div className="bg-gray-100 h-screen flex flex-col">
<QueriesDisabledModal
message={queryError}
isOpen={Boolean(queryError)}
closeModal={closeQueriesDisabledModal}
/>
<header className="bg-white shadow">
<div className="mx-10 my-6">
<h1 className="text-3xl font-bold text-gray-900">Embedding Sphere</h1>
Expand Down
1 change: 0 additions & 1 deletion src/components/Plot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ export function PlotData({ similarities, onSelect }: PlotDataProps) {
}

containerRef.current.append(plot);
console.log("remove");
return () => plot.remove();
}, [similarities, onSelect]);

Expand Down
73 changes: 73 additions & 0 deletions src/components/QueriesDisabledModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { Dialog, Transition } from "@headlessui/react";
import { Fragment } from "react";

export function QueriesDisabledModal({
isOpen,
closeModal,
message,
}: {
isOpen: boolean;
message: string;
closeModal: () => void;
}) {
return (
<>
<Transition appear show={isOpen} as={Fragment}>
<Dialog as="div" className="relative z-10" onClose={closeModal}>
<Transition.Child
as={Fragment}
enter="ease-out duration-300"
enterFrom="opacity-0"
enterTo="opacity-100"
leave="ease-in duration-200"
leaveFrom="opacity-100"
leaveTo="opacity-0"
>
<div className="fixed inset-0 bg-black/25" />
</Transition.Child>

<div className="fixed inset-0 overflow-y-auto">
<div className="flex min-h-full items-center justify-center p-4 text-center">
<Transition.Child
as={Fragment}
enter="ease-out duration-300"
enterFrom="opacity-0 scale-95"
enterTo="opacity-100 scale-100"
leave="ease-in duration-200"
leaveFrom="opacity-100 scale-100"
leaveTo="opacity-0 scale-95"
>
<Dialog.Panel className="w-full max-w-md transform overflow-hidden rounded-2xl bg-white p-6 text-left align-middle shadow-xl transition-all">
<Dialog.Title
as="h3"
className="text-lg font-medium leading-6 text-gray-900"
>
An error occurred
</Dialog.Title>
<div className="mt-2">
<p className="text-sm text-gray-500">{message}</p>
<p className="text-sm text-gray-500 mt-4">
Querying for custom values is only currently supported
running this UI locally and reading an OPENAI_API_KEY from
the process / terminal env.
</p>
</div>

<div className="mt-4">
<button
type="button"
className="inline-flex justify-center rounded-md border border-transparent bg-blue-100 px-4 py-2 text-sm font-medium text-blue-900 hover:bg-blue-200 focus:outline-none focus-visible:ring-2 focus-visible:ring-blue-500 focus-visible:ring-offset-2"
onClick={closeModal}
>
Okay
</button>
</div>
</Dialog.Panel>
</Transition.Child>
</div>
</div>
</Dialog>
</Transition>
</>
);
}
40 changes: 26 additions & 14 deletions src/hooks/useQueryEmbeddings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import OpenAI from "openai";
import { useEffect, useMemo, useState } from "react";
import { ExampleQuery, exampleQueries } from "../helpers/example-queries";

export function useQueryEmbedding(value: string) {
export function useQueryEmbedding(
value: string,
onError: (err: string) => void
) {
const [embeddingQueries, setEmbeddingQueries] = useState<ExampleQuery[]>([]);

const allEmbeddings = useMemo(
Expand All @@ -12,27 +15,36 @@ export function useQueryEmbedding(value: string) {

useEffect(() => {
const q = allEmbeddings.find((e) => e.query === value);
// already got the embeddings
if (q) {
return;
}
const openai = new OpenAI({
apiKey: import.meta.env["VITE_OPENAI_API_KEY"],
dangerouslyAllowBrowser: true,
});

const fetchEmbeddings = async () => {
const chatCompletion = await openai.embeddings.create({
input: value,
model: "text-embedding-3-small",
});
setEmbeddingQueries((prev) => [
...prev,
{ query: value, embedding: chatCompletion.data[0].embedding },
]);
try {
const openai = new OpenAI({
apiKey: import.meta.env["VITE_OPENAI_API_KEY"],
dangerouslyAllowBrowser: true,
});

const chatCompletion = await openai.embeddings.create({
input: value,
model: "text-embedding-3-small",
});

setEmbeddingQueries((prev) => [
...prev,
{ query: value, embedding: chatCompletion.data[0].embedding },
]);
} catch (e) {
if (onError) {
onError((e as Error).message);
}
}
};

fetchEmbeddings();
}, [value, allEmbeddings]);
}, [value, allEmbeddings, onError]);

const activeEmbedding = allEmbeddings.find((e) => e.query === value);
return activeEmbedding;
Expand Down

0 comments on commit bd76aa9

Please sign in to comment.