Ankit93 commited on
Commit
c8abc3a
·
verified ·
1 Parent(s): 34bbf8e

Update src/agent/agenttools.py

Browse files
Files changed (1) hide show
  1. src/agent/agenttools.py +119 -158
src/agent/agenttools.py CHANGED
@@ -2,6 +2,10 @@ from llama_index.core.tools import FunctionTool
2
  from src.setup.utils import retry
3
  from src.controller.customlogger import logging
4
  from src.llm.source_llm import LLMCall
 
 
 
 
5
 
6
 
7
  class ChatBotFunctionTools:
@@ -13,9 +17,8 @@ class ChatBotFunctionTools:
13
  logging.info(f"Tool Call: machine_learning_concept('{query}')")
14
  prompt = (
15
  "You are a Machine Learning teacher.\n"
16
- "Explain the following ML concept in:\n"
17
- "The explaination should include geometrical or mathematical intuition"
18
- "Try to keep answer crisp and compact"
19
  f"User Query: {query}"
20
  )
21
  return self.generator.complete(prompt)
@@ -25,187 +28,145 @@ class ChatBotFunctionTools:
25
  logging.info(f"Tool Call: math_concept('{query}')")
26
  prompt = (
27
  "You are a Math teacher.\n"
28
- "Explain the following mathematics behind the Machine Learning algorithm in details with each step by step :\n"
29
- "Try to keep answer crisp and compact"
30
- f"User Query: {query}"
31
  )
32
  return self.generator.complete(prompt)
33
 
34
  @retry(max_retries=5, delay=1)
35
  def deep_learning_architecture(self, arch):
36
  logging.info(f"Tool Call: deep_learning_architecture('{arch}')")
37
- prompt = f"Explain the {arch} neural network architecture with diagram description"
 
 
 
38
  return self.generator.complete(prompt)
39
 
40
  @retry(max_retries=5, delay=1)
41
- def visualize_algorithm(self, algo):
42
- logging.info(f"Tool Call: visualize_algorithm('{algo}')")
43
- prompt = f"Create visualization code that demonstrates how {algo} works"
 
 
 
 
44
  return self.generator.complete(prompt)
45
 
46
  @retry(max_retries=5, delay=1)
47
  def concept_combiner(self, concepts):
48
  logging.info(f"Tool Call: concept_combiner('{concepts}')")
49
- prompt = f"Explain the relationship between these concepts: {', '.join(concepts)}"
 
 
50
  return self.generator.complete(prompt)
51
 
52
  @retry(max_retries=5, delay=1)
53
  def llm_query(self, concepts):
54
  logging.info(f"Tool Call: llm_query('{concepts}')")
55
- prompt = f"You are an Expert to Answer the following question respond to the best of your knowledge: {', '.join(concepts)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  return self.generator.complete(prompt)
57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  @retry(max_retries=5, delay=1)
59
  def get_tools(self):
60
  return {
61
  "ml_concept": FunctionTool.from_defaults(fn=self.machine_learning_concept),
62
  "dl_architecture": FunctionTool.from_defaults(fn=self.deep_learning_architecture),
63
- "algo_visualizer": FunctionTool.from_defaults(fn=self.visualize_algorithm),
64
  "concept_combiner": FunctionTool.from_defaults(fn=self.concept_combiner),
65
  "math_concept": FunctionTool.from_defaults(fn=self.math_concept),
66
- "llm_query": FunctionTool.from_defaults(fn=self.llm_query)
 
 
 
 
 
 
 
67
  }
