Skip to content
This repository has been archived by the owner on Nov 28, 2024. It is now read-only.

Commit

Permalink
Source
Browse files Browse the repository at this point in the history
  • Loading branch information
glensouza committed Nov 22, 2024
1 parent f23977a commit 32a918c
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions app/backend/approaches/mathassistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
#1. Tool to calculate pythagorean theorem

from langchain.tools import BaseTool
from pydantic import BaseModel
from langchain.chains import LLMMathChain
from typing import Optional
from math import sqrt, cos, sin
from typing import Union
Expand All @@ -53,6 +55,15 @@
"['adjacent_side', 'opposite_side', 'angle']."
)

# Define the BaseCache to make the tool compatible with the Langchain
class BaseCache(BaseModel):
pass
class Callbacks(BaseModel):
pass

# Call model_rebuild for LLMMathChain
LLMMathChain.model_rebuild()

class PythagorasTool(BaseTool):
name: ClassVar[str] = "Hypotenuse calculator"
description: ClassVar[str] = desc
Expand All @@ -76,7 +87,6 @@ def _run(
def _arun(self, query: str):
raise NotImplementedError("This tool does not support async")

tools = [PythagorasTool()]

#________________________________________

Expand All @@ -96,11 +106,12 @@ def _arun(self, radius: int):
raise NotImplementedError("This tool does not support async")


tools = [CircumferenceTool()]

#add math module from Lanhgchain

tools = load_tools(["llm-math","wikipedia"], llm=model)
# Examples of built-in tools
llm_math_tool = load_tools(["llm-math"], llm=model)
llm_wiki_tool = load_tools(["wikipedia"], llm=model)
# Examples of custom tools
llm_pythag_tool = [PythagorasTool()]
llm_pythag_tool = [CircumferenceTool()]


PREFIX = """Act as a math tutor that helps students solve a wide array of mathematical challenges, including arithmetic problems, algebraic equations, geometric proofs, calculus, and statistical analysis, as well as word problems.
Expand All @@ -110,11 +121,11 @@ def _arun(self, radius: int):
In handling math queries, try using your tools initially. If no solution is found, then attempt to solve the problem on your own.
"""


# # Initialize the agent
# Initialize the agent with a single input tool
# You can choose which of the tools to use or create separate agents for different tools
zero_shot_agent_math = initialize_agent(
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
tools=tools,
tools=llm_math_tool,
llm=model,
verbose=True,
max_iterations=10,
Expand All @@ -128,7 +139,7 @@ def _arun(self, radius: int):
async def stream_agent_responses(question):
zero_shot_agent_math = initialize_agent(
agent="zero-shot-react-description",
tools=tools,
tools=llm_math_tool,
llm=model,
verbose=True,
max_iterations=10,
Expand Down Expand Up @@ -249,16 +260,3 @@ def generate_response(question):
Question: {question}
"""













0 comments on commit 32a918c

Please sign in to comment.