XThomasBU commited on
Commit
7f989d6
1 Parent(s): f1da7ee

updated chainlit and literalai, added eval_code

Browse files
.gitignore CHANGED
@@ -167,3 +167,5 @@ cython_debug/
167
  */.chainlit/translations/*
168
  storage/logs/*
169
  vectorstores/*
 
 
 
167
  */.chainlit/translations/*
168
  storage/logs/*
169
  vectorstores/*
170
+
171
+ */.files/*
README.md CHANGED
@@ -68,3 +68,17 @@ vectorstores/ # Stores the created vector databases
68
  - `code/modules/vectorstore/vectorstore.py`: Instantiates the `VectorStore` class to create the vector database.
69
  - `code/modules/vectorstore/store_manager.py`: Instantiates the `VectorStoreManager:` class to manage the vector database, and all associated methods.
70
  - `code/modules/retriever/retriever.py`: Instantiates the `Retriever` class to create the retriever.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  - `code/modules/vectorstore/vectorstore.py`: Instantiates the `VectorStore` class to create the vector database.
69
  - `code/modules/vectorstore/store_manager.py`: Instantiates the `VectorStoreManager:` class to manage the vector database, and all associated methods.
70
  - `code/modules/retriever/retriever.py`: Instantiates the `Retriever` class to create the retriever.
71
+
72
+
73
+ ## Docker
74
+
75
+ The HuggingFace Space is built using the `Dockerfile` in the repository. To run it locally, use the `Dockerfile.dev` file.
76
+
77
+ ```bash
78
+ docker build --tag dev -f Dockerfile.dev .
79
+ docker run -it --rm -p 8051:8051 dev
80
+ ```
81
+
82
+ ## Contributing
83
+
84
+ Please create an issue if you have any suggestions or improvements, and start working on it by creating a branch and by making a pull request to the main branch.
code/.chainlit/config.toml CHANGED
@@ -19,9 +19,6 @@ allow_origins = ["*"]
19
  # follow_symlink = false
20
 
21
  [features]
22
- # Show the prompt playground
23
- prompt_playground = true
24
-
25
  # Process and display HTML in messages. This can be a security risk (see https://stackoverflow.com/questions/19603097/why-is-it-dangerous-to-render-user-generated-html-or-javascript)
26
  unsafe_allow_html = false
27
 
@@ -53,26 +50,20 @@ auto_tag_thread = true
53
  sample_rate = 44100
54
 
55
  [UI]
56
- # Name of the app and chatbot.
57
  name = "AI Tutor"
58
 
59
- # Show the readme while the thread is empty.
60
- show_readme_as_default = true
61
-
62
- # Description of the app and chatbot. This is used for HTML tags.
63
- # description = "AI Tutor - DS598"
64
 
65
  # Large size content are by default collapsed for a cleaner ui
66
  default_collapse_content = true
67
 
68
- # The default value for the expand messages settings.
69
- default_expand_messages = false
70
-
71
  # Hide the chain of thought details from the user in the UI.
72
- hide_cot = false
73
 
74
  # Link to your github repo. This will add a github button in the UI's header.
75
- # github = ""
76
 
77
  # Specify a CSS file that can be used to customize the user interface.
78
  # The CSS file can be served from the public directory or via an external link.
@@ -86,7 +77,7 @@ custom_css = "/public/test.css"
86
  # custom_font = "https://fonts.googleapis.com/css2?family=Inter:wght@400;500;700&display=swap"
87
 
88
  # Specify a custom meta image url.
89
- # custom_meta_image_url = "https://chainlit-cloud.s3.eu-west-3.amazonaws.com/logo/chainlit_banner.png"
90
 
91
  # Specify a custom build directory for the frontend.
92
  # This can be used to customize the frontend code.
@@ -94,18 +85,21 @@ custom_css = "/public/test.css"
94
  # custom_build = "./public/build"
95
 
96
  [UI.theme]
 
97
  #layout = "wide"
98
  #font_family = "Inter, sans-serif"
99
  # Override default MUI light theme. (Check theme.ts)
100
  [UI.theme.light]
101
- #background = "#FAFAFA"
102
- #paper = "#FFFFFF"
103
 
104
  [UI.theme.light.primary]
105
- #main = "#F80061"
106
- #dark = "#980039"
107
- #light = "#FFE7EB"
108
-
 
 
109
  # Override default MUI dark theme. (Check theme.ts)
110
  [UI.theme.dark]
111
  background = "#1C1C1C" # Slightly lighter dark background color
@@ -118,4 +112,4 @@ custom_css = "/public/test.css"
118
 
119
 
120
  [meta]
121
- generated_by = "1.1.202"
 
19
  # follow_symlink = false
20
 
21
  [features]
 
 
 
22
  # Process and display HTML in messages. This can be a security risk (see https://stackoverflow.com/questions/19603097/why-is-it-dangerous-to-render-user-generated-html-or-javascript)
23
  unsafe_allow_html = false
24
 
 
50
  sample_rate = 44100
51
 
52
  [UI]
53
+ # Name of the assistant.
54
  name = "AI Tutor"
55
 
56
+ # Description of the assistant. This is used for HTML tags.
57
+ # description = ""
 
 
 
58
 
59
  # Large size content are by default collapsed for a cleaner ui
60
  default_collapse_content = true
61
 
 
 
 
62
  # Hide the chain of thought details from the user in the UI.
63
+ hide_cot = true
64
 
65
  # Link to your github repo. This will add a github button in the UI's header.
66
+ # github = "https://github.com/DL4DS/dl4ds_tutor"
67
 
68
  # Specify a CSS file that can be used to customize the user interface.
69
  # The CSS file can be served from the public directory or via an external link.
 
77
  # custom_font = "https://fonts.googleapis.com/css2?family=Inter:wght@400;500;700&display=swap"
78
 
79
  # Specify a custom meta image url.
80
+ custom_meta_image_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/f/f5/Boston_University_seal.svg/1200px-Boston_University_seal.svg.png"
81
 
82
  # Specify a custom build directory for the frontend.
83
  # This can be used to customize the frontend code.
 
85
  # custom_build = "./public/build"
86
 
87
  [UI.theme]
88
+ default = "light"
89
  #layout = "wide"
90
  #font_family = "Inter, sans-serif"
91
  # Override default MUI light theme. (Check theme.ts)
92
  [UI.theme.light]
93
+ background = "#FAFAFA"
94
+ paper = "#FFFFFF"
95
 
96
  [UI.theme.light.primary]
97
+ main = "#b22222" # Brighter shade of red
98
+ dark = "#8b0000" # Darker shade of the brighter red
99
+ light = "#ff6347" # Lighter shade of the brighter red
100
+ [UI.theme.light.text]
101
+ primary = "#212121"
102
+ secondary = "#616161"
103
  # Override default MUI dark theme. (Check theme.ts)
104
  [UI.theme.dark]
105
  background = "#1C1C1C" # Slightly lighter dark background color
 
112
 
113
 
114
  [meta]
115
+ generated_by = "1.1.302"
code/main.py CHANGED
@@ -28,6 +28,32 @@ console_handler.setFormatter(formatter)
28
  logger.addHandler(console_handler)
29
 
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  # Adding option to select the chat profile
32
  @cl.set_chat_profiles
33
  async def chat_profile():
@@ -101,13 +127,13 @@ async def start():
101
  llm_tutor = LLMTutor(config, logger=logger)
102
 
103
  chain = llm_tutor.qa_bot()
104
- msg = cl.Message(content=f"Starting the bot {chat_profile}...")
105
- await msg.send()
106
- msg.content = opening_message
107
- await msg.update()
108
 
109
  tags = [chat_profile, config["vectorstore"]["db_option"]]
110
- chat_processor = ChatProcessor(config["chat_logging"]["platform"], tags=tags)
111
  cl.user_session.set("chain", chain)
112
  cl.user_session.set("counter", 0)
113
  cl.user_session.set("chat_processor", chat_processor)
 
28
  logger.addHandler(console_handler)
29
 
30
 
31
+ @cl.set_starters
32
+ async def set_starters():
33
+ return [
34
+ cl.Starter(
35
+ label="recording on CNNs?",
36
+ message="Where can I find the recording for the lecture on Transfromers?",
37
+ icon="/public/adv-screen-recorder-svgrepo-com.svg",
38
+ ),
39
+ cl.Starter(
40
+ label="where's the slides?",
41
+ message="When are the lectures? I can't find the schedule.",
42
+ icon="/public/alarmy-svgrepo-com.svg",
43
+ ),
44
+ cl.Starter(
45
+ label="Due Date?",
46
+ message="When is the final project due?",
47
+ icon="/public/calendar-samsung-17-svgrepo-com.svg",
48
+ ),
49
+ cl.Starter(
50
+ label="Explain backprop.",
51
+ message="I didnt understand the math behind backprop, could you explain it?",
52
+ icon="/public/acastusphoton-svgrepo-com.svg",
53
+ ),
54
+ ]
55
+
56
+
57
  # Adding option to select the chat profile
58
  @cl.set_chat_profiles
59
  async def chat_profile():
 
127
  llm_tutor = LLMTutor(config, logger=logger)
128
 
129
  chain = llm_tutor.qa_bot()
130
+ # msg = cl.Message(content=f"Starting the bot {chat_profile}...")
131
+ # await msg.send()
132
+ # msg.content = opening_message
133
+ # await msg.update()
134
 
135
  tags = [chat_profile, config["vectorstore"]["db_option"]]
136
+ chat_processor = ChatProcessor(config, tags=tags)
137
  cl.user_session.set("chain", chain)
138
  cl.user_session.set("counter", 0)
139
  cl.user_session.set("chat_processor", chat_processor)
code/modules/chat/llm_tutor.py CHANGED
@@ -62,13 +62,16 @@ class CustomConversationalRetrievalChain(ConversationalRetrievalChain):
62
  # question=question, chat_history=chat_history_str, callbacks=callbacks
63
  # )
64
  system = (
65
- "You are an AI Tutor helping a student. Your task is to rephrase the student's question to provide more context from their chat history (only if relevant), ensuring the rephrased question still reflects the student's point of view. "
66
- "The rephrased question should incorporate relevant details from the chat history to make it clearer and more specific. It should also expand upon the original question to provide more context on only what the student provided."
67
- "Always end the rephrased question with the original question in parentheses for reference. "
68
- "Do not change the meaning of the question, and keep the tone and perspective as if it were asked by the student. "
69
- "Here is the chat history for context: \n{chat_history_str}\n"
70
- "Now, rephrase the following question: '{question}'"
 
 
71
  )
 
72
  prompt = ChatPromptTemplate.from_messages(
73
  [
74
  ("system", system),
@@ -91,6 +94,7 @@ class CustomConversationalRetrievalChain(ConversationalRetrievalChain):
91
  docs = await self._aget_docs(new_question, inputs) # type: ignore[call-arg]
92
 
93
  output: Dict[str, Any] = {}
 
94
  if self.response_if_no_docs_found is not None and len(docs) == 0:
95
  output[self.output_key] = self.response_if_no_docs_found
96
  else:
@@ -107,23 +111,20 @@ class CustomConversationalRetrievalChain(ConversationalRetrievalChain):
107
  ]
108
  )
109
  final_prompt = (
110
- "You are an AI Tutor for the course DS598, taught by Prof. Thomas Gardos. "
111
- "Use the following pieces of information to answer the user's question. "
112
- "If you don't know the answer, try your best, but don't try to make up an answer. Keep the flow of the conversation going. "
113
- "Use the chat history just as a gist to answer the question only if it's relevant; otherwise, ignore it. Do not repeat responses in the history. Use the context as a guide to construct your answer. The context for the answer will be under 'Document context:'. Remember, the conext may include text not directly related to the question."
114
- "Make sure to use the source_file field in metadata from each document to provide links to the user to the correct sources. "
115
- "The context is ordered by relevance to the question. "
116
- "Talk in a friendly and personalized manner, similar to how you would speak to a friend who needs help. Make the conversation engaging and avoid sounding repetitive or robotic.\n\n"
117
  f"Chat History:\n{chat_history_str}\n\n"
118
  f"Context:\n{context}\n\n"
119
- f"Student: {new_question}\n"
120
- "Anwer the student's question in a friendly, concise, and engaging manner.\n"
121
  "AI Tutor:"
122
  )
123
 
124
- new_inputs["input"] = final_prompt
125
  new_inputs["question"] = final_prompt
126
- output["final_prompt"] = final_prompt
127
 
128
  answer = await self.combine_docs_chain.arun(
129
  input_documents=docs, callbacks=_run_manager.get_child(), **new_inputs
@@ -132,8 +133,7 @@ class CustomConversationalRetrievalChain(ConversationalRetrievalChain):
132
 
133
  if self.return_source_documents:
134
  output["source_documents"] = docs
135
- if self.return_generated_question:
136
- output["generated_question"] = new_question
137
  return output
138
 
139
 
 
62
  # question=question, chat_history=chat_history_str, callbacks=callbacks
63
  # )
64
  system = (
65
+ "You are someone that rephrases statements. Rephrase the student's question to add context from their chat history if relevant, ensuring it remains from the student's point of view. "
66
+ "Incorporate relevant details from the chat history to make the question clearer and more specific."
67
+ "Do not change the meaning of the original statement, and maintain the student's tone and perspective. "
68
+ "If the question is conversational and doesn't require context, do not rephrase it. "
69
+ "Example: If the student previously asked about backpropagation in the context of deep learning and now asks 'what is it', rephrase to 'What is backprogatation.'. "
70
+ "Example: Do not rephrase if the user is asking something specific like 'cool, suggest a project with transformers to use as my final project'"
71
+ "Chat history: \n{chat_history_str}\n"
72
+ "Rephrase the following question only if necessary: '{question}'"
73
  )
74
+
75
  prompt = ChatPromptTemplate.from_messages(
76
  [
77
  ("system", system),
 
94
  docs = await self._aget_docs(new_question, inputs) # type: ignore[call-arg]
95
 
96
  output: Dict[str, Any] = {}
97
+ output["original_question"] = question
98
  if self.response_if_no_docs_found is not None and len(docs) == 0:
99
  output[self.output_key] = self.response_if_no_docs_found
100
  else:
 
111
  ]
112
  )
113
  final_prompt = (
114
+ "You are an AI Tutor for the course DS598, taught by Prof. Thomas Gardos. Answer the user's question using the provided context. Only use the context if it is relevant. The context is ordered by relevance."
115
+ "If you don't know the answer, do your best without making things up. Keep the conversation flowing naturally. "
116
+ "Use chat history and context as guides but avoid repeating past responses. Provide links from the source_file metadata. Use the source context that is most relevent."
117
+ "Speak in a friendly and engaging manner, like talking to a friend. Avoid sounding repetitive or robotic.\n\n"
 
 
 
118
  f"Chat History:\n{chat_history_str}\n\n"
119
  f"Context:\n{context}\n\n"
120
+ "Answer the student's question below in a friendly, concise, and engaging manner. Use the context and history only if relevant, otherwise, engage in a free-flowing conversation.\n"
121
+ f"Student: {question}\n"
122
  "AI Tutor:"
123
  )
124
 
125
+ # new_inputs["input"] = final_prompt
126
  new_inputs["question"] = final_prompt
127
+ # output["final_prompt"] = final_prompt
128
 
129
  answer = await self.combine_docs_chain.arun(
130
  input_documents=docs, callbacks=_run_manager.get_child(), **new_inputs
 
133
 
134
  if self.return_source_documents:
135
  output["source_documents"] = docs
136
+ output["rephrased_question"] = new_question
 
137
  return output
138
 
139
 
code/modules/chat_processor/chat_processor.py CHANGED
@@ -2,8 +2,9 @@ from modules.chat_processor.literal_ai import LiteralaiChatProcessor
2
 
3
 
4
  class ChatProcessor:
5
- def __init__(self, chat_processor_type, tags=None):
6
- self.chat_processor_type = chat_processor_type
 
7
  self.tags = tags
8
  self._init_processor()
9
 
@@ -16,10 +17,13 @@ class ChatProcessor:
16
  )
17
 
18
  def _process(self, user_message, assistant_message, source_dict):
19
- self.processor.process(user_message, assistant_message, source_dict)
 
 
 
20
 
21
  async def rag(self, user_query: str, chain, cb):
22
- try:
23
  return await self.processor.rag(user_query, chain, cb)
24
- except:
25
  return await chain.acall(user_query, callbacks=[cb])
 
2
 
3
 
4
  class ChatProcessor:
5
+ def __init__(self, config, tags=None):
6
+ self.chat_processor_type = config["chat_logging"]["platform"]
7
+ self.logging = config["chat_logging"]["log_chat"]
8
  self.tags = tags
9
  self._init_processor()
10
 
 
17
  )
18
 
19
  def _process(self, user_message, assistant_message, source_dict):
20
+ if self.logging:
21
+ return self.processor.process(user_message, assistant_message, source_dict)
22
+ else:
23
+ pass
24
 
25
  async def rag(self, user_query: str, chain, cb):
26
+ if self.logging:
27
  return await self.processor.rag(user_query, chain, cb)
28
+ else:
29
  return await chain.acall(user_query, callbacks=[cb])
code/modules/config/config.yml CHANGED
@@ -34,7 +34,7 @@ llm_params:
34
  temperature: 0.7
35
 
36
  chat_logging:
37
- log_chat: True # bool
38
  platform: 'literalai'
39
 
40
  splitter_options:
 
34
  temperature: 0.7
35
 
36
  chat_logging:
37
+ log_chat: False # bool
38
  platform: 'literalai'
39
 
40
  splitter_options:
code/public/acastusphoton-svgrepo-com.svg ADDED
code/public/adv-screen-recorder-svgrepo-com.svg ADDED
code/public/alarmy-svgrepo-com.svg ADDED
code/public/avatars/ai-tutor.png ADDED
code/public/calendar-samsung-17-svgrepo-com.svg ADDED
code/public/test.css CHANGED
@@ -2,15 +2,32 @@ a[href*='https://github.com/Chainlit/chainlit'] {
2
  visibility: hidden;
3
  }
4
 
5
- .message-avatar .MuiAvatar-root {
6
- background-color: transparent; /* Remove the background color */
7
- color: #FFFFFF; /* Change this to your desired text color */
8
- border: 0.25px solid #FFFFFF; /* Add a white border for the circle */
9
- border-radius: 50%; /* Ensure the avatar remains circular */
10
- background-image: url('http://localhost:8051/logo?theme=dark'); /* Path to your logo */
11
- background-size: cover; /* Ensure the logo covers the entire avatar */
12
- background-position: center; /* Center the logo */
13
- background-repeat: no-repeat; /* Prevent the logo from repeating */
14
- width: 38px; /* Adjust the width as needed */
15
- height: 38px; /* Adjust the height as needed */
16
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  visibility: hidden;
3
  }
