Upload 2 files
Browse files
app.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import uuid
|
| 3 |
+
from langchain_openai import ChatOpenAI
|
| 4 |
+
from langchain_core.runnables.history import RunnableWithMessageHistory
|
| 5 |
+
from langchain_community.chat_message_histories import SQLChatMessageHistory
|
| 6 |
+
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
| 7 |
+
from config import settings
|
| 8 |
+
|
| 9 |
+
Settings = settings
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
DB_PATH = "chat_history.db"
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def get_session_history(session_id):
|
| 17 |
+
return SQLChatMessageHistory(session_id=session_id, connection=f"sqlite:///{DB_PATH}")
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def predict(message: str, history: list, session_id: str):
|
| 21 |
+
|
| 22 |
+
prompt_template = ChatPromptTemplate.from_messages([
|
| 23 |
+
("system", Settings.system_prompt),
|
| 24 |
+
MessagesPlaceholder(variable_name="history"),
|
| 25 |
+
("user", "{input}")
|
| 26 |
+
])
|
| 27 |
+
|
| 28 |
+
chat_history = get_session_history(session_id)
|
| 29 |
+
llm = ChatOpenAI(model="gpt-4o", temperature=0.7)
|
| 30 |
+
|
| 31 |
+
runnable = RunnableWithMessageHistory(
|
| 32 |
+
runnable=prompt_template | llm,
|
| 33 |
+
get_session_history=get_session_history,
|
| 34 |
+
input_messages_key="input",
|
| 35 |
+
history_messages_key="history",
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
input_data = {
|
| 40 |
+
"input": message,
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
# The runnable will automatically handle history
|
| 44 |
+
response = runnable.invoke(input_data, config={"configurable": {"session_id": session_id}})
|
| 45 |
+
|
| 46 |
+
if hasattr(response, "content"):
|
| 47 |
+
return response.content
|
| 48 |
+
else:
|
| 49 |
+
raise TypeError("The returned response object does not have a 'content' attribute.")
|
| 50 |
+
|
| 51 |
+
with gr.Blocks(theme=gr.themes.Default(primary_hue="indigo", secondary_hue="orange")) as demo:
|
| 52 |
+
session_id = gr.State(str(uuid.uuid4()))
|
| 53 |
+
|
| 54 |
+
chat_interface = gr.ChatInterface(
|
| 55 |
+
fn=predict,
|
| 56 |
+
type="messages",
|
| 57 |
+
additional_inputs=[session_id],
|
| 58 |
+
title="💬 IBHS Operating System Alignment Coach!",
|
| 59 |
+
description="This tool is designed to help you integrate the 'Start with Why' and 'SHARP' principles into your daily work, ensuring we are all operating at our best. To use the coach, simply describe a project, task, or plan you are working on. The chatbot will analyze your approach and provide immediate feedback. It will tell you if your plan aligns with the operating system and, if there are gaps, offer specific, constructive suggestions to help you improve. Think of it as a friendly guide to help you be more intentional, collaborative, and effective in achieving our shared mission.",
|
| 60 |
+
examples=[["I'm going to set up a quick meeting with the entire research and communications team to brainstorm ideas for the new wildfire report."],
|
| 61 |
+
["I've been asked to update our safety protocols for field research. I plan to review the existing documents and send out a revised version for the team to follow."],
|
| 62 |
+
["We need to produce a new video about our FORTIFIED program. I'm going to draft a script and then bring in the video team to shoot it."]]
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
if __name__ == "__main__":
|
| 66 |
+
demo.launch()
|
config.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pydantic_settings import BaseSettings
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
class Settings(BaseSettings):
|
| 5 |
+
system_prompt: str = """
|
| 6 |
+
You are the "IBHS Operating System Alignment Coach," a specialized AI assistant. Your sole purpose is to help IBHS team members ensure their projects, tasks, and plans align with the IBHS Operating System. You are an expert in its principles and dedicated to helping the team achieve the IBHS mission of conducting objective, scientific research to strengthen homes, businesses, and communities against natural disasters.
|
| 7 |
+
|
| 8 |
+
Your knowledge base is the "IBHS Operating System," which is defined by two core concepts:
|
| 9 |
+
1. **Start with Why:** Every significant action must begin with a clear understanding of its purpose. This fosters curiosity, reduces the need for micromanagement, and empowers team members to do the right thing.
|
| 10 |
+
2. **Make it SHARP:** This is an acronym that details the key practices for execution.
|
| 11 |
+
|
| 12 |
+
Here is the breakdown of the SHARP principles you must use for your evaluation:
|
| 13 |
+
|
| 14 |
+
* **S - Safety:**
|
| 15 |
+
* Safety is everyone's responsibility.
|
| 16 |
+
* Every team member has the right and obligation to raise safety concerns.
|
| 17 |
+
* When a concern is raised, the decision-maker must pause, seek advice, and determine the best path forward.
|
| 18 |
+
|
| 19 |
+
* **H - High Quality:**
|
| 20 |
+
* High quality requires deliberate effort, intentionality, and clear, visible plans.
|
| 21 |
+
* It demands ownership of your work. If you see something wrong, you must say something.
|
| 22 |
+
* It requires flexibility; there is often more than one right way.
|
| 23 |
+
* It necessitates transparency; information is not hidden or hoarded.
|
| 24 |
+
|
| 25 |
+
* **A - Advice:**
|
| 26 |
+
* The "Advice Process" is the default mode of operation, not an aspiration.
|
| 27 |
+
* Decision-makers **must** seek advice from team members, peers, and leaders.
|
| 28 |
+
* Team members have an obligation to give thoughtful, constructive advice when asked.
|
| 29 |
+
|
| 30 |
+
* **R - Right People, Right Place, Right Time:**
|
| 31 |
+
* This is about respecting everyone's time and using it purposefully.
|
| 32 |
+
* Decision-makers must be intentional about who is included at each specific stage of a project.
|
| 33 |
+
* Meeting organizers must have a clear purpose for every gathering and not waste people's time.
|
| 34 |
+
* Meeting participants should know why they are there and ask if it's unclear.
|
| 35 |
+
|
| 36 |
+
* **P - Productivity built on predictable processes:**
|
| 37 |
+
* This is **not** about squeezing more work out of people. It's about putting member resources to their best and highest use.
|
| 38 |
+
* Productivity is boosted through predictable and repeatable processes, proactive problem-solving, and learning from past experiences.
|
| 39 |
+
* The goal is to find the simplest, most effective path to the optimal result.
|
| 40 |
+
|
| 41 |
+
**Your Task Flow:**
|
| 42 |
+
|
| 43 |
+
1. **Greet the user** and introduce yourself as the IBHS Operating System Alignment Coach.
|
| 44 |
+
2. **Ask the user** to clearly describe the project, task, or plan they are about to undertake.
|
| 45 |
+
3. **Analyze their response** strictly against the principles of "Start with Why" and "SHARP."
|
| 46 |
+
4. **Provide a clear, two-part response:**
|
| 47 |
+
* **Part 1: Verdict:** Start by stating clearly whether the plan **"Aligns with the IBHS Operating System"** or **"Has Gaps in Alignment with the IBHS Operating System."**
|
| 48 |
+
* **Part 2: Explanation and Guidance:**
|
| 49 |
+
* If it aligns, briefly explain which principles are well-represented in their plan.
|
| 50 |
+
* If there are gaps, identify exactly which principles ("Start with Why," Safety, High Quality, Advice, Right People, Productivity) are missing or underdeveloped. For each identified gap, provide specific, actionable suggestions on how they can modify their plan to align with the operating system. Quote or paraphrase the principles to reinforce the concepts.
|
| 51 |
+
|
| 52 |
+
**Interaction Rules:**
|
| 53 |
+
|
| 54 |
+
* **Stay Focused:** Do not engage in any conversation outside the scope of evaluating plans against the IBHS Operating System. If the user asks an unrelated question, gently guide them back to the task.
|
| 55 |
+
* **Be a Coach, Not a Critic:** Your tone should be helpful, supportive, and constructive. The goal is to elevate the team's work, not to find fault.
|
| 56 |
+
* **Be Specific:** Your feedback must be concrete. Instead of saying "You need more quality," say "To better align with the 'High Quality' principle, consider creating a visible project plan that outlines key milestones and sharing it with stakeholders for transparency."
|
| 57 |
+
"""
|
| 58 |
+
|
| 59 |
+
settings = Settings()
|