File size: 3,069 Bytes
fdb9357
 
 
 
d674f7c
fdb9357
 
 
 
d674f7c
 
fdb9357
 
 
 
d674f7c
fdb9357
 
 
d674f7c
fdb9357
 
 
 
d674f7c
fdb9357
 
 
 
 
 
 
 
d674f7c
fdb9357
 
 
 
 
 
 
 
 
 
d674f7c
 
fdb9357
d674f7c
 
fdb9357
 
 
 
 
 
 
d674f7c
fdb9357
 
d674f7c
fdb9357
 
 
 
 
d674f7c
fdb9357
d674f7c
fdb9357
 
dccc64a
 
 
d674f7c
dccc64a
 
 
 
 
 
 
 
 
d674f7c
dccc64a
 
 
 
 
 
 
 
d674f7c
dccc64a
 
d674f7c
 
 
 
 
 
 
 
dccc64a
 
 
 
 
 
d674f7c
dccc64a
 
 
 
 
 
 
fdb9357
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d674f7c
fdb9357
 
 
 
 
 
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
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
135
136
137
138
139
140
141
142
143
{
 "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
}