This repository was archived by the owner on May 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
107 lines (83 loc) · 2.86 KB
/
Copy pathmain.py
File metadata and controls
107 lines (83 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
from pathlib import Path
import pypandoc
from dependency_injector.wiring import Provide, inject
from dotenv import load_dotenv
from langchain.globals import set_debug, set_verbose
from langchain.vectorstores import VectorStore
from src.app.api import create_app, run_app
from src.app.discord import BOT
from src.core import containers
from src.domain.content import Content
from src.port.assistant import AssistantPort
from src.port.content import ContentPort
@inject
def run_terminal(
chat: AssistantPort = Provide[containers.Settings.assistant.chat],
):
while True:
question = input("-> **Q**: ")
if question.lower() in ["q", "quit", "exit"]:
break
answer = chat.prompt(question, session_id="cli")
print(f"**-> Q: {question}\n")
print(f"**AI**: {answer}\n")
@inject
def run_discord(
token: str = Provide[containers.Settings.app.discord_token],
):
BOT.run(token)
@inject
def add_documents(
documents: list[Content],
*,
storage: VectorStore = Provide[containers.Settings.storage.vector_storage],
) -> None:
fails_count = 0
for doc in documents:
try:
storage.add_documents([doc])
except (Exception,) as e:
fails_count += 1
print(f"Fail to add document: {e}")
if fails_count:
print(f"{fails_count} documents failed to add")
@inject
def fetch_documents(
code: ContentPort = Provide[containers.Settings.content.git_code],
wiki: ContentPort = Provide[containers.Settings.content.git_wiki],
assets_path: Path = Provide[containers.Settings.core.assets_path],
):
project = "pdf.js"
code_url = f"ssh://git@github.com/mozilla/{project}.git"
code_path = assets_path.joinpath(project)
wiki_url = f"ssh://git@github.com/mozilla/{project}.wiki.git"
wiki_path = assets_path.joinpath(project + ".wiki")
if code_path.exists():
code_docs = code.get_by_path(project, code_path, branch="master")
else:
code_docs = code.get_by_url(project, code_url, branch="master")
if wiki_path.exists():
wiki_docs = wiki.get_by_path(project, wiki_path)
else:
wiki_docs = wiki.get_by_url(project, wiki_url)
add_documents(wiki_docs) # type: ignore
add_documents(code_docs) # type: ignore
@inject
def run_api(
settings: containers.Settings = Provide[containers.Settings],
port: int = Provide[containers.Settings.api.port],
) -> None:
app = create_app(settings)
run_app(app, port)
if __name__ == "__main__":
load_dotenv()
pypandoc.ensure_pandoc_installed()
application = containers.Settings()
application.config.from_yaml("config.yml", envs_required=True, required=True)
application.core.init_resources()
application.wire(modules=[__name__, "src.app.discord"])
set_debug(True)
set_verbose(True)
# fetch_documents()
# run_discord()
run_api()