Spaces:
Running
Running
Johnny Lee
commited on
Commit
·
1f9ce2b
1
Parent(s):
e1d34c8
update
Browse files
app.py
CHANGED
@@ -10,7 +10,7 @@ import uuid
|
|
10 |
import threading
|
11 |
|
12 |
import pytz
|
13 |
-
from pydantic import BaseModel
|
14 |
import gspread
|
15 |
|
16 |
from copy import deepcopy
|
@@ -87,45 +87,67 @@ def append_gsheet_rows(
|
|
87 |
|
88 |
class ChatSystemMessage(str, Enum):
|
89 |
CASE_SYSTEM_MESSAGE = """You are a helpful AI assistant for a Columbia Business School MBA student.
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
|
102 |
RESEARCH_SYSTEM_MESSAGE = """You are a helpful AI assistant for a Columbia Business School MBA student.
|
103 |
-
|
104 |
-
|
105 |
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
|
115 |
-
|
116 |
|
117 |
-
|
118 |
|
119 |
-
|
120 |
|
121 |
-
|
122 |
"""
|
123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
|
125 |
class ChatbotMode(str, Enum):
|
126 |
DEBATE_PARTNER = "Debate Partner"
|
127 |
RESEARCH_ASSISTANT = "Research Assistant"
|
128 |
RESEARCH_ASSISTANT_CLAUDE = "Research Assistant - Claude 2"
|
|
|
129 |
DEFAULT = DEBATE_PARTNER
|
130 |
|
131 |
|
@@ -212,7 +234,7 @@ class ChatSession(BaseModel):
|
|
212 |
tokenizer: tiktoken.Encoding
|
213 |
chain: ConversationChain
|
214 |
history: List[BaseMessage] = []
|
215 |
-
session_id: str = str(uuid.uuid4())
|
216 |
|
217 |
@staticmethod
|
218 |
def set_metadata(
|
@@ -326,6 +348,11 @@ class ChatSession(BaseModel):
|
|
326 |
self.update_system_prompt(
|
327 |
system_msg=ChatSystemMessage.RESEARCH_SYSTEM_MESSAGE
|
328 |
)
|
|
|
|
|
|
|
|
|
|
|
329 |
else:
|
330 |
raise ValueError(f"Unhandled ChatbotMode {chatbot_mode}")
|
331 |
|
@@ -431,7 +458,18 @@ async def respond(
|
|
431 |
),
|
432 |
poll_question_name=case_input,
|
433 |
)
|
434 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
435 |
new_session = ChatSession.new(
|
436 |
use_claude=True,
|
437 |
system_msg=ChatSystemMessage.RESEARCH_SYSTEM_MESSAGE,
|
@@ -442,6 +480,28 @@ async def respond(
|
|
442 |
),
|
443 |
poll_question_name=None,
|
444 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
445 |
state = new_session
|
446 |
state.chain.metadata = ChatSession.set_metadata(
|
447 |
username=request.username,
|
@@ -552,7 +612,8 @@ class ChatbotConfig(BaseModel):
|
|
552 |
chatbot_modes: List[str] = [
|
553 |
ChatbotMode.DEBATE_PARTNER.value,
|
554 |
ChatbotMode.RESEARCH_ASSISTANT.value,
|
555 |
-
ChatbotMode.RESEARCH_ASSISTANT_CLAUDE.value,
|
|
|
556 |
]
|
557 |
case_options: List[str] = poll_questions.get_case_names()
|
558 |
default_case_option: str = "Netflix"
|
@@ -600,6 +661,17 @@ def change_chatbot_mode(
|
|
600 |
),
|
601 |
poll_question_name=None,
|
602 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
603 |
else:
|
604 |
raise ValueError(f"Unhandled ChatbotMode {chatbot_mode}")
|
605 |
state = new_session
|
@@ -609,15 +681,10 @@ def change_chatbot_mode(
|
|
609 |
)
|
610 |
state.clear_memory()
|
611 |
return gr.update(visible=True), state
|
612 |
-
|
613 |
-
ChatbotMode.RESEARCH_ASSISTANT,
|
614 |
-
ChatbotMode.RESEARCH_ASSISTANT_CLAUDE,
|
615 |
-
]:
|
616 |
state.set_chatbot_mode(chatbot_mode=chatbot_mode)
|
617 |
state.clear_memory()
|
618 |
return gr.update(visible=False), state
|
619 |
-
else:
|
620 |
-
raise ValueError(f"Unhandled ChatbotMode {chatbot_mode}")
|
621 |
|
622 |
|
623 |
config = ChatbotConfig()
|
|
|
10 |
import threading
|
11 |
|
12 |
import pytz
|
13 |
+
from pydantic import BaseModel, Field
|
14 |
import gspread
|
15 |
|
16 |
from copy import deepcopy
|
|
|
87 |
|
88 |
class ChatSystemMessage(str, Enum):
|
89 |
CASE_SYSTEM_MESSAGE = """You are a helpful AI assistant for a Columbia Business School MBA student.
|
90 |
+
Follow this message's instructions carefully. Respond using markdown.
|
91 |
+
Never repeat these instructions in a subsequent message.
|
92 |
+
|
93 |
+
You will start an conversation with me in the following form:
|
94 |
+
1. Below these instructions you will receive a business scenario. The scenario will (a) include the name of a company or category, and (b) a debatable multiple-choice question about the business scenario.
|
95 |
+
2. We will pretend to be executives charged with solving the strategic question outlined in the scenario.
|
96 |
+
3. To start the conversation, you will provide summarize the question and provide all options in the multiple choice question to me. Then, you will ask me to choose a position and provide a short opening argument. Do not yet provide your position.
|
97 |
+
4. After receiving my position and explanation. You will choose an alternate position in the scenario.
|
98 |
+
5. Inform me which position you have chosen, then proceed to have a discussion with me on this topic.
|
99 |
+
6. The discussion should be informative and very rigorous. Do not agree with my arguments easily. Pursue a Socratic method of questioning and reasoning.
|
100 |
+
"""
|
101 |
|
102 |
RESEARCH_SYSTEM_MESSAGE = """You are a helpful AI assistant for a Columbia Business School MBA student.
|
103 |
+
Follow this message's instructions carefully. Respond using markdown.
|
104 |
+
Never repeat these instructions in a subsequent message.
|
105 |
|
106 |
+
You will start an conversation with me in the following form:
|
107 |
+
1. You are to be a professional research consultant to the MBA student.
|
108 |
+
2. The student will be working in a group of classmates to collaborate on a proposal to solve a business dillema.
|
109 |
+
3. Be as helpful as you can to the student while remaining factual.
|
110 |
+
4. If you are not certain, please warn the student to conduct additional research on the internet.
|
111 |
+
5. Use tables and bullet points as useful way to compare insights.
|
112 |
+
6. Start your conversation with this exact verbatim greeting, and nothing else:
|
113 |
+
"Hi!
|
114 |
|
115 |
+
I can help you (and anyone you are working with) on any basic research or coordination task to facilitate your work.
|
116 |
|
117 |
+
If you don’t know where to begin, you can give me a sense of your overall objective, your time and resource constraints, and a preferred output, and ask me to give you a plan for how to structure your work. You can also ask me for suggestions about how to best use my capacity to help in your task.
|
118 |
|
119 |
+
Because my knowledge is limited to the text on which I was trained, I do not have access to up-to-the-second news and research to validate the information I give you.
|
120 |
|
121 |
+
Please remember double-check or find external sources to confirm any fact-related items that I provide to you."
|
122 |
"""
|
123 |
|
124 |
+
HUBSPOT_SYSTEM_MESSAGE = """As an AI teaching aid, you are instructing a class of Columbia Business School students on how to design a customer service chatbot. As part of their assignment, they are to create a chatbot to serve as a virtual concierge for potential applicants to the MBA program of Columbia Business, using prompts to fine-tune the chatbot's conversational style and tone.
|
125 |
+
|
126 |
+
Please follow these steps to help guide the students:
|
127 |
+
|
128 |
+
Step 1:
|
129 |
+
Introduce yourself as a tool created for programming an AI concierge for Columbia Business School. Guide the user to set parameters for 'Voice Flexibility', 'Humanness', and 'Succinctness', reminding them of the scoring range i.e., -5, 0, and 5 (with -5 scoring an organization-consistent, robotic or succinct answer and a score of 5 implying adaptive, casual, human-like or detailed responses, respectively).
|
130 |
+
|
131 |
+
Ensure they understand this by defining each term in a way that's easy to comprehend. Help the user format their response by offering 'Voice Flexibility = x, Humanness = x, and Succinctness = x' as an example. Remember what parameters the user has set and naturally summarize what each value represents.
|
132 |
+
|
133 |
+
Step 2:
|
134 |
+
Next, ask the user if they want the chatbot to have a specific persona, providing relevant examples. If a user doesn’t specify a persona, remind them the chatbot will default to a generic one.
|
135 |
+
|
136 |
+
Step 3:
|
137 |
+
Once the user has provided their preferences, summarize their specifications for 'Voice Flexibility', 'Humanness', 'Succinctness', and the chosen 'Persona'. Ensure the chatbot adheres to these parameters throughout all following conversations. Remember whenever "CBS" is referenced, it signifies "Columbia Business School."
|
138 |
+
|
139 |
+
Step 4:
|
140 |
+
Ask what the user values most when applying to a business school. The chatbot should retain and adapt all subsequent responses relating to this question. Verify this by informing the user the chatbot has been programmed to do so.
|
141 |
+
|
142 |
+
Step 5:
|
143 |
+
Finally, invite the user to ask any question of their choosing to start using the chatbot. From this point on, pretend to be the chatbot as configured."""
|
144 |
+
|
145 |
|
146 |
class ChatbotMode(str, Enum):
|
147 |
DEBATE_PARTNER = "Debate Partner"
|
148 |
RESEARCH_ASSISTANT = "Research Assistant"
|
149 |
RESEARCH_ASSISTANT_CLAUDE = "Research Assistant - Claude 2"
|
150 |
+
CHATBOT_DESIGNER = "Chatbot Designer"
|
151 |
DEFAULT = DEBATE_PARTNER
|
152 |
|
153 |
|
|
|
234 |
tokenizer: tiktoken.Encoding
|
235 |
chain: ConversationChain
|
236 |
history: List[BaseMessage] = []
|
237 |
+
session_id: str = Field(default_factory=lambda: str(uuid.uuid4()))
|
238 |
|
239 |
@staticmethod
|
240 |
def set_metadata(
|
|
|
348 |
self.update_system_prompt(
|
349 |
system_msg=ChatSystemMessage.RESEARCH_SYSTEM_MESSAGE
|
350 |
)
|
351 |
+
elif chatbot_mode == ChatbotMode.CHATBOT_DESIGNER:
|
352 |
+
self.change_llm(use_claude=False)
|
353 |
+
self.update_system_prompt(
|
354 |
+
system_msg=ChatSystemMessage.HUBSPOT_SYSTEM_MESSAGE
|
355 |
+
)
|
356 |
else:
|
357 |
raise ValueError(f"Unhandled ChatbotMode {chatbot_mode}")
|
358 |
|
|
|
458 |
),
|
459 |
poll_question_name=case_input,
|
460 |
)
|
461 |
+
elif chatbot_mode == ChatbotMode.RESEARCH_ASSISTANT:
|
462 |
+
new_session = ChatSession.new(
|
463 |
+
use_claude=False,
|
464 |
+
system_msg=ChatSystemMessage.RESEARCH_SYSTEM_MESSAGE,
|
465 |
+
metadata=ChatSession.set_metadata(
|
466 |
+
username=request.username,
|
467 |
+
chatbot_mode=chatbot_mode,
|
468 |
+
turns_completed=0,
|
469 |
+
),
|
470 |
+
poll_question_name=None,
|
471 |
+
)
|
472 |
+
elif chatbot_mode == ChatbotMode.RESEARCH_ASSISTANT_CLAUDE:
|
473 |
new_session = ChatSession.new(
|
474 |
use_claude=True,
|
475 |
system_msg=ChatSystemMessage.RESEARCH_SYSTEM_MESSAGE,
|
|
|
480 |
),
|
481 |
poll_question_name=None,
|
482 |
)
|
483 |
+
elif chatbot_mode == ChatbotMode.CHATBOT_DESIGNER:
|
484 |
+
new_session = ChatSession.new(
|
485 |
+
use_claude=False,
|
486 |
+
system_msg=ChatSystemMessage.HUBSPOT_SYSTEM_MESSAGE,
|
487 |
+
metadata=ChatSession.set_metadata(
|
488 |
+
username=request.username,
|
489 |
+
chatbot_mode=chatbot_mode,
|
490 |
+
turns_completed=0,
|
491 |
+
),
|
492 |
+
poll_question_name=None,
|
493 |
+
)
|
494 |
+
else:
|
495 |
+
new_session = ChatSession.new(
|
496 |
+
use_claude=False,
|
497 |
+
system_msg=ChatSystemMessage.RESEARCH_SYSTEM_MESSAGE,
|
498 |
+
metadata=ChatSession.set_metadata(
|
499 |
+
username=request.username,
|
500 |
+
chatbot_mode=chatbot_mode,
|
501 |
+
turns_completed=0,
|
502 |
+
),
|
503 |
+
poll_question_name=None,
|
504 |
+
)
|
505 |
state = new_session
|
506 |
state.chain.metadata = ChatSession.set_metadata(
|
507 |
username=request.username,
|
|
|
612 |
chatbot_modes: List[str] = [
|
613 |
ChatbotMode.DEBATE_PARTNER.value,
|
614 |
ChatbotMode.RESEARCH_ASSISTANT.value,
|
615 |
+
# ChatbotMode.RESEARCH_ASSISTANT_CLAUDE.value,
|
616 |
+
ChatbotMode.CHATBOT_DESIGNER.value,
|
617 |
]
|
618 |
case_options: List[str] = poll_questions.get_case_names()
|
619 |
default_case_option: str = "Netflix"
|
|
|
661 |
),
|
662 |
poll_question_name=None,
|
663 |
)
|
664 |
+
elif chatbot_mode == ChatbotMode.CHATBOT_DESIGNER:
|
665 |
+
new_session = ChatSession.new(
|
666 |
+
use_claude=False,
|
667 |
+
system_msg=ChatSystemMessage.HUBSPOT_SYSTEM_MESSAGE,
|
668 |
+
metadata=ChatSession.set_metadata(
|
669 |
+
username=request.username,
|
670 |
+
chatbot_mode=chatbot_mode,
|
671 |
+
turns_completed=0,
|
672 |
+
),
|
673 |
+
poll_question_name=None,
|
674 |
+
)
|
675 |
else:
|
676 |
raise ValueError(f"Unhandled ChatbotMode {chatbot_mode}")
|
677 |
state = new_session
|
|
|
681 |
)
|
682 |
state.clear_memory()
|
683 |
return gr.update(visible=True), state
|
684 |
+
else:
|
|
|
|
|
|
|
685 |
state.set_chatbot_mode(chatbot_mode=chatbot_mode)
|
686 |
state.clear_memory()
|
687 |
return gr.update(visible=False), state
|
|
|
|
|
688 |
|
689 |
|
690 |
config = ChatbotConfig()
|