Location>code7788 >text

2 AI Mind Chains You Need to Understand

Popularity:357 ℃/2024-08-15 13:55:57

The AI assistants we use generally go through the 2 steps of pre-training and fine-tuning, and although the trained models can answer many general-purpose classes of questions, they are still helpless when it comes to complex problems.

It wasn't until someone came up withthought chainway before addressing the model's ability to reason in the face of complex problems.

1, what is the chain of thought

thought chain(Chain of Thought, CoT) is the way used to improve the reasoning ability of AI models. Its core principle is 8 words:Simplify and break them down one by one

Thought Chain works by mimicking the human process of thinking through a problem by breaking down complex problems step-by-step and then solving these simple problems one by one moving forward to arrive at the final answer.

2. Ways of realizing intelligences

An AI Agent is used in scenarios that are smarter and emphasize more on reasoning, and thought chains are used in AI Agent scenarios, where they can be used to their advantage.

There are 2 common mechanisms for implementing thought chains in the AI Agent domain.Plan-and-ExecutorMechanisms andReActMechanisms.

2.1 Plan-and-Executor mechanism

Plan-and-ExecutorThe mechanism is separationPlanning and implementationThe 2 segments. It divides the problem solving process into two stages:Planning and implementation

Planning phase:

In this phase, the main focus is on analyzing the problem in the intelligentsia and developing a detailed plan for the solution. This stage usually involves a large number of computational processes that are used to determine the optimal plan of action. The planning results in the output of a specificaction plan

Implementation phase:

In this phase, the intelligences follow the action plan generated in the planning phase toprogressive implementationEach step. And monitor and adjust during the execution process to ensure the smooth execution of the program.

vantage

This mechanism is characterized by the separation of planning and execution, a separation that allows each stage to focus more on the task at hand, thus increasing efficiency. It is suitable for tasks that require high complexity and complex planning in advance.

drawbacks

There may be uncertainties during implementation, and this approach, because it is planned in advance, may not be adaptable to change and require frequent adjustments to the plan.

Example of a Plan-and-Executor step

For example, if I want to know when and where Jay Chou's latest concert is in 2024, it will be broken down into the following steps through the Plan-and-Executor mechanism:

Planning phase:

1. Find "Jay Chou Newest Concert Time and Venue 2024" on the search engine.
2. Check the official website or credible news websites for relevant information.
3. Summarize and record the concert dates and venues.

Implementation phase:

1. Looked for "Jay Chou's latest concert time and place in 2024" on search engines.
   - Result: Some relevant web links were found. 2.
2. Check the official website or credible news website for relevant information.
   - Result: We found the time and venue of Jay Chou's latest concert in 2024 on his official website.
3. Aggregate and record the concert dates and venues.
   - Result: 2024 Jay Chou's latest concert will be held on May 20, 2024 in Beijing.

2.2 ReAct mechanism

ReActThe mechanism is a combination of reasoning (Reasoning) and actions (Action) combined with an implementation that also introduces the observation (Observation) link in each execution (Action) are followed by an observation (Observation) the current state of affairs before reasoning about the next step (Reason)。

It emphasizes reacting and taking action as soon as a change in the environment is perceived, rather than making a detailed plan first.

vantage

Adaptable and able to respond quickly to environmental changes. Better suited to environments with high dynamics and uncertainty.

drawbacks

May be less efficient in complex tasks due to lack of pre-planning, where every step is performed: observation, reasoning, action.

Examples of ReAct steps

For example, if I want to know when and where Jay Chou's latest concert is in 2024, it will be broken down into the following steps through the ReAct mechanism:

Reasoning 1: The user wants to know what is the time and place of Jay's latest concert in 2024 and needs to find the latest information.
Action 1: Call Google's search API to perform a search.
Observation 1: The search ends, and the search results show some web information about The Latest Jay Chou Concert in 2024.

Inference 2: The search comes up with more web pages, probably browse the first 6 web pages for specific content.
Action 2: Click on the first webpage and start browsing.
Observation 2: The browsing ends, and the content of the browsed web pages mentions information about Jay's latest concert in 2024.

