From a63c9123dda1ff9700a1d97d16bd79d91d6385ca Mon Sep 17 00:00:00 2001 From: Enrico Ros Date: Fri, 24 Mar 2023 01:39:51 -0700 Subject: [PATCH] Improve the Api (/api/chat) payload --- pages/api/chat.ts | 12 +++--------- pages/index.tsx | 12 +++++++++++- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/pages/api/chat.ts b/pages/api/chat.ts index 2c5b0b789..0c70728c1 100644 --- a/pages/api/chat.ts +++ b/pages/api/chat.ts @@ -1,8 +1,6 @@ import type { NextRequest } from 'next/server'; import { createParser, ParsedEvent, ReconnectInterval } from 'eventsource-parser'; -import { UiMessage } from '../../components/ChatMessage'; - if (!process.env.OPENAI_API_KEY) console.warn('OPENAI_API_KEY has not been provided in this deployment environment. ' + @@ -11,7 +9,7 @@ if (!process.env.OPENAI_API_KEY) // definition for OpenAI wire types -interface ChatMessage { +export interface ChatMessage { role: 'assistant' | 'system' | 'user'; content: string; } @@ -134,7 +132,7 @@ async function OpenAIStream(apiKey: string, payload: Omit ({ - role: role, - content: text, - })); // select key const apiKey = userApiKey || process.env.OPENAI_API_KEY || ''; @@ -164,7 +158,7 @@ export default async function handler(req: NextRequest) { const stream: ReadableStream = await OpenAIStream(apiKey, { model, - messages: chatGptInputMessages, + messages, temperature, max_tokens, }); diff --git a/pages/index.tsx b/pages/index.tsx index 9b35300d8..18040fe1f 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -7,6 +7,7 @@ import SettingsOutlinedIcon from '@mui/icons-material/SettingsOutlined'; import SmartToyOutlinedIcon from '@mui/icons-material/SmartToyOutlined'; import SmartToyTwoToneIcon from '@mui/icons-material/SmartToyTwoTone'; +import { ChatApiInput } from './api/chat'; import { ChatGptModelData, isValidOpenAIApiKey, loadOpenAIApiKey, Settings } from '../components/Settings'; import { ChatMessage, UiMessage } from '../components/ChatMessage'; import { Composer } from '../components/Composer'; @@ -137,10 +138,19 @@ export default function Conversation() { const getBotMessageStreaming = async (messages: UiMessage[]) => { + const payload: ChatApiInput = { + apiKey: loadOpenAIApiKey(), + model: chatModel, + messages: messages.map(({ role, text }) => ({ + role: role, + content: text, + })), + }; + const response = await fetch('/api/chat', { method: 'POST', headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ apiKey: loadOpenAIApiKey(), model: chatModel, messages: messages }), + body: JSON.stringify(payload), }); if (response.body) {