Skip to content

Commit

Permalink
Giving life to Orpheus ! - Added chatbot functionality. (#76)
Browse files Browse the repository at this point in the history
* Initial commit

* Create .env

* Deleted .env file because openai disables the api key.

I had to delete the .env with the API key because OPENAI can detect it. Please put OPENAI_API_KEY as an env variable.

* Fixed possible spam issue and enhanced response.

* Conditionally gate GPT to only run if key is set

---------

Co-authored-by: Max Wofford <[email protected]>
  • Loading branch information
DevSrijit and maxwofford authored May 2, 2023
1 parent 03eb461 commit 6f290ba
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 0 deletions.
60 changes: 60 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"js-yaml": "^3.13.1",
"jsonwebtoken": "^8.5.1",
"lodash": "^4.17.21",
"openai": "^3.2.1",
"pluralize": "^8.0.0",
"pusher": "^5.1.1-beta"
},
Expand Down
10 changes: 10 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import interactionLeaderInvite from "./interactions/leaderInvite";
import interactionAddress from "./interactions/address";
import interactionClubAddress from "./interactions/clubAddress";
import interactionDM from "./interactions/dm";
import interactionGPT from "./interactions/gpt";
import interactionStartup from "./interactions/startup";
import interactionWordcloud from "./interactions/wordcloud";
import interactionReport from "./interactions/report";
Expand Down Expand Up @@ -93,6 +94,15 @@ controller.hears(
interactionBreakoutUpdate
);

if (process.env.OPENAI_API_KEY) {
// temporarily gate this to only run when the key is set
controller.hears(
["Orpheus", "orpheus", "OrpheusGPT", "orpheusgpt", "Orpheusgpt"],
"direct_message,indirect_mention,direct_mention",
interactionGPT
);
}

controller.hears("info", "direct_message,direct_mention", interactionInfo);

controller.hears("hacktoberfest", "ambient", interactionHacktoberfest);
Expand Down
62 changes: 62 additions & 0 deletions src/interactions/gpt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { transcript } from "../utils";
const { Configuration, OpenAIApi } = require("openai");

const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);

async function createCompletion(text) {
const response = await openai.createCompletion({
model: "davinci:ft-hack-club-2023-04-24-16-24-28",
prompt: text,
temperature: 0.2,
max_tokens: 1500,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
});

return response.data.choices[0].text;
}

const interactionGPT = (bot, message) => {
const { channel, text } = message;
text = text.replace("Orpheus", "").trim();
let prompt = [
`Create a chatbot conversation between a user and Hack Club's mascot Orpheus. The conversation should feel natural and friendly, with Orpheus showcasing its unique personality and style. The chatbot should be able to respond to a wide range of user messages, both related and unrelated to coding. The chatbot should also be able to recognize when the user needs help with coding and provide relevant assistance. Finally, the chatbot should not reveal itself as an AI model or a chatbot, but rather as Orpheus itself.`,
`Here are some example conversations:`,
`User: Orpheus, let's chat! What have you been up to lately?`,
`Orpheus: Hey there! I've been up to all sorts of things lately. Just the other day, I was learning about new coding languages and trying to figure out how to apply them in my adventures. What about you? Have you been working on any coding projects recently?`,
`User: Actually, I'm a bit stuck on a coding problem. Do you think you could help me out?`,
`Orpheus: Of course! I'm always happy to help out my fellow hackers. What's the problem you're facing? Maybe I can help you figure it out.`,
`User: I'm having trouble with this piece of code. It keeps giving me errors and I can't figure out why.`,
`Orpheus: Hmm, that's definitely a tricky problem. Have you tried checking your syntax and making sure all your variables are properly defined? Sometimes that can cause errors. If that doesn't work, we can dive deeper into the problem and see what's really going on.`,
`User: Thanks, Orpheus! You're the best. Do you have any tips for learning coding faster?`,
`Orpheus: Well, one thing that's helped me is practicing coding challenges and pushing myself to try new things. It can also be really helpful to work with other people and bounce ideas off of each other. And of course, never give up! Coding can be tough sometimes, but it's always worth it in the end."`,
`Now the real user sends a message, with reference to the prompt and your knowledge, respond in the best way possible.\n
User: ${text}\n
Orpheus: `,
].join("\n");
let returntext = createCompletion(prompt);
if (channel == "C0266FRGT") {
return; // #announcements
}
returntext.then((result) => {
if (result.includes("Orpheus:")) {
result = result.replace("Orpheus:", "").trim();
}
if (result.includes("<@")) {
result =
"Response cannot contain a ping, please try to avoid pinging people.";
}
bot.reply(message, transcript(result), (err, src) => {
if (err) {
console.error(err);
return;
}
});
});
};

export default interactionGPT;

1 comment on commit 6f290ba

@DevSrijit
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great ! :D

Please sign in to comment.