Create elan_assistant.py
Browse files- services/elan_assistant.py +87 -0
services/elan_assistant.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Main ELAN assistant service that coordinates all components.
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
from .vector_search import VectorSearchService
|
| 6 |
+
from .llm_service import LLMService
|
| 7 |
+
from utils.text_processing import TextProcessor
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class ElanAssistant:
|
| 11 |
+
"""Main assistant service that coordinates all components."""
|
| 12 |
+
|
| 13 |
+
def __init__(self):
|
| 14 |
+
"""Initialize the ELAN assistant with all required services."""
|
| 15 |
+
self.vector_search = VectorSearchService()
|
| 16 |
+
self.llm_service = LLMService()
|
| 17 |
+
self.text_processor = TextProcessor()
|
| 18 |
+
|
| 19 |
+
def process_message(self, message: str) -> str:
|
| 20 |
+
"""
|
| 21 |
+
Process a user message and return appropriate response.
|
| 22 |
+
|
| 23 |
+
Args:
|
| 24 |
+
message: The user's input message
|
| 25 |
+
|
| 26 |
+
Returns:
|
| 27 |
+
str: Generated response
|
| 28 |
+
"""
|
| 29 |
+
# Check if message contains XML/EAF content
|
| 30 |
+
if self.text_processor.is_xml_content(message):
|
| 31 |
+
return self._process_xml_modification(message)
|
| 32 |
+
else:
|
| 33 |
+
return self._process_question(message)
|
| 34 |
+
|
| 35 |
+
def _process_question(self, question: str) -> str:
|
| 36 |
+
"""
|
| 37 |
+
Process a regular question using vector search and LLM.
|
| 38 |
+
|
| 39 |
+
Args:
|
| 40 |
+
question: The user's question
|
| 41 |
+
|
| 42 |
+
Returns:
|
| 43 |
+
str: Generated answer
|
| 44 |
+
"""
|
| 45 |
+
# Get relevant context from vector search
|
| 46 |
+
context = self.vector_search.get_context(question)
|
| 47 |
+
|
| 48 |
+
# Generate answer using LLM
|
| 49 |
+
response = self.llm_service.generate_answer(question, context)
|
| 50 |
+
|
| 51 |
+
return response
|
| 52 |
+
|
| 53 |
+
def _process_xml_modification(self, eaf_content: str) -> str:
|
| 54 |
+
"""
|
| 55 |
+
Process XML/EAF file modification request.
|
| 56 |
+
|
| 57 |
+
Args:
|
| 58 |
+
eaf_content: The EAF file content with instructions
|
| 59 |
+
|
| 60 |
+
Returns:
|
| 61 |
+
str: Modified EAF content
|
| 62 |
+
"""
|
| 63 |
+
try:
|
| 64 |
+
# Split content into instructions and chunks
|
| 65 |
+
instructions, chunks = self.text_processor.split_eaf_content(eaf_content)
|
| 66 |
+
|
| 67 |
+
# Process each chunk
|
| 68 |
+
processed_chunks = []
|
| 69 |
+
total_chunks = len(chunks)
|
| 70 |
+
|
| 71 |
+
for i, chunk in enumerate(chunks, 1):
|
| 72 |
+
processed_chunk = self.llm_service.process_xml_chunk(
|
| 73 |
+
chunk=chunk,
|
| 74 |
+
instructions=instructions,
|
| 75 |
+
current_chunk=i,
|
| 76 |
+
total_chunks=total_chunks
|
| 77 |
+
)
|
| 78 |
+
processed_chunks.append(processed_chunk)
|
| 79 |
+
|
| 80 |
+
# Combine processed chunks
|
| 81 |
+
combined_result = self.text_processor.combine_chunks(processed_chunks)
|
| 82 |
+
|
| 83 |
+
return combined_result
|
| 84 |
+
|
| 85 |
+
except Exception as e:
|
| 86 |
+
print(f"Error in EAF file modification: {e}")
|
| 87 |
+
return "I'm sorry, an error occurred while modifying the EAF file."
|