diff --git a/service/src/main/.env b/service/src/main/.env new file mode 100644 index 00000000..e812af95 --- /dev/null +++ b/service/src/main/.env @@ -0,0 +1,3 @@ +INSE_OPENALEX_WORKS_API_KEY=am1wWXBaMEJEVWJoZkE5MnBaUlk6TWlvbW1LRlMzNjdDUHgzYmVkUmI5UQ== +INSE_LLM_API_TOKEN=c2d5e7ccf7a4760dd3d19bed2caed9d85ff7437400e0ea82a6d6f912ebc2f212 +INSE_AUTH_SECRET=change-me-to-a-secure-secret-key diff --git a/service/src/main/inse/config.py b/service/src/main/inse/config.py index ad171904..4443c7c8 100644 --- a/service/src/main/inse/config.py +++ b/service/src/main/inse/config.py @@ -18,6 +18,7 @@ load_dotenv() class Settings(BaseSettings): + model_config = SettingsConfigDict(env_prefix='inse_', env_file='.env', env_file_encoding='utf-8', extra='ignore') # database config database_url: str = "sqlite:///../data/inse-sqlite.db" database_debug: bool = False @@ -25,33 +26,16 @@ class Settings(BaseSettings): # authorization config auth_disabled: bool = False auth_standalone: bool = True - auth_secret: str = "change-me-to-a-secure-secret-key" + auth_secret: str = '' # OpenAlex works search config - openalex_works_search_base_url: str = "https://project.nes.uni-due.de/elastic" - # IMPORTANT: wildcard * needed to query all work indices - openalex_works_search_index: str = "openalex-works*" - openalex_works_api_key: str = ( - "am1wWXBaMEJEVWJoZkE5MnBaUlk6TWlvbW1LRlMzNjdDUHgzYmVkUmI5UQ==" - ) + openalex_works_search_base_url: str = 'https://project.nes.uni-due.de/elastic' + openalex_works_search_index: str = 'openalex-works*' + openalex_works_api_key: str = '' openalex_works_search_timeout_seconds: float = 30.0 - # LLM config - llm_api_url: Optional[str] = None - llm_api_key: Optional[str] = None - llm_model: Optional[str] = None - - # elasticsearch config - elasticsearch_url: Optional[str] = None - elasticsearch_api_key: Optional[str] = None - - model_config = SettingsConfigDict(env_file=".env") - - allowed_origins: List[str] = ["http://localhost:4200"] - - servers: List[Dict[str, str]] = [ - {"url": "http://localhost:8000", "description": "Local development server"}, - ] - + llm_api_url: str = 'https://project.nes.uni-due.de/llm2/api/generate' + llm_api_token: str = '' + llm_model: str = 'gemma4:e4b' settings = Settings() diff --git a/service/src/main/inse/integrations/elasticsearch.py b/service/src/main/inse/integrations/elasticsearch.py index e3ad986f..e8346b07 100644 --- a/service/src/main/inse/integrations/elasticsearch.py +++ b/service/src/main/inse/integrations/elasticsearch.py @@ -5,15 +5,15 @@ from .. import config def search( index: str, - query_field: str, - query_value: str + query_field: str = None, + query_value: str = None, + body: dict = None, ) -> dict[str, Any]: headers = { - "Authorization": f"ApiKey {config.settings.elasticsearch_api_key}", + "Authorization": f"ApiKey {config.settings.openalex_works_api_key}", "Content-Type": "application/json" } - payload = { "query": { "query_string": { @@ -22,12 +22,38 @@ def search( } } } + if body != None: + payload = body with httpx.Client() as client: response = client.post( - f"{config.settings.elasticsearch_url}/{index}/_search", + f"{config.settings.openalex_works_search_base_url}/{index}/_search", json=payload, headers=headers ) response.raise_for_status() return response.json() +def get( + index: str, + doc_id:str, + body: dict = None, + +) -> dict[str, Any]: + + headers = { + "Authorization": f"ApiKey {config.settings.openalex_works_api_key}", + "Content-Type": "application/json" + } + payload = body + + # get document directly + with httpx.Client() as client: + response = client.get( + f"{config.settings.openalex_works_search_base_url}/{index}/_doc/{doc_id}", + json=payload, + headers=headers + ) + response.raise_for_status() + return response.json() + + diff --git a/service/src/main/inse/logic.py b/service/src/main/inse/logic.py index 88c95c16..9b39c0c5 100644 --- a/service/src/main/inse/logic.py +++ b/service/src/main/inse/logic.py @@ -136,8 +136,8 @@ def build_topic_cache(page_size: int = 200, save_json: bool = True) -> dict: """ output_filename: str = "topic_hierarchy_cache.json" headers = { - "Authorization": f"ApiKey {config.settings.elasticsearch_api_key}", - "Content-Type": "application/json", + "Authorization": f"ApiKey {config.settings.openalex_works_api_key}", + "Content-Type": "application/json" } taxonomy_cache = {} @@ -145,12 +145,12 @@ def build_topic_cache(page_size: int = 200, save_json: bool = True) -> dict: with httpx.Client(timeout=30.0) as client: while True: - payload = {"query": {"match_all": {}}, "from": offset, "size": page_size} - response = client.post( - config.settings.elasticsearch_url + "/openalex-topics/_search", - json=payload, - headers=headers, - ) + payload = { + "query": {"match_all": {}}, + "from": offset, + "size": page_size + } + response = client.post(config.settings.openalex_works_search_base_url + 'openalex-topics/_search', json=payload, headers=headers) response.raise_for_status() data = response.json() @@ -347,16 +347,16 @@ def generate_person_bio(author_id: str, include: Optional[set[str]] = None) -> s return bio -def get_person_by_id( - author_id: str, include: Optional[str] = None -) -> AuthorsListResponse: +def get_person_by_id(author_id: str, include: Optional[str] = None) -> AuthorsListResponse: + body = { + "_source":include.split(",") if include else True, + } try: - hit = es_client.get( + hit = elasticsearch.get( index="openalex-authors", - id=author_id, - source=include.split(",") if include else True, + body=body, + doc_id=author_id ) - except NotFoundError: raise HTTPException( status_code=404, detail=f"Person with id {author_id} not found" @@ -384,13 +384,25 @@ def search_persons_by_name( "display_name": {"query": author_name, "boost": 3} } }, - {"match": {"display_name": {"query": author_name, "fuzziness": 1}}}, + # { + # "match": { + # "display_name": { + # "query": author_name, + # "fuzziness": 1 + # } + # } + # } ], "minimum_should_match": 1, } } + body = { + "size":limit, + "query":search_query, + "_source":include.split(",") if include else True, + } try: - response = es_client.search( + response = elasticsearch.search( index="openalex-authors", query=search_query, source=include.split(",") if include else True, @@ -426,10 +438,17 @@ def search_persons_by_institution( }, } } - print(include, institution_name) + body = { + "size":limit, + "query":search_query, + "_source":include.split(",") if include else True, + "sort":[{"works_count": {"order": "desc"}}] + } + print(body) try: - response = es_client.search( + response = elasticsearch.search( index="openalex-authors", + body=body source=include.split(",") if include else True, query=search_query, size=limit,