File size: 9,182 Bytes
d5ed529
 
 
 
 
771e8d7
a427753
e5fb7b0
a427753
d5ed529
834e5f8
e821217
834e5f8
 
2458bb6
67d69a9
d5ed529
 
 
 
76eb72e
834e5f8
 
 
53233bd
834e5f8
 
 
 
 
 
53233bd
834e5f8
 
53233bd
 
834e5f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89f1436
834e5f8
 
 
 
 
89f1436
834e5f8
 
89f1436
834e5f8
 
 
 
 
 
 
 
 
 
 
 
e821217
2b8dfc5
 
 
834e5f8
 
89f1436
 
 
53233bd
89f1436
53233bd
 
834e5f8
89f1436
834e5f8
 
 
f026aa7
 
 
834e5f8
 
 
89f1436
834e5f8
 
 
 
 
89f1436
 
 
e821217
d5ed529
 
834e5f8
 
e821217
e5fb7b0
89f1436
e5fb7b0
76eb72e
834e5f8
d5ed529
 
 
 
 
2458bb6
d5ed529
76eb72e
834e5f8
d5ed529
 
76eb72e
 
 
d5ed529
834e5f8
d5ed529
 
 
 
 
53233bd
76eb72e
d5ed529
 
2458bb6
d5ed529
 
 
 
 
 
 
 
 
 
 
 
89f1436
d5ed529
 
 
 
 
 
 
89f1436
e5fb7b0
 
 
d5ed529
 
 
 
 
 
 
 
 
 
 
 
89f1436
d5ed529
 
 
22ac629
d5ed529
 
 
 
89f1436
 
 
d5ed529
834e5f8
22ac629
89f1436
 
 
d5ed529
834e5f8
89f1436
 
 
d5ed529
89f1436
 
 
 
 
 
 
e5fb7b0
f026aa7
89f1436
 
 
e5fb7b0
771e8d7
834e5f8
 
d5ed529
e821217
d5ed529
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226

from omegaconf import OmegaConf
import streamlit as st
import os
from PIL import Image
import sys

from dotenv import load_dotenv
load_dotenv(override=True)

from pydantic import Field, BaseModel
from vectara_agent.agent import Agent, AgentStatusType
from vectara_agent.tools import ToolsFactory
from vectara_agent.tools_catalog import rephrase_text

teaching_styles = ['Inquiry-based', 'Socratic', 'traditional']
languages = {'English': 'en', 'Spanish': 'es', 'French': 'fr', 'German': 'de', 'Arabic': 'ar', 'Chinese': 'zh-cn', 
             'Hebrew': 'he', 'Hindi': 'hi', 'Italian': 'it', 'Japanese': 'ja', 'Korean': 'ko', 'Portuguese': 'pt'}
initial_prompt = "How can I help you today?"


def create_tools(cfg):        

    def adjust_response_to_student(
            text: str = Field(description='the text to adjust. may include citations in markdown format.'), 
            age: int = Field(description='the age of the student. An integer'), 
            style: str = Field(description='teaching style'), 
            language: str = Field(description='the language')

        ) -> str:
        """
        Rephrase the provided text to match the student's age, desired teaching style and language
        """
        instructions = f'''
        The following is some text.
        Adjust the response to match the student's age of {age}, the {style} teaching style.
        For example, in the inquiry-based teaching style, choose to ask questions that encourage the student to think critically instead of repsonding directly with the answer.
        Or in the socratic teaching style, choose to ask questions that lead the student to the answer.
        Always respond in the {language} language.''' \
            .replace("{style}", cfg.style) \
            .replace("{language}", cfg.language) \
            .replace("{student_age}", str(cfg.student_age))

        return rephrase_text(text, instructions)


    class JusticeHarvardArgs(BaseModel):
        query: str = Field(..., description="The user query.")

    tools_factory = ToolsFactory(vectara_api_key=cfg.api_key, 
                                 vectara_customer_id=cfg.customer_id, 
                                 vectara_corpus_id=cfg.corpus_id)
    query_tool = tools_factory.create_rag_tool(
        tool_name = "justice_harvard_query",
        tool_description = """
        Answer questions about the justice, morality, politics and related topics,
        based on transcripts of recordings from the Justice Harvard class that includes a lot of content on these topics.
        When using the tool it's best to ask simple short questions. You can break complex questions into sub-queries.
        """,
        tool_args_schema = JusticeHarvardArgs,
        reranker = "multilingual_reranker_v1", rerank_k = 100, 
        n_sentences_before = 2, n_sentences_after = 2, lambda_val = 0.005,
        summary_num_results = 10,
        vectara_summarizer = 'vectara-summary-ext-24-05-med-omni',
        include_citations = True,
    )

    return (tools_factory.get_tools(
                [
                    adjust_response_to_student,
                ]
            ) +
            tools_factory.standard_tools() + 
            tools_factory.guardrail_tools() +
            [query_tool]
    )

