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() diff --git a/service/src/main/inse/integrations/llm.py b/service/src/main/inse/integrations/llm.py new file mode 100644 index 00000000..8ec7a76d --- /dev/null +++ b/service/src/main/inse/integrations/llm.py @@ -0,0 +1,32 @@ +from typing import Any +import httpx +from .. import config + + +def call( + system_prompt: str, + user_prompt: str, + model: str = None, + stream: bool = False, + timeout: int = 30 +) -> 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=timeout) 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", "") diff --git a/service/src/main/inse/logic.py b/service/src/main/inse/logic.py index b7aaad0f..03706004 100644 --- a/service/src/main/inse/logic.py +++ b/service/src/main/inse/logic.py @@ -20,6 +20,8 @@ import json 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" @@ -200,11 +202,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 +211,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 = [] @@ -294,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)