Skip to content

Gradio react agent chatbot

GradioReActAgentPack #

Bases: BaseLlamaPack

Gradio聊天机器人,用于与ReActAgent包进行对话。

Source code in llama_index/packs/gradio_react_agent_chatbot/base.py
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
class GradioReActAgentPack(BaseLlamaPack):
    """Gradio聊天机器人,用于与ReActAgent包进行对话。"""

    def __init__(
        self,
        tools_list: Optional[List[str]] = list(SUPPORTED_TOOLS.keys()),
        **kwargs: Any,
    ) -> None:
        """初始化参数。"""
        try:
            from ansi2html import Ansi2HTMLConverter
        except ImportError:
            raise ImportError("Please install ansi2html via `pip install ansi2html`")

        tools = []
        for t in tools_list:
            try:
                tools.append(SUPPORTED_TOOLS[t]())
            except KeyError:
                raise KeyError(f"Tool {t} is not supported.")
        self.tools = tools

        self.llm = OpenAI(model="gpt-4-1106-preview", max_tokens=2000)
        self.agent = ReActAgent.from_tools(
            tools=functools.reduce(
                lambda x, y: x.to_tool_list() + y.to_tool_list(), self.tools
            ),
            llm=self.llm,
            verbose=True,
        )

        self.thoughts = ""
        self.conv = Ansi2HTMLConverter()

    def get_modules(self) -> Dict[str, Any]:
        """获取模块。"""
        return {"agent": self.agent, "llm": self.llm, "tools": self.tools}

    def _handle_user_message(self, user_message, history):
        """处理用户提交的消息。清空消息框,并将消息追加到历史记录中。
"""
        return "", [*history, (user_message, "")]

    def _generate_response(
        self, chat_history: List[Tuple[str, str]]
    ) -> Tuple[str, List[Tuple[str, str]]]:
        """生成来自代理的响应,并捕获ReActAgent的思考过程中的标准输出。
"""
        with Capturing() as output:
            response = self.agent.stream_chat(chat_history[-1][0])
        ansi = "\n========\n".join(output)
        html_output = self.conv.convert(ansi)
        for token in response.response_gen:
            chat_history[-1][1] += token
            yield chat_history, str(html_output)

    def _reset_chat(self) -> Tuple[str, str]:
        """重置代理的聊天记录。并清除所有对话框。"""
        # clear agent history
        self.agent.reset()
        return "", "", ""  # clear textboxes

    def run(self, *args: Any, **kwargs: Any) -> Any:
        """运行流水线。"""
        import gradio as gr

        demo = gr.Blocks(
            theme="gstaff/xkcd",
            css="#box { height: 420px; overflow-y: scroll !important}",
        )
        with demo:
            gr.Markdown(
                "# Gradio ReActAgent Powered by LlamaIndex and LlamaHub 🦙\n"
                "This Gradio app is powered by LlamaIndex's `ReActAgent` with\n"
                "OpenAI's GPT-4-Turbo as the LLM. The tools are listed below.\n"
                "## Tools\n"
                "- [ArxivToolSpec](https://llamahub.ai/l/tools-arxiv)\n"
                "- [WikipediaToolSpec](https://llamahub.ai/l/tools-wikipedia)"
            )
            with gr.Row():
                chat_window = gr.Chatbot(
                    label="Message History",
                    scale=3,
                )
                console = gr.HTML(elem_id="box")
            with gr.Row():
                message = gr.Textbox(label="Write A Message", scale=4)
                clear = gr.ClearButton()

            message.submit(
                self._handle_user_message,
                [message, chat_window],
                [message, chat_window],
                queue=False,
            ).then(
                self._generate_response,
                chat_window,
                [chat_window, console],
            )
            clear.click(self._reset_chat, None, [message, chat_window, console])

        demo.launch(server_name="0.0.0.0", server_port=8080)

get_modules #

get_modules() -> Dict[str, Any]

获取模块。

Source code in llama_index/packs/gradio_react_agent_chatbot/base.py
67
68
69
def get_modules(self) -> Dict[str, Any]:
    """获取模块。"""
    return {"agent": self.agent, "llm": self.llm, "tools": self.tools}

run #

run(*args: Any, **kwargs: Any) -> Any

运行流水线。

Source code in llama_index/packs/gradio_react_agent_chatbot/base.py
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
def run(self, *args: Any, **kwargs: Any) -> Any:
    """运行流水线。"""
    import gradio as gr

    demo = gr.Blocks(
        theme="gstaff/xkcd",
        css="#box { height: 420px; overflow-y: scroll !important}",
    )
    with demo:
        gr.Markdown(
            "# Gradio ReActAgent Powered by LlamaIndex and LlamaHub 🦙\n"
            "This Gradio app is powered by LlamaIndex's `ReActAgent` with\n"
            "OpenAI's GPT-4-Turbo as the LLM. The tools are listed below.\n"
            "## Tools\n"
            "- [ArxivToolSpec](https://llamahub.ai/l/tools-arxiv)\n"
            "- [WikipediaToolSpec](https://llamahub.ai/l/tools-wikipedia)"
        )
        with gr.Row():
            chat_window = gr.Chatbot(
                label="Message History",
                scale=3,
            )
            console = gr.HTML(elem_id="box")
        with gr.Row():
            message = gr.Textbox(label="Write A Message", scale=4)
            clear = gr.ClearButton()

        message.submit(
            self._handle_user_message,
            [message, chat_window],
            [message, chat_window],
            queue=False,
        ).then(
            self._generate_response,
            chat_window,
            [chat_window, console],
        )
        clear.click(self._reset_chat, None, [message, chat_window, console])

    demo.launch(server_name="0.0.0.0", server_port=8080)