Reasoning 3: Summarize the question in response to the content of the webpage.
Result: output the final answer to the user.

3、Code example

3.1 Plan-and-Executor mechanism

LangChain framework has implemented the Plan-and-Executor mechanism, three lines of code can be called:

Core Code:

# loading plan
planner = load_chat_planner(model)
# Load Actuator
executor = load_agent_executor(model, tools, verbose=True)
# Load Agent
agent = PlanAndExecute(planner=planner, executor=executor, verbose=True)

Full Code:

from langchain_openai import ChatOpenAI
from langchain_experimental.plan_and_execute import (
    PlanAndExecute, load_agent_executor, load_chat_planner
)
from import BaseTool
from langchain_experimental.tools import PythonREPLTool

model = ChatOpenAI(
    model="gpt-3.5-turbo",
    openai_api_key="sk-xxxxxx",
    openai_api_base="/v1",
)

# Definition tools
class SumNumberTool(BaseTool):
    name = "Number Addition Calculator"
    description = "When you are asked to calculate2when adding numbers,Use this tool"

    def _run(self, a, b):
        return a["title"] + b["title"]

# Add to tool collection
tools = [SumNumberTool()]

# loading plan
planner = load_chat_planner(model)
# Load Actuator
executor = load_agent_executor(model, tools, verbose=True)
# Load Agent
agent = PlanAndExecute(planner=planner, executor=executor, verbose=True)

("Do the math for me. 3.941592623412424 + 4.3434532535353results")

Execution process:

The execution process is shown in the figure below, and from the process, we can see that the Agent does first plan the Nstepsand then step by stepstep

3.2 ReAct mechanism

LangChain framework has implemented the ReAct mechanism, two lines of code can be called:

Core Code:

# utilizationreActcues
prompt = ("hwchase17/structured-chat-agent")
# establishAgent
agent = create_structured_chat_agent(llm=model, tools=tools, prompt=prompt)

Full Code:

from langchain import hub
from import create_structured_chat_agent, AgentExecutor, tool
from import ConversationBufferMemory
from import HumanMessage
from import BaseTool
from langchain_openai import ChatOpenAI

model = ChatOpenAI(
    model="gpt-3.5-turbo",
    openai_api_key="sk-xxxxxx",
    openai_api_base="/v1",
)

# Definition tools
class SumNumberTool(BaseTool):
    name = "Number Addition Calculator"
    description = "When you are asked to calculate2when adding numbers,Use this tool"

    def _run(self, a, b):
        return a["title"] + b["title"]

# Add to tool collection
tools = [SumNumberTool()]

# utilizationreActcues
prompt = ("hwchase17/structured-chat-agent")

# establishAgent
agent = create_structured_chat_agent(llm=model, tools=tools, prompt=prompt)

# establish记忆组件
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)

# establishAgentactuators
agent_executor = AgentExecutor.from_agent_and_tools(
    agent=agent, tools=tools, memory=memory, verbose=True, handle_parsing_errors=True
)

agent_executor.invoke({"input": "Do the math for me. 3.941592623412424 + 4.3434532535353results"})

Execution process:

The execution process of the ReAct mechanism is left to the reader to try out. Compared to Plan-and-Executor, the ReAct mechanism has less planningstepsThe link.

4. Completion

This article mainly talks about the chain of thought, AI Agent scenarios in the chain of thought of 2 kinds of implementation mechanisms, code examples, the specific choice depends on the application scenarios and the complexity of the problem. I hope it will be helpful to you!

The end of this article! Welcome to pay attention to, add micro letter (ylxiao) exchange, the whole network can be searched (programmer half a cigarette)

Link to original article:/s/MTh0x9RYwnLNvaV_U1jOcQ

Recently launchedKnowledge PlanetDeveloper AI Plus", an AI app development column designed to help developers with one more skill in these difficult and transformative times. And.1V1 technical consulting, clearing obstacles in the path of career development and technology. Early bird price of $9. Add a tweet (yclxiao) to inquire.