68
-
69
-
70
-
71
- # class LearningAgent:
72
- # """Core agent that orchestrates tool usage for learning system"""
73
- #
74
- # def __init__(self, llm_value):
75
- # self.llm = LLMCall(llm_type=llm_value).get_llm()
76
- # self.tools = self._setup_tools()
77
- # self.chat_history: List[Dict[str, str]] = [] # Stores properly formatted messages
78
- # self.max_history = 20
79
- #
80
- # def _setup_tools(self) -> Dict[str, FunctionTool]:
81
- # """Initialize all learning tools"""
82
- # return {
83
- # **self._setup_ml_tools(),
84
- # **self._setup_dl_tools(),
85
- # **self._setup_graph_tools(),
86
- # **self._setup_utility_tools()
87
- # }
88
- #
89
- # def _setup_ml_tools(self) -> Dict[str, FunctionTool]:
90
- # """Machine Learning tools"""
91
- #
92
- # def ml_concept_explainer(query: str) -> str:
93
- # prompt = f"Explain this ML concept in simple terms with examples: {query}"
94
- # return self.llm.complete(prompt).text
95
- #
96
- # return {
97
- # "ml_concept": FunctionTool.from_defaults(fn=ml_concept_explainer)
98
- # }
99
- #
100
- # def _setup_dl_tools(self) -> Dict[str, FunctionTool]:
101
- # """Deep Learning tools"""
102
- #
103
- # def dl_architecture(arch: str) -> str:
104
- # prompt = f"Explain the {arch} neural network architecture with diagram description"
105
- # return self.llm.complete(prompt).text
106
- #
107
- # return {
108
- # "dl_architecture": FunctionTool.from_defaults(fn=dl_architecture)
109
- # }
110
- #
111
- # def _setup_graph_tools(self) -> Dict[str, FunctionTool]:
112
- # """Graph/Visualization tools"""
113
- #
114
- # def visualize_algorithm(algo: str) -> str:
115
- # prompt = f"Create visualization code that demonstrates how {algo} works"
116
- # return self.llm.complete(prompt).text
117
- #
118
- # return {
119
- # "algo_visualizer": FunctionTool.from_defaults(fn=visualize_algorithm)
120
- # }
121
- #
122
- # def _setup_utility_tools(self) -> Dict[str, FunctionTool]:
123
- # """Utility tools"""
124
- #
125
- # def concept_combiner(concepts: List[str]) -> str:
126
- # prompt = f"Explain the relationship between these concepts: {', '.join(concepts)}"
127
- # return self.llm.complete(prompt).text
128
- #
129
- # return {
130
- # "concept_combiner": FunctionTool.from_defaults(fn=concept_combiner)
131
- # }
132
- #
133
- # def extract_json_from_markdown(self, markdown_text):
134
- # """
135
- # Extract and parse JSON content from a markdown-style code block.
136
- # Handles formats like ```json ... ```
137
- # """
138
- # try:
139
- # # Extract JSON block using regex
140
- # match = re.search(r"```json\s*(\{.*?\})\s*```", markdown_text, re.DOTALL)
141
- # if not match:
142
- # raise ValueError("No JSON block found in markdown")
143
- #
144
- # json_str = match.group(1)
145
- # return json.loads(json_str)
146
- # except Exception as e:
147
- # print(f"[ERROR] Could not parse JSON: {e}")
148
- # return None
149
- #
150
- # def determine_tools(self, query: str):
151
- # """Decide which tools to use based on query"""
152
- # previous_questions = ""
153
- # if self.chat_history:
154
- # previous_questions = "\n".join([msg["content"] for msg in self.chat_history if msg["role"] == "user"][:-1]) \
155
- # if self.chat_history else "No previous questions"
156
- # prompt = f"""Analyze this learning query and select appropriate tools also form the condensed query based on
157
- # previous_questions and Query:
158
- # Query: {query}
159
- # Previous Query: {previous_questions}
160
- # Available Tools: {list(self.tools.keys())}
161
- # Return dictionary of tool names and condensed query as dictionary in the below format:
162
- # ```json
163
- # {{
164
- # "condensed_query": condensed query considering chat history and user query as string,
165
- # "tool_names": tool names as comma-separated list
166
- # }}
167
- # ```
168
- # Do Not add additional text"""
169
- #
170
- # response = self.llm.complete(prompt).text
171
- # response = self.extract_json_from_markdown(response)
172
- # return [t.strip() for t in response['tool_names'].split(",") if t.strip() in self.tools], response["condensed_query"]
173
- #
174
- # def execute_tools(self, tools: List[str], query: str) -> tuple[str, str]:
175
- # """Execute multiple tools and combine results"""
176
- # tool_results = []
177
- # content_results = []
178
- #
179
- # for tool in tools:
180
- # try:
181
- # tool_output = self.tools[tool](query)
182
- # content = tool_output.content if isinstance(tool_output, ToolOutput) else str(tool_output)
183
- # tool_results.append(tool)
184
- # content_results.append(content)
185
- # except Exception as e:
186
- # logging.error(f"Tool {tool} failed: {str(e)}")
187
- # tool_results.append(tool)
188
- # content_results.append(f"Error: {str(e)}")
189
- #
190
- # if len(tool_results) > 1:
191
- # combined = "\n\n".join(f"**{t}**:\n{c}" for t, c in zip(tool_results, content_results))
192
- # explanation = self.tools["concept_combiner"](content_results)
193
- # return "multiple", f"{combined}\n\n**Combined Analysis**:\n{explanation}"
194
- # return tool_results[0], content_results[0]
195
- #
196
- # def process_query(self, query: str) -> tuple[List[Dict[str, str]], str, str]:
197
- # """Process query and return properly formatted messages"""
198
- # tools, condensed_query = self.determine_tools(query)
199
- # logging.info(f"Selected tools: {tools}")
200
- #
201
- # tool_used, response = self.execute_tools(tools, condensed_query)
202
- #
203
- # # Format messages for Gradio Chatbot
204
- # user_msg = {"role": "user", "content": query.title()}
205
- # assistant_msg = {"role": "assistant", "content": response}
206
- #
207
- # self.chat_history.extend([user_msg, assistant_msg])
208
- # if len(self.chat_history) > self.max_history * 2: # *2 for user+assistant pairs
209
- # self.chat_history = self.chat_history[-(self.max_history * 2):]
210
- #
211
- # return self.chat_history, tool_used, response
 
