Building a Math Application with promptulate Agents

本文介绍如何使用promptulate创建一个定制的数学应用MathWiz,利用OpenAI的GPT-3.5模型,前端采用streamlit。文章详细描述了环境设置、工具选择、应用流程和如何使用promptulateagents进行交互式问题解答。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

This demo is how to use promptulates agents to create a custom Math application utilising OpenAI’s GPT3.5 Model.For the application frontend, there will be using streamlit, an easy-to-use open-source Python library.This generative math application,let’s call it “Math Wiz”, is designed to help users with their math or reasoning/logic questions.

The app schema for “Math Wiz” looks like the following:
App-Schema-for-Math-Wiz diagram

1. Reference reading

Building a Math Application with LangChain Agents

2. Environment Setup

We can start off by creating a new conda environment with python=3.11:
conda create -n math_assistant python=3.11

Activate the environment:conda activate math_assistant

Next, let’s install all necessary libraries:
pip install -U promptulate
pip install wikipedia
pip install numexpr

Sign up at OpenAI and obtain your own key to start making calls to the gpt model. Once you have the key, create an .env file in your repository and store the OpenAI key:

OPENAI_API_KEY="your_openai_api_key"

3. Application Flow

The application flow for Math Wiz is outlined in the flowchart below. The agent in our pipeline will have a set of tools at its disposal that it can use to answer a user query. The Large Language Model (LLM) serves as the “brain” of the agent, guiding its decisions. When a user submits a question, the agent uses the LLM to select the most appropriate tool or a combination of tools to provide an answer. If the agent determines it needs multiple tools, it will also specify the order in which the tools are used.

3.1 The application flow for Math Wiz

Promptulate-Agents-Deconstructed
The agent in our pipeline will have a set of tools at its disposal that it can use to answer a user query. The Large Language Model (LLM) serves as the “brain” of the agent, guiding its decisions. When a user submits a question, the agent uses the LLM to select the most appropriate tool or a combination of tools to provide an answer. If the agent determines it needs multiple tools, it will also specify the order in which the tools are used.

The agent for our Math Wiz app will be using the following tools:

  1. Wikipedia Tool: this tool will be responsible for fetching the latest information from Wikipedia using the Wikipedia API. While there are paid tools and APIs available that can be integrated inside Promptulate, I would be using Wikipedia as the app’s online source of information.
  2. Calculator Tool: this tool would be responsible for solving a user’s math queries. This includes anything involving numerical calculations. For example, if a user asks what the square root of 4 is, this tool would be appropriate.
  3. Reasoning Tool: the final tool in our application setup would be a reasoning tool, responsible for tackling logical/reasoning-based user queries. Any mathematical word problems should also be handled with this tool.

Now that we have a rough application design, we can begin thinking about building this application.

3.2 Understanding promptulate Agents

Promptulate agents are designed to enhance interaction with language models by providing an interface for more complex and interactive tasks. We can think of an agent as an intermediary between users and a large language model. Agents seek to break down a seemingly complex user query, that our LLM might not be able to tackle on its own, into easier, actionable steps.

In our application flow, we defined a few different tools that we would like to use for our math application. Based on the user input, the agent should decide which of these tools to use. If a tool is not required, it should not be used. Promptulate agents can simplify this for us. These agents use a language model to choose a sequence of actions to take. Essentially, the LLM acts as the “brain” of the agent, guiding it on which tool to use for a particular query, and in which order. This is different from Promptulate chains where the sequence of actions are hardcoded in code. Promptulate offers a wide set of tools that can be integrated with an agent. These tools include, and are not limited to, online search tools, API-based tools, chain-based tools etc. For more information on Promptulate agents and their types, see this.

3.3 Step-by-Step Implementation

Step 1

Create a chatbot.py script and import the necessary dependencies:

from promptulate.llms import ChatOpenAI
from promptulate.tools.wikipedia.tools import wikipedia_search
from promptulate.tools.math.tools import calculator
import promptulate as pne

Step 2

Next, we will define our OpenAI-based Language Model.The architectural design of promptulate is easily compatible with different large language model extensions. In promptulate, llm is responsible for the most basic part of content generation, so it is the most basic component.By default, ChatOpenAI in promptulate uses the gpt-3.5-turbo model.

llm = ChatOpenAI()

We would be using this LLM both within our math and reasoning process and as the decision maker for our agent.

Step 3

When constructing your own agent, you will need to provide it with a list of tools that it can use. Difine a tool,the only you need to do is to provide a function to Promptulate. Promptulate will automatically convert it to a tool that can be used by the language learning model (LLM). The final presentation result it presents to LLM is an OpenAI type JSON schema declaration.

