Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +31 -2
- prompts.py +1 -3
app.py
CHANGED
@@ -5,7 +5,8 @@ import os
|
|
5 |
from PIL import Image
|
6 |
import re
|
7 |
from translate import Translator
|
8 |
-
|
|
|
9 |
|
10 |
from llama_index.indices.managed.vectara import VectaraIndex
|
11 |
from llama_index.core.agent import ReActAgent
|
@@ -13,6 +14,7 @@ from llama_index.llms.openai import OpenAI
|
|
13 |
from llama_index.core.tools import QueryEngineTool, ToolMetadata
|
14 |
from llama_index.core.utils import print_text
|
15 |
from llama_index.core.agent.react.formatter import ReActChatFormatter
|
|
|
16 |
|
17 |
from prompts import prompt_template
|
18 |
|
@@ -33,6 +35,29 @@ def launch_bot():
|
|
33 |
vectara_customer_id=cfg.customer_id,
|
34 |
vectara_corpus_id=cfg.corpus_id)
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
# Create the Vectara Tool
|
37 |
vectara_tool = QueryEngineTool(
|
38 |
query_engine = vectara.as_query_engine(summary_enabled = True, summary_num_results = 10, summary_response_lang = languages[cfg.language],
|
@@ -47,13 +72,15 @@ def launch_bot():
|
|
47 |
"""),
|
48 |
)
|
49 |
|
|
|
|
|
50 |
# Create the agent
|
51 |
prompt = prompt_template.replace("{style}", cfg.style) \
|
52 |
.replace("{language}", cfg.language) \
|
53 |
.replace("{student_age}", str(cfg.student_age))
|
54 |
|
55 |
st.session_state.agent = ReActAgent.from_tools(
|
56 |
-
tools=[vectara_tool], llm=llm,
|
57 |
verbose=True,
|
58 |
react_chat_formatter = ReActChatFormatter(system_header=prompt)
|
59 |
)
|
@@ -142,6 +169,8 @@ def launch_bot():
|
|
142 |
message = {"role": "assistant", "content": cleaned, "avatar": '🤖'}
|
143 |
st.session_state.messages.append(message)
|
144 |
|
|
|
|
|
145 |
if __name__ == "__main__":
|
146 |
launch_bot()
|
147 |
|
|
|
5 |
from PIL import Image
|
6 |
import re
|
7 |
from translate import Translator
|
8 |
+
from pydantic import Field
|
9 |
+
import sys
|
10 |
|
11 |
from llama_index.indices.managed.vectara import VectaraIndex
|
12 |
from llama_index.core.agent import ReActAgent
|
|
|
14 |
from llama_index.core.tools import QueryEngineTool, ToolMetadata
|
15 |
from llama_index.core.utils import print_text
|
16 |
from llama_index.core.agent.react.formatter import ReActChatFormatter
|
17 |
+
from llama_index.core.tools import FunctionTool
|
18 |
|
19 |
from prompts import prompt_template
|
20 |
|
|
|
35 |
vectara_customer_id=cfg.customer_id,
|
36 |
vectara_corpus_id=cfg.corpus_id)
|
37 |
|
38 |
+
# Create tool to adapt output to style, age and language
|
39 |
+
def adjust_response_to_student(
|
40 |
+
text: str = Field(descrition='the original text'),
|
41 |
+
age: int = Field(description='the age of the student. An integer'),
|
42 |
+
style: str = Field(description='teaching style'),
|
43 |
+
language: str = Field(description='the language')
|
44 |
+
) -> str:
|
45 |
+
"""
|
46 |
+
Rephrase the text to match the student's age, learning style and language
|
47 |
+
"""
|
48 |
+
llm = OpenAI(model="gpt-4o", temperature=0)
|
49 |
+
prompt = f'''
|
50 |
+
The following is response the teacher is planning to provide to a student based on their question.
|
51 |
+
Please adjust the response to match the student's age of {age}, the {style} teaching style and the {language} language.
|
52 |
+
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.
|
53 |
+
Or in the socratic teaching style, choose to ask questions that lead the student to the answer.
|
54 |
+
original response: {text}
|
55 |
+
adjusted response:
|
56 |
+
'''
|
57 |
+
response = llm.complete(prompt)
|
58 |
+
return response
|
59 |
+
|
60 |
+
|
61 |
# Create the Vectara Tool
|
62 |
vectara_tool = QueryEngineTool(
|
63 |
query_engine = vectara.as_query_engine(summary_enabled = True, summary_num_results = 10, summary_response_lang = languages[cfg.language],
|
|
|
72 |
"""),
|
73 |
)
|
74 |
|
75 |
+
rephrase_tool = FunctionTool.from_defaults(adjust_response_to_student)
|
76 |
+
|
77 |
# Create the agent
|
78 |
prompt = prompt_template.replace("{style}", cfg.style) \
|
79 |
.replace("{language}", cfg.language) \
|
80 |
.replace("{student_age}", str(cfg.student_age))
|
81 |
|
82 |
st.session_state.agent = ReActAgent.from_tools(
|
83 |
+
tools=[vectara_tool, rephrase_tool], llm=llm,
|
84 |
verbose=True,
|
85 |
react_chat_formatter = ReActChatFormatter(system_header=prompt)
|
86 |
)
|
|
|
169 |
message = {"role": "assistant", "content": cleaned, "avatar": '🤖'}
|
170 |
st.session_state.messages.append(message)
|
171 |
|
172 |
+
sys.stdout.flush()
|
173 |
+
|
174 |
if __name__ == "__main__":
|
175 |
launch_bot()
|
176 |
|
prompts.py
CHANGED
@@ -47,8 +47,6 @@ You should keep repeating the above format until you have enough information
|
|
47 |
to answer the question without using any more tools.
|
48 |
|
49 |
At that point, you MUST respond
|
50 |
-
Take time and carefully craft your response so that it is appropriate for a {student_age} year old student and the {style} teaching style.
|
51 |
-
You can consider a few specific formats for the response and pick the one that is most consistent with the {style} teaching style.
|
52 |
|
53 |
Respond in the one of the following two formats:
|
54 |
|
@@ -69,7 +67,7 @@ ADDITIONAL INSTRUCTIONS:
|
|
69 |
- Make sure your response relies on the tools you have used and the information from those tools.
|
70 |
- Do not base your response on information that was not provided by the tools.
|
71 |
- The tool response may include citations in the form [1], [2], etc. Ignore these citations.
|
72 |
-
- Always
|
73 |
|
74 |
## Current Conversation
|
75 |
Below is the current conversation consisting of interleaving student and assistant messages.
|
|
|
47 |
to answer the question without using any more tools.
|
48 |
|
49 |
At that point, you MUST respond
|
|
|
|
|
50 |
|
51 |
Respond in the one of the following two formats:
|
52 |
|
|
|
67 |
- Make sure your response relies on the tools you have used and the information from those tools.
|
68 |
- Do not base your response on information that was not provided by the tools.
|
69 |
- The tool response may include citations in the form [1], [2], etc. Ignore these citations.
|
70 |
+
- Always use the rephrase tool at the end in order to ensure it fits the student's age and the desired teaching style.
|
71 |
|
72 |
## Current Conversation
|
73 |
Below is the current conversation consisting of interleaving student and assistant messages.
|