2
  from src.setup.utils import retry
3
  from src.controller.customlogger import logging
4
  from src.llm.source_llm import LLMCall
5
+ import base64
6
+ import matplotlib.pyplot as plt
7
+ from io import BytesIO
8
+
9
 
10
 
11
  class ChatBotFunctionTools:
 
17
  logging.info(f"Tool Call: machine_learning_concept('{query}')")
18
  prompt = (
19
  "You are a Machine Learning teacher.\n"
20
+ "Explain the following ML concept clearly, with geometric or mathematical intuition.\n"
21
+ "Keep it crisp, compact, and meaningful.\n"
 
22
  f"User Query: {query}"
23
  )
24
  return self.generator.complete(prompt)
 
28
  logging.info(f"Tool Call: math_concept('{query}')")
29
  prompt = (
30
  "You are a Math teacher.\n"
31
+ "Explain the mathematics behind the following ML-related concept in a step-by-step manner:\n"
32
+ f"{query}\n"
33
+ "Keep it compact and clear."
34
  )
35
  return self.generator.complete(prompt)
36
 
37
  @retry(max_retries=5, delay=1)
38
  def deep_learning_architecture(self, arch):
39
  logging.info(f"Tool Call: deep_learning_architecture('{arch}')")
40
+ prompt = (
41
+ f"You are a Deep Learning instructor.\n"
42
+ f"Explain the architecture of {arch} with a diagram description and its components' roles."
43
+ )
44
  return self.generator.complete(prompt)
45
 
46
  @retry(max_retries=5, delay=1)
47
+ def mathematical_intuition(self, algo):
48
+ logging.info(f"Tool Call: mathematical_intuition('{algo}')")
49
+ prompt = (
50
+ f"You are a Machine Learning expert.\n"
51
+ f"Provide the mathematical intuition behind the algorithm '{algo}'. "
52
+ "Keep the explanation compact and intuitive."
53
+ )
54
  return self.generator.complete(prompt)
55
 
56
  @retry(max_retries=5, delay=1)
57
  def concept_combiner(self, concepts):
58
  logging.info(f"Tool Call: concept_combiner('{concepts}')")
59
+ prompt = (
60
+ f"Explain the relationship and integration between the following concepts: {', '.join(concepts)}"
61
+ )
62
  return self.generator.complete(prompt)
63
 
64
  @retry(max_retries=5, delay=1)
65
  def llm_query(self, concepts):
66
  logging.info(f"Tool Call: llm_query('{concepts}')")
67
+ prompt = (
68
+ f"You are an expert LLM assistant. Respond to the following questions or topics with the best technical insight:\n"
69
+ f"{', '.join(concepts)}"
70
+ )
71
+ return self.generator.complete(prompt)
72
+
73
+ @retry(max_retries=5, delay=1)
74
+ def recommend_learning_path(self, topic_area):
75
+ logging.info(f"Tool Call: recommend_learning_path('{topic_area}')")
76
+ prompt = (
77
+ f"You are an AI tutor. Provide a structured learning path for '{topic_area}', including prerequisites, "
78
+ "recommended topics by order, and example projects."
79
+ )
80
+ return self.generator.complete(prompt)
81
+
82
+ @retry(max_retries=5, delay=1)
83
+ def quiz_question(self, topic):
84
+ logging.info(f"Tool Call: quiz_question('{topic}')")
85
+ prompt = (
86
+ f"Generate a 3-question quiz on '{topic}' with multiple choice options and answers. "
87
+ "Format: Question, 4 options (A–D), and the correct answer."
88
+ )
89
+ return self.generator.complete(prompt)
90
+
91
+ @retry(max_retries=5, delay=1)
92
+ def code_example(self, concept):
93
+ logging.info(f"Tool Call: code_example('{concept}')")
94
+ prompt = (
95
+ f"Provide a short Python code example demonstrating the concept '{concept}', "
96
+ "and explain the key parts in comments or brief explanations."
97
+ )
98
  return self.generator.complete(prompt)
