Skip to content

Commit

Permalink
Merge pull request #921 from microsoft/bugs/pydantic-update
Browse files Browse the repository at this point in the history
Update to resolve Pydantic Startup Issue (@ryonsteelemsft)
  • Loading branch information
bjakems authored Dec 2, 2024
2 parents 098e1db + 830968a commit eadbb2e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
31 changes: 21 additions & 10 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_circumference_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
1 change: 1 addition & 0 deletions app/backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ tabulate==0.9.0
tiktoken==0.7.0
uvicorn==0.30.6
wikipedia==1.4.0
pydantic==2.10.1

0 comments on commit eadbb2e

Please sign in to comment.