-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmain.py
More file actions
156 lines (125 loc) · 3.91 KB
/
Copy pathmain.py
File metadata and controls
156 lines (125 loc) · 3.91 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
"""
CLI runner for the DocuBot tinker activity.
Supports three modes:
1. Naive LLM generation over all docs (Phase 0)
2. Retrieval only (Phase 1)
3. RAG: retrieval plus LLM generation (Phase 2)
"""
from dotenv import load_dotenv
load_dotenv()
from docubot import DocuBot
from llm_client import GeminiClient
from dataset import SAMPLE_QUERIES
def try_create_llm_client():
"""
Tries to create a GeminiClient.
Returns (llm_client, has_llm: bool).
"""
try:
client = GeminiClient()
return client, True
except RuntimeError as exc:
print("Warning: LLM features are disabled.")
print(f"Reason: {exc}")
print("You can still run retrieval only mode.\n")
return None, False
def choose_mode(has_llm):
"""
Asks the user which mode to run.
Returns "1", "2", "3", or "q".
"""
print("Choose a mode:")
if has_llm:
print(" 1) Naive LLM over full docs (no retrieval)")
else:
print(" 1) Naive LLM over full docs (unavailable, no GEMINI_API_KEY)")
print(" 2) Retrieval only (no LLM)")
if has_llm:
print(" 3) RAG (retrieval + LLM)")
else:
print(" 3) RAG (unavailable, no GEMINI_API_KEY)")
print(" q) Quit")
choice = input("Enter choice: ").strip().lower()
return choice
def get_query_or_use_samples():
"""
Ask the user if they want to run all sample queries or a single custom query.
Returns:
queries: list of strings
label: short description of the source of queries
"""
print("\nPress Enter to run built in sample queries.")
custom = input("Or type a single custom query: ").strip()
if custom:
return [custom], "custom query"
else:
return SAMPLE_QUERIES, "sample queries"
def run_naive_llm_mode(bot, has_llm):
"""
Mode 1:
Naive LLM generation over the full docs corpus.
"""
if not has_llm or bot.llm_client is None:
print("\nNaive LLM mode is not available (no GEMINI_API_KEY).\n")
return
queries, label = get_query_or_use_samples()
print(f"\nRunning naive LLM mode on {label}...\n")
all_text = bot.full_corpus_text()
for query in queries:
print("=" * 60)
print(f"Question: {query}\n")
answer = bot.llm_client.naive_answer_over_full_docs(query, all_text)
print("Answer:")
print(answer)
print()
def run_retrieval_only_mode(bot):
"""
Mode 2:
Retrieval only answers. No LLM involved.
"""
queries, label = get_query_or_use_samples()
print(f"\nRunning retrieval only mode on {label}...\n")
for query in queries:
print("=" * 60)
print(f"Question: {query}\n")
answer = bot.answer_retrieval_only(query)
print("Retrieved snippets:")
print(answer)
print()
def run_rag_mode(bot, has_llm):
"""
Mode 3:
Retrieval plus LLM generation.
"""
if not has_llm or bot.llm_client is None:
print("\nRAG mode is not available (no GEMINI_API_KEY).\n")
return
queries, label = get_query_or_use_samples()
print(f"\nRunning RAG mode on {label}...\n")
for query in queries:
print("=" * 60)
print(f"Question: {query}\n")
answer = bot.answer_rag(query)
print("Answer:")
print(answer)
print()
def main():
print("DocuBot Tinker Activity")
print("=======================\n")
llm_client, has_llm = try_create_llm_client()
bot = DocuBot(llm_client=llm_client)
while True:
choice = choose_mode(has_llm)
if choice == "q":
print("\nGoodbye.")
break
elif choice == "1":
run_naive_llm_mode(bot, has_llm)
elif choice == "2":
run_retrieval_only_mode(bot)
elif choice == "3":
run_rag_mode(bot, has_llm)
else:
print("\nUnknown choice. Please pick 1, 2, 3, or q.\n")
if __name__ == "__main__":
main()