File size: 2,934 Bytes
53d0330
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import uuid
from langchain_openai import ChatOpenAI
from langchain_core.runnables.history import RunnableWithMessageHistory
from langchain_community.chat_message_histories import SQLChatMessageHistory
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from config import settings

Settings = settings



DB_PATH = "chat_history.db"


def get_session_history(session_id):
    return SQLChatMessageHistory(session_id=session_id, connection=f"sqlite:///{DB_PATH}")


def predict(message: str, history: list, session_id: str):

    prompt_template = ChatPromptTemplate.from_messages([
        ("system", Settings.system_prompt),
        MessagesPlaceholder(variable_name="history"),
        ("user", "{input}")
    ])

    chat_history = get_session_history(session_id)
    llm = ChatOpenAI(model="gpt-4o", temperature=0.7)

    runnable = RunnableWithMessageHistory(
        runnable=prompt_template | llm,
        get_session_history=get_session_history,
        input_messages_key="input",
        history_messages_key="history",
    )


    input_data = {
        "input": message,
    }

    # The runnable will automatically handle history
    response = runnable.invoke(input_data, config={"configurable": {"session_id": session_id}})

    if hasattr(response, "content"):
        return response.content
    else:
        raise TypeError("The returned response object does not have a 'content' attribute.")

with gr.Blocks(theme=gr.themes.Default(primary_hue="indigo", secondary_hue="orange")) as demo:
    session_id = gr.State(str(uuid.uuid4()))

    chat_interface = gr.ChatInterface(
        fn=predict,
        type="messages",
        additional_inputs=[session_id],
        title="💬 IBHS Operating System Alignment Coach!",
        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.",
        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."],
                  ["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."],
                  ["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."]]
    )

if __name__ == "__main__":
    demo.launch()