4
 
5
+
6
+ /* Target the container of the image and set a custom background image */
7
+ .MuiAvatar-root.MuiAvatar-circular.css-m2icte {
8
+ background-image: url('/public/avatars/ai-tutor.png'); /* Replace with your custom image URL */
9
+ background-size: cover; /* Ensure the image covers the entire container */
10
+ background-position: center; /* Center the image */
11
+ width: 100px; /* Ensure the dimensions match the original */
12
+ height: 100px; /* Ensure the dimensions match the original */
13
+ border-radius: 50%; /* Maintain circular shape */
14
+ }
15
+
16
+ /* Hide the default image */
17
+ .MuiAvatar-root.MuiAvatar-circular.css-m2icte .MuiAvatar-img.css-1hy9t21 {
18
+ display: none;
19
+ }
20
+
21
+ .MuiAvatar-root.MuiAvatar-circular.css-v72an7 {
22
+ background-image: url('/public/avatars/ai-tutor.png'); /* Replace with your custom image URL */
23
+ background-size: cover; /* Ensure the image covers the entire container */
24
+ background-position: center; /* Center the image */
25
+ width: 40px; /* Ensure the dimensions match the original */
26
+ height: 40px; /* Ensure the dimensions match the original */
27
+ border-radius: 50%; /* Maintain circular shape */
28
+ }
29
+
30
+ /* Hide the default image */
31
+ .MuiAvatar-root.MuiAvatar-circular.css-v72an7 .MuiAvatar-img.css-1hy9t21 {
32
+ display: none;
33
+ }
eval_code/true_lens.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
requirements.txt CHANGED
@@ -1,22 +1,25 @@
1
  # Automatically generated by https://github.com/damnever/pigar.
