This commit is contained in:
Martin McKeaveney 2023-04-26 15:56:46 +01:00
parent 84f52683b2
commit 4f020a4db4
2 changed files with 47 additions and 45 deletions

View File

@ -1,4 +1,4 @@
import { Configuration, OpenAIApi } from "openai"; import { Configuration, OpenAIApi } from "openai"
import { import {
AutomationActionStepId, AutomationActionStepId,
AutomationStepSchema, AutomationStepSchema,
@ -7,7 +7,7 @@ import {
AutomationIOType, AutomationIOType,
} from "@budibase/types" } from "@budibase/types"
import * as automationUtils from "../automationUtils" import * as automationUtils from "../automationUtils"
import environment from "../../environment"; import environment from "../../environment"
enum Model { enum Model {
GPT_35_TURBO = "gpt-3.5-turbo", GPT_35_TURBO = "gpt-3.5-turbo",
@ -61,7 +61,8 @@ export async function run({ inputs, context }: AutomationStepInput) {
if (!environment.OPENAI_API_KEY) { if (!environment.OPENAI_API_KEY) {
return { return {
success: false, success: false,
response: "OpenAI API Key not configured - please add the OPENAI_API_KEY environment variable.", response:
"OpenAI API Key not configured - please add the OPENAI_API_KEY environment variable.",
} }
} }
@ -75,19 +76,19 @@ export async function run({ inputs, context }: AutomationStepInput) {
try { try {
const configuration = new Configuration({ const configuration = new Configuration({
apiKey: environment.OPENAI_API_KEY, apiKey: environment.OPENAI_API_KEY,
}); })
const openai = new OpenAIApi(configuration); const openai = new OpenAIApi(configuration)
const completion = await openai.createChatCompletion({ const completion = await openai.createChatCompletion({
model: inputs.model, model: inputs.model,
messages: [ messages: [
{ {
role: "user", role: "user",
content: inputs.prompt content: inputs.prompt,
} },
], ],
}); })
let response = completion?.data?.choices[0]?.message?.content let response = completion?.data?.choices[0]?.message?.content

View File

@ -1,23 +1,26 @@
const setup = require("./utilities") const setup = require("./utilities")
import environment from "../../environment"; import environment from "../../environment"
import openai from "openai" import openai from "openai"
jest.mock("openai", jest.fn(() => ({ jest.mock(
Configuration: jest.fn(), "openai",
OpenAIApi: jest.fn(() => ({ jest.fn(() => ({
createChatCompletion: jest.fn(() => ({ Configuration: jest.fn(),
data: { OpenAIApi: jest.fn(() => ({
choices: [ createChatCompletion: jest.fn(() => ({
{ data: {
message: { choices: [
content: "This is a test" {
message: {
content: "This is a test",
},
}, },
} ],
] },
} })),
})) })),
})) }))
}))) )
const OPENAI_PROMPT = "What is the meaning of life?" const OPENAI_PROMPT = "What is the meaning of life?"
@ -34,52 +37,50 @@ describe("test the openai action", () => {
afterAll(setup.afterAll) afterAll(setup.afterAll)
it("should present the correct error message when the OPENAI_API_KEY variable isn't set", async () => { it("should present the correct error message when the OPENAI_API_KEY variable isn't set", async () => {
delete environment.OPENAI_API_KEY delete environment.OPENAI_API_KEY
let res = await setup.runStep("OPEN_AI", let res = await setup.runStep("OPEN_AI", {
{ prompt: OPENAI_PROMPT,
prompt: OPENAI_PROMPT })
} expect(res.response).toEqual(
"OpenAI API Key not configured - please add the OPENAI_API_KEY environment variable."
) )
expect(res.response).toEqual("OpenAI API Key not configured - please add the OPENAI_API_KEY environment variable.")
expect(res.success).toBeFalsy() expect(res.success).toBeFalsy()
}) })
it("should be able to receive a response from ChatGPT given a prompt", async () => { it("should be able to receive a response from ChatGPT given a prompt", async () => {
const res = await setup.runStep("OPEN_AI", const res = await setup.runStep("OPEN_AI", {
{ prompt: OPENAI_PROMPT,
prompt: OPENAI_PROMPT })
}
)
expect(res.response).toEqual("This is a test") expect(res.response).toEqual("This is a test")
expect(res.success).toBeTruthy() expect(res.success).toBeTruthy()
}) })
it("should present the correct error message when a prompt is not provided", async () => { it("should present the correct error message when a prompt is not provided", async () => {
const res = await setup.runStep("OPEN_AI", const res = await setup.runStep("OPEN_AI", {
{ prompt: null,
prompt: null })
} expect(res.response).toEqual(
"Budibase OpenAI Automation Failed: No prompt supplied"
) )
expect(res.response).toEqual("Budibase OpenAI Automation Failed: No prompt supplied")
expect(res.success).toBeFalsy() expect(res.success).toBeFalsy()
}) })
it("should present the correct error message when an error is thrown from the createChatCompletion call", async () => { it("should present the correct error message when an error is thrown from the createChatCompletion call", async () => {
openai.OpenAIApi.mockImplementation(() => ({ openai.OpenAIApi.mockImplementation(() => ({
createChatCompletion: jest.fn(() => { createChatCompletion: jest.fn(() => {
throw new Error("An error occurred while calling createChatCompletion"); throw new Error("An error occurred while calling createChatCompletion")
}), }),
})); }))
const res = await setup.runStep("OPEN_AI", { const res = await setup.runStep("OPEN_AI", {
prompt: OPENAI_PROMPT, prompt: OPENAI_PROMPT,
}); })
expect(res.response).toEqual("Error: An error occurred while calling createChatCompletion") expect(res.response).toEqual(
"Error: An error occurred while calling createChatCompletion"
)
expect(res.success).toBeFalsy() expect(res.success).toBeFalsy()
}); })
}) })