Merge pull request 'feature/91-llm-elasticsearch-service' (#94) from feature/91-llm-elasticsearch-service into dev
LocationBasedServices/smartguide/pipeline/head This commit looks good

Reviewed-on: #94
This commit was merged in pull request #94.
This commit is contained in:
2026-05-28 16:59:34 +02:00
4 changed files with 73 additions and 33 deletions
@@ -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()
+32
View File
@@ -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", "")
+8 -33
View File
@@ -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)