99
 
100
+ @retry(max_retries=5, delay=1)
101
+ def doubt_clarifier(self, user_doubt):
102
+ logging.info(f"Tool Call: doubt_clarifier('{user_doubt}')")
103
+ prompt = (
104
+ f"A student has the following doubt: '{user_doubt}'. "
105
+ "As a teacher, explain it in simple terms, use analogies if helpful."
106
+ )
107
+ return self.generator.complete(prompt)
108
+
109
+ @retry(max_retries=5, delay=1)
110
+ def daily_study_plan(self, goals):
111
+ logging.info(f"Tool Call: daily_study_plan('{goals}')")
112
+ prompt = (
113
+ f"Create a one-week daily study plan to help achieve the following goals: '{goals}'. "
114
+ "Break it down by day with suggested hours, topics, and activities."
115
+ )
116
+ return self.generator.complete(prompt)
117
+
118
+ @retry(max_retries=5, delay=1)
119
+ def dsa_concept(self, concept):
120
+ logging.info(f"Tool Call: dsa_concept('{concept}')")
121
+ prompt = (
122
+ f"Explain the data structure or algorithm '{concept}' with real-life analogy, use-case, and time complexity. "
123
+ "Keep it beginner-friendly but technically accurate."
124
+ )
125
+ return self.generator.complete(prompt)
126
+
127
+ @retry(max_retries=5, delay=1)
128
+
129
+ def plot_generator(self, query):
130
+ logging.info(f"Tool Call: plot_generator('{query}')")
131
+ # Ask the LLM to generate Python code for plotting
132
+ code_prompt = (
133
+ f"You are a data visualization assistant. Generate Python code using matplotlib or seaborn to create a plot for: '{query}'.\n"
134
+ "Keep it clear and labeled. Assume necessary imports like matplotlib.pyplot are already done."
135
+ )
136
+ code = self.generator.complete(code_prompt)
137
+
138
+ # Attempt to execute the plot code safely
139
+ try:
140
+ # Setup isolated namespace and capture the figure
141
+ local_env = {}
142
+ exec_globals = {"plt": plt}
143
+ exec(code, exec_globals, local_env)
144
+
145
+ # Capture and encode the plot
146
+ buf = BytesIO()
147
+ plt.savefig(buf, format='png')
148
+ plt.close()
149
+ buf.seek(0)
150
+ encoded_img = base64.b64encode(buf.read()).decode('utf-8')
151
+ return {"image": encoded_img, "code": code}
152
+ except Exception as e:
153
+ return {"error": str(e), "code": code}
154
+
155
+
156
  @retry(max_retries=5, delay=1)
157
  def get_tools(self):
158
  return {
159
  "ml_concept": FunctionTool.from_defaults(fn=self.machine_learning_concept),
160
  "dl_architecture": FunctionTool.from_defaults(fn=self.deep_learning_architecture),
161
+ "mathematical_intuition": FunctionTool.from_defaults(fn=self.mathematical_intuition),
162
  "concept_combiner": FunctionTool.from_defaults(fn=self.concept_combiner),
163
  "math_concept": FunctionTool.from_defaults(fn=self.math_concept),
164
+ "llm_query": FunctionTool.from_defaults(fn=self.llm_query),
165
+ "learning_path": FunctionTool.from_defaults(fn=self.recommend_learning_path),
166
+ "quiz": FunctionTool.from_defaults(fn=self.quiz_question),
167
+ "code_example": FunctionTool.from_defaults(fn=self.code_example),
168
+ "doubt_clarifier": FunctionTool.from_defaults(fn=self.doubt_clarifier),
169
+ "daily_study_plan": FunctionTool.from_defaults(fn=self.daily_study_plan),
170
+ "dsa_concept": FunctionTool.from_defaults(fn=self.dsa_concept),
171
+ "plot_generator": FunctionTool.from_defaults(fn=self.plot_generator)
172
  }