From 4a37c8d04181452bc26d2ab945d14c5129bb8b20 Mon Sep 17 00:00:00 2001 From: Steven Tischer Date: Thu, 28 May 2026 16:37:24 +0200 Subject: [PATCH 1/5] feat(backend): add centralized elasticsearch query function --- .../src/main/inse/integrations/__init__.py | 0 .../main/inse/integrations/elasticsearch.py | 33 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 service/src/main/inse/integrations/__init__.py create mode 100644 service/src/main/inse/integrations/elasticsearch.py diff --git a/service/src/main/inse/integrations/__init__.py b/service/src/main/inse/integrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/service/src/main/inse/integrations/elasticsearch.py b/service/src/main/inse/integrations/elasticsearch.py new file mode 100644 index 00000000..e3ad986f --- /dev/null +++ b/service/src/main/inse/integrations/elasticsearch.py @@ -0,0 +1,33 @@ +from typing import Any, Optional +import httpx +from .. import config + + +def search( + index: str, + query_field: str, + query_value: str +) -> dict[str, Any]: + + headers = { + "Authorization": f"ApiKey {config.settings.elasticsearch_api_key}", + "Content-Type": "application/json" + } + + payload = { + "query": { + "query_string": { + "default_field": query_field, + "query": query_value + } + } + } + + with httpx.Client() as client: + response = client.post( + f"{config.settings.elasticsearch_url}/{index}/_search", + json=payload, + headers=headers + ) + response.raise_for_status() + return response.json() -- 2.56.1 From 28c79e113a452fb5fc9d3e44bba701aa87893c63 Mon Sep 17 00:00:00 2001 From: Steven Tischer Date: Thu, 28 May 2026 16:38:35 +0200 Subject: [PATCH 2/5] refactor(backend): integrate elasticsearch service into search_persons function --- service/src/main/inse/logic.py | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/service/src/main/inse/logic.py b/service/src/main/inse/logic.py index b7aaad0f..504f4ed1 100644 --- a/service/src/main/inse/logic.py +++ b/service/src/main/inse/logic.py @@ -20,6 +20,7 @@ import json from . import schema from . import model from . import config +from .integrations import elasticsearch logger = logging.getLogger(__name__) topic_cache_filename: str = "topic_hierarchy_cache.json" @@ -200,11 +201,6 @@ def count_topic_hierarchy(topic_names: List[str]) -> dict: def search_persons(author_id: Optional[str] = None, query: Optional[str] = None, include: Optional[set[str]] = None) -> model.PersonSearchResult: - headers = { - "Authorization": f"ApiKey {config.settings.elasticsearch_api_key}", - "Content-Type": "application/json" - } - if author_id: query_field = "_id" query_value = author_id @@ -214,19 +210,11 @@ def search_persons(author_id: Optional[str] = None, query: Optional[str] = None, else: return "Either author_id or query must be provided" - payload = { - "query": { - "query_string": { - "default_field": query_field, - "query": query_value - } - } - } - - with httpx.Client() as client: - response = client.post(config.settings.elasticsearch_url + '/openalex-authors/_search', json=payload, headers=headers) - response.raise_for_status() - data = response.json() + data = elasticsearch.search( + index="openalex-authors", + query_field=query_field, + query_value=query_value + ) include_set = {tag.strip().lower() for tag in include} if include else set() persons = [] -- 2.56.1 From afd414e004bc59c2c328ff44a48ee86feabd5c25 Mon Sep 17 00:00:00 2001 From: Steven Tischer Date: Thu, 28 May 2026 16:41:26 +0200 Subject: [PATCH 3/5] feat(backend): add centralized llm query function --- service/src/main/inse/integrations/llm.py | 31 +++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 service/src/main/inse/integrations/llm.py diff --git a/service/src/main/inse/integrations/llm.py b/service/src/main/inse/integrations/llm.py new file mode 100644 index 00000000..69c17f70 --- /dev/null +++ b/service/src/main/inse/integrations/llm.py @@ -0,0 +1,31 @@ +from typing import Any +import httpx +from .. import config + + +def call( + system_prompt: str, + user_prompt: str, + model: str = None, + stream: bool = False +) -> str: + + headers = { + "Authorization": f"Bearer {config.settings.llm_api_token}", + "Content-Type": "application/json" + } + + if model is None: + model = config.settings.llm_model + + payload = { + "model": model, + "prompt": f"{system_prompt}\n{user_prompt}", + "stream": stream + } + + with httpx.Client(timeout=30.0) as client: + response = client.post(config.settings.llm_api_url, json=payload, headers=headers) + response.raise_for_status() + data = response.json() + return data.get("response", "") -- 2.56.1 From 3e1a81e68b667c455967fc274c767cfbffe26827 Mon Sep 17 00:00:00 2001 From: Steven Tischer Date: Thu, 28 May 2026 16:43:28 +0200 Subject: [PATCH 4/5] refactor(backend): integrate llm integration into generate_person_bio --- service/src/main/inse/logic.py | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/service/src/main/inse/logic.py b/service/src/main/inse/logic.py index 504f4ed1..03706004 100644 --- a/service/src/main/inse/logic.py +++ b/service/src/main/inse/logic.py @@ -21,6 +21,7 @@ from . import schema from . import model from . import config from .integrations import elasticsearch +from .integrations import llm logger = logging.getLogger(__name__) topic_cache_filename: str = "topic_hierarchy_cache.json" @@ -282,20 +283,6 @@ def generate_person_bio(author_id: str) -> str: system_prompt = "Erstelle eine Beschreibung der Person in 3-5 Sätzen. Gebe nur die Beschreibung zurück, ohne Einleitung oder Schluss." user_prompt = f"Daten zur Person:\n{person_json}" - headers = { - "Authorization": f"Bearer {config.settings.llm_api_token}", - "Content-Type": "application/json" - } - payload = { - "model": config.settings.llm_model, - "prompt": f"{system_prompt}\n{user_prompt}", - "stream": False - } - - with httpx.Client(timeout=30.0) as client: - response = client.post(config.settings.llm_api_url, json=payload, headers=headers) - response.raise_for_status() - data = response.json() - return data.get("response", "") + return llm.call(system_prompt=system_prompt, user_prompt=user_prompt) -- 2.56.1 From e6c66e90a6980805a399349954f6a74916f19768 Mon Sep 17 00:00:00 2001 From: Steven Tischer Date: Thu, 28 May 2026 16:45:09 +0200 Subject: [PATCH 5/5] feat(backend): update call function to use dynamic timeout parameter --- service/src/main/inse/integrations/llm.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/service/src/main/inse/integrations/llm.py b/service/src/main/inse/integrations/llm.py index 69c17f70..8ec7a76d 100644 --- a/service/src/main/inse/integrations/llm.py +++ b/service/src/main/inse/integrations/llm.py @@ -7,7 +7,8 @@ def call( system_prompt: str, user_prompt: str, model: str = None, - stream: bool = False + stream: bool = False, + timeout: int = 30 ) -> str: headers = { @@ -24,7 +25,7 @@ def call( "stream": stream } - with httpx.Client(timeout=30.0) as client: + with httpx.Client(timeout=timeout) as client: response = client.post(config.settings.llm_api_url, json=payload, headers=headers) response.raise_for_status() data = response.json() -- 2.56.1