Skip to content

Commit

Permalink
OpenAiGptService should map the CLI model param values to the OpenAI …
Browse files Browse the repository at this point in the history
…API model param values

Prompt:
Modify the OpenAiGptService class.
In the call() method, if model == "gpt3" then set model to "gpt-3.5-turbo"
In the call() method, if model == "gpt4" then set model to "gpt-4"
Add tests for your changes in test/OpenAiGptService.test.js
Use sinon to assert that openai.createChatCompletion receives the correct model value
  • Loading branch information
ferrislucas committed May 4, 2023
1 parent 86ecf0f commit be7a7f8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/services/OpenAiGptService.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { encode } from "gpt-3-encoder"

export default class OpenAiGptService {
static async call(prompt, model) {
if (model == "gpt3") model = "gpt-3.5-turbo";
if (model == "gpt4") model = "gpt-4";

const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY
})
Expand Down
23 changes: 23 additions & 0 deletions test/OpenAiGptService.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,27 @@ describe('OpenAiGptService', () => {
])
}));
});

it('should pass the correct model value to openai.createChatCompletion', async () => {
const prompt = 'What is the capital of France?';
const expectedResult = 'The capital of France is Paris.';
const models = ['gpt3', 'gpt4'];
const expectedModels = ['gpt-3.5-turbo', 'gpt-4'];

const configStub = sinon.stub(ConfigService, 'retrieveConfig').resolves({ api: { temperature: 0.5 } });
const openaiStub = sinon.stub(OpenAIApi.prototype, 'createChatCompletion').resolves({
data: {
choices: [
{ message: { content: expectedResult } }
]
}
});

for (let i = 0; i < models.length; i++) {
await OpenAiGptService.call(prompt, models[i]);
sinon.assert.calledWith(openaiStub.getCall(i), sinon.match({ model: expectedModels[i] }));
}

sinon.assert.callCount(openaiStub, models.length);
});
});

0 comments on commit be7a7f8

Please sign in to comment.