ofermend commited on
Commit
ef023d4
·
1 Parent(s): a4fedc5
Files changed (2) hide show
  1. agent.py +44 -36
  2. requirements.txt +1 -1
agent.py CHANGED
@@ -17,9 +17,16 @@ languages = {'English': 'en', 'Spanish': 'es', 'French': 'fr', 'German': 'de', '
17
  'Hebrew': 'he', 'Hindi': 'hi', 'Italian': 'it', 'Japanese': 'ja', 'Korean': 'ko', 'Portuguese': 'pt'}
18
  initial_prompt = "How can I help you today?"
19
 
20
- def create_assistant_tools(cfg, agent_config):
 
 
 
 
 
 
21
 
22
  def adjust_response_to_student(
 
23
  text: str = Field(description='the text to adjust. may include citations in markdown format.'),
24
  age: int = Field(description='the age of the student. An integer'),
25
  style: str = Field(description='teaching style'),
@@ -38,44 +45,45 @@ def create_assistant_tools(cfg, agent_config):
38
  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.
39
  Or in the socratic teaching style, choose to ask questions that lead the student to the answer.
40
  Always respond in the {language} language.''' \
41
- .replace("{style}", cfg.style) \
42
- .replace("{language}", cfg.language) \
43
- .replace("{student_age}", str(cfg.student_age))
44
 
45
- rephrase_text = ToolsCatalog(agent_config).rephrase_text
46
  return rephrase_text(text, instructions)
47
 
48
 
49
- class JusticeHarvardArgs(BaseModel):
50
- query: str = Field(..., description="The user query.")
51
-
52
- vec_factory = VectaraToolFactory(vectara_api_key=cfg.api_key,vectara_corpus_key=cfg.corpus_key)
53
- summarizer = 'vectara-summary-ext-24-05-med-omni'
54
- query_tool = vec_factory.create_rag_tool(
55
- tool_name = "ask_about_justice_harvard",
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
- """,
60
- tool_args_schema = JusticeHarvardArgs,
61
- reranker = "multilingual_reranker_v1", rerank_k = 100,
62
- n_sentences_before = 2, n_sentences_after = 2, lambda_val = 0.005,
63
- summary_num_results = 10,
64
- vectara_summarizer = summarizer,
65
- include_citations = True,
66
- )
67
-
68
- tools_factory = ToolsFactory()
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, agent_progress_callback=None):
81
  bot_instructions = f"""
@@ -94,7 +102,7 @@ def initialize_agent(_cfg, agent_progress_callback=None):
94
  agent_config = AgentConfig()
95
  agent = Agent(
96
  agent_config=agent_config,
97
- tools=create_assistant_tools(_cfg, agent_config=agent_config),
98
  topic="justice, morality, politics, and philosophy",
99
  custom_instructions=bot_instructions,
100
  agent_progress_callback=agent_progress_callback
 
17
  'Hebrew': 'he', 'Hindi': 'hi', 'Italian': 'it', 'Japanese': 'ja', 'Korean': 'ko', 'Portuguese': 'pt'}
18
  initial_prompt = "How can I help you today?"
19
 
20
+ class AgentTools:
21
+ def __init__(self, _cfg, agent_config):
22
+ self.tools_factory = ToolsFactory()
23
+ self.agent_config = agent_config
24
+ self.cfg = _cfg
25
+ self.vec_factory = VectaraToolFactory(vectara_api_key=_cfg.api_key,
26
+ vectara_corpus_key=_cfg.corpus_key)
27
 
28
  def adjust_response_to_student(
29
+ self,
30
  text: str = Field(description='the text to adjust. may include citations in markdown format.'),
31
  age: int = Field(description='the age of the student. An integer'),
32
  style: str = Field(description='teaching style'),
 
45
  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.
46
  Or in the socratic teaching style, choose to ask questions that lead the student to the answer.
47
  Always respond in the {language} language.''' \
48
+ .replace("{style}", self.cfg.style) \
49
+ .replace("{language}", self.cfg.language) \
50
+ .replace("{student_age}", str(self.cfg.student_age))
51
 
52
+ rephrase_text = ToolsCatalog(self.cfg).rephrase_text
53
  return rephrase_text(text, instructions)
54
 
55
 
56
+ def get_tools(self):
57
+ class JusticeHarvardArgs(BaseModel):
58
+ query: str = Field(..., description="The user query.")
59
+
60
+ vec_factory = VectaraToolFactory(vectara_api_key=self.cfg.api_key,vectara_corpus_key=self.cfg.corpus_key)
61
+ summarizer = 'vectara-summary-ext-24-05-med-omni'
62
+ query_tool = vec_factory.create_rag_tool(
63
+ tool_name = "ask_about_justice_harvard",
64
+ tool_description = """
65
+ Answer questions about the justice, morality, politics and related topics,
66
+ based on transcripts of recordings from the Justice Harvard class that includes a lot of content on these topics.
67
+ """,
68
+ tool_args_schema = JusticeHarvardArgs,
69
+ reranker = "multilingual_reranker_v1", rerank_k = 100,
70
+ n_sentences_before = 2, n_sentences_after = 2, lambda_val = 0.005,
71
+ summary_num_results = 10,
72
+ vectara_summarizer = summarizer,
73
+ include_citations = True,
74
+ )
75
+
76
+ tools_factory = ToolsFactory()
77
+ return (
78
+ [tools_factory.create_tool(tool) for tool in
79
+ [
80
+ self.adjust_response_to_student,
81
+ ]
82
+ ] +
83
+ tools_factory.standard_tools() +
84
+ tools_factory.guardrail_tools() +
85
+ [query_tool]
86
+ )
87
 
88
  def initialize_agent(_cfg, agent_progress_callback=None):
89
  bot_instructions = f"""
 
102
  agent_config = AgentConfig()
103
  agent = Agent(
104
  agent_config=agent_config,
105
+ tools=AgentTools(_cfg, agent_config).get_tools(),
106
  topic="justice, morality, politics, and philosophy",
107
  custom_instructions=bot_instructions,
108
  agent_progress_callback=agent_progress_callback
requirements.txt CHANGED
@@ -6,4 +6,4 @@ streamlit_feedback==0.1.3
6
  uuid==1.30
7
  langdetect==1.0.9
8
  langcodes==3.4.0
9
- vectara-agentic==0.2.0
 
6
  uuid==1.30
7
  langdetect==1.0.9
8
  langcodes==3.4.0
9
+ vectara-agentic==0.2.1