Skip to content

Commit 25a3634

Browse files
authored
Merge pull request #8 from olp-cs/upgrade-api-to-pydantic-v2
Upgrade Harmony API to Pydantic v2
2 parents 0872203 + 4f960e8 commit 25a3634

File tree

7 files changed

+16
-15
lines changed

7 files changed

+16
-15
lines changed

harmony_api/core/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import os
2929
from typing import Union
3030

31-
from pydantic import BaseSettings
31+
from pydantic_settings import BaseSettings
3232

3333
GOOGLE_APPLICATION_CREDENTIALS = os.getenv("GOOGLE_APPLICATION_CREDENTIALS", '{}')
3434
if GOOGLE_APPLICATION_CREDENTIALS:

harmony_api/helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def get_example_instruments() -> List[Instrument]:
6060
str(os.getcwd()) + "/example_questionnaires.json", "r", encoding="utf-8"
6161
) as file:
6262
for line in file:
63-
instrument = Instrument.parse_raw(line)
63+
instrument = Instrument.model_validate_json(line)
6464
example_instruments.append(instrument)
6565

6666
return example_instruments

harmony_api/routers/text_router.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,11 @@ def match(match_body: MatchBody) -> MatchResponse:
186186

187187
# Model
188188
model = match_body.parameters
189-
if model.dict() not in ALL_HARMONY_API_MODELS:
189+
if model.model_dump(mode="json") not in ALL_HARMONY_API_MODELS:
190190
raise http_exceptions.CouldNotProcessRequestHTTPException(
191191
"Could not process request because the model does not exist."
192192
)
193-
if not helpers.check_model_availability(model.dict()):
193+
if not helpers.check_model_availability(model.model_dump(mode="json")):
194194
raise http_exceptions.CouldNotProcessRequestHTTPException(
195195
"Could not process request because the model is not available."
196196
)

harmony_api/scheduler.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@
2525
"""
2626

2727
from fastapi.concurrency import run_in_threadpool
28-
from rocketry import Rocketry
29-
from rocketry.conds import cron
28+
# from rocketry import Rocketry
29+
# from rocketry.conds import cron
3030

3131
from harmony_api.services.instruments_cache import InstrumentsCache
3232
from harmony_api.services.vectors_cache import VectorsCache
3333

34-
app = Rocketry(executation="async")
34+
# app = Rocketry(executation="async")
3535

3636

37-
@app.task(cron("0 */12 * * *"))
37+
# @app.task(cron("0 */12 * * *"))
3838
async def do_every_12th_hour():
3939
"""
4040
Save the caches to disk every 12th hour.

harmony_api/services/instruments_cache.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def save(self):
113113
# Instruments to dict
114114
cache_parsed: dict[str, List] = {}
115115
for key, value in self.__cache.items():
116-
instruments = [x.dict() for x in value]
116+
instruments = [x.model_dump(mode="json")() for x in value]
117117
cache_parsed[key] = instruments
118118

119119
with open(cache_file_path, "w", encoding="utf8") as file:

main.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
from harmony_api.routers.info_router import router as info_router
4141
from harmony_api.routers.text_router import router as text_router
4242
from harmony_api.services.instruments_cache import InstrumentsCache
43-
from harmony_api.scheduler import app as app_rocketry
43+
# from harmony_api.scheduler import app as app_rocketry
4444
from harmony_api.services.vectors_cache import VectorsCache
4545

4646
description = """
@@ -89,7 +89,7 @@ class Server(uvicorn.Server):
8989
"""
9090

9191
def handle_exit(self, sig: int, frame):
92-
app_rocketry.session.shut_down()
92+
# app_rocketry.session.shut_down()
9393

9494
return super().handle_exit(sig, frame)
9595

@@ -112,11 +112,12 @@ async def main():
112112
)
113113

114114
api = asyncio.create_task(server.serve())
115-
scheduler = asyncio.create_task(app_rocketry.serve())
115+
# scheduler = asyncio.create_task(app_rocketry.serve())
116116

117117
# Start both applications (FastAPI & Rocketry)
118118
print("INFO:\t Starting applications...")
119-
await asyncio.wait([api, scheduler])
119+
# await asyncio.wait([api, scheduler])
120+
await asyncio.wait(api)
120121

121122

122123
if __name__ == "__main__":

requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
fastapi==0.109.1
22
requests==2.31.0
3-
pydantic==1.10.7
3+
pydantic==2.8.2
4+
pydantic-settings==2.4.0
45
uvicorn==0.21.1
56
pandas==2.0.0
67
tika==2.6.0
@@ -10,7 +11,6 @@ XlsxWriter==3.0.9
1011
openpyxl==3.1.2
1112
sentence-transformers==2.2.2
1213
wget==3.2
13-
rocketry==2.5.1
1414
openai==1.30.2
1515
vertexai==1.49.0
1616
numpy==1.26.4

0 commit comments

Comments
 (0)