Skip to content

Commit

Permalink
Merge pull request #698 from ArtBlocks/season-3
Browse files Browse the repository at this point in the history
trivia season 3
  • Loading branch information
grantoesterling authored Nov 25, 2024
2 parents 1e53588 + a045d1f commit 5261b5f
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 366 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@
"graphql-tag": "^2.12.6",
"husky": "^8.0.3",
"jest": "^28.0.3",
"langchain": "^0.0.66",
"lodash.deburr": "^4.1.0",
"ms": "^2.0.0",
"node-fetch": "^2.6.1",
"node-html-parser": "^3.2.0",
"openai": "^4.73.0",
"prettier": "^2.6.2",
"react": "18.2.0",
"reconnecting-websocket": "^4.4.0",
Expand Down
48 changes: 26 additions & 22 deletions src/Classes/TriviaBot.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as dotenv from 'dotenv'
dotenv.config()
import { OpenAIChat } from 'langchain/llms/openai'
import { OpenAI } from 'openai'
import { ProjectBot } from './ProjectBot'
import { Client, EmbedBuilder, Message, TextChannel } from 'discord.js'
import { projectConfig } from '..'
Expand All @@ -9,33 +9,21 @@ import axios from 'axios'
import { getTokenApiUrl, replaceVideoWithGIF } from './APIBots/utils'
import { getAllTriviaScores, updateTriviaScore } from '../Data/supabase'

// NOTE: if you change this, you'll need to manually update the supabase upsert query in updateTriviaScore
export const CURRENT_SEASON = 'season_two'
export const CURRENT_SEASON = 'season_three'

export class TriviaBot {
bot: Client
model?: OpenAIChat
model?: OpenAI
channel?: TextChannel
previousQuestion?: Message
previousQuestionEmbed?: EmbedBuilder

previousAnswers: string[] = []
currentTriviaAnswer: string
constructor(bot: Client) {
this.bot = bot
this.model = process.env.OPENAI_API_KEY
? new OpenAIChat({
modelName: 'gpt-3.5-turbo', // With valid API keys can also use 'gpt-4'
temperature: 0,
prefixMessages: [
{
role: 'system',
content: `
You are a bot that thinks of trivia questions with art projects as the answers. I will provide the title of the project and the project description.
DO NOT include the answer in your response.
`,
},
],
? new OpenAI({
apiKey: process.env.OPENAI_API_KEY, // This is the default and can be omitted
})
: undefined

Expand Down Expand Up @@ -149,14 +137,30 @@ Next question:`
throw new Error("Can't ask trivia question - no OpenAI API key")
}

let question = await this.model.call(
`Generate a short, cryptic, poetic, vague, difficult riddle that has the answer: "${project.projectName}". It is VERY important that you DO NOT include the answer in your response. The project description is: "${project.description}"`
)
const completion = await this.model.chat.completions.create({
model: 'gpt-4o-mini',
messages: [
{
role: 'system',
content:
'You are a trivia bot that generates trivia questions about generative art projects. The most important rule is NEVER to state the answer in your response',
},
{
role: 'user',
content: `Generate a short, cryptic, poetic, vague, difficult riddle that has the answer: "${project.projectName}". It is VERY important that you DO NOT include the answer in your response. The project description is: "${project.description}"`,
},
],
})

let answer = completion.choices[0].message.content ?? ''
const regex = new RegExp(project.projectName, 'gi')
question = question.replaceAll(regex, '_____')
answer = answer.replaceAll(regex, '_____')

embed.setDescription(question)
if (!answer.length) {
throw new Error('No answer from GPT')
}

embed.setDescription(answer)

return embed
}
Expand Down
7 changes: 5 additions & 2 deletions src/Data/supabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ export const updateTriviaScore = async (username: string): Promise<number> => {
seasonScore = parseInt(data[0][CURRENT_SEASON] ?? 0) + 1
}

// NOTE: When changing seasons, we have to manually change the upsert column here. Super annoying I know.
const { error } = await supabaseClient
.from(process.env.TRIVIA_TABLE ?? '')
.upsert({ user: `${username}`, score: totalScore, season_two: seasonScore })
.upsert({
user: `${username}`,
score: totalScore,
[CURRENT_SEASON]: seasonScore,
})

if (error) {
console.error('Error updating trivia score', error)
Expand Down
Loading

0 comments on commit 5261b5f

Please sign in to comment.