This is a Next.js with OpenAI
integration. More info on OpenAI API Reference
node v18 is neccesary to run this app.
First, install the dependencies:
npm install
# or
yarn install
Then, replace .env_template file to just .env file and put your credentials
OPENAI_API_KEY=
OPENAI_ORGANIZATION=
Finally run the development server:
npm run dev
# or
yarn dev
# or
pnpm dev
# or
bun dev
Open http://localhost:3000 with your browser to see the result.
Service Logic:
-
Method: setModel
- Description: This method sets the model to be used in the API calls. Default model is gpt-4-1106-preview
-
Method: getModels
- Description: This method returns the list of models available in OpenAI API.
-
Method: createAssistant
- Description: This method creates an assistant in OpenAI API.
-
Method: createThread
- Description: This method creates a thread in OpenAI API.
-
Method: deleteThread
- Description: This method delete a thread from threadId
-
Method: createMessage
- Description: This method creates a message in OpenAI API.
-
Method: runner
- Description: This method creates a runner in OpenAI API.
-
Method: retrieve
- Description: This method retrieves a runner in OpenAI API.
-
Method: getMessages
- Description: This method retrieves messages from threadId.
API Logic:
-
Path: GET pages/api/models.ts
- Method: getModels
- Description: This method returns the list of models available in OpenAI API.
- Method: getModels
-
Path: POST pages/api/assistant.ts
- Method: createAssistant
- Description: This method creates an assistant in OpenAI API. It's neccesary to pass instructions parameter, tools parameter is optional and it's an array of tools
- Method: createAssistant
-
Path: POST pages/api/thread.ts - Method: createThread - Description: This method creates a thread in OpenAI API.
-
Path: DELETE pages/api/thread.ts - Method: deleteThread - Description: This method delete a thread from threadId in OpenAI API.
-
Path: POST pages/api/chat.ts - Method: createMessage, runner, retrieve, longPollingCall, getMessages - Description: This method creates a message in OpenAI API. Then it creates a runner and waits for the runner to be completed. It's neccesary to pass threadId, assistantId and text parameters. instructions parameter is optional.
-
Path: POST pages/api/messages.ts - Method: getMessages - Description: This method returns messages from threadId
API routes can be accessed on http://localhost:3000/api/test. This endpoint can be edited in pages/api/test.ts
.
The pages/api
directory is mapped to /api/*
. Files in this directory are treated as API routes instead of React pages.
To learn more about Next.js, take a look at the following resources:
- Next.js Documentation - learn about Next.js features and API.
- Learn Next.js - an interactive Next.js tutorial.
You can check out the Next.js GitHub repository - your feedback and contributions are welcome!
The easiest way to deploy your Next.js app is to use the Vercel Platform from the creators of Next.js.
Check out our Next.js deployment documentation for more details.