Skip to content

Commit

Permalink
fix: update code to new OpenAI API
Browse files Browse the repository at this point in the history
  • Loading branch information
simoneb committed Jan 31, 2024
1 parent 8feea16 commit 7daa5ea
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
10 changes: 4 additions & 6 deletions packages/slack-bot/src/bot.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// https://github.com/seratch/slack-app-examples/blob/86bd224476814a42c41c133f9009ea66c0717517/serverless-bolt-template/gcp-js/app.js
import bolt from '@slack/bolt'
import { Configuration, OpenAIApi } from 'openai'
import OpenAI from 'openai'
import { getAnswer } from './getAnswer.js'
import { transcribe } from './utils.js'

Expand All @@ -17,11 +17,9 @@ const app = new App({

const expressApp = expressReceiver.app

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

const errorResponse =
'It appears I have run into an issue looking up an answer for you. Please try again'
Expand Down
16 changes: 12 additions & 4 deletions packages/slack-bot/src/getAnswer.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ function subscribeToEmbeddingChanges() {

/**
* Create a context for a question by finding the most similar context from the dataframe
* @param {Object} args
* @param {import('openai').OpenAI} args.openai
*/
async function createContext({
openai,
Expand All @@ -73,12 +75,12 @@ async function createContext({
embeddingModel = defaultEmbeddingModel
}) {
// Get the embeddings for the question
const response = await openai.createEmbedding({
const response = await openai.embeddings.create({
model: embeddingModel,
input: question
})

const queryEmbedding = response.data.data[0].embedding
const queryEmbedding = response.data[0].embedding

// Get the distances from the embeddings
const distances = distancesFromEmbeddings({
Expand All @@ -104,6 +106,12 @@ async function createContext({
return context
}

/**
*
* @param {Object} args
* @param {import('openai').OpenAI} args.openai
* @returns
*/
async function getAnswer({
dataSet: customDataSet,
model = 'gpt-4',
Expand All @@ -130,7 +138,7 @@ async function getAnswer({
embeddingModel
})

const response = await openai.createChatCompletion({
const response = await openai.chat.completions.create({
messages: [
{ role: 'system', content: 'You are a helpful assistant' },
{
Expand Down Expand Up @@ -172,7 +180,7 @@ async function getAnswer({
model
})

return response.data.choices[0].message.content.trim()
return response.choices[0].message.content.trim()
}

export { getAnswer }
Expand Down
19 changes: 12 additions & 7 deletions packages/slack-bot/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,18 @@ export async function downloadAudio(url, id) {
})
}

/**
*
* @param {*} file
* @param {import('openai').OpenAI} openai
* @returns
*/
export async function transcribe(file, openai) {
const p = await downloadAudio(file.url_private_download, file.id)
const transcribe = await openai.createTranscription(
f.createReadStream(p),
'whisper-1',
undefined,
'text'
)
return transcribe.data
const transcribe = await openai.audio.transcriptions.create({
file: f.createReadStream(p),
model: 'whisper-1',
response_format: 'text'
})
return transcribe.text
}

0 comments on commit 7daa5ea

Please sign in to comment.