This code adds simple GPT functionality to your Slack workspace. When ever someone mentions your Slack app it will respond. Step by Step tutorial coming soon.
// authenticates you with the API standard library
// type `await lib.` to display API autocomplete
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
// We define our variables using `context.params` property
// All endpoints on Autocode have a `context.params` property that contains the data for each event
let slackInput = context.params.event.event.text;
let botName =
context.params.event.event.blocks[0].elements[0].elements[0].user_id;
let userName = context.params.event.event.user;
let message = context.params.event.event.text;
// We prepare a prompt that primes GPT-3 with the context that it's operating as a Slack bot and give a few examples messages,
// and we pass in the user message and ask it to generate a completion.
let prompt = [
`You are a chat bot inside of a Slack workspace. Your name is <@${botName}>`,
`You respond to questions users ask you, which could be anything. Your goal is to be helpful and welcoming.`,
`Inside users messages to you, they'll refer to you by saying <@${botName}> somewhere in the message.`,
`User input may be multi-line, and you can respond with multiple lines as well. Here are some examples:`,
``,
`${userName} said: Hi <@${botName}>!`,
`Your response: Hello ${userName}, I hope you are having a wonderful day.`,
`${userName} said: <@${botName}> what is the capital of france`,
`Your response: The capital of France is Paris.`,
`${userName} said: i don't like you <@${botName}>...`,
``,
`also i'm bored.`,
`Your response: I like you ${userName}! I hope I can grow on you.`,
``,
`${userName} said: yo <@${botName}> why are the ocean and the sky blue?`,
`The blue color of the ocean and sky are caused by a phenomenon known as Rayleigh scattering. When sunlight passes through the atmosphere, it must interact with tiny air molecules in order to reach the Earth's surface. These molecules scatter short wavelength colors, such as blue and violet, scattering them in all directions. The scattered light then reflects off of the ocean, creating the blue color we see`,
`${userName} said: ${message}`,
`Your response: `,
].join('\n');
let response = '';
// we make an API call to the completions endpoint and set the model we want to interact with
let completion = await lib.openai.playground['@0.0.4'].completions.create({
model: `text-davinci-003`,
prompt: [`${prompt}`],
max_tokens: 512,
temperature: 1,
top_p: 1,
n: 1,
echo: false,
presence_penalty: 0,
frequency_penalty: 0,
best_of: 1,
});
response = completion.choices[0].text;
// We then make an API call to Slack to post a message with the response and tag the prompting slack user
await lib.slack.channels['@0.7.3'].messages.create({
channel: `#general`,
text: `${response} <@${userName}>`,
});