2
 
 
3
  beautifulsoup4==4.12.3
4
- chainlit==1.1.202
5
  langchain==0.1.20
6
  langchain-community==0.0.38
7
  langchain-core==0.1.52
 
8
  llama-parse==0.4.4
 
 
9
  pysrt==1.1.2
10
  python-dotenv==1.0.1
11
  PyYAML==6.0.1
12
  RAGatouille==0.0.8.post2
13
  requests==2.32.3
 
14
  torch==2.3.1
15
  tqdm==4.66.4
16
  transformers==4.41.2
17
- llama-cpp-python==0.2.77
18
- fake_useragent==1.5.1
19
- chromadb==0.5.0
20
- pymupdf==1.24.5
21
- literalai==0.0.601
22
- umap-learn==0.5.6
 
1
  # Automatically generated by https://github.com/damnever/pigar.
2
 
3
+ aiohttp==3.9.5
4
  beautifulsoup4==4.12.3
5
+ chainlit==1.1.302
6
  langchain==0.1.20
7
  langchain-community==0.0.38
8
  langchain-core==0.1.52
9
+ literalai==0.0.604
10
  llama-parse==0.4.4
11
+ numpy==1.26.4
12
+ pandas==2.2.2
13
  pysrt==1.1.2
14
  python-dotenv==1.0.1
15
  PyYAML==6.0.1
16
  RAGatouille==0.0.8.post2
17
  requests==2.32.3
18
+ scikit-learn==1.5.0
19
  torch==2.3.1
20
  tqdm==4.66.4
21
  transformers==4.41.2
22
+ trulens_eval==0.31.0
23
+ umap-learn==0.5.6
24
+ trulens-eval==0.31.0
25
+ llama-cpp-python==0.2.77