Skip to content

Commit

Permalink
Adds select box to choose chatgpt model (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanPGill authored May 28, 2024
1 parent b5653f3 commit 23688cf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
31 changes: 31 additions & 0 deletions apps/www/src/components/Chat.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { Message as MessageComponent } from "@/components/demo/message/Message";
import { Button } from "@/components/ui/Button";
import { Input } from "@/components/ui/Input";
import {
Select,
SelectContent,
SelectGroup,
SelectItem,
SelectTrigger,
} from "@/components/ui/Select";
import { Textarea } from "@/components/ui/Textarea";
import { Icons } from "@/icons";
import { cn } from "@/lib/utils";
Expand All @@ -11,6 +18,7 @@ import * as R from "remeda";

const IS_SERVER = typeof window === "undefined";
const CHAT_OPENAI_API_KEY = "CHAT_OPENAI_API_KEY";
const CHAT_GPT_MODELS = ["gpt-3.5-turbo", "gpt-4-turbo", "gpt-4o"];

const ChatMessage: React.FC<{
message: Message;
Expand Down Expand Up @@ -45,6 +53,8 @@ export const Chat = () => {
const [currentApiKey, setCurrentApiKey] = React.useState<string>(
storage?.getItem(CHAT_OPENAI_API_KEY) ?? "",
);
const [selectedChatGptModel, setSelectedChatGptModel] =
React.useState<string>(CHAT_GPT_MODELS[0]);
const { messages, input, handleInputChange, handleSubmit, isLoading } =
useChat({
api: "/api/chat",
Expand All @@ -57,6 +67,7 @@ export const Chat = () => {
],
body: {
apiKey: currentApiKey,
model: selectedChatGptModel,
},
});

Expand All @@ -65,11 +76,31 @@ export const Chat = () => {
setCurrentApiKey(newApiKey);
};

const handleUpdateChatGptModel = (value: string) => {
setSelectedChatGptModel(value);
};

const messagesWithoutSystem = messages.slice(1);
const reversedMessagesWithoutSystem = R.reverse(messagesWithoutSystem);
return (
<div className="bg-muted/50 relative overflow-y-hidden w-full h-[calc(100vh-theme(spacing.18)-2px)]">
<div className="absolute top-4 left-4">
<Select onValueChange={handleUpdateChatGptModel}>
<SelectTrigger className="w-[180px] mb-4">
{selectedChatGptModel}
</SelectTrigger>
<SelectContent>
<SelectGroup>
{CHAT_GPT_MODELS.map((model: string) => {
return (
<SelectItem key={model} value={model}>
{model}
</SelectItem>
);
})}
</SelectGroup>
</SelectContent>
</Select>
<Input
value={currentApiKey}
className="focus-within:border-white"
Expand Down
5 changes: 3 additions & 2 deletions apps/www/src/pages/api/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const prerender = false;
const chatRequestSchema = z.object({
messages: z.any(),
apiKey: z.string(),
model: z.string(),
});

export const POST: APIRoute = async ({ request }) => {
Expand All @@ -21,12 +22,12 @@ export const POST: APIRoute = async ({ request }) => {
{ status: 400 },
);
}
const { apiKey, messages } = result.data;
const { apiKey, messages, model } = result.data;
const openai = new OpenAI({
apiKey,
});
const completion = await openai.chat.completions.create({
model: "gpt-3.5-turbo",
model,
messages: messages,
stream: true,
});
Expand Down

0 comments on commit 23688cf

Please sign in to comment.