{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from langchain.chains import PALChain\n", "from langchain import OpenAI\n", "from langchain.chat_models import ChatOpenAI" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "llm = ChatOpenAI(temperature=0, max_tokens=512)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "pal_chain = PALChain.from_math_prompt(llm, verbose=True)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new PALChain chain...\u001b[0m\n", "\u001b[32;1m\u001b[1;3mdef solution():\n", " \"\"\"145 ÷ 5 × 6\"\"\"\n", " result = 145 / 5 * 6\n", " return result\u001b[0m\n", "174.0\n", "\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ "'174.0'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# question = \"在操场上活动的男生有67人,女生有31人,男、女生有同样多的人离开操场后,还在操场的男生人数是女生人数的4倍,离开操场的男、女生一共有多少人?\"\n", "# question = \"同学们进行广播操比赛,全班正好排成相等的6行。小红排在第二行,从头数,她站在第5个位置,从后数她站在第3个位置,这个班共有( )人\"\n", "question = \"145 ÷ 5 × 6\"\n", "pal_chain.run(question)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "from langchain.agents import Tool\n", "from langchain.utilities import PythonREPL" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "python_repl = PythonREPL()" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "\n" ] }, { "data": { "text/plain": [ "'2\\n'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "python_repl.run(\"print(1+1)\")" ] } ], "metadata": { "kernelspec": { "display_name": "base", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.11" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }