ofermend commited on
Commit
42fc73e
1 Parent(s): 7b6cfab
Files changed (1) hide show
  1. agent.py +117 -0
agent.py ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import os
3
+
4
+ from omegaconf import OmegaConf
5
+
6
+ from dotenv import load_dotenv
7
+ load_dotenv(override=True)
8
+
9
+ from pydantic import Field, BaseModel
10
+ from vectara_agent.agent import Agent
11
+ from vectara_agent.tools import ToolsFactory, VectaraToolFactory
12
+ from vectara_agent.tools_catalog import rephrase_text
13
+
14
+ teaching_styles = ['Inquiry-based', 'Socratic', 'traditional']
15
+ languages = {'English': 'en', 'Spanish': 'es', 'French': 'fr', 'German': 'de', 'Arabic': 'ar', 'Chinese': 'zh-cn',
16
+ 'Hebrew': 'he', 'Hindi': 'hi', 'Italian': 'it', 'Japanese': 'ja', 'Korean': 'ko', 'Portuguese': 'pt'}
17
+ initial_prompt = "How can I help you today?"
18
+
19
+ def create_assistant_tools(cfg):
20
+
21
+ def adjust_response_to_student(
22
+ text: str = Field(description='the text to adjust. may include citations in markdown format.'),
23
+ age: int = Field(description='the age of the student. An integer'),
24
+ style: str = Field(description='teaching style'),
25
+ language: str = Field(description='the language')
26
+
27
+ ) -> str:
28
+ """
29
+ Rephrase the provided text to match the student's age, desired teaching style and language.
30
+ """
31
+ instructions = f'''
32
+ The following is some text, which may include citations in markdown format.
33
+ Adjust the response to match the student's age of {age}, the {style} teaching style.
34
+ Where possible, maintain the citations in context of the response (instead of listing them at the end).
35
+ When showing citations, use markdown in the following format: [[N](URL)], where N is the citation sequential number.
36
+ You can renumber citations if needed.
37
+ 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.
38
+ Or in the socratic teaching style, choose to ask questions that lead the student to the answer.
39
+ Always respond in the {language} language.''' \
40
+ .replace("{style}", cfg.style) \
41
+ .replace("{language}", cfg.language) \
42
+ .replace("{student_age}", str(cfg.student_age))
43
+
44
+ return rephrase_text(text, instructions)
45
+
46
+
47
+ class JusticeHarvardArgs(BaseModel):
48
+ query: str = Field(..., description="The user query.")
49
+
50
+ vec_factory = VectaraToolFactory(vectara_api_key=cfg.api_key,
51
+ vectara_customer_id=cfg.customer_id,
52
+ vectara_corpus_id=cfg.corpus_id)
53
+ tools_factory = ToolsFactory()
54
+ query_tool = vec_factory.create_rag_tool(
55
+ tool_name = "justice_harvard_query",
56
+ tool_description = """
57
+ Answer questions about the justice, morality, politics and related topics,
58
+ based on transcripts of recordings from the Justice Harvard class that includes a lot of content on these topics.
59
+ When using the tool it's best to ask simple short questions. You can break complex questions into sub-queries.
60
+ """,
61
+ tool_args_schema = JusticeHarvardArgs,
62
+ reranker = "multilingual_reranker_v1", rerank_k = 100,
63
+ n_sentences_before = 2, n_sentences_after = 2, lambda_val = 0.005,
64
+ summary_num_results = 10,
65
+ vectara_summarizer = 'vectara-summary-ext-24-05-med-omni',
66
+ include_citations = True,
67
+ )
68
+
69
+ return (
70
+ [tools_factory.create_tool(tool) for tool in
71
+ [
72
+ adjust_response_to_student,
73
+ ]
74
+ ] +
75
+ tools_factory.standard_tools() +
76
+ tools_factory.guardrail_tools() +
77
+ [query_tool]
78
+ )
79
+
80
+ def initialize_agent(_cfg, update_func=None):
81
+ bot_instructions = f"""
82
+ - You are a helpful teacher assistant, with expertise in education in various teaching styles.
83
+ - Obtain information using tools to answer the user's query.
84
+ - If the tool cannot provide information relevant to the user's query, tell the user that you are unable to provide an answer.
85
+ - If the tool can provide relevant information, use the adjust_response_to_student tool
86
+ to rephrase the text (including citations if any) to ensure it fits the student's age of {_cfg.student_age},
87
+ the {_cfg.style} teaching style and the {_cfg.language} language.
88
+ - When including links to the Justice Harvard videos, the url should include the start time (from the metadata).
89
+ For example: https://youtube.com/watch?v=0O2Rq4HJBxw&t=1234 represents a link to a youtube video starting at 1234 seconds.
90
+ - When showing citations, renumber them if needed and use markdown in the following format: [[N](URL)], where N is the citation sequential number.
91
+ - Response in a concise and clear manner, and provide the most relevant information to the student.
92
+ - Never discuss politics, and always respond politely.
93
+ """
94
+
95
+ agent = Agent(
96
+ tools=create_assistant_tools(_cfg),
97
+ topic="justice, morality, politics, and philosophy",
98
+ custom_instructions=bot_instructions,
99
+ update_func=update_func
100
+ )
101
+ return agent
102
+
103
+
104
+ def get_agent_config() -> OmegaConf:
105
+ cfg = OmegaConf.create({
106
+ 'customer_id': str(os.environ['VECTARA_CUSTOMER_ID']),
107
+ 'corpus_id': str(os.environ['VECTARA_CORPUS_ID']),
108
+ 'api_key': str(os.environ['VECTARA_API_KEY']),
109
+ 'examples': os.environ.get('QUERY_EXAMPLES', None),
110
+ 'title': "Justice Harvard Teaching Assistant",
111
+ 'demo_welcome': "Welcome to the Justice Harvard e-learning assistant demo.",
112
+ 'demo_description': "Ask questions about the Justice Harvard class and get answers from an expert teaching assistant.",
113
+ 'style': teaching_styles[0],
114
+ 'language': 'English',
115
+ 'student_age': 18
116
+ })
117
+ return cfg