Skip to content

Commit

Permalink
Improve package structure for re-usability
Browse files Browse the repository at this point in the history
  • Loading branch information
tiadams committed Feb 22, 2024
1 parent 21a7bf5 commit fbeabdd
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.
28 changes: 28 additions & 0 deletions index/api/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
from starlette.middleware.cors import CORSMiddleware
from starlette.responses import RedirectResponse

from index.repository.sqllite import SQLLiteRepository
from index.embedding import MPNetAdapter

app = FastAPI(
title="INDEX",
description="Intelligent data steward toolbox using Large Language Model embeddings "
Expand Down Expand Up @@ -31,6 +34,8 @@
)

logger = logging.getLogger("uvicorn.info")
repository = SQLLiteRepository()
embedding_model = MPNetAdapter()


@app.get("/", include_in_schema=False)
Expand All @@ -41,3 +46,26 @@ def swagger_redirect():
@app.get("/version", tags=["info"])
def get_current_version():
return app.version


@app.post("/mappings", tags=["mappings"])
async def get_closest_mappings_for_text(text: str):
embedding = embedding_model.get_embedding(text)
closest_mappings, similarities = repository.get_closest_mappings(embedding)
response_data = []
for mapping, similarity in zip(closest_mappings, similarities):
concept = mapping.concept
terminology = concept.terminology
response_data.append({
"concept": {
"id": concept.id,
"name": concept.name,
"terminology": {
"id": terminology.id,
"name": terminology.name
}
},
"text": mapping.text,
"similarity": similarity
})
return response_data
Empty file added index/repository/__init__.py
Empty file.
23 changes: 23 additions & 0 deletions index/repository/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from abc import ABC, abstractmethod


class BaseRepository(ABC):
@abstractmethod
def store(self, model_object_instance):
"""Store a single model object instance."""
pass

@abstractmethod
def store_all(self, model_object_instances):
"""Store multiple model object instances."""
pass

@abstractmethod
def get_closest_mappings(self, embedding, limit=5):
"""Get the closest mappings based on embedding."""
pass

@abstractmethod
def shut_down(self):
"""Shut down the repository."""
pass
8 changes: 3 additions & 5 deletions index/db/repository.py → index/repository/sqllite.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import json

from typing import Union, List

import numpy as np
from sqlalchemy import create_engine, func
from sqlalchemy.orm import sessionmaker, aliased
from index.db.model import Base, Terminology, Concept, Mapping
from index.repository.base import BaseRepository


class SQLLiteRepository:
class SQLLiteRepository(BaseRepository):

def __init__(self, mode="disk"):
if mode == "disk":
Expand Down Expand Up @@ -70,4 +68,4 @@ def get_closest_mappings(self, embedding, limit=5):
return result_mappings, distances

def shut_down(self):
self.session.close()
self.session.close()

0 comments on commit fbeabdd

Please sign in to comment.