2023-06-06 01:38:15 +03:00
|
|
|
import os
|
|
|
|
import time
|
|
|
|
|
|
|
|
from langchain.document_loaders import GitLoader
|
|
|
|
from langchain.schema import Document
|
|
|
|
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
2023-06-28 20:39:27 +03:00
|
|
|
from models.brains import Brain
|
|
|
|
from models.files import File
|
2023-06-17 00:36:53 +03:00
|
|
|
from utils.file import compute_sha1_from_content
|
2023-06-19 22:15:35 +03:00
|
|
|
from utils.vectors import Neurons
|
2023-06-06 01:38:15 +03:00
|
|
|
|
|
|
|
|
2023-07-10 15:27:49 +03:00
|
|
|
async def process_github(
|
|
|
|
repo,
|
|
|
|
enable_summarization,
|
|
|
|
brain_id,
|
|
|
|
user_openai_api_key,
|
|
|
|
):
|
2023-06-06 01:38:15 +03:00
|
|
|
random_dir_name = os.urandom(16).hex()
|
|
|
|
dateshort = time.strftime("%Y%m%d")
|
|
|
|
loader = GitLoader(
|
2023-06-29 19:26:03 +03:00
|
|
|
clone_url=repo,
|
|
|
|
repo_path="/tmp/" + random_dir_name,
|
2023-06-06 01:38:15 +03:00
|
|
|
)
|
|
|
|
documents = loader.load()
|
|
|
|
os.system("rm -rf /tmp/" + random_dir_name)
|
|
|
|
|
2023-06-19 18:53:07 +03:00
|
|
|
chunk_size = 500
|
2023-06-06 01:38:15 +03:00
|
|
|
chunk_overlap = 0
|
|
|
|
text_splitter = RecursiveCharacterTextSplitter.from_tiktoken_encoder(
|
2023-07-10 15:27:49 +03:00
|
|
|
chunk_size=chunk_size, chunk_overlap=chunk_overlap
|
|
|
|
)
|
|
|
|
|
2023-06-06 01:38:15 +03:00
|
|
|
documents = text_splitter.split_documents(documents)
|
|
|
|
print(documents[:1])
|
|
|
|
|
|
|
|
for doc in documents:
|
2023-07-10 15:27:49 +03:00
|
|
|
if doc.metadata["file_type"] in [
|
|
|
|
".pyc",
|
|
|
|
".png",
|
|
|
|
".svg",
|
|
|
|
".env",
|
|
|
|
".lock",
|
|
|
|
".gitignore",
|
|
|
|
".gitmodules",
|
|
|
|
".gitattributes",
|
|
|
|
".gitkeep",
|
|
|
|
".git",
|
|
|
|
".json",
|
|
|
|
]:
|
2023-06-06 01:38:15 +03:00
|
|
|
continue
|
|
|
|
metadata = {
|
|
|
|
"file_sha1": compute_sha1_from_content(doc.page_content.encode("utf-8")),
|
2023-07-10 15:27:49 +03:00
|
|
|
"file_size": len(doc.page_content) * 8,
|
2023-06-06 01:38:15 +03:00
|
|
|
"file_name": doc.metadata["file_name"],
|
|
|
|
"chunk_size": chunk_size,
|
|
|
|
"chunk_overlap": chunk_overlap,
|
|
|
|
"date": dateshort,
|
2023-07-10 15:27:49 +03:00
|
|
|
"summarization": "true" if enable_summarization else "false",
|
2023-06-06 01:38:15 +03:00
|
|
|
}
|
2023-07-10 15:27:49 +03:00
|
|
|
doc_with_metadata = Document(page_content=doc.page_content, metadata=metadata)
|
|
|
|
|
|
|
|
file = File(
|
|
|
|
file_sha1=compute_sha1_from_content(doc.page_content.encode("utf-8"))
|
|
|
|
)
|
|
|
|
|
2023-06-29 19:26:03 +03:00
|
|
|
file_exists = file.file_already_exists()
|
|
|
|
|
|
|
|
if not file_exists:
|
|
|
|
print(f"Creating entry for file {file.file_sha1} in vectors...")
|
2023-08-03 21:24:42 +03:00
|
|
|
neurons = Neurons()
|
2023-07-10 15:27:49 +03:00
|
|
|
created_vector = neurons.create_vector(
|
|
|
|
doc_with_metadata, user_openai_api_key
|
|
|
|
)
|
2023-06-29 19:26:03 +03:00
|
|
|
print("Created vector sids ", created_vector)
|
|
|
|
print("Created vector for ", doc.metadata["file_name"])
|
2023-06-28 20:39:27 +03:00
|
|
|
|
2023-06-29 19:26:03 +03:00
|
|
|
file_exists_in_brain = file.file_already_exists_in_brain(brain_id)
|
2023-06-28 20:39:27 +03:00
|
|
|
|
2023-06-29 19:26:03 +03:00
|
|
|
if not file_exists_in_brain:
|
2023-06-28 20:39:27 +03:00
|
|
|
brain = Brain(id=brain_id)
|
2023-06-29 19:26:03 +03:00
|
|
|
file.link_file_to_brain(brain)
|
2023-07-10 15:27:49 +03:00
|
|
|
return {
|
|
|
|
"message": f"✅ Github with {len(documents)} files has been uploaded.",
|
|
|
|
"type": "success",
|
|
|
|
}
|