Refactor code structure for improved readability and maintainability
This commit is contained in:
@@ -2,6 +2,19 @@
|
||||
|
||||
Wird von ingest_episode_event_step und query_graph_step genutzt.
|
||||
Lazy-initialisiert beim ersten Aufruf.
|
||||
|
||||
Umgebungsvariablen:
|
||||
GRAPHITI_LLM_PROVIDER – 'gemini' (default) oder 'xai'
|
||||
GOOGLE_API_KEY – Google AI Studio Key (für gemini)
|
||||
GRAPHITI_LLM_MODEL – LLM-Hauptmodell (default: gemini-3-flash-preview)
|
||||
GRAPHITI_LLM_SMALL_MODEL – LLM-Hilfsmodell (default: gemini-2.5-flash-lite)
|
||||
XAI_API_KEY – xAI-Key (für xai-Provider)
|
||||
XAI_BASE_URL – optional, Standard: https://api.x.ai/v1
|
||||
XAI_MODEL – optional, Standard: grok-4-1-fast-reasoning
|
||||
XAI_SMALL_MODEL – optional, Standard: grok-3-mini-fast
|
||||
OPENAI_API_KEY – OpenAI-Key für Embeddings
|
||||
GRAPHITI_EMBED_BASE_URL – optional, Standard: https://api.openai.com/v1
|
||||
GRAPHITI_EMBED_MODEL – optional, Standard: text-embedding-3-small
|
||||
"""
|
||||
import asyncio
|
||||
import os
|
||||
@@ -9,7 +22,6 @@ from typing import Any, Optional
|
||||
|
||||
from graphiti_core import Graphiti
|
||||
from graphiti_core.llm_client import LLMConfig
|
||||
from graphiti_core.llm_client.openai_generic_client import OpenAIGenericClient
|
||||
from graphiti_core.embedder import OpenAIEmbedder, OpenAIEmbedderConfig
|
||||
|
||||
from services.graphiti_tracer import build_langfuse_tracer
|
||||
@@ -24,22 +36,7 @@ _graphiti_init_lock = asyncio.Lock()
|
||||
|
||||
|
||||
async def get_graphiti(ctx: Optional[Any] = None) -> Graphiti:
|
||||
"""Gibt den gecachten Graphiti-Client zurück (Singleton).
|
||||
|
||||
Args:
|
||||
ctx: Optionaler Motia-Context für Logging während der Initialisierung.
|
||||
|
||||
Benötigte Umgebungsvariablen:
|
||||
NEO4J_URI – bolt://host:7687
|
||||
NEO4J_USER – neo4j
|
||||
NEO4J_PASSWORD – Pflicht
|
||||
XAI_API_KEY – xAI-Key für LLM (grok)
|
||||
XAI_BASE_URL – optional, Standard: https://api.x.ai/v1
|
||||
XAI_MODEL – optional, Standard: grok-4-1-fast-reasoning
|
||||
OPENAI_API_KEY – OpenAI-Key für Embeddings
|
||||
GRAPHITI_EMBED_BASE_URL – optional, Standard: https://api.openai.com/v1
|
||||
GRAPHITI_EMBED_MODEL – optional, Standard: text-embedding-3-small
|
||||
"""
|
||||
"""Gibt den gecachten Graphiti-Client zurück (Singleton)."""
|
||||
global _graphiti_client
|
||||
if _graphiti_client is None:
|
||||
async with _graphiti_init_lock:
|
||||
@@ -61,26 +58,40 @@ def _log(ctx: Optional[Any], message: str, level: str = "info") -> None:
|
||||
print(f"[GraphitiClient] {message}")
|
||||
|
||||
|
||||
def _build_llm_client():
|
||||
provider = os.environ.get("GRAPHITI_LLM_PROVIDER", "gemini").lower()
|
||||
|
||||
if provider == "gemini":
|
||||
from graphiti_core.llm_client.gemini_client import GeminiClient
|
||||
return GeminiClient(
|
||||
config=LLMConfig(
|
||||
api_key=os.environ["GOOGLE_API_KEY"],
|
||||
model=os.environ.get("GRAPHITI_LLM_MODEL", "gemini-3-flash-preview"),
|
||||
small_model=os.environ.get("GRAPHITI_LLM_SMALL_MODEL", "gemini-2.5-flash-lite"),
|
||||
)
|
||||
)
|
||||
else: # xai
|
||||
from graphiti_core.llm_client.openai_generic_client import OpenAIGenericClient
|
||||
return OpenAIGenericClient(
|
||||
config=LLMConfig(
|
||||
api_key=os.environ["XAI_API_KEY"],
|
||||
base_url=os.environ.get("XAI_BASE_URL", "https://api.x.ai/v1"),
|
||||
model=os.environ.get("XAI_MODEL", "grok-4-1-fast-reasoning"),
|
||||
small_model=os.environ.get("XAI_SMALL_MODEL", "grok-3-mini-fast"),
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
async def _build_graphiti() -> Graphiti:
|
||||
neo4j_uri = os.environ["NEO4J_URI"]
|
||||
neo4j_user = os.environ.get("NEO4J_USER", "neo4j")
|
||||
neo4j_password = os.environ["NEO4J_PASSWORD"]
|
||||
|
||||
xai_base_url = os.environ.get("XAI_BASE_URL", "https://api.x.ai/v1")
|
||||
xai_api_key = os.environ["XAI_API_KEY"]
|
||||
xai_model = os.environ.get("XAI_MODEL", "grok-4-1-fast-reasoning")
|
||||
|
||||
embed_api_key = os.environ.get("OPENAI_API_KEY") or os.environ.get("GRAPHITI_EMBED_API_KEY", xai_api_key)
|
||||
embed_api_key = os.environ.get("OPENAI_API_KEY") or os.environ["GRAPHITI_EMBED_API_KEY"]
|
||||
embed_base_url = os.environ.get("GRAPHITI_EMBED_BASE_URL", "https://api.openai.com/v1")
|
||||
embed_model = os.environ.get("GRAPHITI_EMBED_MODEL", "text-embedding-3-small")
|
||||
|
||||
llm_client = OpenAIGenericClient(
|
||||
config=LLMConfig(
|
||||
api_key=xai_api_key,
|
||||
base_url=xai_base_url,
|
||||
model=xai_model,
|
||||
)
|
||||
)
|
||||
llm_client = _build_llm_client()
|
||||
|
||||
embedder = OpenAIEmbedder(
|
||||
config=OpenAIEmbedderConfig(
|
||||
|
||||
Reference in New Issue
Block a user