A Discord bot powered by OpenAI's GPT, designed to engage in context-aware conversations and generate coherent responses.
- Real-time conversation with users in Discord channels
- Context-aware conversation using conversation history
- Token optimization by managing conversation length and API usage
- Customizable command prefix
app └── src ├── config │ └── openai.js ├── features │ └── gpt │ ├── gpt.controller.js │ ├── gpt.service.js │ └── utils │ ├── intents.js ├── index.js ├── .env ├── package.json └── README.md
The Conversation Context feature enables the bot to maintain a context-aware conversation with users by keeping track of the conversation history for each user or channel. This history is then sent as part of the prompt to the OpenAI API, ensuring that the generated responses are contextually relevant and coherent.
-
Store conversation history: The bot stores the conversation history for each user or channel in a data structure, such as an object or a Map.
-
Update conversation history: When a new message is received, the bot appends the message to the corresponding user's or channel's conversation history.
-
Create API prompt: The bot concatenates the conversation history and the new message to create the prompt for the OpenAI API.
-
Send prompt to OpenAI API: The concatenated prompt is sent to the OpenAI API, which generates a contextually relevant and coherent response based on the conversation history.
-
Update conversation history with response: The bot stores the generated response in the conversation history for the user or channel.
-
Send response to user or channel: The bot sends the generated response back to the user or channel, maintaining a context-aware conversation.
User: "Tell me a joke." Bot: "Why did the chicken cross the road? To get to the other side!"
User: "That's an old one. Tell me another." Bot: "Sure, here's another one: Why don't scientists trust atoms? Because they make up everything!"
As shown in the example above, the bot is able to maintain context and generate coherent responses by leveraging the Conversation Context feature.