Saving chat_history in mongodb
Browse files- app.py +77 -68
- main.py +5 -4
- mapping.py +119 -0
- memory.py +62 -0
- requirements.txt +2 -1
- utils.py +41 -133
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
from main import index, run
|
3 |
from gtts import gTTS
|
4 |
-
import os, time
|
5 |
|
6 |
from transformers import pipeline
|
7 |
|
@@ -11,86 +11,95 @@ p = pipeline("automatic-speech-recognition", model="openai/whisper-base")
|
|
11 |
|
12 |
models = ["GPT-3.5", "Flan UL2", "Flan T5"]
|
13 |
|
14 |
-
def add_text(history, text, model):
|
15 |
-
print("Question asked: " + text)
|
16 |
-
response = run_model(text, model)
|
17 |
-
history = history + [(text, response)]
|
18 |
-
print(history)
|
19 |
-
return history, ""
|
20 |
-
|
21 |
-
|
22 |
-
def run_model(text, model):
|
23 |
-
start_time = time.time()
|
24 |
-
print("start time:" + str(start_time))
|
25 |
-
response = run(text, model)
|
26 |
-
end_time = time.time()
|
27 |
-
# If response contains string `SOURCES:`, then add a \n before `SOURCES`
|
28 |
-
if "SOURCES:" in response:
|
29 |
-
response = response.replace("SOURCES:", "\nSOURCES:")
|
30 |
-
# response = response + "\n\n" + "Time taken: " + str(end_time - start_time)
|
31 |
-
print(response)
|
32 |
-
print("Time taken: " + str(end_time - start_time))
|
33 |
-
return response
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
def get_output(history, audio, model):
|
38 |
-
|
39 |
-
txt = p(audio)["text"]
|
40 |
-
# history.append(( (audio, ) , txt))
|
41 |
-
audio_path = 'response.wav'
|
42 |
-
response = run_model(txt, model)
|
43 |
-
# Remove all text from SOURCES: to the end of the string
|
44 |
-
trimmed_response = response.split("SOURCES:")[0]
|
45 |
-
myobj = gTTS(text=trimmed_response, lang='en', slow=False)
|
46 |
-
myobj.save(audio_path)
|
47 |
-
# split audio by / and keep the last element
|
48 |
-
# audio = audio.split("/")[-1]
|
49 |
-
# audio = audio + ".wav"
|
50 |
-
history.append(( (audio, ) , (audio_path, )))
|
51 |
-
print(history)
|
52 |
-
return history
|
53 |
-
|
54 |
-
def set_model(history, model):
|
55 |
-
print("Model selected: " + model)
|
56 |
-
history = get_first_message(history)
|
57 |
-
index(model)
|
58 |
-
return history
|
59 |
-
|
60 |
-
|
61 |
-
def get_first_message(history):
|
62 |
-
history = [(None,
|
63 |
-
'Learn about <a href="https://www.coursera.org/learn/3d-printing-revolution/home">3D printing Revolution</a> course with referred sources.')]
|
64 |
-
return history
|
65 |
-
|
66 |
-
|
67 |
-
def bot(history):
|
68 |
-
return history
|
69 |
-
|
70 |
with gr.Blocks(theme='snehilsanyal/scikit-learn') as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
# Title on top in middle of the page
|
|
|
72 |
|
73 |
-
gr.
|
74 |
-
|
75 |
-
chatbot = gr.Chatbot(get_first_message([]), elem_id="chatbot", label='Coursera Chatbot').style(height=500, container=False)
|
76 |
|
77 |
# with gr.Row():
|
78 |
-
|
79 |
radio = gr.Radio(models, label="Choose a model", value="GPT-3.5", type="value", visible=False)
|
80 |
with gr.Row():
|
81 |
# with gr.Column(scale=0.75):
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
|
87 |
# with gr.Column(scale=0.25):
|
88 |
-
|
89 |
|
90 |
with gr.Row():
|
91 |
gr.Examples(
|
92 |
examples=['What is 3D printing?', 'Who are the instructors of the course?', 'What is the course about?',
|
93 |
-
'Which software can be used to create a design file for 3D printing?',
|
|
|
|
|
94 |
label="Examples")
|
95 |
|
96 |
txt.submit(add_text, [chatbot, txt, radio], [chatbot, txt], postprocess=False).then(
|
@@ -103,7 +112,7 @@ with gr.Blocks(theme='snehilsanyal/scikit-learn') as demo:
|
|
103 |
|
104 |
radio.change(fn=set_model, inputs=[chatbot, radio], outputs=[chatbot]).then(bot, chatbot, chatbot)
|
105 |
|
106 |
-
audio.change(lambda:None, None, audio)
|
107 |
|
108 |
set_model(chatbot, radio.value)
|
109 |
|
|
|
1 |
import gradio as gr
|
2 |
from main import index, run
|
3 |
from gtts import gTTS
|
4 |
+
import os, time, uuid
|
5 |
|
6 |
from transformers import pipeline
|
7 |
|
|
|
11 |
|
12 |
models = ["GPT-3.5", "Flan UL2", "Flan T5"]
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
with gr.Blocks(theme='snehilsanyal/scikit-learn') as demo:
|
15 |
+
state = gr.State([])
|
16 |
+
|
17 |
+
def create_session_id():
|
18 |
+
return str(uuid.uuid4())
|
19 |
+
|
20 |
+
def add_text(history, text, model):
|
21 |
+
print("Question asked: " + text)
|
22 |
+
response = run_model(text, model)
|
23 |
+
history = history + [(text, response)]
|
24 |
+
print(history)
|
25 |
+
return history, ""
|
26 |
+
|
27 |
+
|
28 |
+
def run_model(text, model):
|
29 |
+
start_time = time.time()
|
30 |
+
print("start time:" + str(start_time))
|
31 |
+
response = run(text, model, state.session_id)
|
32 |
+
end_time = time.time()
|
33 |
+
# If response contains string `SOURCES:`, then add a \n before `SOURCES`
|
34 |
+
if "SOURCES:" in response:
|
35 |
+
response = response.replace("SOURCES:", "\nSOURCES:")
|
36 |
+
# response = response + "\n\n" + "Time taken: " + str(end_time - start_time)
|
37 |
+
print(response)
|
38 |
+
print("Time taken: " + str(end_time - start_time))
|
39 |
+
return response
|
40 |
+
|
41 |
+
|
42 |
+
def get_output(history, audio, model):
|
43 |
+
txt = p(audio)["text"]
|
44 |
+
# history.append(( (audio, ) , txt))
|
45 |
+
audio_path = 'response.wav'
|
46 |
+
response = run_model(txt, model)
|
47 |
+
# Remove all text from SOURCES: to the end of the string
|
48 |
+
trimmed_response = response.split("SOURCES:")[0]
|
49 |
+
myobj = gTTS(text=trimmed_response, lang='en', slow=False)
|
50 |
+
myobj.save(audio_path)
|
51 |
+
# split audio by / and keep the last element
|
52 |
+
# audio = audio.split("/")[-1]
|
53 |
+
# audio = audio + ".wav"
|
54 |
+
history.append(((audio,), (audio_path,)))
|
55 |
+
print(history)
|
56 |
+
return history
|
57 |
+
|
58 |
+
|
59 |
+
def set_model(history, model):
|
60 |
+
print("Model selected: " + model)
|
61 |
+
history = get_first_message(history)
|
62 |
+
index(model, state.session_id)
|
63 |
+
return history
|
64 |
+
|
65 |
+
|
66 |
+
def get_first_message(history):
|
67 |
+
history = [(None,
|
68 |
+
'Learn about <a href="https://www.coursera.org/learn/3d-printing-revolution/home">3D printing Revolution</a> course with referred sources.')]
|
69 |
+
return history
|
70 |
+
|
71 |
+
|
72 |
+
def bot(history):
|
73 |
+
return history
|
74 |
+
|
75 |
+
|
76 |
+
state.session_id = create_session_id()
|
77 |
+
print("Session ID: " + state.session_id)
|
78 |
# Title on top in middle of the page
|
79 |
+
# gr.HTML("<h1 style='text-align: center;'>Course Assistant - 3D Printing Revolution</h1>")
|
80 |
|
81 |
+
chatbot = gr.Chatbot(get_first_message([]), elem_id="chatbot", label='3D Printing Revolution').style(height=400,
|
82 |
+
container=False)
|
|
|
83 |
|
84 |
# with gr.Row():
|
85 |
+
# Create radio button to select model
|
86 |
radio = gr.Radio(models, label="Choose a model", value="GPT-3.5", type="value", visible=False)
|
87 |
with gr.Row():
|
88 |
# with gr.Column(scale=0.75):
|
89 |
+
txt = gr.Textbox(
|
90 |
+
label="Ask your question here",
|
91 |
+
placeholder="Enter text and press enter", lines=1
|
92 |
+
).style(container=False)
|
93 |
|
94 |
# with gr.Column(scale=0.25):
|
95 |
+
audio = gr.Audio(source="microphone", type="filepath", visible=False)
|
96 |
|
97 |
with gr.Row():
|
98 |
gr.Examples(
|
99 |
examples=['What is 3D printing?', 'Who are the instructors of the course?', 'What is the course about?',
|
100 |
+
'Which software can be used to create a design file for 3D printing?',
|
101 |
+
'What are the key takeaways from the course?', 'How to create a 3D printing design file?'],
|
102 |
+
inputs=[txt],
|
103 |
label="Examples")
|
104 |
|
105 |
txt.submit(add_text, [chatbot, txt, radio], [chatbot, txt], postprocess=False).then(
|
|
|
112 |
|
113 |
radio.change(fn=set_model, inputs=[chatbot, radio], outputs=[chatbot]).then(bot, chatbot, chatbot)
|
114 |
|
115 |
+
audio.change(lambda: None, None, audio)
|
116 |
|
117 |
set_model(chatbot, radio.value)
|
118 |
|
main.py
CHANGED
@@ -1,10 +1,11 @@
|
|
1 |
-
from utils import get_search_index, generate_answer, set_model_and_embeddings
|
2 |
|
3 |
-
def index(model):
|
|
|
4 |
set_model_and_embeddings(model)
|
5 |
get_search_index(model)
|
6 |
return True
|
7 |
|
8 |
-
def run(question, model):
|
9 |
-
index(model)
|
10 |
return generate_answer(question)
|
|
|
1 |
+
from utils import get_search_index, generate_answer, set_model_and_embeddings, set_session_id
|
2 |
|
3 |
+
def index(model, session_id):
|
4 |
+
set_session_id(session_id)
|
5 |
set_model_and_embeddings(model)
|
6 |
get_search_index(model)
|
7 |
return True
|
8 |
|
9 |
+
def run(question, model, session_id):
|
10 |
+
index(model, session_id)
|
11 |
return generate_answer(question)
|
mapping.py
ADDED
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Create a dict of filenames and their corresponding url links to be used as source in line #203
|
2 |
+
FILE_URL_MAPPING = {
|
3 |
+
'docs/01_course-orientation/01_about-the-course/01_introduction-to-the-3d-printing-specialization.en.txt':
|
4 |
+
'https://www.coursera.org/learn/3d-printing-revolution/lecture/mLvWU/introduction-to-the-3d-printing-specialization',
|
5 |
+
'docs/01_course-orientation/01_about-the-course/02_welcome-to-the-3d-printing-revolution.en.txt':
|
6 |
+
'https://www.coursera.org/learn/3d-printing-revolution/lecture/zQ7lG/welcome-to-the-3d-printing-revolution',
|
7 |
+
|
8 |
+
'docs/03_module-2-why-is-it-revolutionary/03_the-3d-printing-revolution-facts-concepts/02_how-will-3d-printing-change-business.en.txt':
|
9 |
+
'https://www.coursera.org/learn/3d-printing-revolution/lecture/D8VUj/how-will-3d-printing-change-business',
|
10 |
+
|
11 |
+
'docs/03_module-2-why-is-it-revolutionary/03_the-3d-printing-revolution-facts-concepts/01_whats-special-about-3d-printing.en.txt':
|
12 |
+
'https://www.coursera.org/learn/3d-printing-revolution/lecture/WmJQL/whats-special-about-3d-printing',
|
13 |
+
|
14 |
+
'docs/03_module-2-why-is-it-revolutionary/03_the-3d-printing-revolution-facts-concepts/04_remixing-products-exercise-overview.en.txt':
|
15 |
+
'https://www.coursera.org/learn/3d-printing-revolution/lecture/J3Tq3/remixing-products-exercise-overview',
|
16 |
+
|
17 |
+
'docs/03_module-2-why-is-it-revolutionary/03_the-3d-printing-revolution-facts-concepts/03_the-future-of-3d-printing.en.txt':
|
18 |
+
'https://www.coursera.org/learn/3d-printing-revolution/lecture/tjSlh/the-future-of-3d-printing',
|
19 |
+
|
20 |
+
'docs/03_module-2-why-is-it-revolutionary/02_an-early-look-at-the-coming-revolution/01_tour-of-the-illinois-makerlab-vishal-sachdev.en.txt':
|
21 |
+
'https://www.coursera.org/learn/3d-printing-revolution/lecture/rcVnN/tour-of-the-illinois-makerlab-vishal-sachdev',
|
22 |
+
|
23 |
+
'docs/03_module-2-why-is-it-revolutionary/02_an-early-look-at-the-coming-revolution/02_meet-the-makers-arielle-rausin-cameron-alberg-scott-zelman.en.txt':
|
24 |
+
'https://www.coursera.org/learn/3d-printing-revolution/lecture/J2nD2/meet-the-makers-arielle-rausin-cameron-alberg-scott-zelman',
|
25 |
+
'docs/03_module-2-why-is-it-revolutionary/04_the-revolutionaries/03_shapeways-lauren-slowik.en.txt':
|
26 |
+
'https://www.coursera.org/learn/3d-printing-revolution/lecture/mi1zr/shapeways-lauren-slowik',
|
27 |
+
'docs/03_module-2-why-is-it-revolutionary/04_the-revolutionaries/04_3d-fashion-francis-bitonti.en.txt':
|
28 |
+
'https://www.coursera.org/learn/3d-printing-revolution/lecture/KmYxf/3d-fashion-francis-bitonti',
|
29 |
+
'docs/03_module-2-why-is-it-revolutionary/04_the-revolutionaries/06_whats-next-hod-lipson.en.txt':
|
30 |
+
'https://www.coursera.org/learn/3d-printing-revolution/lecture/3zKRN/whats-next-hod-lipson',
|
31 |
+
'docs/03_module-2-why-is-it-revolutionary/04_the-revolutionaries/05_3d-printed-battery-paul-braun.en.txt':
|
32 |
+
'https://www.coursera.org/learn/3d-printing-revolution/lecture/mrEAK/3d-printed-battery-paul-braun',
|
33 |
+
'docs/03_module-2-why-is-it-revolutionary/04_the-revolutionaries/02_normal-nikki-kaufman.en.txt':
|
34 |
+
'https://www.coursera.org/learn/3d-printing-revolution/lecture/I8QXq/normal-nikki-kaufman',
|
35 |
+
'docs/03_module-2-why-is-it-revolutionary/04_the-revolutionaries/01_voodoo-manufacturing-max-friefeld.en.txt':
|
36 |
+
'https://www.coursera.org/learn/3d-printing-revolution/lecture/atrTP/voodoo-manufacturing-max-friefeld',
|
37 |
+
'docs/04_course-conclusion/01_course-wrap-up-whats-next/01_the-3d-printing-revolution-wrap-up.en.txt':
|
38 |
+
'https://www.coursera.org/learn/3d-printing-revolution/lecture/f6XvX/the-3d-printing-revolution-wrap-up',
|
39 |
+
'docs/04_course-conclusion/01_course-wrap-up-whats-next/03_gies-online-programs.en.txt':
|
40 |
+
'https://www.coursera.org/learn/3d-printing-revolution/lecture/JdvZk/gies-online-programs',
|
41 |
+
|
42 |
+
'docs/02_module-1-what-is-3d-printing/05_optional-content/01_like-this-course-learn-more-with-the-imba-optional.en.txt':
|
43 |
+
'https://www.coursera.org/learn/3d-printing-revolution/lecture/MIATt/like-this-course-learn-more-with-the-imba-optional',
|
44 |
+
'docs/02_module-1-what-is-3d-printing/03_3d-printing-facts-concepts/04_where-designs-come-from.en.txt':
|
45 |
+
'https://www.coursera.org/learn/3d-printing-revolution/lecture/0fU1x/where-designs-come-from',
|
46 |
+
'docs/02_module-1-what-is-3d-printing/03_3d-printing-facts-concepts/05_where-to-find-3d-printers.en.txt':
|
47 |
+
'https://www.coursera.org/learn/3d-printing-revolution/lecture/3XTPj/where-to-find-3d-printers',
|
48 |
+
'docs/02_module-1-what-is-3d-printing/03_3d-printing-facts-concepts/01_history-of-3d-printing.en.txt':
|
49 |
+
'https://www.coursera.org/learn/3d-printing-revolution/lecture/HMvuh/history-of-3d-printing',
|
50 |
+
'docs/02_module-1-what-is-3d-printing/03_3d-printing-facts-concepts/02_how-3d-printers-work.en.txt':
|
51 |
+
'https://www.coursera.org/learn/3d-printing-revolution/lecture/dNpvw/how-3d-printers-work',
|
52 |
+
'docs/02_module-1-what-is-3d-printing/03_3d-printing-facts-concepts/03_materials-costs.en.txt':
|
53 |
+
'https://www.coursera.org/learn/3d-printing-revolution/lecture/j63D2/materials-costs',
|
54 |
+
'docs/02_module-1-what-is-3d-printing/03_3d-printing-facts-concepts/06_3d-printing-applications.en.txt':
|
55 |
+
'https://www.coursera.org/learn/3d-printing-revolution/lecture/EJdi9/3d-printing-applications',
|
56 |
+
|
57 |
+
'docs/02_module-1-what-is-3d-printing/04_more-3d-printing-insights/03_selective-laser-melting-rodrigo-gutierrez.en.txt':
|
58 |
+
'https://www.coursera.org/learn/3d-printing-revolution/lecture/kkpwz/selective-laser-melting-rodrigo-gutierrez',
|
59 |
+
'docs/02_module-1-what-is-3d-printing/04_more-3d-printing-insights/04_3d-printing-ecosystem-aaron-roy.en.txt':
|
60 |
+
'https://www.coursera.org/learn/3d-printing-revolution/lecture/2zjzy/3d-printing-ecosystem-aaron-roy',
|
61 |
+
|
62 |
+
'docs/02_module-1-what-is-3d-printing/04_more-3d-printing-insights/01_3d-printing-demonstration-danny-lohan.en.txt':
|
63 |
+
'https://www.coursera.org/learn/3d-printing-revolution/lecture/5b8IG/3d-printing-demonstration-danny-lohan',
|
64 |
+
|
65 |
+
'docs/02_module-1-what-is-3d-printing/04_more-3d-printing-insights/02_3d-printing-vs-additive-manufacturing-mark-cotteleer.en.txt':
|
66 |
+
'https://www.coursera.org/learn/3d-printing-revolution/lecture/qBvTb/3d-printing-vs-additive-manufacturing-mark-cotteleer',
|
67 |
+
'docs/02_module-1-what-is-3d-printing/02_3d-printing-insights/02_my-3d-printing-story-aric-rindfleisch.en.txt':
|
68 |
+
'https://www.coursera.org/learn/3d-printing-revolution/lecture/hhWc8/my-3d-printing-story-aric-rindfleisch',
|
69 |
+
|
70 |
+
'docs/02_module-1-what-is-3d-printing/02_3d-printing-insights/04_3d-printing-the-maker-movement-hackerspaces-chris-meyer.en.txt':
|
71 |
+
'https://www.coursera.org/learn/3d-printing-revolution/lecture/8HO88/3d-printing-the-maker-movement-hackerspaces-chris-meyer',
|
72 |
+
'docs/02_module-1-what-is-3d-printing/02_3d-printing-insights/06_what-would-you-make-exercise-overview.en.txt':
|
73 |
+
'https://www.coursera.org/learn/3d-printing-revolution/lecture/kfDp2/what-would-you-make-exercise-overview',
|
74 |
+
|
75 |
+
'docs/02_module-1-what-is-3d-printing/02_3d-printing-insights/05_the-birth-of-desktop-3d-printing-matt-griffin.en.txt':
|
76 |
+
'https://www.coursera.org/learn/3d-printing-revolution/lecture/RlvEF/the-birth-of-desktop-3d-printing-matt-griffin',
|
77 |
+
'docs/02_module-1-what-is-3d-printing/02_3d-printing-insights/03_tour-of-sector67-chris-meyer.en.txt':
|
78 |
+
'https://www.coursera.org/learn/3d-printing-revolution/lecture/HXJrm/tour-of-sector67-chris-meyer',
|
79 |
+
'docs/02_module-1-what-is-3d-printing/02_3d-printing-insights/01_views-on-3d-printing-champaign-new-york.en.txt':
|
80 |
+
'https://www.coursera.org/learn/3d-printing-revolution/lecture/jvULv/views-on-3d-printing-champaign-new-york',
|
81 |
+
'docs/01_course-orientation/01_about-the-course/05_glossary_instructions.html':
|
82 |
+
'https://www.coursera.org/learn/3d-printing-revolution/supplement/515pX/glossary',
|
83 |
+
'docs/01_course-orientation/01_about-the-course/03_syllabus_instructions.html':
|
84 |
+
'https://www.coursera.org/learn/3d-printing-revolution/supplement/l2J8d/syllabus',
|
85 |
+
'docs/01_course-orientation/01_about-the-course/06_learner-stories_instructions.html':
|
86 |
+
'https://www.coursera.org/learn/3d-printing-revolution/supplement/goE8a/learner-stories',
|
87 |
+
'docs/01_course-orientation/01_about-the-course/04_about-the-discussion-forums_instructions.html':
|
88 |
+
'https://www.coursera.org/learn/3d-printing-revolution/supplement/HA7H7/about-the-discussion-forums',
|
89 |
+
'docs/01_course-orientation/02_about-your-classmates/02_social-media_illinois.edu.html':
|
90 |
+
'https://www.facebook.com/illinois.edu',
|
91 |
+
'docs/01_course-orientation/02_about-your-classmates/01_updating-your-profile_instructions.html':
|
92 |
+
'https://www.coursera.org/learn/3d-printing-revolution/supplement/f2iFO/updating-your-profile',
|
93 |
+
|
94 |
+
'docs/01_course-orientation/02_about-your-classmates/02_social-media_termsDknlfPe-5xKJJaHWl4j6kveY2GjKiZxK_vd-JDIN3-6piB5w.2PhI_l8TrILDNPPI-v-VPg.kolgdmfhbhzcbgm3-lv_.html':
|
95 |
+
'https://www.coursera.org/about/termsDknlfPe-5xKJJaHWl4j6kveY2GjKiZxK_vd-JDIN3-6piB5w.2PhI_l8TrILDNPPI-v-VPg.kOLGdmfhbhzCbGM3-lv_j477mwb9Ffuxi5xhEac6Biqu1NvspELKPGjNRjoAzuv8LasLtq22lxzrgdd9C8Y4JQ4gHm7FuRqTL4rlby3Pb_N4mpVXMkT83a3Ob_0QWVHv7LiZghGDTwCWYxU4lMfZpfsqsm7PoQ7HfKUFvHwgUIbOGox3ZZgJtBE2t-TDkbegcktpcn6k2VqZZ0WYvQTad7oijs5WHJLfL7EYiUGb01udFqMaOLIPP1msztyo496GDNUgBSvsJcPfHE20dluqe5_KzaSoXzKxXdiW12DjKJk_XDNc14mf41U17h5HgMXg',
|
96 |
+
'docs/01_course-orientation/02_about-your-classmates/02_social-media_instructions.html':
|
97 |
+
'https://www.coursera.org/learn/3d-printing-revolution/supplement/1vga3/social-media',
|
98 |
+
|
99 |
+
'docs/03_module-2-why-is-it-revolutionary/03_the-3d-printing-revolution-facts-concepts/05_remixing-products-exercise_peer_assignment_instructions.html':
|
100 |
+
'https://www.coursera.org/learn/3d-printing-revolution/peer/FAZ0P/remixing-products-exercise',
|
101 |
+
'docs/03_module-2-why-is-it-revolutionary/01_module-2-information/01_module-2-overview_instructions.html':
|
102 |
+
'https://www.coursera.org/learn/3d-printing-revolution/supplement/UwuNp/module-2-overview',
|
103 |
+
'docs/04_course-conclusion/01_course-wrap-up-whats-next/02_congratulations_instructions.html':
|
104 |
+
'https://www.coursera.org/learn/3d-printing-revolution/supplement/vISgA/congratulations',
|
105 |
+
'docs/05_Resources/03_3d-printing-services-and-products/01__resources.html':
|
106 |
+
'https://www.coursera.org/learn/3d-printing-revolution/resources/aPz3d',
|
107 |
+
'docs/05_Resources/04_3d-printing-community/01__resources.html':
|
108 |
+
'https://www.coursera.org/learn/3d-printing-revolution/resources/lfbM9',
|
109 |
+
'docs/05_Resources/02_3d-printing-softwares/01__resources.html':
|
110 |
+
'https://www.coursera.org/learn/3d-printing-revolution/resources/ltlHt',
|
111 |
+
'docs/05_Resources/01_books-articles/01__resources.html':
|
112 |
+
'https://www.coursera.org/learn/3d-printing-revolution/resources/FH3x3',
|
113 |
+
'docs/05_Resources/05_explore-the-imba/01__resources.html':
|
114 |
+
'https://www.coursera.org/learn/3d-printing-revolution/resources/0AejF',
|
115 |
+
'docs/02_module-1-what-is-3d-printing/01_module-1-overview/01_module-1-overview_instructions.html':
|
116 |
+
'https://www.coursera.org/learn/3d-printing-revolution/supplement/HZXB5/module-1-overview',
|
117 |
+
|
118 |
+
'docs/02_module-1-what-is-3d-printing/02_3d-printing-insights/07_what-would-you-make-exercise_peer_assignment_instructions.html':
|
119 |
+
'https://www.coursera.org/learn/3d-printing-revolution/peer/t8bqq/what-would-you-make-exercise'}
|
memory.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
from datetime import datetime
|
3 |
+
from pymongo import errors
|
4 |
+
from langchain.schema import AIMessage, BaseMessage, HumanMessage, messages_from_dict, _message_to_dict
|
5 |
+
from langchain.memory import MongoDBChatMessageHistory
|
6 |
+
import logging
|
7 |
+
from typing import List
|
8 |
+
|
9 |
+
|
10 |
+
logger = logging.getLogger(__name__)
|
11 |
+
class CustomMongoDBChatMessageHistory(MongoDBChatMessageHistory):
|
12 |
+
|
13 |
+
@property
|
14 |
+
def messages(self) -> List[BaseMessage]: # type: ignore
|
15 |
+
"""Retrieve the messages from MongoDB"""
|
16 |
+
from pymongo import errors
|
17 |
+
cursor = None
|
18 |
+
try:
|
19 |
+
cursor = self.collection.find({"SessionId": self.session_id})
|
20 |
+
except errors.OperationFailure as error:
|
21 |
+
logger.error(error)
|
22 |
+
|
23 |
+
document_count = self.collection.count_documents({"SessionId": self.session_id})
|
24 |
+
|
25 |
+
if cursor and document_count > 0:
|
26 |
+
document = cursor[0] # Get the first document with the matching session id
|
27 |
+
items = document["messages"] # Get the messages array from the document
|
28 |
+
else:
|
29 |
+
items = []
|
30 |
+
|
31 |
+
messages = messages_from_dict([json.loads(item) for item in items])
|
32 |
+
return messages
|
33 |
+
|
34 |
+
def add_user_message(self, message: str) -> None:
|
35 |
+
self.append(HumanMessage(content=message))
|
36 |
+
|
37 |
+
def add_ai_message(self, message: str) -> None:
|
38 |
+
self.append(AIMessage(content=message))
|
39 |
+
|
40 |
+
def append(self, message: BaseMessage) -> None:
|
41 |
+
"""Append the message to the record in MongoDB with the desired format"""
|
42 |
+
|
43 |
+
# Determine the sender based on the message type
|
44 |
+
sender = "ai" if isinstance(message, AIMessage) else "human"
|
45 |
+
|
46 |
+
|
47 |
+
# Create the message object with the desired format
|
48 |
+
message_obj = {
|
49 |
+
"type": sender,
|
50 |
+
"content": message.content,
|
51 |
+
"timestamp": datetime.utcnow()
|
52 |
+
}
|
53 |
+
|
54 |
+
try:
|
55 |
+
# Update the messages array with the new message object
|
56 |
+
self.collection.update_one(
|
57 |
+
{"SessionId": self.session_id},
|
58 |
+
{"$push": {"messages": json.dumps(_message_to_dict(message))}},
|
59 |
+
upsert=True
|
60 |
+
)
|
61 |
+
except errors.WriteError as err:
|
62 |
+
logger.error(err)
|
requirements.txt
CHANGED
@@ -7,4 +7,5 @@ transformers
|
|
7 |
gtts
|
8 |
torch
|
9 |
tiktoken
|
10 |
-
huggingface-hub
|
|
|
|
7 |
gtts
|
8 |
torch
|
9 |
tiktoken
|
10 |
+
huggingface-hub
|
11 |
+
pymongo
|
utils.py
CHANGED
@@ -17,143 +17,26 @@ from langchain.prompts.chat import (
|
|
17 |
from langchain.text_splitter import CharacterTextSplitter
|
18 |
from langchain.vectorstores.faiss import FAISS
|
19 |
from langchain.cache import InMemoryCache
|
20 |
-
|
21 |
-
|
22 |
-
file_url_mapping = {
|
23 |
-
'docs/01_course-orientation/01_about-the-course/01_introduction-to-the-3d-printing-specialization.en.txt':
|
24 |
-
'https://www.coursera.org/learn/3d-printing-revolution/lecture/mLvWU/introduction-to-the-3d-printing-specialization',
|
25 |
-
'docs/01_course-orientation/01_about-the-course/02_welcome-to-the-3d-printing-revolution.en.txt':
|
26 |
-
'https://www.coursera.org/learn/3d-printing-revolution/lecture/zQ7lG/welcome-to-the-3d-printing-revolution',
|
27 |
-
|
28 |
-
'docs/03_module-2-why-is-it-revolutionary/03_the-3d-printing-revolution-facts-concepts/02_how-will-3d-printing-change-business.en.txt':
|
29 |
-
'https://www.coursera.org/learn/3d-printing-revolution/lecture/D8VUj/how-will-3d-printing-change-business',
|
30 |
-
|
31 |
-
'docs/03_module-2-why-is-it-revolutionary/03_the-3d-printing-revolution-facts-concepts/01_whats-special-about-3d-printing.en.txt':
|
32 |
-
'https://www.coursera.org/learn/3d-printing-revolution/lecture/WmJQL/whats-special-about-3d-printing',
|
33 |
-
|
34 |
-
'docs/03_module-2-why-is-it-revolutionary/03_the-3d-printing-revolution-facts-concepts/04_remixing-products-exercise-overview.en.txt':
|
35 |
-
'https://www.coursera.org/learn/3d-printing-revolution/lecture/J3Tq3/remixing-products-exercise-overview',
|
36 |
-
|
37 |
-
'docs/03_module-2-why-is-it-revolutionary/03_the-3d-printing-revolution-facts-concepts/03_the-future-of-3d-printing.en.txt':
|
38 |
-
'https://www.coursera.org/learn/3d-printing-revolution/lecture/tjSlh/the-future-of-3d-printing',
|
39 |
-
|
40 |
-
'docs/03_module-2-why-is-it-revolutionary/02_an-early-look-at-the-coming-revolution/01_tour-of-the-illinois-makerlab-vishal-sachdev.en.txt':
|
41 |
-
'https://www.coursera.org/learn/3d-printing-revolution/lecture/rcVnN/tour-of-the-illinois-makerlab-vishal-sachdev',
|
42 |
-
|
43 |
-
'docs/03_module-2-why-is-it-revolutionary/02_an-early-look-at-the-coming-revolution/02_meet-the-makers-arielle-rausin-cameron-alberg-scott-zelman.en.txt':
|
44 |
-
'https://www.coursera.org/learn/3d-printing-revolution/lecture/J2nD2/meet-the-makers-arielle-rausin-cameron-alberg-scott-zelman',
|
45 |
-
'docs/03_module-2-why-is-it-revolutionary/04_the-revolutionaries/03_shapeways-lauren-slowik.en.txt':
|
46 |
-
'https://www.coursera.org/learn/3d-printing-revolution/lecture/mi1zr/shapeways-lauren-slowik',
|
47 |
-
'docs/03_module-2-why-is-it-revolutionary/04_the-revolutionaries/04_3d-fashion-francis-bitonti.en.txt':
|
48 |
-
'https://www.coursera.org/learn/3d-printing-revolution/lecture/KmYxf/3d-fashion-francis-bitonti',
|
49 |
-
'docs/03_module-2-why-is-it-revolutionary/04_the-revolutionaries/06_whats-next-hod-lipson.en.txt':
|
50 |
-
'https://www.coursera.org/learn/3d-printing-revolution/lecture/3zKRN/whats-next-hod-lipson',
|
51 |
-
'docs/03_module-2-why-is-it-revolutionary/04_the-revolutionaries/05_3d-printed-battery-paul-braun.en.txt':
|
52 |
-
'https://www.coursera.org/learn/3d-printing-revolution/lecture/mrEAK/3d-printed-battery-paul-braun',
|
53 |
-
'docs/03_module-2-why-is-it-revolutionary/04_the-revolutionaries/02_normal-nikki-kaufman.en.txt':
|
54 |
-
'https://www.coursera.org/learn/3d-printing-revolution/lecture/I8QXq/normal-nikki-kaufman',
|
55 |
-
'docs/03_module-2-why-is-it-revolutionary/04_the-revolutionaries/01_voodoo-manufacturing-max-friefeld.en.txt':
|
56 |
-
'https://www.coursera.org/learn/3d-printing-revolution/lecture/atrTP/voodoo-manufacturing-max-friefeld',
|
57 |
-
'docs/04_course-conclusion/01_course-wrap-up-whats-next/01_the-3d-printing-revolution-wrap-up.en.txt':
|
58 |
-
'https://www.coursera.org/learn/3d-printing-revolution/lecture/f6XvX/the-3d-printing-revolution-wrap-up',
|
59 |
-
'docs/04_course-conclusion/01_course-wrap-up-whats-next/03_gies-online-programs.en.txt':
|
60 |
-
'https://www.coursera.org/learn/3d-printing-revolution/lecture/JdvZk/gies-online-programs',
|
61 |
-
|
62 |
-
'docs/02_module-1-what-is-3d-printing/05_optional-content/01_like-this-course-learn-more-with-the-imba-optional.en.txt':
|
63 |
-
'https://www.coursera.org/learn/3d-printing-revolution/lecture/MIATt/like-this-course-learn-more-with-the-imba-optional',
|
64 |
-
'docs/02_module-1-what-is-3d-printing/03_3d-printing-facts-concepts/04_where-designs-come-from.en.txt':
|
65 |
-
'https://www.coursera.org/learn/3d-printing-revolution/lecture/0fU1x/where-designs-come-from',
|
66 |
-
'docs/02_module-1-what-is-3d-printing/03_3d-printing-facts-concepts/05_where-to-find-3d-printers.en.txt':
|
67 |
-
'https://www.coursera.org/learn/3d-printing-revolution/lecture/3XTPj/where-to-find-3d-printers',
|
68 |
-
'docs/02_module-1-what-is-3d-printing/03_3d-printing-facts-concepts/01_history-of-3d-printing.en.txt':
|
69 |
-
'https://www.coursera.org/learn/3d-printing-revolution/lecture/HMvuh/history-of-3d-printing',
|
70 |
-
'docs/02_module-1-what-is-3d-printing/03_3d-printing-facts-concepts/02_how-3d-printers-work.en.txt':
|
71 |
-
'https://www.coursera.org/learn/3d-printing-revolution/lecture/dNpvw/how-3d-printers-work',
|
72 |
-
'docs/02_module-1-what-is-3d-printing/03_3d-printing-facts-concepts/03_materials-costs.en.txt':
|
73 |
-
'https://www.coursera.org/learn/3d-printing-revolution/lecture/j63D2/materials-costs',
|
74 |
-
'docs/02_module-1-what-is-3d-printing/03_3d-printing-facts-concepts/06_3d-printing-applications.en.txt':
|
75 |
-
'https://www.coursera.org/learn/3d-printing-revolution/lecture/EJdi9/3d-printing-applications',
|
76 |
-
|
77 |
-
'docs/02_module-1-what-is-3d-printing/04_more-3d-printing-insights/03_selective-laser-melting-rodrigo-gutierrez.en.txt':
|
78 |
-
'https://www.coursera.org/learn/3d-printing-revolution/lecture/kkpwz/selective-laser-melting-rodrigo-gutierrez',
|
79 |
-
'docs/02_module-1-what-is-3d-printing/04_more-3d-printing-insights/04_3d-printing-ecosystem-aaron-roy.en.txt':
|
80 |
-
'https://www.coursera.org/learn/3d-printing-revolution/lecture/2zjzy/3d-printing-ecosystem-aaron-roy',
|
81 |
-
|
82 |
-
'docs/02_module-1-what-is-3d-printing/04_more-3d-printing-insights/01_3d-printing-demonstration-danny-lohan.en.txt':
|
83 |
-
'https://www.coursera.org/learn/3d-printing-revolution/lecture/5b8IG/3d-printing-demonstration-danny-lohan',
|
84 |
-
|
85 |
-
'docs/02_module-1-what-is-3d-printing/04_more-3d-printing-insights/02_3d-printing-vs-additive-manufacturing-mark-cotteleer.en.txt':
|
86 |
-
'https://www.coursera.org/learn/3d-printing-revolution/lecture/qBvTb/3d-printing-vs-additive-manufacturing-mark-cotteleer',
|
87 |
-
'docs/02_module-1-what-is-3d-printing/02_3d-printing-insights/02_my-3d-printing-story-aric-rindfleisch.en.txt':
|
88 |
-
'https://www.coursera.org/learn/3d-printing-revolution/lecture/hhWc8/my-3d-printing-story-aric-rindfleisch',
|
89 |
-
|
90 |
-
'docs/02_module-1-what-is-3d-printing/02_3d-printing-insights/04_3d-printing-the-maker-movement-hackerspaces-chris-meyer.en.txt':
|
91 |
-
'https://www.coursera.org/learn/3d-printing-revolution/lecture/8HO88/3d-printing-the-maker-movement-hackerspaces-chris-meyer',
|
92 |
-
'docs/02_module-1-what-is-3d-printing/02_3d-printing-insights/06_what-would-you-make-exercise-overview.en.txt':
|
93 |
-
'https://www.coursera.org/learn/3d-printing-revolution/lecture/kfDp2/what-would-you-make-exercise-overview',
|
94 |
-
|
95 |
-
'docs/02_module-1-what-is-3d-printing/02_3d-printing-insights/05_the-birth-of-desktop-3d-printing-matt-griffin.en.txt':
|
96 |
-
'https://www.coursera.org/learn/3d-printing-revolution/lecture/RlvEF/the-birth-of-desktop-3d-printing-matt-griffin',
|
97 |
-
'docs/02_module-1-what-is-3d-printing/02_3d-printing-insights/03_tour-of-sector67-chris-meyer.en.txt':
|
98 |
-
'https://www.coursera.org/learn/3d-printing-revolution/lecture/HXJrm/tour-of-sector67-chris-meyer',
|
99 |
-
'docs/02_module-1-what-is-3d-printing/02_3d-printing-insights/01_views-on-3d-printing-champaign-new-york.en.txt':
|
100 |
-
'https://www.coursera.org/learn/3d-printing-revolution/lecture/jvULv/views-on-3d-printing-champaign-new-york',
|
101 |
-
'docs/01_course-orientation/01_about-the-course/05_glossary_instructions.html':
|
102 |
-
'https://www.coursera.org/learn/3d-printing-revolution/supplement/515pX/glossary',
|
103 |
-
'docs/01_course-orientation/01_about-the-course/03_syllabus_instructions.html':
|
104 |
-
'https://www.coursera.org/learn/3d-printing-revolution/supplement/l2J8d/syllabus',
|
105 |
-
'docs/01_course-orientation/01_about-the-course/06_learner-stories_instructions.html':
|
106 |
-
'https://www.coursera.org/learn/3d-printing-revolution/supplement/goE8a/learner-stories',
|
107 |
-
'docs/01_course-orientation/01_about-the-course/04_about-the-discussion-forums_instructions.html':
|
108 |
-
'https://www.coursera.org/learn/3d-printing-revolution/supplement/HA7H7/about-the-discussion-forums',
|
109 |
-
'docs/01_course-orientation/02_about-your-classmates/02_social-media_illinois.edu.html':
|
110 |
-
'https://www.facebook.com/illinois.edu',
|
111 |
-
'docs/01_course-orientation/02_about-your-classmates/01_updating-your-profile_instructions.html':
|
112 |
-
'https://www.coursera.org/learn/3d-printing-revolution/supplement/f2iFO/updating-your-profile',
|
113 |
-
|
114 |
-
'docs/01_course-orientation/02_about-your-classmates/02_social-media_termsDknlfPe-5xKJJaHWl4j6kveY2GjKiZxK_vd-JDIN3-6piB5w.2PhI_l8TrILDNPPI-v-VPg.kolgdmfhbhzcbgm3-lv_.html':
|
115 |
-
'https://www.coursera.org/about/termsDknlfPe-5xKJJaHWl4j6kveY2GjKiZxK_vd-JDIN3-6piB5w.2PhI_l8TrILDNPPI-v-VPg.kOLGdmfhbhzCbGM3-lv_j477mwb9Ffuxi5xhEac6Biqu1NvspELKPGjNRjoAzuv8LasLtq22lxzrgdd9C8Y4JQ4gHm7FuRqTL4rlby3Pb_N4mpVXMkT83a3Ob_0QWVHv7LiZghGDTwCWYxU4lMfZpfsqsm7PoQ7HfKUFvHwgUIbOGox3ZZgJtBE2t-TDkbegcktpcn6k2VqZZ0WYvQTad7oijs5WHJLfL7EYiUGb01udFqMaOLIPP1msztyo496GDNUgBSvsJcPfHE20dluqe5_KzaSoXzKxXdiW12DjKJk_XDNc14mf41U17h5HgMXg',
|
116 |
-
'docs/01_course-orientation/02_about-your-classmates/02_social-media_instructions.html':
|
117 |
-
'https://www.coursera.org/learn/3d-printing-revolution/supplement/1vga3/social-media',
|
118 |
-
|
119 |
-
'docs/03_module-2-why-is-it-revolutionary/03_the-3d-printing-revolution-facts-concepts/05_remixing-products-exercise_peer_assignment_instructions.html':
|
120 |
-
'https://www.coursera.org/learn/3d-printing-revolution/peer/FAZ0P/remixing-products-exercise',
|
121 |
-
'docs/03_module-2-why-is-it-revolutionary/01_module-2-information/01_module-2-overview_instructions.html':
|
122 |
-
'https://www.coursera.org/learn/3d-printing-revolution/supplement/UwuNp/module-2-overview',
|
123 |
-
'docs/04_course-conclusion/01_course-wrap-up-whats-next/02_congratulations_instructions.html':
|
124 |
-
'https://www.coursera.org/learn/3d-printing-revolution/supplement/vISgA/congratulations',
|
125 |
-
'docs/05_Resources/03_3d-printing-services-and-products/01__resources.html':
|
126 |
-
'https://www.coursera.org/learn/3d-printing-revolution/resources/aPz3d',
|
127 |
-
'docs/05_Resources/04_3d-printing-community/01__resources.html':
|
128 |
-
'https://www.coursera.org/learn/3d-printing-revolution/resources/lfbM9',
|
129 |
-
'docs/05_Resources/02_3d-printing-softwares/01__resources.html':
|
130 |
-
'https://www.coursera.org/learn/3d-printing-revolution/resources/ltlHt',
|
131 |
-
'docs/05_Resources/01_books-articles/01__resources.html':
|
132 |
-
'https://www.coursera.org/learn/3d-printing-revolution/resources/FH3x3',
|
133 |
-
'docs/05_Resources/05_explore-the-imba/01__resources.html':
|
134 |
-
'https://www.coursera.org/learn/3d-printing-revolution/resources/0AejF',
|
135 |
-
'docs/02_module-1-what-is-3d-printing/01_module-1-overview/01_module-1-overview_instructions.html':
|
136 |
-
'https://www.coursera.org/learn/3d-printing-revolution/supplement/HZXB5/module-1-overview',
|
137 |
-
|
138 |
-
'docs/02_module-1-what-is-3d-printing/02_3d-printing-insights/07_what-would-you-make-exercise_peer_assignment_instructions.html':
|
139 |
-
'https://www.coursera.org/learn/3d-printing-revolution/peer/t8bqq/what-would-you-make-exercise'}
|
140 |
-
|
141 |
|
142 |
langchain.llm_cache = InMemoryCache()
|
143 |
|
144 |
-
global model_name
|
145 |
-
|
146 |
models = ["GPT-3.5", "Flan UL2", "GPT-4", "Flan T5"]
|
147 |
|
148 |
pickle_file = "_vs.pkl"
|
149 |
index_file = "_vs.index"
|
150 |
models_folder = "models/"
|
|
|
151 |
|
152 |
llm = ChatOpenAI(model_name="gpt-4", temperature=0.1)
|
153 |
|
154 |
embeddings = OpenAIEmbeddings(model='text-embedding-ada-002')
|
155 |
|
156 |
-
|
|
|
|
|
|
|
157 |
|
158 |
memory = ConversationBufferWindowMemory(memory_key="chat_history", k=10)
|
159 |
|
@@ -172,11 +55,24 @@ messages = [
|
|
172 |
CHAT_PROMPT = ChatPromptTemplate.from_messages(messages)
|
173 |
|
174 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
175 |
def set_model_and_embeddings(model):
|
176 |
-
global chat_history
|
177 |
set_model(model)
|
178 |
# set_embeddings(model)
|
179 |
-
chat_history = []
|
180 |
|
181 |
|
182 |
def set_model(model):
|
@@ -263,7 +159,7 @@ def fetch_data_for_embeddings():
|
|
263 |
|
264 |
# use file_url_mapping to set metadata of document to url which has been set as the source
|
265 |
for document in document_list:
|
266 |
-
document.metadata["url"] =
|
267 |
|
268 |
print("document list: " + str(len(document_list)))
|
269 |
return document_list
|
@@ -288,7 +184,7 @@ def create_chunk_documents():
|
|
288 |
|
289 |
|
290 |
def get_qa_chain(vectorstore_index):
|
291 |
-
global llm
|
292 |
print(llm)
|
293 |
|
294 |
# embeddings_filter = EmbeddingsFilter(embeddings=embeddings, similarity_threshold=0.76)
|
@@ -297,7 +193,7 @@ def get_qa_chain(vectorstore_index):
|
|
297 |
search_kwargs={"score_threshold": .7})
|
298 |
|
299 |
chain = ConversationalRetrievalChain.from_llm(llm, retriever, return_source_documents=True,
|
300 |
-
verbose=True,
|
301 |
combine_docs_chain_kwargs={"prompt": CHAT_PROMPT})
|
302 |
return chain
|
303 |
|
@@ -310,12 +206,17 @@ def get_chat_history(inputs) -> str:
|
|
310 |
|
311 |
|
312 |
def generate_answer(question) -> str:
|
313 |
-
global
|
314 |
chain = get_qa_chain(vectorstore_index)
|
315 |
-
|
|
|
316 |
result = chain(
|
317 |
-
{"question": question, "chat_history":
|
318 |
-
|
|
|
|
|
|
|
|
|
319 |
sources = []
|
320 |
print(result)
|
321 |
|
@@ -326,3 +227,10 @@ def generate_answer(question) -> str:
|
|
326 |
|
327 |
source = ',\n'.join(set(sources))
|
328 |
return result['answer'] + '\nSOURCES: ' + source
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
from langchain.text_splitter import CharacterTextSplitter
|
18 |
from langchain.vectorstores.faiss import FAISS
|
19 |
from langchain.cache import InMemoryCache
|
20 |
+
from memory import CustomMongoDBChatMessageHistory
|
21 |
+
from mapping import FILE_URL_MAPPING
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
langchain.llm_cache = InMemoryCache()
|
24 |
|
|
|
|
|
25 |
models = ["GPT-3.5", "Flan UL2", "GPT-4", "Flan T5"]
|
26 |
|
27 |
pickle_file = "_vs.pkl"
|
28 |
index_file = "_vs.index"
|
29 |
models_folder = "models/"
|
30 |
+
MONGO_DB_URL = os.environ['MONGO_DB_URL']
|
31 |
|
32 |
llm = ChatOpenAI(model_name="gpt-4", temperature=0.1)
|
33 |
|
34 |
embeddings = OpenAIEmbeddings(model='text-embedding-ada-002')
|
35 |
|
36 |
+
message_history = CustomMongoDBChatMessageHistory(
|
37 |
+
connection_string=MONGO_DB_URL, session_id='session_id', database_name='coursera_bots',
|
38 |
+
collection_name='3d_printing_revolution'
|
39 |
+
)
|
40 |
|
41 |
memory = ConversationBufferWindowMemory(memory_key="chat_history", k=10)
|
42 |
|
|
|
55 |
CHAT_PROMPT = ChatPromptTemplate.from_messages(messages)
|
56 |
|
57 |
|
58 |
+
def set_session_id(session_id):
|
59 |
+
global message_history, memory
|
60 |
+
# check if message_history with same session id exists
|
61 |
+
if message_history.session_id == session_id:
|
62 |
+
print("Session id already set: " + str(message_history.session_id))
|
63 |
+
else:
|
64 |
+
# create new message history with session id
|
65 |
+
print("Setting session id to " + str(session_id))
|
66 |
+
message_history = CustomMongoDBChatMessageHistory(
|
67 |
+
connection_string=MONGO_DB_URL, session_id=session_id, database_name='coursera_bots',
|
68 |
+
collection_name='printing_3d_revolution'
|
69 |
+
)
|
70 |
+
memory = ConversationBufferWindowMemory(memory_key="chat_history", chat_memory=message_history, k=10, return_messages=True)
|
71 |
+
|
72 |
+
|
73 |
def set_model_and_embeddings(model):
|
|
|
74 |
set_model(model)
|
75 |
# set_embeddings(model)
|
|
|
76 |
|
77 |
|
78 |
def set_model(model):
|
|
|
159 |
|
160 |
# use file_url_mapping to set metadata of document to url which has been set as the source
|
161 |
for document in document_list:
|
162 |
+
document.metadata["url"] = FILE_URL_MAPPING.get(document.metadata["source"])
|
163 |
|
164 |
print("document list: " + str(len(document_list)))
|
165 |
return document_list
|
|
|
184 |
|
185 |
|
186 |
def get_qa_chain(vectorstore_index):
|
187 |
+
global llm
|
188 |
print(llm)
|
189 |
|
190 |
# embeddings_filter = EmbeddingsFilter(embeddings=embeddings, similarity_threshold=0.76)
|
|
|
193 |
search_kwargs={"score_threshold": .7})
|
194 |
|
195 |
chain = ConversationalRetrievalChain.from_llm(llm, retriever, return_source_documents=True,
|
196 |
+
verbose=True,
|
197 |
combine_docs_chain_kwargs={"prompt": CHAT_PROMPT})
|
198 |
return chain
|
199 |
|
|
|
206 |
|
207 |
|
208 |
def generate_answer(question) -> str:
|
209 |
+
global vectorstore_index
|
210 |
chain = get_qa_chain(vectorstore_index)
|
211 |
+
history = memory.chat_memory.messages
|
212 |
+
print("chat history: " + str(history))
|
213 |
result = chain(
|
214 |
+
{"question": question, "chat_history": history})
|
215 |
+
# chat_tuple = (question, result["answer"])
|
216 |
+
# chat_history.append(chat_tuple)
|
217 |
+
save_chat_history(question, result)
|
218 |
+
chat_history = [message.content for message in memory.chat_memory.messages]
|
219 |
+
print("chat history after response: " + str(chat_history))
|
220 |
sources = []
|
221 |
print(result)
|
222 |
|
|
|
227 |
|
228 |
source = ',\n'.join(set(sources))
|
229 |
return result['answer'] + '\nSOURCES: ' + source
|
230 |
+
|
231 |
+
|
232 |
+
def save_chat_history(question, result):
|
233 |
+
|
234 |
+
memory.chat_memory.add_user_message(question)
|
235 |
+
memory.chat_memory.add_ai_message(result["answer"])
|
236 |
+
print("chat history on saving: " + str(memory.chat_memory.messages))
|