Пробуем новую функцию индексации
This commit is contained in:
@@ -146,36 +146,76 @@ class LocalKnowledgeBase:
|
|||||||
print(f"\n📁 Загружено файлов: {files_processed}")
|
print(f"\n📁 Загружено файлов: {files_processed}")
|
||||||
print(f"📄 Всего документов до сплиттинга: {len(all_documents)}")
|
print(f"📄 Всего документов до сплиттинга: {len(all_documents)}")
|
||||||
|
|
||||||
# Для C++ файлов используем кастомный сплиттер
|
# Для C++ файлов используем РАЗНЫЕ стратегии для .cpp и .h/.hpp
|
||||||
cpp_files_docs = [d for d in all_documents if d.metadata.get("file_ext") in [".cpp", ".hpp", ".h", ".cc"]]
|
cpp_source_docs = [d for d in all_documents if d.metadata.get("file_ext") in [".cpp", ".cc"]]
|
||||||
|
cpp_header_docs = [d for d in all_documents if d.metadata.get("file_ext") in [".hpp", ".h"]]
|
||||||
other_docs = [d for d in all_documents if d.metadata.get("file_ext") not in [".cpp", ".hpp", ".h", ".cc"]]
|
other_docs = [d for d in all_documents if d.metadata.get("file_ext") not in [".cpp", ".hpp", ".h", ".cc"]]
|
||||||
|
|
||||||
chunks = []
|
chunks = []
|
||||||
|
|
||||||
# Сплиттим C++ с кастомными разделителями
|
# === .cpp файлы: приоритетные, сохраняем контекст ===
|
||||||
if cpp_files_docs:
|
if cpp_source_docs:
|
||||||
print("\n🔧 Сплиттинг C++ файлов...")
|
print("\n🔧 Сплиттинг .cpp файлов (приоритетные)...")
|
||||||
cpp_splitter = RecursiveCharacterTextSplitter(
|
cpp_source_separators = [
|
||||||
separators=cpp_separators,
|
# Только логические блоки - функции, классы, namespaces
|
||||||
chunk_size=800, # Увеличил для C++
|
"\nclass ",
|
||||||
chunk_overlap=150, # Больше overlap для контекста
|
"\nstruct ",
|
||||||
|
"\nnamespace ",
|
||||||
|
"\nvoid ", "\nint ", "\nfloat ", "\ndouble ", "\nbool ", "\nauto ",
|
||||||
|
"\ntemplate<",
|
||||||
|
"\nconst ", "\nstatic ", "\nexplicit ",
|
||||||
|
]
|
||||||
|
cpp_source_splitter = RecursiveCharacterTextSplitter(
|
||||||
|
separators=cpp_source_separators,
|
||||||
|
chunk_size=1500, # Увеличили для сохранения контекста
|
||||||
|
chunk_overlap=200, # Больше overlap
|
||||||
length_function=len,
|
length_function=len,
|
||||||
keep_separator=True, # Сохраняем разделители
|
keep_separator=True,
|
||||||
)
|
)
|
||||||
cpp_chunks = cpp_splitter.split_documents(cpp_files_docs)
|
source_chunks = cpp_source_splitter.split_documents(cpp_source_docs)
|
||||||
chunks.extend(cpp_chunks)
|
# Добавляем приоритет в метаданные
|
||||||
print(f" C++ чанков: {len(cpp_chunks)}")
|
for chunk in source_chunks:
|
||||||
|
chunk.metadata["priority"] = 1 # Высший приоритет
|
||||||
|
chunks.extend(source_chunks)
|
||||||
|
print(f" .cpp чанков: {len(source_chunks)}")
|
||||||
|
|
||||||
|
# === .h/.hpp файлы: менее агрессивный сплиттинг ===
|
||||||
|
if cpp_header_docs:
|
||||||
|
print("\n🔧 Сплиттинг header файлов...")
|
||||||
|
cpp_header_separators = [
|
||||||
|
# Только большие логические блоки
|
||||||
|
"\nclass ",
|
||||||
|
"\nstruct ",
|
||||||
|
"\nnamespace ",
|
||||||
|
"\ntemplate<",
|
||||||
|
"\npublic:", "\nprivate:", "\nprotected:",
|
||||||
|
"\nvirtual ",
|
||||||
|
]
|
||||||
|
cpp_header_splitter = RecursiveCharacterTextSplitter(
|
||||||
|
separators=cpp_header_separators,
|
||||||
|
chunk_size=1200,
|
||||||
|
chunk_overlap=150,
|
||||||
|
length_function=len,
|
||||||
|
keep_separator=True,
|
||||||
|
)
|
||||||
|
header_chunks = cpp_header_splitter.split_documents(cpp_header_docs)
|
||||||
|
# Добавляем пониженный приоритет
|
||||||
|
for chunk in header_chunks:
|
||||||
|
chunk.metadata["priority"] = 2 # Низший приоритет
|
||||||
|
chunks.extend(header_chunks)
|
||||||
|
print(f" Header чанков: {len(header_chunks)}")
|
||||||
|
|
||||||
# Для остальных файлов - стандартный сплиттер
|
# Для остальных файлов - стандартный сплиттер
|
||||||
if other_docs:
|
if other_docs:
|
||||||
print("\n📄 Сплиттинг остальных файлов...")
|
print("\n📄 Сплиттинг остальных файлов...")
|
||||||
text_splitter = RecursiveCharacterTextSplitter.from_language(
|
text_splitter = RecursiveCharacterTextSplitter(
|
||||||
language=Language.CPP,
|
chunk_size=1000,
|
||||||
chunk_size=500,
|
|
||||||
chunk_overlap=100,
|
chunk_overlap=100,
|
||||||
length_function=len
|
length_function=len
|
||||||
)
|
)
|
||||||
other_chunks = text_splitter.split_documents(other_docs)
|
other_chunks = text_splitter.split_documents(other_docs)
|
||||||
|
for chunk in other_chunks:
|
||||||
|
chunk.metadata["priority"] = 3
|
||||||
chunks.extend(other_chunks)
|
chunks.extend(other_chunks)
|
||||||
print(f" Остальных чанков: {len(other_chunks)}")
|
print(f" Остальных чанков: {len(other_chunks)}")
|
||||||
|
|
||||||
@@ -213,7 +253,8 @@ class LocalKnowledgeBase:
|
|||||||
"text": doc.page_content,
|
"text": doc.page_content,
|
||||||
"source": doc.metadata.get("source", "unknown"),
|
"source": doc.metadata.get("source", "unknown"),
|
||||||
"page": doc.metadata.get("page", 0),
|
"page": doc.metadata.get("page", 0),
|
||||||
"file_type": "cpp",
|
"file_type": doc.metadata.get("file_ext", ""),
|
||||||
|
"priority": doc.metadata.get("priority", 3), # Добавить
|
||||||
"function_name": function_name, # ✅ Добавил
|
"function_name": function_name, # ✅ Добавил
|
||||||
"class_name": func_match.group(1) if func_match else None, # ✅ Добавил
|
"class_name": func_match.group(1) if func_match else None, # ✅ Добавил
|
||||||
"line_start": doc.metadata.get("line", 0), # Если есть в метаданных
|
"line_start": doc.metadata.get("line", 0), # Если есть в метаданных
|
||||||
@@ -254,13 +295,20 @@ class LocalKnowledgeBase:
|
|||||||
# Форматируем результаты
|
# Форматируем результаты
|
||||||
results = []
|
results = []
|
||||||
for hit in search_result.points:
|
for hit in search_result.points:
|
||||||
results.append(
|
# Добавляем "штраф" для header-файлов
|
||||||
{
|
score = hit.score
|
||||||
"text": hit.payload["text"],
|
if hit.payload.get("file_ext") in [".h", ".hpp"]:
|
||||||
"score": hit.score,
|
score *= 0.7 # Снижаем приоритет на 30%
|
||||||
"source": hit.payload.get("source", "unknown"),
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
return results
|
results.append({
|
||||||
|
"text": hit.payload["text"],
|
||||||
|
"score": score,
|
||||||
|
"source": hit.payload.get("source", "unknown"),
|
||||||
|
"file_ext": hit.payload.get("file_ext", ""),
|
||||||
|
})
|
||||||
|
|
||||||
|
# Пересортируем по новому score
|
||||||
|
results.sort(key=lambda x: x["score"], reverse=True)
|
||||||
|
|
||||||
|
return results[:top_k]
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user