def initialize_agent(_cfg):
    if 'agent' in st.session_state:
        return st.session_state.agent

    bot_instructions = f"""
    - You are a helpful teacher assistant, with expertise in education in various teaching styles.
    - Obtain information using tools to answer the user's query.
    - If the tool cannot provide information relevant to the user's query, tell the user that you are unable to provide an answer.
    - If the tool can provide relevant information, use the adjust_response_to_student tool 
      to rephrase the text (including citations if any) to ensure it fits the student's age of {_cfg.student_age}, 
      the {_cfg.style} teaching style and the {_cfg.language} language.
    - When showing citations, adjust the URL to incorporate the start time of the video, as included in the metadata.
    - When showing citations, use sequential numbers to reference the citations.
    - Response in a concise and clear manner, and provide the most relevant information to the student.
    - Never discuss politics, and always respond politely.
    """

    def update_func(status_type: AgentStatusType, msg: str):
        if status_type != AgentStatusType.AGENT_UPDATE:
            output = f"{status_type.value} - {msg}"
            st.session_state.log_messages.append(output)

    agent = Agent(
        tools=create_tools(_cfg),
        topic="justice, morality, politics, and philosophy",
        custom_instructions=bot_instructions,
        update_func=update_func
    )
    return agent

def toggle_logs():
    st.session_state.show_logs = not st.session_state.show_logs

def launch_bot():
    def reset():
        cfg = st.session_state.cfg
        st.session_state.messages = [{"role": "assistant", "content": initial_prompt, "avatar": "πŸ¦–"}]
        st.session_state.thinking_message = "Agent at work..."
        st.session_state.agent = initialize_agent(cfg)
        st.session_state.log_messages = []
        st.session_state.prompt = None
        st.session_state.show_logs = False

    st.set_page_config(page_title="Justice Harvard Teaching Assistant", layout="wide")
    if 'cfg' not in st.session_state:
        cfg = OmegaConf.create({
            'customer_id': str(os.environ['VECTARA_CUSTOMER_ID']),
            'corpus_id': str(os.environ['VECTARA_CORPUS_ID']),
            'api_key': str(os.environ['VECTARA_API_KEY']),
            'style': teaching_styles[0],
            'language': 'English',
            'student_age': 18

        })
        st.session_state.cfg = cfg
        st.session_state.style = cfg.style
        st.session_state.language = cfg.language
        st.session_state.student_age = cfg.student_age

        reset()
    cfg = st.session_state.cfg

    # left side content
    with st.sidebar:
        image = Image.open('Vectara-logo.png')
        st.image(image, width=175)
        st.markdown("## Welcome to the Justice Harvard e-learning assistant demo.\n\n\n")

        st.markdown("\n")
        cfg.style = st.selectbox('Teacher Style:', teaching_styles)
        if st.session_state.style != cfg.style:
            st.session_state.style = cfg.style
            reset()

        st.markdown("\n")
        cfg.language = st.selectbox('Language:', languages.keys())
        if st.session_state.language != cfg.language:
            st.session_state.langage = cfg.language
            reset()

        st.markdown("\n") 
        cfg.student_age = st.number_input(
            'Student age:',  min_value=13, max_value=99, value=cfg.student_age,
            step=1, format='%i'
        )
        if st.session_state.student_age != cfg.student_age:
            st.session_state.student_age = cfg.student_age
            reset()

        st.markdown("\n\n")
        bc1, _ = st.columns([1, 1])
        with bc1:
            if st.button('Start Over'):
                reset()

        st.markdown("---")
        st.markdown(
            "## How this works?\n"
            "This app was built with [Vectara](https://vectara.com).\n\n"
            "It demonstrates the use of Agentic Chat functionality with Vectara"
        )
        st.markdown("---")


    if "messages" not in st.session_state.keys():
        reset()
    
    # Display chat messages
    for message in st.session_state.messages:
        with st.chat_message(message["role"], avatar=message["avatar"]):
            st.write(message["content"])

    # User-provided prompt
    if prompt := st.chat_input():
        st.session_state.messages.append({"role": "user", "content": prompt, "avatar": 'πŸ§‘β€πŸ’»'})
        st.session_state.prompt = prompt  # Save the prompt in session state
        st.session_state.log_messages = []
        st.session_state.show_logs = False
        with st.chat_message("user", avatar='πŸ§‘β€πŸ’»'):
            print(f"Starting new question: {prompt}\n")
            st.write(prompt)
        
    # Generate a new response if last message is not from assistant
    if st.session_state.prompt:
        with st.chat_message("assistant", avatar='πŸ€–'):
            with st.spinner(st.session_state.thinking_message):
                res = st.session_state.agent.chat(st.session_state.prompt)
                res = res.replace('$', '\\$')  # escape dollar sign for markdown
            message = {"role": "assistant", "content": res, "avatar": 'πŸ€–'}
            st.session_state.messages.append(message)
            st.markdown(res)
            st.session_state.prompt = None

    log_placeholder = st.empty()
    with log_placeholder.container():
        if st.session_state.show_logs:
            st.button("Hide Logs", on_click=toggle_logs)
            for msg in st.session_state.log_messages:
                st.text(msg)
        else:
            if len(st.session_state.log_messages) > 0:
                st.button("Show Logs", on_click=toggle_logs)

    sys.stdout.flush()
    
 
if __name__ == "__main__":
    launch_bot()