Skip to content

Commit

Permalink
[ENH] Made base_api_url an environment variable (#37)
Browse files Browse the repository at this point in the history
* Made base_api_url an environment variable

* Made changes to the test file

* Update app/api/url_generator.py

Co-authored-by: Arman Jahanpour <77515879+rmanaem@users.noreply.github.com>

* Update README.md

Co-authored-by: Arman Jahanpour <77515879+rmanaem@users.noreply.github.com>

* Update tests/test_url_generator.py

Co-authored-by: Arman Jahanpour <77515879+rmanaem@users.noreply.github.com>

* Required changes made

* Changes made to the test

* Raised runtime error for missing env var

* Minor change

---------

Co-authored-by: Arman Jahanpour <77515879+rmanaem@users.noreply.github.com>
  • Loading branch information
Raya679 and rmanaem authored Aug 14, 2024
1 parent c81129f commit c119ea4
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 17 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ The aim of query-tool-ai would be to make this search process more user-friendly
### Set the environment variables -
| Environment Variable | Type | Required | Default Value if Not Set | Example |
| ---------------------- | ------- | ---------------------------------------- | ------------------------ | --------------------------------------------------------- |
| `NB_API_QUERY_URL` | string | Yes | - | `https://api.neurobagel.org/query/?` |
| `HOST` | string | No | `0.0.0.0` | `127.0.0.1` |
| `PORT` | integer | No | `8000` | `8080` |


### Option 1 : Docker :
- #### First, [install Docker](https://docs.docker.com/get-docker/).
Expand Down
12 changes: 11 additions & 1 deletion app/api/url_generator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import json
import os
from dotenv import load_dotenv
from app.llm_processing.extractions import extract_information
from app.api.validators import (
validate_age_order,
Expand All @@ -11,6 +13,8 @@
get_image_modality_termURL,
)

load_dotenv()


def get_api_url(user_query: str) -> str:
"""
Expand All @@ -22,7 +26,13 @@ def get_api_url(user_query: str) -> str:
Returns:
str: The constructed API URL.
"""
base_api_url = "https://api.neurobagel.org/query/?"
base_api_url = os.getenv("NB_API_QUERY_URL")
if not base_api_url:
raise RuntimeError(
"The application was launched but could not find the NB_API_QUERY_URL environment variable."
)


llm_response = extract_information(user_query)

try:
Expand Down
1 change: 1 addition & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# Start the Ollama services first
ollama serve &
ollama pull mistral &
ollama run mistral &

# Start the FastAPI application
Expand Down
52 changes: 36 additions & 16 deletions tests/test_url_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,30 @@
from app.api.url_generator import get_api_url


def test_get_api_url_without_env_var(monkeypatch):
# Unset the environment variable
monkeypatch.delenv("NB_API_QUERY_URL", raising=False)

user_query = "Some query"

with pytest.raises(RuntimeError, match="The application was launched but could not find the NB_API_QUERY_URL environment variable."):
get_api_url(user_query)


def test_json_decode_error():
with patch(
"app.api.url_generator.extract_information",
return_value="{invalid_json: true",
with patch.dict(
"os.environ",
{"NB_API_QUERY_URL": "https://api.neurobagel.org/query/?"},
):
result = get_api_url("some user query")
assert (
result
== "I'm sorry, but I couldn't find the information you're looking for. Could you provide more details or clarify your question?"
)
with patch(
"app.api.url_generator.extract_information",
return_value="{invalid_json: true",
):
result = get_api_url("some user query")
assert (
result
== "I'm sorry, but I couldn't find the information you're looking for. Could you provide more details or clarify your question?"
)


# Define the mock responses for diagnosis and assessment term URLs
Expand Down Expand Up @@ -105,13 +119,19 @@ def test_get_api_url(
mock_input_side_effect,
expected_output,
):
with patch(
"app.api.url_generator.extract_information",
return_value=mock_llm_response,
with patch.dict(
"os.environ",
{"NB_API_QUERY_URL": "https://api.neurobagel.org/query/?"},
):
if mock_input_side_effect is not None:
with patch("builtins.input", side_effect=mock_input_side_effect):
with patch(
"app.api.url_generator.extract_information",
return_value=mock_llm_response,
):
if mock_input_side_effect is not None:
with patch(
"builtins.input", side_effect=mock_input_side_effect
):
result = get_api_url(user_query)
else:
result = get_api_url(user_query)
else:
result = get_api_url(user_query)
assert result == expected_output
assert result == expected_output

0 comments on commit c119ea4

Please sign in to comment.