Skip to content

Commit

Permalink
Updated LLM Test
Browse files Browse the repository at this point in the history
  • Loading branch information
himanshugt16 committed Nov 17, 2024
1 parent ee81b1f commit 354dae9
Showing 1 changed file with 70 additions and 1 deletion.
71 changes: 70 additions & 1 deletion tests/unit_test/llm_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from aiohttp import ClientConnectionError
from mongoengine import connect

from kairon.shared.rest_client import AioRestClient
from kairon.shared.utils import Utility

Utility.load_system_metadata()
Expand Down Expand Up @@ -1287,4 +1288,72 @@ async def test_gpt3_faq_embedding_predict_with_query_prompt(self, mock_embedding
"input": [query], 'metadata': {'user': user, 'bot': bot, 'invocation': None},
"api_key": key,
"num_retries": 3}
assert not DeepDiff(mock_embedding.call_args[1], expected, ignore_order=True)
assert not DeepDiff(mock_embedding.call_args[1], expected, ignore_order=True)

@pytest.mark.asyncio
@mock.patch.object(AioRestClient, "request", autospec=True)
async def test_collection_exists_success(self, mock_request):
collection_name = "test_collection"
bot = "test_collection_exists_success"
user = "test_new"

llm_secret = LLMSecret(
llm_type="openai",
api_key="openai_key",
models=["model1", "model2"],
api_base_url="https://api.example.com",
bot=bot,
user=user
)
llm_secret.save()

mock_request.return_value = {"status": "ok"}

llm_processor = LLMProcessor(bot, DEFAULT_LLM)

result = await llm_processor.__collection_exists__(collection_name)

mock_request.assert_called_once_with(
mock.ANY,
http_url=f"{llm_processor.db_url}/collections/{collection_name}",
request_method="GET",
headers=llm_processor.headers,
return_json=True,
timeout=5
)
assert result is True
LLMSecret.objects.delete()

@pytest.mark.asyncio
@mock.patch.object(AioRestClient, "request", autospec=True)
async def test_collection_exists_failure(self, mock_request):
collection_name = "test_collection"
bot = "test_collection_exists_failure"
user = "test_new"

llm_secret = LLMSecret(
llm_type="openai",
api_key="openai_key",
models=["model1", "model2"],
api_base_url="https://api.example.com",
bot=bot,
user=user
)
llm_secret.save()

mock_request.side_effect = Exception("Connection error")

llm_processor = LLMProcessor(bot, DEFAULT_LLM)

result = await llm_processor.__collection_exists__(collection_name)

mock_request.assert_called_once_with(
mock.ANY,
http_url=f"{llm_processor.db_url}/collections/{collection_name}",
request_method="GET",
headers=llm_processor.headers,
return_json=True,
timeout=5
)
assert result is False
LLMSecret.objects.delete()

0 comments on commit 354dae9

Please sign in to comment.