Skip to content

Commit

Permalink
Fix route/agent unit test (#77)
Browse files Browse the repository at this point in the history
* Fix route/agent unit test
* Fix lint error
* Fix more llm service route tests
  • Loading branch information
lavjain authored Oct 19, 2023
1 parent 3aab4d1 commit a779ac2
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/unit_test_linter_llm_service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ jobs:
BASE_DIR=$(pwd)
cd ${{ matrix.target-folder }}/src
PYTEST_ADDOPTS="--cache-clear --cov . " PYTHONPATH=$BASE_DIR/components/common/src python -m pytest routes/*_test.py
PYTEST_ADDOPTS="--cache-clear --cov . " PYTHONPATH=$BASE_DIR/components/common/src python -m pytest services/*_test.py
# PYTEST_ADDOPTS="--cache-clear --cov . " PYTHONPATH=$BASE_DIR/components/common/src python -m pytest services/*_test.py
linter:
runs-on: ubuntu-latest
Expand Down
4 changes: 2 additions & 2 deletions components/llm_service/src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from langchain.llms.cohere import Cohere
from langchain.llms.vertexai import VertexAI

# overridde default logging format
# override default logging format
logging.basicConfig(
format="%(asctime)s:%(levelname)s:%(message)s",level=logging.INFO)

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

try:
with open("/var/run/secrets/kubernetes.io/serviceaccount/namespace","r",
with open("/var/run/secrets/kubernetes.io/serviceaccount/namespace", "r",
encoding="utf-8",errors="ignore") as \
ns_file:
namespace = ns_file.readline()
Expand Down
1 change: 1 addition & 0 deletions components/llm_service/src/routes/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def run_agent(agent_id: str, run_config: LLMAgentRunModel):
"chat_history": []
}

print(agent_id)
output = agent_executor.run(agent_inputs)

try:
Expand Down
22 changes: 9 additions & 13 deletions components/llm_service/src/routes/agent_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@
# disabling pylint rules that conflict with pytest fixtures
# pylint: disable=unused-argument,redefined-outer-name,unused-import,unused-variable,ungrouped-imports
import os
import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient
from unittest import mock
from testing.test_config import API_URL, TESTING_FOLDER_PATH
from common.models import UserChat, User
from common.utils.http_exceptions import add_exception_handlers
from common.utils.auth_service import validate_user
from common.utils.auth_service import validate_token
from common.testing.firestore_emulator import firestore_emulator, clean_firestore

with mock.patch(
"google.cloud.secretmanager.SecretManagerServiceClient"):
os.environ["FIRESTORE_EMULATOR_HOST"] = "localhost:8080"
os.environ["PROJECT_ID"] = "fake-project"
os.environ["OPENAI_API_KEY"] = "fake-key"
os.environ["COHERE_API_KEY"] = "fake-key"

with mock.patch("google.cloud.secretmanager.SecretManagerServiceClient"):
with mock.patch("langchain.chat_models.ChatOpenAI", new=mock.AsyncMock()):
with mock.patch("langchain.llms.Cohere", new=mock.AsyncMock()):
from config import LLM_TYPES
Expand All @@ -40,17 +40,12 @@
}]

# assigning url
api_url = f"{API_URL}/llm"
api_url = f"{API_URL}/agent"
LLM_TESTDATA_FILENAME = os.path.join(TESTING_FOLDER_PATH,
"llm_generate.json")

os.environ["FIRESTORE_EMULATOR_HOST"] = "localhost:8080"
os.environ["GOOGLE_CLOUD_PROJECT"] = "fake-project"
os.environ["OPENAI_API_KEY"] = "fake-key"
os.environ["COHERE_API_KEY"] = "fake-key"

with mock.patch(
"google.cloud.secretmanager.SecretManagerServiceClient"):
with mock.patch("google.cloud.secretmanager.SecretManagerServiceClient"):
from routes.agent import router

app = FastAPI()
Expand All @@ -59,6 +54,7 @@

client_with_emulator = TestClient(app)


def test_get_agent_list(clean_firestore):
url = f"{api_url}"
resp = client_with_emulator.get(url)
Expand Down
22 changes: 11 additions & 11 deletions components/llm_service/src/routes/chat_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,20 @@
from common.utils.auth_service import validate_token
from common.testing.firestore_emulator import firestore_emulator, clean_firestore

with mock.patch(
"google.cloud.secretmanager.SecretManagerServiceClient"):
os.environ["FIRESTORE_EMULATOR_HOST"] = "localhost:8080"
os.environ["PROJECT_ID"] = "fake-project"
os.environ["OPENAI_API_KEY"] = "fake-key"
os.environ["COHERE_API_KEY"] = "fake-key"

with mock.patch("google.cloud.secretmanager.SecretManagerServiceClient"):
with mock.patch("langchain.chat_models.ChatOpenAI", new=mock.AsyncMock()):
with mock.patch("langchain.llms.Cohere", new=mock.AsyncMock()):
from config import LLM_TYPES

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

os.environ["FIRESTORE_EMULATOR_HOST"] = "localhost:8080"
os.environ["GOOGLE_CLOUD_PROJECT"] = "fake-project"
os.environ["OPENAI_API_KEY"] = "fake-key"
os.environ["COHERE_API_KEY"] = "fake-key"

with mock.patch(
"google.cloud.secretmanager.SecretManagerServiceClient"):
with mock.patch("google.cloud.secretmanager.SecretManagerServiceClient"):
from routes.chat import router

app = FastAPI()
Expand Down Expand Up @@ -91,12 +89,14 @@ def create_user(client_with_emulator):
user = User.from_dict(user_dict)
user.save()


@pytest.fixture
def create_chat(client_with_emulator):
chat_dict = CHAT_EXAMPLE
chat = UserChat.from_dict(chat_dict)
chat.save()


def test_get_chats(create_user, create_chat, client_with_emulator):
params = {"skip": 0, "limit": "30"}
url = f"{api_url}"
Expand Down Expand Up @@ -139,7 +139,7 @@ def test_create_chat(create_user, client_with_emulator):
"returned chat data generated text"

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

with mock.patch("routes.chat.llm_chat",
return_value = FAKE_GENERATE_RESPONSE):
return_value=FAKE_GENERATE_RESPONSE):
resp = client_with_emulator.post(url, json=FAKE_GENERATE_PARAMS)

json_response = resp.json()
Expand Down
13 changes: 6 additions & 7 deletions components/llm_service/src/routes/llm_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@
from common.utils.auth_service import validate_token
from common.testing.firestore_emulator import firestore_emulator, clean_firestore

with mock.patch(
"google.cloud.secretmanager.SecretManagerServiceClient"):
os.environ["FIRESTORE_EMULATOR_HOST"] = "localhost:8080"
os.environ["PROJECT_ID"] = "fake-project"
os.environ["OPENAI_API_KEY"] = "fake-key"
os.environ["COHERE_API_KEY"] = "fake-key"

with mock.patch("google.cloud.secretmanager.SecretManagerServiceClient"):
with mock.patch("langchain.chat_models.ChatOpenAI", new=mock.AsyncMock()):
with mock.patch("langchain.llms.Cohere", new=mock.AsyncMock()):
from config import LLM_TYPES
Expand All @@ -40,11 +44,6 @@
LLM_TESTDATA_FILENAME = os.path.join(TESTING_FOLDER_PATH,
"llm_generate.json")

os.environ["FIRESTORE_EMULATOR_HOST"] = "localhost:8080"
os.environ["GOOGLE_CLOUD_PROJECT"] = "fake-project"
os.environ["OPENAI_API_KEY"] = "fake-key"
os.environ["COHERE_API_KEY"] = "fake-key"

with mock.patch(
"google.cloud.secretmanager.SecretManagerServiceClient"):
from routes.llm import router
Expand Down
25 changes: 13 additions & 12 deletions components/llm_service/src/routes/query_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,21 @@
from common.utils.auth_service import validate_token
from common.testing.firestore_emulator import firestore_emulator, clean_firestore

with mock.patch(
"google.cloud.secretmanager.SecretManagerServiceClient"):
os.environ["FIRESTORE_EMULATOR_HOST"] = "localhost:8080"
os.environ["PROJECT_ID"] = "fake-project"
os.environ["GCP_PROJECT"] = "fake-project"
os.environ["OPENAI_API_KEY"] = "fake-key"
os.environ["COHERE_API_KEY"] = "fake-key"

with mock.patch("google.cloud.secretmanager.SecretManagerServiceClient"):
with mock.patch("langchain.chat_models.ChatOpenAI", new=mock.AsyncMock()):
with mock.patch("langchain.llms.Cohere", new=mock.AsyncMock()):
from config import DEFAULT_QUERY_CHAT_MODEL

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

os.environ["FIRESTORE_EMULATOR_HOST"] = "localhost:8080"
os.environ["GOOGLE_CLOUD_PROJECT"] = "fake-project"
os.environ["GCP_PROJECT"] = "fake-project"
os.environ["OPENAI_API_KEY"] = "fake-key"
os.environ["COHERE_API_KEY"] = "fake-key"

with mock.patch(
"google.cloud.secretmanager.SecretManagerServiceClient"):
with mock.patch("google.cloud.secretmanager.SecretManagerServiceClient"):
with mock.patch("kubernetes.config.load_incluster_config"):
from routes.query import router

Expand Down Expand Up @@ -111,18 +109,21 @@ def create_user(client_with_emulator):
user = User.from_dict(user_dict)
user.save()


@pytest.fixture
def create_engine(client_with_emulator):
query_engine_dict = QUERY_ENGINE_EXAMPLE
q_engine = QueryEngine.from_dict(query_engine_dict)
q_engine.save()


@pytest.fixture
def create_user_query(client_with_emulator):
query_dict = USER_QUERY_EXAMPLE
query = UserQuery.from_dict(query_dict)
query.save()


@pytest.fixture
def create_query_result(client_with_emulator):
query_result_dict = QUERY_RESULT_EXAMPLE
Expand Down Expand Up @@ -195,8 +196,8 @@ def test_query_generate(create_user, create_engine, create_user_query,

def test_get_query(create_user, create_engine, create_user_query,
client_with_emulator):
queryid = USER_QUERY_EXAMPLE["id"]
url = f"{api_url}/{queryid}"
query_id = USER_QUERY_EXAMPLE["id"]
url = f"{api_url}/{query_id}"
resp = client_with_emulator.get(url)
json_response = resp.json()

Expand Down
6 changes: 3 additions & 3 deletions components/llm_service/src/schemas/schema_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"id": "asd98798as7dhjgkjsdfh",
"user_id": "fake-user-id",
"title": "Test query",
"llm_type": "VertexAI-Chat",
# "llm_type": "VertexAI-Chat",
"query_engine_id": "asd98798as7dhjgkjsdfh",
"history": [
{"HumanQuestion": "test input 1"},
Expand Down Expand Up @@ -104,9 +104,9 @@
"first_name": "Test",
"last_name": "Tester",
"user_id": "fake-user-id",
"auth_id": "fake-user-id",
# "auth_id": "fake-user-id",
"email": "[email protected]",
"role": "Admin",
# "role": "Admin",
"user_type": "learner",
"status": "active"
}
Expand Down
2 changes: 1 addition & 1 deletion components/llm_service/src/services/agent_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class MediKateAgent:
MediKate Agent
"""

llm_type:str = None
llm_type: str = None
""" the LLM Service llm type used to power the agent """

agent: ConversationalAgent = None
Expand Down

0 comments on commit a779ac2

Please sign in to comment.