Repository navigation
Expand file tree
/
Copy pathexample.py
More file actions
84 lines (66 loc) · 2.41 KB
/
Copy pathexample.py
File metadata and controls
84 lines (66 loc) · 2.41 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
import json
import logging
import dspy
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "src"))
from docpilot.dspyclasses import MultiHopRAG
from docpilot.utils.llama_utils import get_vector_store_index, load_docs
from docpilot.utils.logger import setup_logging
from config import Config as config
logger = logging.getLogger(__name__)
def to_html_file(data: str):
with open("example.html", "w", encoding="utf-8") as f:
data = """ <html>
<head>
<style>
img { display: block; width: 200px; }
</style>
</head>
<body>
""" + data + """
</body>
</html>
"""
f.write(data)
def m_main():
message_handler = {
"query": lambda msg: logger.info("Query: %s", msg),
"files": lambda msg: logger.info("Files: %s", msg),
"answer_with_images": to_html_file,
"answer": print,
}
docs, image_docs, image_mappings = load_docs('data', config.PG_CONNECTION_URI, config.embed_table)
with open("./labels/image_mappings_debug.json", "w") as f:
json.dump(image_mappings, f, indent=2)
logger.info("Dumped %d image mappings to labels/image_mappings_debug.json", len(image_mappings))
index = get_vector_store_index(docs, config.embed_model, embeddings_table=config.embed_table, uri=config.PG_CONNECTION_URI)
image_index = get_vector_store_index(image_docs, config.embed_model, embeddings_table="data_images", uri=config.PG_CONNECTION_URI)
multi_hop = MultiHopRAG(index=index, image_index=image_index, num_passages=3)
chatbot = dspy.LM(
model="ollama/"+config.ollama_model,
system_prompt="Strictly follow the given instructions and adhere to the given format",
base_url=config.ollama_url,
cache=False,
)
dspy.settings.configure(lm=chatbot)
_in = False
prompt = ''
out = None
while True:
if not _in:
prompt = input(">> ").strip()
prompt = prompt if prompt else "Hi"
out = multi_hop.forward(prompt)
_in = True
try:
msg = next(out)
message_handler[msg['type']](msg['content'])
except StopIteration:
choice = input("History (Y/[N])? ").strip().lower()
if choice == 'y':
print(chatbot.inspect_history(n=3))
_in = False
if __name__ == "__main__":
setup_logging()
m_main()