1414
1515import numpy as np
1616from jinja2 import Template , StrictUndefined
17- from nexent .vector_database .base import VectorDatabaseCore
1817from sklearn .cluster import KMeans
1918from sklearn .metrics import silhouette_score
2019from sklearn .metrics .pairwise import cosine_similarity
2120
2221from consts .const import LANGUAGE
22+ from database .model_management_db import get_model_by_model_id
23+ from nexent .core .utils .observer import MessageObserver
24+ from nexent .core .models import OpenAIModel
25+ from nexent .vector_database .base import VectorDatabaseCore
26+ from utils .llm_utils import call_llm_for_system_prompt
2327from utils .prompt_template_utils import (
2428 get_document_summary_prompt_template ,
2529 get_cluster_summary_reduce_prompt_template ,
@@ -568,37 +572,22 @@ def summarize_document(document_content: str, filename: str, language: str = LAN
568572
569573 # Call LLM if model_id and tenant_id are provided
570574 if model_id and tenant_id :
571- from smolagents import OpenAIServerModel
572- from database .model_management_db import get_model_by_model_id
573- from utils .config_utils import get_model_name_from_config
574- from consts .const import MESSAGE_ROLE
575-
575+
576576 # Get model configuration
577577 llm_model_config = get_model_by_model_id (model_id = model_id , tenant_id = tenant_id )
578578 if not llm_model_config :
579579 logger .warning (f"No model configuration found for model_id: { model_id } , tenant_id: { tenant_id } " )
580580 return f"[Document Summary: { filename } ] (max { max_words } words) - Content: { document_content [:200 ]} ..."
581-
582- # Create LLM instance
583- llm = OpenAIServerModel (
584- model_id = get_model_name_from_config (llm_model_config ) if llm_model_config else "" ,
585- api_base = llm_model_config .get ("base_url" , "" ),
586- api_key = llm_model_config .get ("api_key" , "" ),
587- temperature = 0.3 ,
588- top_p = 0.95
581+
582+ document_summary = call_llm_for_system_prompt (
583+ model_id = model_id ,
584+ user_prompt = user_prompt ,
585+ system_prompt = system_prompt ,
586+ callback = None ,
587+ tenant_id = tenant_id
589588 )
590-
591- # Build messages
592- messages = [
593- {"role" : MESSAGE_ROLE ["SYSTEM" ], "content" : system_prompt },
594- {"role" : MESSAGE_ROLE ["USER" ], "content" : user_prompt }
595- ]
596-
597- # Call LLM, allow more tokens for generation
598- response = llm (messages , max_tokens = max_words * 2 )
599- if not response or not response .content :
600- return ""
601- return response .content .strip ()
589+
590+ return (document_summary or "" ).strip ()
602591 else :
603592 # Fallback to placeholder if no model configuration
604593 logger .warning ("No model_id or tenant_id provided, using placeholder summary" )
@@ -642,10 +631,6 @@ def summarize_cluster(document_summaries: List[str], language: str = LANGUAGE["Z
642631
643632 # Call LLM if model_id and tenant_id are provided
644633 if model_id and tenant_id :
645- from smolagents import OpenAIServerModel
646- from database .model_management_db import get_model_by_model_id
647- from utils .config_utils import get_model_name_from_config
648- from consts .const import MESSAGE_ROLE
649634
650635 # Get model configuration
651636 llm_model_config = get_model_by_model_id (model_id = model_id , tenant_id = tenant_id )
@@ -654,25 +639,15 @@ def summarize_cluster(document_summaries: List[str], language: str = LANGUAGE["Z
654639 return f"[Cluster Summary] (max { max_words } words) - Based on { len (document_summaries )} documents"
655640
656641 # Create LLM instance
657- llm = OpenAIServerModel (
658- model_id = get_model_name_from_config ( llm_model_config ) if llm_model_config else "" ,
659- api_base = llm_model_config . get ( "base_url" , "" ) ,
660- api_key = llm_model_config . get ( "api_key" , "" ) ,
661- temperature = 0.3 ,
662- top_p = 0.95
642+ cluster_summary = call_llm_for_system_prompt (
643+ model_id = model_id ,
644+ user_prompt = user_prompt ,
645+ system_prompt = system_prompt ,
646+ callback = None ,
647+ tenant_id = tenant_id
663648 )
664-
665- # Build messages
666- messages = [
667- {"role" : MESSAGE_ROLE ["SYSTEM" ], "content" : system_prompt },
668- {"role" : MESSAGE_ROLE ["USER" ], "content" : user_prompt }
669- ]
670-
671- # Call LLM
672- response = llm (messages , max_tokens = max_words * 2 ) # Allow more tokens for generation
673- if not response or not response .content :
674- return ""
675- return response .content .strip ()
649+
650+ return (cluster_summary or "" ).strip ()
676651 else :
677652 # Fallback to placeholder if no model configuration
678653 logger .warning ("No model_id or tenant_id provided, using placeholder summary" )
0 commit comments