Repository: nomic-ai/gpt4all
File: gpt4all-chat/tests/python/test_server_api.py (Python)

Code before:
    'id': 'placeholder',
    'model': 'Llama 3.2 1B Instruct',
    'object': 'text_completion',
    'usage': {
        'completion_tokens': 6,
        'prompt_tokens': 5,
        'total_tokens': 11,
    },
}


def test_with_models(chat_server_with_model: None) -> None:
    response = request.get('models', wait=True)
    assert response == {
        'data': [EXPECTED_MODEL_INFO],
        'object': 'list',
    }

    # Test the specific model endpoint
    response = request.get('models/Llama 3.2 1B Instruct', wait=True)
    assert response == EXPECTED_MODEL_INFO

    # Test the completions endpoint
    with pytest.raises(HTTPError) as excinfo:
        request.post('completions', data=None, wait=True)
    assert excinfo.value.response.status_code == 400

    data = {
        'model': 'Llama 3.2 1B Instruct',
        'prompt': 'The quick brown fox',
        'temperature': 0,
    }

    response = request.post('completions', data=data, wait=True)
    assert 'choices' in response
    assert response['choices'][0]['text'] == ' jumps over the lazy dog.'
    assert 'created' in response
    response.pop('created')  # Remove the dynamic field for comparison
    assert response == EXPECTED_COMPLETIONS_RESPONSE

    data['temperature'] = 0.5

    pytest.xfail('This causes an assertion failure in the app. See https://github.com/nomic-ai/gpt4all/issues/3133')
    with pytest.raises(HTTPError) as excinfo:
        response = request.post('completions', data=data, wait=True)


Review comment (suggestion):
We can be more strict here, at least by checking the number of choices and the keys in a choice:
```suggestion
    assert len(response['choices']) == 1
    assert response['choices'][0].keys() == {'text', 'index', 'logprobs', 'finish_reason'}
    assert response['choices'][0]['text'] == ' jumps over the lazy dog.'
```

Code after:
EXPECTED_COMPLETIONS_RESPONSE = {
    'choices': [
        {
            'finish_reason': 'stop',
            'index': 0,
            'logprobs': None,
            'references': None,
            'text': ' jumps over the lazy dog.',
        },
    ],
    'id': 'placeholder',
    'model': 'Llama 3.2 1B Instruct',
    'object': 'text_completion',
    'usage': {
        'completion_tokens': 6,
        'prompt_tokens': 5,
        'total_tokens': 11,
    },
}


def test_with_models(chat_server_with_model: None) -> None:
    response = request.get('models', wait=True)
    assert response == {
        'data': [EXPECTED_MODEL_INFO],
        'object': 'list',
    }

    # Test the specific model endpoint
    response = request.get('models/Llama 3.2 1B Instruct')
    assert response == EXPECTED_MODEL_INFO

    # Test the completions endpoint
    status_code, response = request.post('completions', data=None, raise_for_status=False)
    assert status_code == 400
    assert response == {'error': {
        'code': None,
        'message': 'error parsing request JSON: illegal value',
        'param': None,
        'type': 'invalid_request_error',
    }}

    data = {
        'model': 'Llama 3.2 1B Instruct',
        'prompt': 'The quick brown fox',
        'temperature': 0,
    }

    response = request.post('completions', data=data)
    assert len(response['choices']) == 1
    assert response['choices'][0].keys() == {'text', 'index', 'logprobs', 'references', 'finish_reason'}