File size: 2,228 Bytes
1b9f020
 
 
 
 
040f332
 
1b9f020
9d0c72d
1b9f020
 
040f332
 
1b9f020
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d0c72d
1b9f020
9d0c72d
1b9f020
 
 
9d0c72d
 
 
 
1b9f020
9d0c72d
1b9f020
 
 
 
 
 
 
 
040f332
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from langchain.agents import initialize_agent, Tool, AgentExecutor
from .reasoning_strategy import ReasoningStrategy, ReasoningConfig
from langchain.docstore.wikipedia import Wikipedia
from langchain.agents import initialize_agent, Tool, AgentExecutor
from langchain.agents.react.base import DocstoreExplorer
from typing import Callable, Optional
import pprint

class ReActStrategy(ReasoningStrategy):
    def __init__(self, config: ReasoningConfig, display: Callable):
        super().__init__(config=config, display=display)
        print("Creating reAct strategy with config: ",)
        pprint.pprint(vars(config))

    def run(self, question) -> str:
        print('Using ReAct')
        self.display("Using 'ReAct' - (Reasoning and Action)")

        docstore = DocstoreExplorer(Wikipedia())
        tools = [
            Tool(
                name="Search",
                func=docstore.search,
                description="Search for a term in the docstore.",
            ),
            Tool(
                name="Lookup",
                func=docstore.lookup,
                description="Lookup a term in the docstore.",
            )
        ]
        re_act = initialize_agent(tools, self.llm, agent="react-docstore", verbose=True)
        agent_executor = AgentExecutor.from_agent_and_tools(
            agent=re_act.agent,
            tools=tools,
            verbose=True,
        )
        response_re_act = agent_executor.run(question)
        print(response_re_act)
        self.display(response_re_act)
        return response_re_act

def get_re_act_config(temperature: float = 0.7) -> ReasoningConfig:
    usage = """
    The solution for this problem requires searching for further information online, 
    generating reasoning traces and task-specific actions in an interleaved manner. 
    Starting with incomplete information this technique will prompt for the need to get 
    additional helpful information at each step. It allows for dynamic reasoning to create, 
    maintain, and adjust high-level plans for acting, while also interacting with external 
    sources to incorporate additional information into reasoning 
    """
    return ReasoningConfig(usage=usage, temperature=temperature)