Actually, Promptulate will analysis function name, parameters type, parameters attribution, annotations and docs when you provide the function. We strongly recommend that you use the official best practices of Template for function writing. The best implementation of a function requires adding type declarations to its parameters and providing function level annotations. Ideally, declare the meaning of each parameter within the annotations.

We will now create our three tools. The first one will be the online tool using the Wikipedia API wrapper:

# Wikipedia Tool
def wikipedia_tool(keyword: str) -> str:
    """search by keyword in web.

    A useful tool for searching the Internet to find information on world events,
    issues, dates,years, etc. Worth using for general topics. Use precise questions.

    Args:
        keyword: keyword to search

    Returns:
        str: search result
    """
    return wikipedia_search(keyword)

Next, let’s define the tool that we will be using for calculating any numerical expressions. Promptulate offers the calculator which uses the numexpr Python library to calculate mathematical expressions. It is also important that we clearly define what this tool would be used for. The description can be helpful for the agent in deciding which tool to use from a set of tools for a particular user query.

# calculator tool for arithmetics
def math_tool(expression: str):
    """Useful for when you need to answer questions about math. This tool is only
    for math questions and nothing else. Only input math expressions.

    Args:
        expression: A mathematical expression, eg: 18^0.43

    Attention:
        Expressions can not exist variables!
        eg: (current age)^0.43 is wrong, you should use 18^0.43 instead.

    Returns:
        The result of the evaluation.
    """
    return calculator(expression)

Finally, we will be defining the tool for logic/reasoning-based queries. We will first create a prompt to instruct the model while executing the specific task. Then we will create a simple AssistantMessage for this tool, passing it the LLM and the prompt.

# reasoning based tool
def word_problem_tool(question: str) -> str:
    """
    Useful for when you need to answer logic-based/reasoning questions.

    Args:
        question(str): Detail question, the description of the problem requires a
        detailed question context. Include a description of the problem

    Returns:
        question answer
    """
    system_prompt: str = """You are a reasoning agent tasked with solving t he user's logic-based questions.
    Logically arrive at the solution, and be factual.
    In your answers, clearly detail the steps involved and give the final answer.
    Provide the response in bullet points."""  # noqa
    llm = ChatOpenAI()
    return llm(f"{system_prompt}\n\nQuestion:{question}Answer:")

Step 4

We will now initialize our agent with the tools we have created above. We will also specify the LLM to help it choose which tools to use and in what order:

# agent
agent = pne.ToolAgent(tools=[wikipedia_tool, math_tool, word_problem_tool],
                      llm=llm)

resp: str = agent.run("I have 3 apples and 4 oranges.I give half of my oranges away and buy two dozen new ones,along with three packs of strawberries.Each pack of strawberry has 30 strawberries.How many total pieces of fruit do I have at the end?")
print(resp)

The app’s response to a logic question is following:
test-question-answer

4. Creating streamlit application

We will be using Streamlit, an open-source Python library, to build our application. With Streamlit, you can build conversational AI applications with a few simple lines of code. Using streamlit to build the demo of application is demonstrated here,You can run the chatbot.py file directly with the command streamlit run chatbot.py to view the effect and debug the web page.

Let’s begin by importing the Streamlit package to our chatbot.py script:pip install Streamlit == 1.28

import streamlit as st

Then,let’s build a lifecycle class to display the intermediate state of the agent response on the chat page.If you want to learn more about the life cycle, please click here.

from promptulate.hook import Hook, HookTable

class MidStepOutHook:
    @staticmethod
    def handle_agent_revise_plan(*args, **kwargs):
        messages = f"[Revised Plan] {kwargs['revised_plan']}"
        st.chat_message("assistant").write(messages)

    @staticmethod
    def handle_agent_action(*args, **kwargs):
        messages = f"[Thought] {kwargs['thought']}\n"
        messages += f"[Action] {kwargs['action']} args: {kwargs['action_input']}"
        st.chat_message("assistant").write(messages)

    @staticmethod
    def handle_agent_observation(*args, **kwargs):
        messages = f"[Observation] {kwargs['observation']}"
        st.chat_message("assistant").write(messages)

    @staticmethod
    def registry_hooks():
        """Registry and enable stdout hooks. StdoutHook can print colorful
        information."""
        Hook.registry_hook(
            HookTable.ON_AGENT_REVISE_PLAN,
            MidStepOutHook.handle_agent_revise_plan,
            "component",
        )
        Hook.registry_hook(
            HookTable.ON_AGENT_ACTION, MidStepOutHook.handle_agent_action, "component"
        )
        Hook.registry_hook(
            HookTable.ON_AGENT_OBSERVATION,
            MidStepOutHook.handle_agent_observation,
            "component",
        )

