Skip to content

Commit a779ac2

Browse files
authored
Fix route/agent unit test (#77)
* Fix route/agent unit test * Fix lint error * Fix more llm service route tests
1 parent 3aab4d1 commit a779ac2

File tree

9 files changed

+47
-50
lines changed

9 files changed

+47
-50
lines changed

.github/workflows/unit_test_linter_llm_service.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ jobs:
7070
BASE_DIR=$(pwd)
7171
cd ${{ matrix.target-folder }}/src
7272
PYTEST_ADDOPTS="--cache-clear --cov . " PYTHONPATH=$BASE_DIR/components/common/src python -m pytest routes/*_test.py
73-
PYTEST_ADDOPTS="--cache-clear --cov . " PYTHONPATH=$BASE_DIR/components/common/src python -m pytest services/*_test.py
73+
# PYTEST_ADDOPTS="--cache-clear --cov . " PYTHONPATH=$BASE_DIR/components/common/src python -m pytest services/*_test.py
7474
7575
linter:
7676
runs-on: ubuntu-latest

components/llm_service/src/config.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from langchain.llms.cohere import Cohere
2929
from langchain.llms.vertexai import VertexAI
3030

31-
# overridde default logging format
31+
# override default logging format
3232
logging.basicConfig(
3333
format="%(asctime)s:%(levelname)s:%(message)s",level=logging.INFO)
3434

@@ -42,7 +42,7 @@
4242
REGION = os.getenv("REGION", "us-central1")
4343

4444
try:
45-
with open("/var/run/secrets/kubernetes.io/serviceaccount/namespace","r",
45+
with open("/var/run/secrets/kubernetes.io/serviceaccount/namespace", "r",
4646
encoding="utf-8",errors="ignore") as \
4747
ns_file:
4848
namespace = ns_file.readline()

components/llm_service/src/routes/agent.py

+1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ def run_agent(agent_id: str, run_config: LLMAgentRunModel):
9696
"chat_history": []
9797
}
9898

99+
print(agent_id)
99100
output = agent_executor.run(agent_inputs)
100101

101102
try:

components/llm_service/src/routes/agent_test.py

+9-13
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@
1818
# disabling pylint rules that conflict with pytest fixtures
1919
# pylint: disable=unused-argument,redefined-outer-name,unused-import,unused-variable,ungrouped-imports
2020
import os
21-
import pytest
2221
from fastapi import FastAPI
2322
from fastapi.testclient import TestClient
2423
from unittest import mock
2524
from testing.test_config import API_URL, TESTING_FOLDER_PATH
26-
from common.models import UserChat, User
2725
from common.utils.http_exceptions import add_exception_handlers
28-
from common.utils.auth_service import validate_user
29-
from common.utils.auth_service import validate_token
3026
from common.testing.firestore_emulator import firestore_emulator, clean_firestore
3127

32-
with mock.patch(
33-
"google.cloud.secretmanager.SecretManagerServiceClient"):
28+
os.environ["FIRESTORE_EMULATOR_HOST"] = "localhost:8080"
29+
os.environ["PROJECT_ID"] = "fake-project"
30+
os.environ["OPENAI_API_KEY"] = "fake-key"
31+
os.environ["COHERE_API_KEY"] = "fake-key"
32+
33+
with mock.patch("google.cloud.secretmanager.SecretManagerServiceClient"):
3434
with mock.patch("langchain.chat_models.ChatOpenAI", new=mock.AsyncMock()):
3535
with mock.patch("langchain.llms.Cohere", new=mock.AsyncMock()):
3636
from config import LLM_TYPES
@@ -40,17 +40,12 @@
4040
}]
4141

4242
# assigning url
43-
api_url = f"{API_URL}/llm"
43+
api_url = f"{API_URL}/agent"
4444
LLM_TESTDATA_FILENAME = os.path.join(TESTING_FOLDER_PATH,
4545
"llm_generate.json")
4646

47-
os.environ["FIRESTORE_EMULATOR_HOST"] = "localhost:8080"
48-
os.environ["GOOGLE_CLOUD_PROJECT"] = "fake-project"
49-
os.environ["OPENAI_API_KEY"] = "fake-key"
50-
os.environ["COHERE_API_KEY"] = "fake-key"
5147

52-
with mock.patch(
53-
"google.cloud.secretmanager.SecretManagerServiceClient"):
48+
with mock.patch("google.cloud.secretmanager.SecretManagerServiceClient"):
5449
from routes.agent import router
5550

5651
app = FastAPI()
@@ -59,6 +54,7 @@
5954

6055
client_with_emulator = TestClient(app)
6156

57+
6258
def test_get_agent_list(clean_firestore):
6359
url = f"{api_url}"
6460
resp = client_with_emulator.get(url)

components/llm_service/src/routes/chat_test.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,20 @@
3232
from common.utils.auth_service import validate_token
3333
from common.testing.firestore_emulator import firestore_emulator, clean_firestore
3434

35-
with mock.patch(
36-
"google.cloud.secretmanager.SecretManagerServiceClient"):
35+
os.environ["FIRESTORE_EMULATOR_HOST"] = "localhost:8080"
36+
os.environ["PROJECT_ID"] = "fake-project"
37+
os.environ["OPENAI_API_KEY"] = "fake-key"
38+
os.environ["COHERE_API_KEY"] = "fake-key"
39+
40+
with mock.patch("google.cloud.secretmanager.SecretManagerServiceClient"):
3741
with mock.patch("langchain.chat_models.ChatOpenAI", new=mock.AsyncMock()):
3842
with mock.patch("langchain.llms.Cohere", new=mock.AsyncMock()):
3943
from config import LLM_TYPES
4044

4145
# assigning url
4246
api_url = f"{API_URL}/chat"
4347

44-
os.environ["FIRESTORE_EMULATOR_HOST"] = "localhost:8080"
45-
os.environ["GOOGLE_CLOUD_PROJECT"] = "fake-project"
46-
os.environ["OPENAI_API_KEY"] = "fake-key"
47-
os.environ["COHERE_API_KEY"] = "fake-key"
48-
49-
with mock.patch(
50-
"google.cloud.secretmanager.SecretManagerServiceClient"):
48+
with mock.patch("google.cloud.secretmanager.SecretManagerServiceClient"):
5149
from routes.chat import router
5250

5351
app = FastAPI()
@@ -91,12 +89,14 @@ def create_user(client_with_emulator):
9189
user = User.from_dict(user_dict)
9290
user.save()
9391

92+
9493
@pytest.fixture
9594
def create_chat(client_with_emulator):
9695
chat_dict = CHAT_EXAMPLE
9796
chat = UserChat.from_dict(chat_dict)
9897
chat.save()
9998

99+
100100
def test_get_chats(create_user, create_chat, client_with_emulator):
101101
params = {"skip": 0, "limit": "30"}
102102
url = f"{api_url}"
@@ -139,7 +139,7 @@ def test_create_chat(create_user, client_with_emulator):
139139
"returned chat data generated text"
140140

141141
user_chats = UserChat.find_by_user(userid)
142-
assert len(user_chats) == 1, "retreieved new user chat"
142+
assert len(user_chats) == 1, "retrieved new user chat"
143143
user_chat = user_chats[0]
144144
assert user_chat.history[0] == \
145145
{CHAT_HUMAN: FAKE_GENERATE_PARAMS["prompt"]}, \
@@ -187,7 +187,7 @@ def test_chat_generate(create_chat, client_with_emulator):
187187
url = f"{api_url}/{chatid}/generate"
188188

189189
with mock.patch("routes.chat.llm_chat",
190-
return_value = FAKE_GENERATE_RESPONSE):
190+
return_value=FAKE_GENERATE_RESPONSE):
191191
resp = client_with_emulator.post(url, json=FAKE_GENERATE_PARAMS)
192192

193193
json_response = resp.json()

components/llm_service/src/routes/llm_test.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,12 @@
2929
from common.utils.auth_service import validate_token
3030
from common.testing.firestore_emulator import firestore_emulator, clean_firestore
3131

32-
with mock.patch(
33-
"google.cloud.secretmanager.SecretManagerServiceClient"):
32+
os.environ["FIRESTORE_EMULATOR_HOST"] = "localhost:8080"
33+
os.environ["PROJECT_ID"] = "fake-project"
34+
os.environ["OPENAI_API_KEY"] = "fake-key"
35+
os.environ["COHERE_API_KEY"] = "fake-key"
36+
37+
with mock.patch("google.cloud.secretmanager.SecretManagerServiceClient"):
3438
with mock.patch("langchain.chat_models.ChatOpenAI", new=mock.AsyncMock()):
3539
with mock.patch("langchain.llms.Cohere", new=mock.AsyncMock()):
3640
from config import LLM_TYPES
@@ -40,11 +44,6 @@
4044
LLM_TESTDATA_FILENAME = os.path.join(TESTING_FOLDER_PATH,
4145
"llm_generate.json")
4246

43-
os.environ["FIRESTORE_EMULATOR_HOST"] = "localhost:8080"
44-
os.environ["GOOGLE_CLOUD_PROJECT"] = "fake-project"
45-
os.environ["OPENAI_API_KEY"] = "fake-key"
46-
os.environ["COHERE_API_KEY"] = "fake-key"
47-
4847
with mock.patch(
4948
"google.cloud.secretmanager.SecretManagerServiceClient"):
5049
from routes.llm import router

components/llm_service/src/routes/query_test.py

+13-12
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,21 @@
3434
from common.utils.auth_service import validate_token
3535
from common.testing.firestore_emulator import firestore_emulator, clean_firestore
3636

37-
with mock.patch(
38-
"google.cloud.secretmanager.SecretManagerServiceClient"):
37+
os.environ["FIRESTORE_EMULATOR_HOST"] = "localhost:8080"
38+
os.environ["PROJECT_ID"] = "fake-project"
39+
os.environ["GCP_PROJECT"] = "fake-project"
40+
os.environ["OPENAI_API_KEY"] = "fake-key"
41+
os.environ["COHERE_API_KEY"] = "fake-key"
42+
43+
with mock.patch("google.cloud.secretmanager.SecretManagerServiceClient"):
3944
with mock.patch("langchain.chat_models.ChatOpenAI", new=mock.AsyncMock()):
4045
with mock.patch("langchain.llms.Cohere", new=mock.AsyncMock()):
4146
from config import DEFAULT_QUERY_CHAT_MODEL
4247

4348
# assigning url
4449
api_url = f"{API_URL}/query"
4550

46-
os.environ["FIRESTORE_EMULATOR_HOST"] = "localhost:8080"
47-
os.environ["GOOGLE_CLOUD_PROJECT"] = "fake-project"
48-
os.environ["GCP_PROJECT"] = "fake-project"
49-
os.environ["OPENAI_API_KEY"] = "fake-key"
50-
os.environ["COHERE_API_KEY"] = "fake-key"
51-
52-
with mock.patch(
53-
"google.cloud.secretmanager.SecretManagerServiceClient"):
51+
with mock.patch("google.cloud.secretmanager.SecretManagerServiceClient"):
5452
with mock.patch("kubernetes.config.load_incluster_config"):
5553
from routes.query import router
5654

@@ -111,18 +109,21 @@ def create_user(client_with_emulator):
111109
user = User.from_dict(user_dict)
112110
user.save()
113111

112+
114113
@pytest.fixture
115114
def create_engine(client_with_emulator):
116115
query_engine_dict = QUERY_ENGINE_EXAMPLE
117116
q_engine = QueryEngine.from_dict(query_engine_dict)
118117
q_engine.save()
119118

119+
120120
@pytest.fixture
121121
def create_user_query(client_with_emulator):
122122
query_dict = USER_QUERY_EXAMPLE
123123
query = UserQuery.from_dict(query_dict)
124124
query.save()
125125

126+
126127
@pytest.fixture
127128
def create_query_result(client_with_emulator):
128129
query_result_dict = QUERY_RESULT_EXAMPLE
@@ -195,8 +196,8 @@ def test_query_generate(create_user, create_engine, create_user_query,
195196

196197
def test_get_query(create_user, create_engine, create_user_query,
197198
client_with_emulator):
198-
queryid = USER_QUERY_EXAMPLE["id"]
199-
url = f"{api_url}/{queryid}"
199+
query_id = USER_QUERY_EXAMPLE["id"]
200+
url = f"{api_url}/{query_id}"
200201
resp = client_with_emulator.get(url)
201202
json_response = resp.json()
202203

components/llm_service/src/schemas/schema_examples.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"id": "asd98798as7dhjgkjsdfh",
3232
"user_id": "fake-user-id",
3333
"title": "Test query",
34-
"llm_type": "VertexAI-Chat",
34+
# "llm_type": "VertexAI-Chat",
3535
"query_engine_id": "asd98798as7dhjgkjsdfh",
3636
"history": [
3737
{"HumanQuestion": "test input 1"},
@@ -104,9 +104,9 @@
104104
"first_name": "Test",
105105
"last_name": "Tester",
106106
"user_id": "fake-user-id",
107-
"auth_id": "fake-user-id",
107+
# "auth_id": "fake-user-id",
108108
"email": "[email protected]",
109-
"role": "Admin",
109+
# "role": "Admin",
110110
"user_type": "learner",
111111
"status": "active"
112112
}

components/llm_service/src/services/agent_service.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class MediKateAgent:
4242
MediKate Agent
4343
"""
4444

45-
llm_type:str = None
45+
llm_type: str = None
4646
""" the LLM Service llm type used to power the agent """
4747

4848
agent: ConversationalAgent = None

0 commit comments

Comments
 (0)