def test_generate_questions_empty_data(monkeypatch): monkeypatch.setattr(openai.Completion, 'create', mock_create) request_data = GPTRequest(api_key="MockKey", data=[], num_responses=2) with pytest.raises(Exception): gpt3_generator = GPT3ParaphraseGenerator(request_data=request_data) gpt3_generator.paraphrases() request_data = GPTRequest(api_key="MockKey", data=[""], num_responses=2) with pytest.raises(Exception): gpt3_generator = GPT3ParaphraseGenerator(request_data=request_data) gpt3_generator.paraphrases() request_data = GPTRequest(api_key="MockKey", data=[ "Are there any more test questions?", "Are there more questions?", "" ], num_responses=2) with pytest.raises(Exception): gpt3_generator = GPT3ParaphraseGenerator(request_data=request_data) gpt3_generator.paraphrases() request_data = GPTRequest(api_key="MockKey", data=[ "Are there any more test questions?", "Are there more questions?" ], num_responses=2) gpt3_generator = GPT3ParaphraseGenerator(request_data=request_data) resp = gpt3_generator.paraphrases() assert resp == {'Response text from gpt3'}
def test_generate_questions_empty_api_key(monkeypatch): monkeypatch.setattr(openai.Completion, 'create', mock_create) request_data = GPTRequest(api_key="", data=["Are there any more test questions?"], num_responses=2) with pytest.raises(Exception): gpt3_generator = GPT3ParaphraseGenerator(request_data=request_data) gpt3_generator.paraphrases()
def test_generate_questions(monkeypatch): monkeypatch.setattr(openai.Completion, 'create', mock_create) request_data = GPTRequest(api_key="MockKey", data=["Are there any more test questions?"], num_responses=2) gpt3_generator = GPT3ParaphraseGenerator(request_data=request_data) augmented_questions = gpt3_generator.paraphrases() assert augmented_questions == {"Response text from gpt3"}
def test_questions_set_generation(monkeypatch): monkeypatch.setattr(GPT, 'submit_request', mock_submit_request) request_data = GPTRequest(api_key="MockKey", data=["Are there any more test questions?"], num_responses=13) gpt3_generator = GPT3ParaphraseGenerator(request_data=request_data) augmented_questions = gpt3_generator.paraphrases() expected_augmented_questions = { "Are there any further test questions?", "Is there another test question?", "Are there more test questions?" } assert augmented_questions == expected_augmented_questions
def test_generate_questions_invalid_api_key(): from openai import APIError responses.add( url="https://api.openai.com/v1/engines/davinci/completions", method="POST", status=500, body= "Incorrect API key provided: InvalidKey. You can find your API key at https://beta.openai.com." ) request_data = GPTRequest(api_key="InvalidKey", data=["Are there any more test questions?"], num_responses=2) gpt3_generator = GPT3ParaphraseGenerator(request_data=request_data) with pytest.raises( APIError, match= r'.*Incorrect API key provided: InvalidKey. You can find your API key at https://beta.openai.com..*' ): gpt3_generator.paraphrases()
def test_generate_questions_invalid_api_key(): responses.add( url="https://api.openai.com/v1/engines/davinci/completions", method="POST", body= "Incorrect API key provided: InvalidKey. You can find your API key at https://beta.openai.com." ) request_data = GPTRequest(api_key="InvalidKey", data=["Are there any more test questions?"], num_responses=2) gpt3_generator = GPT3ParaphraseGenerator(request_data=request_data) try: gpt3_generator.paraphrases() except Exception as e: assert str( e ) == "Invalid response body from API: Incorrect API key provided: InvalidKey. You can find your API key at https://beta.openai.com. (HTTP response code was 200)" with pytest.raises(Exception): gpt3_generator.paraphrases()