Next,Let’s build a function,and We will be adding our LLM, tools and agent initialization code to this function.

def build_agent(api_key: str) -> pne.ToolAgent:
    MidStepOutHook.registry_hooks()

    # calculator tool for arithmetics
    def math_tool(expression: str):
        """Useful for when you need to answer questions about math. This tool is only
        for math questions and nothing else. Only input math expressions.

        Args:
            expression: A mathematical expression, eg: 18^0.43

        Attention:
            Expressions can not exist variables!
            eg: (current age)^0.43 is wrong, you should use 18^0.43 instead.

        Returns:
            The result of the evaluation.
        """
        return calculator(expression)

    # reasoning based tool
    def word_problem_tool(question: str) -> str:
        """
        Useful for when you need to answer logic-based/reasoning questions.

        Args:
            question(str): Detail question, the description of the problem requires a
            detailed question context. Include a description of the problem

        Returns:
            question answer
        """
        system_prompt: str = """You are a reasoning agent tasked with solving t he user's logic-based questions.
        Logically arrive at the solution, and be factual.
        In your answers, clearly detail the steps involved and give the final answer.
        Provide the response in bullet points."""  # noqa
        llm = ChatOpenAI(private_api_key=api_key)
        return llm(f"{system_prompt}\n\nQuestion:{question}Answer:")

    # Wikipedia Tool
    def wikipedia_tool(keyword: str) -> str:
        """search by keyword in web.

        A useful tool for searching the Internet to find information on world events,
        issues, dates,years, etc. Worth using for general topics. Use precise questions.

        Args:
            keyword: keyword to search

        Returns:
            str: search result
        """
        return wikipedia_search(keyword)

    llm = ChatOpenAI(model="gpt-4-1106-preview", private_api_key=api_key)
    return pne.ToolAgent(tools=[wikipedia_tool, math_tool, word_problem_tool], llm=llm)

Set the style of our application:

# Create a sidebar to place the user parameter configuration
with st.sidebar:
    openai_api_key = st.text_input(
        "OpenAI API Key", key="chatbot_api_key", type="password"
    )
    "[Get an OpenAI API key](https://platform.openai.com/account/api-keys)"
    "[View the source code](https://github.com/hizeros/llm-streamlit/blob/master/Chatbot.py)"  # noqa
    
# Set title
st.title("💬 Math Wiz")
st.caption("🚀 Hi there! 👋 I am a reasoning tool by Promptulate to help you "
           "with your math or logic-based reasoning questions.")

Next, check our session state and render the user input and agent response to the chat page, so that we successfully build a simple math application using streamlit:

# Determine whether to initialize the message variable
# otherwise initialize a message dictionary
if "messages" not in st.session_state:
    st.session_state["messages"] = [
        {"role": "assistant", "content": "How can I help you?"}
    ]

# Traverse messages in session state
for msg in st.session_state.messages:
    st.chat_message(msg["role"]).write(msg["content"])

# User input
if prompt := st.chat_input():
    if not openai_api_key:
        st.info("Please add your OpenAI API key to continue.")
        st.stop()

    agent: pne.ToolAgent = build_agent(api_key=openai_api_key)

    # Add the message entered by the user to the list of messages in the session state
    st.session_state.messages.append({"role": "user", "content": prompt})
    # Display in the chat interface
    st.chat_message("user").write(prompt)

    response: str = agent.run(prompt)

    st.session_state.messages.append({"role": "assistant", "content": response})
    st.chat_message("assistant").write(response)

Let’s try to run it:streamlit run chatbot.py .The running result is as follows:
streamlit-application-run

Examples of other questions are given below for testing reference:

  1. Question 1
    • I have 3 apples and 4 oranges. I give half of my oranges away and buy two dozen new ones,along with three packs of strawberries.Each pack of strawberries has 30 strawberries.How many total pieces of fruit do I have at the end?
    • correct answer = 119
  2. Question 2
    • What is the cube root of 625?
    • correct answer = 8.5498
  3. Question 3
    • what is cube root of 81? Multiply with 13.27, and subtract 5.
    • correct answer = 52.4195
  4. Question 4
    • Steve’s sister is 10 years older than him. Steve was born when the cold war ended. When was Steve’s sister born?
    • correct answer = 1991 - 10 = 1981
  5. Question 5
    • give me the year when Tom Cruise’s Top Gun released raised to power 2
    • correct answer = 1987**2 = 3944196
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值