远兮 commited on
Commit
d674f7c
·
1 Parent(s): 3de6db3

add test user redis

Browse files
agent_chat_memory.ipynb CHANGED
@@ -2,7 +2,7 @@
2
  "cells": [
3
  {
4
  "cell_type": "code",
5
- "execution_count": null,
6
  "metadata": {},
7
  "outputs": [],
8
  "source": [
@@ -16,7 +16,7 @@
16
  },
17
  {
18
  "cell_type": "code",
19
- "execution_count": null,
20
  "metadata": {},
21
  "outputs": [],
22
  "source": [
@@ -32,7 +32,7 @@
32
  },
33
  {
34
  "cell_type": "code",
35
- "execution_count": null,
36
  "metadata": {},
37
  "outputs": [],
38
  "source": [
@@ -41,7 +41,7 @@
41
  },
42
  {
43
  "cell_type": "code",
44
- "execution_count": null,
45
  "metadata": {},
46
  "outputs": [],
47
  "source": [
@@ -51,36 +51,163 @@
51
  },
52
  {
53
  "cell_type": "code",
54
- "execution_count": null,
55
  "metadata": {},
56
- "outputs": [],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  "source": [
58
  "agent_chain.run(input=\"你好,我叫李振\")"
59
  ]
60
  },
61
  {
62
  "cell_type": "code",
63
- "execution_count": null,
64
  "metadata": {},
65
- "outputs": [],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  "source": [
67
  "agent_chain.run(input=\"今天是几号\")"
68
  ]
69
  },
70
  {
71
  "cell_type": "code",
72
- "execution_count": null,
73
  "metadata": {},
74
- "outputs": [],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
  "source": [
76
  "agent_chain.run(input=\"回答对吗\")"
77
  ]
78
  },
79
  {
80
  "cell_type": "code",
81
- "execution_count": null,
82
  "metadata": {},
83
- "outputs": [],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  "source": [
85
  "agent_chain.run(input=\"我叫什么\")"
86
  ]
@@ -102,7 +229,7 @@
102
  "name": "python",
103
  "nbconvert_exporter": "python",
104
  "pygments_lexer": "ipython3",
105
- "version": "3.10.10"
106
  },
107
  "orig_nbformat": 4
108
  },
 
2
  "cells": [
3
  {
4
  "cell_type": "code",
5
+ "execution_count": 10,
6
  "metadata": {},
7
  "outputs": [],
8
  "source": [
 
16
  },
17
  {
18
  "cell_type": "code",
19
+ "execution_count": 11,
20
  "metadata": {},
21
  "outputs": [],
22
  "source": [
 
32
  },
33
  {
34
  "cell_type": "code",
35
+ "execution_count": 12,
36
  "metadata": {},
37
  "outputs": [],
38
  "source": [
 
41
  },
42
  {
43
  "cell_type": "code",
44
+ "execution_count": 13,
45
  "metadata": {},
46
  "outputs": [],
47
  "source": [
 
51
  },
52
  {
53
  "cell_type": "code",
54
+ "execution_count": 14,
55
  "metadata": {},
56
+ "outputs": [
57
+ {
58
+ "name": "stdout",
59
+ "output_type": "stream",
60
+ "text": [
61
+ "\n",
62
+ "\n",
63
+ "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
64
+ "\u001b[32;1m\u001b[1;3m{\n",
65
+ " \"action\": \"Final Answer\",\n",
66
+ " \"action_input\": \"你好,李振!有什么我可以帮你的吗?\"\n",
67
+ "}\u001b[0m\n",
68
+ "\n",
69
+ "\u001b[1m> Finished chain.\u001b[0m\n"
70
+ ]
71
+ },
72
+ {
73
+ "data": {
74
+ "text/plain": [
75
+ "'你好,李振!有什么我可以帮你的吗?'"
76
+ ]
77
+ },
78
+ "execution_count": 14,
79
+ "metadata": {},
80
+ "output_type": "execute_result"
81
+ }
82
+ ],
83
  "source": [
84
  "agent_chain.run(input=\"你好,我叫李振\")"
85
  ]
86
  },
87
  {
88
  "cell_type": "code",
89
+ "execution_count": 15,
90
  "metadata": {},
91
+ "outputs": [
92
+ {
93
+ "name": "stdout",
94
+ "output_type": "stream",
95
+ "text": [
96
+ "\n",
97
+ "\n",
98
+ "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
99
+ "\u001b[32;1m\u001b[1;3m```json\n",
100
+ "{\n",
101
+ " \"action\": \"Current Search\",\n",
102
+ " \"action_input\": \"今天日期\"\n",
103
+ "}\n",
104
+ "```\u001b[0m\n",
105
+ "Observation: \u001b[36;1m\u001b[1;3m随着Today'sDate365,迅速得到当前日期。 无论你有一台电脑,平板电脑或智能手机,当前日期会立即出现。 今天的日期是:. 星期六; 2023 六月24; 2023/06/24 ...\u001b[0m\n",
106
+ "Thought:\u001b[32;1m\u001b[1;3m```json\n",
107
+ "{\n",
108
+ " \"action\": \"Final Answer\",\n",
109
+ " \"action_input\": \"今天的日期是:星期六; 2023 六月24; 2023/06/24\"\n",
110
+ "}\n",
111
+ "```\u001b[0m\n",
112
+ "\n",
113
+ "\u001b[1m> Finished chain.\u001b[0m\n"
114
+ ]
115
+ },
116
+ {
117
+ "data": {
118
+ "text/plain": [
119
+ "'今天的日期是:星期六; 2023 六月24; 2023/06/24'"
120
+ ]
121
+ },
122
+ "execution_count": 15,
123
+ "metadata": {},
124
+ "output_type": "execute_result"
125
+ }
126
+ ],
127
  "source": [
128
  "agent_chain.run(input=\"今天是几号\")"
129
  ]
130
  },
131
  {
132
  "cell_type": "code",
133
+ "execution_count": 16,
134
  "metadata": {},
135
+ "outputs": [
136
+ {
137
+ "name": "stdout",
138
+ "output_type": "stream",
139
+ "text": [
140
+ "\n",
141
+ "\n",
142
+ "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
143
+ "\u001b[32;1m\u001b[1;3m{\n",
144
+ " \"action\": \"Current Search\",\n",
145
+ " \"action_input\": \"回答对吗\"\n",
146
+ "}\u001b[0m\n",
147
+ "Observation: \u001b[36;1m\u001b[1;3m对吗?对吗是疑问对吧是反问|“对吗?”,是自己有疑问,想知道对还是不对。 “对吧?”,是自己认为对,但不是很肯定。|对吗? 是问别人的意见对吧?\u001b[0m\n",
148
+ "Thought:\u001b[32;1m\u001b[1;3m{\n",
149
+ " \"action\": \"Final Answer\",\n",
150
+ " \"action_input\": \"回答对吗? 是问别人的意见对吧?\"\n",
151
+ "}\u001b[0m\n",
152
+ "\n",
153
+ "\u001b[1m> Finished chain.\u001b[0m\n"
154
+ ]
155
+ },
156
+ {
157
+ "data": {
158
+ "text/plain": [
159
+ "'回答对吗? 是问别人的意见对吧?'"
160
+ ]
161
+ },
162
+ "execution_count": 16,
163
+ "metadata": {},
164
+ "output_type": "execute_result"
165
+ }
166
+ ],
167
  "source": [
168
  "agent_chain.run(input=\"回答对吗\")"
169
  ]
170
  },
171
  {
172
  "cell_type": "code",
173
+ "execution_count": 17,
174
  "metadata": {},
175
+ "outputs": [
176
+ {
177
+ "name": "stdout",
178
+ "output_type": "stream",
179
+ "text": [
180
+ "\n",
181
+ "\n",
182
+ "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
183
+ "\u001b[32;1m\u001b[1;3m```json\n",
184
+ "{\n",
185
+ " \"action\": \"Current Search\",\n",
186
+ " \"action_input\": \"what is my name\"\n",
187
+ "}\n",
188
+ "```\u001b[0m\n",
189
+ "Observation: \u001b[36;1m\u001b[1;3m\"My Name Is\" is a song by American rapper Eminem from his second album The Slim Shady LP. It is also the opening song and lead single of the album. The song samples British singer Labi Siffre's 1975 track \"I Got The...\" as a bass and guitar riff by British pop rock duo Chas & Dave.\u001b[0m\n",
190
+ "Thought:\u001b[32;1m\u001b[1;3m```json\n",
191
+ "{\n",
192
+ " \"action\": \"Final Answer\",\n",
193
+ " \"action_input\": \"I'm sorry, but the response to your last comment was not related to your question of 'what is my name'. Would you like me to look up your name for you?\"\n",
194
+ "}\n",
195
+ "```\u001b[0m\n",
196
+ "\n",
197
+ "\u001b[1m> Finished chain.\u001b[0m\n"
198
+ ]
199
+ },
200
+ {
201
+ "data": {
202
+ "text/plain": [
203
+ "\"I'm sorry, but the response to your last comment was not related to your question of 'what is my name'. Would you like me to look up your name for you?\""
204
+ ]
205
+ },
206
+ "execution_count": 17,
207
+ "metadata": {},
208
+ "output_type": "execute_result"
209
+ }
210
+ ],
211
  "source": [
212
  "agent_chain.run(input=\"我叫什么\")"
213
  ]
 
229
  "name": "python",
230
  "nbconvert_exporter": "python",
231
  "pygments_lexer": "ipython3",
232
+ "version": "3.10.11"
233
  },
234
  "orig_nbformat": 4
235
  },
agent_execution_time_iterations.ipynb CHANGED
@@ -2,7 +2,7 @@
2
  "cells": [
3
  {
4
  "cell_type": "code",
5
- "execution_count": 9,
6
  "metadata": {},
7
  "outputs": [],
8
  "source": [
@@ -14,7 +14,7 @@
14
  },
15
  {
16
  "cell_type": "code",
17
- "execution_count": 10,
18
  "metadata": {},
19
  "outputs": [],
20
  "source": [
@@ -23,7 +23,7 @@
23
  },
24
  {
25
  "cell_type": "code",
26
- "execution_count": 11,
27
  "metadata": {},
28
  "outputs": [],
29
  "source": [
@@ -32,7 +32,7 @@
32
  },
33
  {
34
  "cell_type": "code",
35
- "execution_count": 12,
36
  "metadata": {},
37
  "outputs": [],
38
  "source": [
@@ -41,7 +41,7 @@
41
  },
42
  {
43
  "cell_type": "code",
44
- "execution_count": 13,
45
  "metadata": {},
46
  "outputs": [],
47
  "source": [
@@ -56,7 +56,7 @@
56
  },
57
  {
58
  "cell_type": "code",
59
- "execution_count": 14,
60
  "metadata": {},
61
  "outputs": [
62
  {
@@ -65,34 +65,60 @@
65
  "text": [
66
  "\n",
67
  "\n",
68
- "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
69
- "\u001b[32;1m\u001b[1;3m What can I do to answer this question?\n",
70
- "Action: Jester\n",
71
- "Action Input: foo\u001b[0m\n",
72
- "Observation: \u001b[36;1m\u001b[1;3mfoo\u001b[0m\n",
73
- "Thought:\u001b[32;1m\u001b[1;3m Is there more I can do?\n",
74
- "Action: Jester\n",
75
- "Action Input: foo\u001b[0m\n",
76
- "Observation: \u001b[36;1m\u001b[1;3mfoo\u001b[0m\n",
77
- "Thought:\u001b[32;1m\u001b[1;3m Is there more I can do?\n",
78
- "Action: Jester\n",
79
- "Action Input: foo\u001b[0m\n",
80
- "Observation: \u001b[36;1m\u001b[1;3mfoo\u001b[0m\n",
81
- "Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n",
82
- "Final Answer: foo\u001b[0m\n",
83
- "\n",
84
- "\u001b[1m> Finished chain.\u001b[0m\n"
85
  ]
86
  },
87
  {
88
- "data": {
89
- "text/plain": [
90
- "'foo'"
91
- ]
92
- },
93
- "execution_count": 14,
94
- "metadata": {},
95
- "output_type": "execute_result"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  }
97
  ],
98
  "source": [
@@ -101,7 +127,7 @@
101
  },
102
  {
103
  "cell_type": "code",
104
- "execution_count": 17,
105
  "metadata": {},
106
  "outputs": [],
107
  "source": [
@@ -110,7 +136,7 @@
110
  },
111
  {
112
  "cell_type": "code",
113
- "execution_count": 18,
114
  "metadata": {},
115
  "outputs": [
116
  {
 
2
  "cells": [
3
  {
4
  "cell_type": "code",
5
+ "execution_count": 1,
6
  "metadata": {},
7
  "outputs": [],
8
  "source": [
 
14
  },
15
  {
16
  "cell_type": "code",
17
+ "execution_count": 2,
18
  "metadata": {},
19
  "outputs": [],
20
  "source": [
 
23
  },
24
  {
25
  "cell_type": "code",
26
+ "execution_count": 3,
27
  "metadata": {},
28
  "outputs": [],
29
  "source": [
 
32
  },
33
  {
34
  "cell_type": "code",
35
+ "execution_count": 4,
36
  "metadata": {},
37
  "outputs": [],
38
  "source": [
 
41
  },
42
  {
43
  "cell_type": "code",
44
+ "execution_count": 5,
45
  "metadata": {},
46
  "outputs": [],
47
  "source": [
 
56
  },
57
  {
58
  "cell_type": "code",
59
+ "execution_count": 7,
60
  "metadata": {},
61
  "outputs": [
62
  {
 
65
  "text": [
66
  "\n",
67
  "\n",
68
+ "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  ]
70
  },
71
  {
72
+ "name": "stderr",
73
+ "output_type": "stream",
74
+ "text": [
75
+ "Retrying langchain.llms.openai.completion_with_retry.<locals>._completion_with_retry in 4.0 seconds as it raised APIError: parse endPoint error,please check path {\"error\":{\"code\":\"path not support\",\"message\":\"parse endPoint error,please check path\",\"param\":\"\",\"type\":\"invalid_request_error\"}} 556 {'error': {'code': 'path not support', 'message': 'parse endPoint error,please check path', 'param': '', 'type': 'invalid_request_error'}} {'Date': 'Mon, 26 Jun 2023 08:39:47 GMT', 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': '130', 'Connection': 'keep-alive', 'X-Zyb-Trace-Id': '9a3b303586dbda76', 'tracecode': '23874188890171513031062616'}.\n",
76
+ "Retrying langchain.llms.openai.completion_with_retry.<locals>._completion_with_retry in 4.0 seconds as it raised APIError: parse endPoint error,please check path {\"error\":{\"code\":\"path not support\",\"message\":\"parse endPoint error,please check path\",\"param\":\"\",\"type\":\"invalid_request_error\"}} 556 {'error': {'code': 'path not support', 'message': 'parse endPoint error,please check path', 'param': '', 'type': 'invalid_request_error'}} {'Date': 'Mon, 26 Jun 2023 08:39:51 GMT', 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': '130', 'Connection': 'keep-alive', 'X-Zyb-Trace-Id': '8158e3ceb252601', 'tracecode': '23917917750171513035062616'}.\n",
77
+ "Retrying langchain.llms.openai.completion_with_retry.<locals>._completion_with_retry in 4.0 seconds as it raised APIError: parse endPoint error,please check path {\"error\":{\"code\":\"path not support\",\"message\":\"parse endPoint error,please check path\",\"param\":\"\",\"type\":\"invalid_request_error\"}} 556 {'error': {'code': 'path not support', 'message': 'parse endPoint error,please check path', 'param': '', 'type': 'invalid_request_error'}} {'Date': 'Mon, 26 Jun 2023 08:39:57 GMT', 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': '130', 'Connection': 'keep-alive', 'X-Zyb-Trace-Id': 'e0f613def18f6196', 'tracecode': '23973472300171513031062616'}.\n",
78
+ "Retrying langchain.llms.openai.completion_with_retry.<locals>._completion_with_retry in 8.0 seconds as it raised APIError: parse endPoint error,please check path {\"error\":{\"code\":\"path not support\",\"message\":\"parse endPoint error,please check path\",\"param\":\"\",\"type\":\"invalid_request_error\"}} 556 {'error': {'code': 'path not support', 'message': 'parse endPoint error,please check path', 'param': '', 'type': 'invalid_request_error'}} {'Date': 'Mon, 26 Jun 2023 08:40:01 GMT', 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': '130', 'Connection': 'keep-alive', 'X-Zyb-Trace-Id': '405129fb51d18208', 'tracecode': '24016147720171513035062616'}.\n",
79
+ "Retrying langchain.llms.openai.completion_with_retry.<locals>._completion_with_retry in 10.0 seconds as it raised APIError: parse endPoint error,please check path {\"error\":{\"code\":\"path not support\",\"message\":\"parse endPoint error,please check path\",\"param\":\"\",\"type\":\"invalid_request_error\"}} 556 {'error': {'code': 'path not support', 'message': 'parse endPoint error,please check path', 'param': '', 'type': 'invalid_request_error'}} {'Date': 'Mon, 26 Jun 2023 08:40:10 GMT', 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': '130', 'Connection': 'keep-alive', 'X-Zyb-Trace-Id': 'd9103584a7e2067d', 'tracecode': '24101028070171513031062616'}.\n"
80
+ ]
81
+ },
82
+ {
83
+ "ename": "APIError",
84
+ "evalue": "parse endPoint error,please check path {\"error\":{\"code\":\"path not support\",\"message\":\"parse endPoint error,please check path\",\"param\":\"\",\"type\":\"invalid_request_error\"}} 556 {'error': {'code': 'path not support', 'message': 'parse endPoint error,please check path', 'param': '', 'type': 'invalid_request_error'}} {'Date': 'Mon, 26 Jun 2023 08:40:20 GMT', 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': '130', 'Connection': 'keep-alive', 'X-Zyb-Trace-Id': 'b24a6bb210f6b0cb', 'tracecode': '24204725470171513035062616'}",
85
+ "output_type": "error",
86
+ "traceback": [
87
+ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
88
+ "\u001b[0;31mAPIError\u001b[0m Traceback (most recent call last)",
89
+ "Cell \u001b[0;32mIn[7], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m agent\u001b[39m.\u001b[39;49mrun(adversarial_prompt)\n",
90
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/langchain/chains/base.py:236\u001b[0m, in \u001b[0;36mChain.run\u001b[0;34m(self, callbacks, *args, **kwargs)\u001b[0m\n\u001b[1;32m 234\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mlen\u001b[39m(args) \u001b[39m!=\u001b[39m \u001b[39m1\u001b[39m:\n\u001b[1;32m 235\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mValueError\u001b[39;00m(\u001b[39m\"\u001b[39m\u001b[39m`run` supports only one positional argument.\u001b[39m\u001b[39m\"\u001b[39m)\n\u001b[0;32m--> 236\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39;49m(args[\u001b[39m0\u001b[39;49m], callbacks\u001b[39m=\u001b[39;49mcallbacks)[\u001b[39mself\u001b[39m\u001b[39m.\u001b[39moutput_keys[\u001b[39m0\u001b[39m]]\n\u001b[1;32m 238\u001b[0m \u001b[39mif\u001b[39;00m kwargs \u001b[39mand\u001b[39;00m \u001b[39mnot\u001b[39;00m args:\n\u001b[1;32m 239\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39m(kwargs, callbacks\u001b[39m=\u001b[39mcallbacks)[\u001b[39mself\u001b[39m\u001b[39m.\u001b[39moutput_keys[\u001b[39m0\u001b[39m]]\n",
91
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/langchain/chains/base.py:140\u001b[0m, in \u001b[0;36mChain.__call__\u001b[0;34m(self, inputs, return_only_outputs, callbacks)\u001b[0m\n\u001b[1;32m 138\u001b[0m \u001b[39mexcept\u001b[39;00m (\u001b[39mKeyboardInterrupt\u001b[39;00m, \u001b[39mException\u001b[39;00m) \u001b[39mas\u001b[39;00m e:\n\u001b[1;32m 139\u001b[0m run_manager\u001b[39m.\u001b[39mon_chain_error(e)\n\u001b[0;32m--> 140\u001b[0m \u001b[39mraise\u001b[39;00m e\n\u001b[1;32m 141\u001b[0m run_manager\u001b[39m.\u001b[39mon_chain_end(outputs)\n\u001b[1;32m 142\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mprep_outputs(inputs, outputs, return_only_outputs)\n",
92
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/langchain/chains/base.py:134\u001b[0m, in \u001b[0;36mChain.__call__\u001b[0;34m(self, inputs, return_only_outputs, callbacks)\u001b[0m\n\u001b[1;32m 128\u001b[0m run_manager \u001b[39m=\u001b[39m callback_manager\u001b[39m.\u001b[39mon_chain_start(\n\u001b[1;32m 129\u001b[0m {\u001b[39m\"\u001b[39m\u001b[39mname\u001b[39m\u001b[39m\"\u001b[39m: \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m\u001b[39m__class__\u001b[39m\u001b[39m.\u001b[39m\u001b[39m__name__\u001b[39m},\n\u001b[1;32m 130\u001b[0m inputs,\n\u001b[1;32m 131\u001b[0m )\n\u001b[1;32m 132\u001b[0m \u001b[39mtry\u001b[39;00m:\n\u001b[1;32m 133\u001b[0m outputs \u001b[39m=\u001b[39m (\n\u001b[0;32m--> 134\u001b[0m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_call(inputs, run_manager\u001b[39m=\u001b[39;49mrun_manager)\n\u001b[1;32m 135\u001b[0m \u001b[39mif\u001b[39;00m new_arg_supported\n\u001b[1;32m 136\u001b[0m \u001b[39melse\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_call(inputs)\n\u001b[1;32m 137\u001b[0m )\n\u001b[1;32m 138\u001b[0m \u001b[39mexcept\u001b[39;00m (\u001b[39mKeyboardInterrupt\u001b[39;00m, \u001b[39mException\u001b[39;00m) \u001b[39mas\u001b[39;00m e:\n\u001b[1;32m 139\u001b[0m run_manager\u001b[39m.\u001b[39mon_chain_error(e)\n",
93
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/langchain/agents/agent.py:922\u001b[0m, in \u001b[0;36mAgentExecutor._call\u001b[0;34m(self, inputs, run_manager)\u001b[0m\n\u001b[1;32m 920\u001b[0m \u001b[39m# We now enter the agent loop (until it returns something).\u001b[39;00m\n\u001b[1;32m 921\u001b[0m \u001b[39mwhile\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_should_continue(iterations, time_elapsed):\n\u001b[0;32m--> 922\u001b[0m next_step_output \u001b[39m=\u001b[39m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_take_next_step(\n\u001b[1;32m 923\u001b[0m name_to_tool_map,\n\u001b[1;32m 924\u001b[0m color_mapping,\n\u001b[1;32m 925\u001b[0m inputs,\n\u001b[1;32m 926\u001b[0m intermediate_steps,\n\u001b[1;32m 927\u001b[0m run_manager\u001b[39m=\u001b[39;49mrun_manager,\n\u001b[1;32m 928\u001b[0m )\n\u001b[1;32m 929\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39misinstance\u001b[39m(next_step_output, AgentFinish):\n\u001b[1;32m 930\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_return(\n\u001b[1;32m 931\u001b[0m next_step_output, intermediate_steps, run_manager\u001b[39m=\u001b[39mrun_manager\n\u001b[1;32m 932\u001b[0m )\n",
94
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/langchain/agents/agent.py:766\u001b[0m, in \u001b[0;36mAgentExecutor._take_next_step\u001b[0;34m(self, name_to_tool_map, color_mapping, inputs, intermediate_steps, run_manager)\u001b[0m\n\u001b[1;32m 764\u001b[0m \u001b[39mexcept\u001b[39;00m \u001b[39mException\u001b[39;00m \u001b[39mas\u001b[39;00m e:\n\u001b[1;32m 765\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mnot\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mhandle_parsing_errors:\n\u001b[0;32m--> 766\u001b[0m \u001b[39mraise\u001b[39;00m e\n\u001b[1;32m 767\u001b[0m text \u001b[39m=\u001b[39m \u001b[39mstr\u001b[39m(e)\u001b[39m.\u001b[39msplit(\u001b[39m\"\u001b[39m\u001b[39m`\u001b[39m\u001b[39m\"\u001b[39m)[\u001b[39m1\u001b[39m]\n\u001b[1;32m 768\u001b[0m observation \u001b[39m=\u001b[39m \u001b[39m\"\u001b[39m\u001b[39mInvalid or incomplete response\u001b[39m\u001b[39m\"\u001b[39m\n",
95
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/langchain/agents/agent.py:759\u001b[0m, in \u001b[0;36mAgentExecutor._take_next_step\u001b[0;34m(self, name_to_tool_map, color_mapping, inputs, intermediate_steps, run_manager)\u001b[0m\n\u001b[1;32m 753\u001b[0m \u001b[39m\u001b[39m\u001b[39m\"\"\"Take a single step in the thought-action-observation loop.\u001b[39;00m\n\u001b[1;32m 754\u001b[0m \n\u001b[1;32m 755\u001b[0m \u001b[39mOverride this to take control of how the agent makes and acts on choices.\u001b[39;00m\n\u001b[1;32m 756\u001b[0m \u001b[39m\"\"\"\u001b[39;00m\n\u001b[1;32m 757\u001b[0m \u001b[39mtry\u001b[39;00m:\n\u001b[1;32m 758\u001b[0m \u001b[39m# Call the LLM to see what to do.\u001b[39;00m\n\u001b[0;32m--> 759\u001b[0m output \u001b[39m=\u001b[39m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49magent\u001b[39m.\u001b[39;49mplan(\n\u001b[1;32m 760\u001b[0m intermediate_steps,\n\u001b[1;32m 761\u001b[0m callbacks\u001b[39m=\u001b[39;49mrun_manager\u001b[39m.\u001b[39;49mget_child() \u001b[39mif\u001b[39;49;00m run_manager \u001b[39melse\u001b[39;49;00m \u001b[39mNone\u001b[39;49;00m,\n\u001b[1;32m 762\u001b[0m \u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49minputs,\n\u001b[1;32m 763\u001b[0m )\n\u001b[1;32m 764\u001b[0m \u001b[39mexcept\u001b[39;00m \u001b[39mException\u001b[39;00m \u001b[39mas\u001b[39;00m e:\n\u001b[1;32m 765\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mnot\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mhandle_parsing_errors:\n",
96
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/langchain/agents/agent.py:442\u001b[0m, in \u001b[0;36mAgent.plan\u001b[0;34m(self, intermediate_steps, callbacks, **kwargs)\u001b[0m\n\u001b[1;32m 430\u001b[0m \u001b[39m\u001b[39m\u001b[39m\"\"\"Given input, decided what to do.\u001b[39;00m\n\u001b[1;32m 431\u001b[0m \n\u001b[1;32m 432\u001b[0m \u001b[39mArgs:\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 439\u001b[0m \u001b[39m Action specifying what tool to use.\u001b[39;00m\n\u001b[1;32m 440\u001b[0m \u001b[39m\"\"\"\u001b[39;00m\n\u001b[1;32m 441\u001b[0m full_inputs \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mget_full_inputs(intermediate_steps, \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs)\n\u001b[0;32m--> 442\u001b[0m full_output \u001b[39m=\u001b[39m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mllm_chain\u001b[39m.\u001b[39;49mpredict(callbacks\u001b[39m=\u001b[39;49mcallbacks, \u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mfull_inputs)\n\u001b[1;32m 443\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39moutput_parser\u001b[39m.\u001b[39mparse(full_output)\n",
97
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/langchain/chains/llm.py:213\u001b[0m, in \u001b[0;36mLLMChain.predict\u001b[0;34m(self, callbacks, **kwargs)\u001b[0m\n\u001b[1;32m 198\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mpredict\u001b[39m(\u001b[39mself\u001b[39m, callbacks: Callbacks \u001b[39m=\u001b[39m \u001b[39mNone\u001b[39;00m, \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs: Any) \u001b[39m-\u001b[39m\u001b[39m>\u001b[39m \u001b[39mstr\u001b[39m:\n\u001b[1;32m 199\u001b[0m \u001b[39m \u001b[39m\u001b[39m\"\"\"Format prompt with kwargs and pass to LLM.\u001b[39;00m\n\u001b[1;32m 200\u001b[0m \n\u001b[1;32m 201\u001b[0m \u001b[39m Args:\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 211\u001b[0m \u001b[39m completion = llm.predict(adjective=\"funny\")\u001b[39;00m\n\u001b[1;32m 212\u001b[0m \u001b[39m \"\"\"\u001b[39;00m\n\u001b[0;32m--> 213\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39;49m(kwargs, callbacks\u001b[39m=\u001b[39;49mcallbacks)[\u001b[39mself\u001b[39m\u001b[39m.\u001b[39moutput_key]\n",
98
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/langchain/chains/base.py:140\u001b[0m, in \u001b[0;36mChain.__call__\u001b[0;34m(self, inputs, return_only_outputs, callbacks)\u001b[0m\n\u001b[1;32m 138\u001b[0m \u001b[39mexcept\u001b[39;00m (\u001b[39mKeyboardInterrupt\u001b[39;00m, \u001b[39mException\u001b[39;00m) \u001b[39mas\u001b[39;00m e:\n\u001b[1;32m 139\u001b[0m run_manager\u001b[39m.\u001b[39mon_chain_error(e)\n\u001b[0;32m--> 140\u001b[0m \u001b[39mraise\u001b[39;00m e\n\u001b[1;32m 141\u001b[0m run_manager\u001b[39m.\u001b[39mon_chain_end(outputs)\n\u001b[1;32m 142\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mprep_outputs(inputs, outputs, return_only_outputs)\n",
99
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/langchain/chains/base.py:134\u001b[0m, in \u001b[0;36mChain.__call__\u001b[0;34m(self, inputs, return_only_outputs, callbacks)\u001b[0m\n\u001b[1;32m 128\u001b[0m run_manager \u001b[39m=\u001b[39m callback_manager\u001b[39m.\u001b[39mon_chain_start(\n\u001b[1;32m 129\u001b[0m {\u001b[39m\"\u001b[39m\u001b[39mname\u001b[39m\u001b[39m\"\u001b[39m: \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m\u001b[39m__class__\u001b[39m\u001b[39m.\u001b[39m\u001b[39m__name__\u001b[39m},\n\u001b[1;32m 130\u001b[0m inputs,\n\u001b[1;32m 131\u001b[0m )\n\u001b[1;32m 132\u001b[0m \u001b[39mtry\u001b[39;00m:\n\u001b[1;32m 133\u001b[0m outputs \u001b[39m=\u001b[39m (\n\u001b[0;32m--> 134\u001b[0m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_call(inputs, run_manager\u001b[39m=\u001b[39;49mrun_manager)\n\u001b[1;32m 135\u001b[0m \u001b[39mif\u001b[39;00m new_arg_supported\n\u001b[1;32m 136\u001b[0m \u001b[39melse\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_call(inputs)\n\u001b[1;32m 137\u001b[0m )\n\u001b[1;32m 138\u001b[0m \u001b[39mexcept\u001b[39;00m (\u001b[39mKeyboardInterrupt\u001b[39;00m, \u001b[39mException\u001b[39;00m) \u001b[39mas\u001b[39;00m e:\n\u001b[1;32m 139\u001b[0m run_manager\u001b[39m.\u001b[39mon_chain_error(e)\n",
100
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/langchain/chains/llm.py:69\u001b[0m, in \u001b[0;36mLLMChain._call\u001b[0;34m(self, inputs, run_manager)\u001b[0m\n\u001b[1;32m 64\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39m_call\u001b[39m(\n\u001b[1;32m 65\u001b[0m \u001b[39mself\u001b[39m,\n\u001b[1;32m 66\u001b[0m inputs: Dict[\u001b[39mstr\u001b[39m, Any],\n\u001b[1;32m 67\u001b[0m run_manager: Optional[CallbackManagerForChainRun] \u001b[39m=\u001b[39m \u001b[39mNone\u001b[39;00m,\n\u001b[1;32m 68\u001b[0m ) \u001b[39m-\u001b[39m\u001b[39m>\u001b[39m Dict[\u001b[39mstr\u001b[39m, \u001b[39mstr\u001b[39m]:\n\u001b[0;32m---> 69\u001b[0m response \u001b[39m=\u001b[39m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mgenerate([inputs], run_manager\u001b[39m=\u001b[39;49mrun_manager)\n\u001b[1;32m 70\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mcreate_outputs(response)[\u001b[39m0\u001b[39m]\n",
101
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/langchain/chains/llm.py:79\u001b[0m, in \u001b[0;36mLLMChain.generate\u001b[0;34m(self, input_list, run_manager)\u001b[0m\n\u001b[1;32m 77\u001b[0m \u001b[39m\u001b[39m\u001b[39m\"\"\"Generate LLM result from inputs.\"\"\"\u001b[39;00m\n\u001b[1;32m 78\u001b[0m prompts, stop \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mprep_prompts(input_list, run_manager\u001b[39m=\u001b[39mrun_manager)\n\u001b[0;32m---> 79\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mllm\u001b[39m.\u001b[39;49mgenerate_prompt(\n\u001b[1;32m 80\u001b[0m prompts, stop, callbacks\u001b[39m=\u001b[39;49mrun_manager\u001b[39m.\u001b[39;49mget_child() \u001b[39mif\u001b[39;49;00m run_manager \u001b[39melse\u001b[39;49;00m \u001b[39mNone\u001b[39;49;00m\n\u001b[1;32m 81\u001b[0m )\n",
102
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/langchain/llms/base.py:134\u001b[0m, in \u001b[0;36mBaseLLM.generate_prompt\u001b[0;34m(self, prompts, stop, callbacks)\u001b[0m\n\u001b[1;32m 127\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mgenerate_prompt\u001b[39m(\n\u001b[1;32m 128\u001b[0m \u001b[39mself\u001b[39m,\n\u001b[1;32m 129\u001b[0m prompts: List[PromptValue],\n\u001b[1;32m 130\u001b[0m stop: Optional[List[\u001b[39mstr\u001b[39m]] \u001b[39m=\u001b[39m \u001b[39mNone\u001b[39;00m,\n\u001b[1;32m 131\u001b[0m callbacks: Callbacks \u001b[39m=\u001b[39m \u001b[39mNone\u001b[39;00m,\n\u001b[1;32m 132\u001b[0m ) \u001b[39m-\u001b[39m\u001b[39m>\u001b[39m LLMResult:\n\u001b[1;32m 133\u001b[0m prompt_strings \u001b[39m=\u001b[39m [p\u001b[39m.\u001b[39mto_string() \u001b[39mfor\u001b[39;00m p \u001b[39min\u001b[39;00m prompts]\n\u001b[0;32m--> 134\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mgenerate(prompt_strings, stop\u001b[39m=\u001b[39;49mstop, callbacks\u001b[39m=\u001b[39;49mcallbacks)\n",
103
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/langchain/llms/base.py:191\u001b[0m, in \u001b[0;36mBaseLLM.generate\u001b[0;34m(self, prompts, stop, callbacks)\u001b[0m\n\u001b[1;32m 189\u001b[0m \u001b[39mexcept\u001b[39;00m (\u001b[39mKeyboardInterrupt\u001b[39;00m, \u001b[39mException\u001b[39;00m) \u001b[39mas\u001b[39;00m e:\n\u001b[1;32m 190\u001b[0m run_manager\u001b[39m.\u001b[39mon_llm_error(e)\n\u001b[0;32m--> 191\u001b[0m \u001b[39mraise\u001b[39;00m e\n\u001b[1;32m 192\u001b[0m run_manager\u001b[39m.\u001b[39mon_llm_end(output)\n\u001b[1;32m 193\u001b[0m \u001b[39mreturn\u001b[39;00m output\n",
104
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/langchain/llms/base.py:185\u001b[0m, in \u001b[0;36mBaseLLM.generate\u001b[0;34m(self, prompts, stop, callbacks)\u001b[0m\n\u001b[1;32m 180\u001b[0m run_manager \u001b[39m=\u001b[39m callback_manager\u001b[39m.\u001b[39mon_llm_start(\n\u001b[1;32m 181\u001b[0m {\u001b[39m\"\u001b[39m\u001b[39mname\u001b[39m\u001b[39m\"\u001b[39m: \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m\u001b[39m__class__\u001b[39m\u001b[39m.\u001b[39m\u001b[39m__name__\u001b[39m}, prompts, invocation_params\u001b[39m=\u001b[39mparams\n\u001b[1;32m 182\u001b[0m )\n\u001b[1;32m 183\u001b[0m \u001b[39mtry\u001b[39;00m:\n\u001b[1;32m 184\u001b[0m output \u001b[39m=\u001b[39m (\n\u001b[0;32m--> 185\u001b[0m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_generate(prompts, stop\u001b[39m=\u001b[39;49mstop, run_manager\u001b[39m=\u001b[39;49mrun_manager)\n\u001b[1;32m 186\u001b[0m \u001b[39mif\u001b[39;00m new_arg_supported\n\u001b[1;32m 187\u001b[0m \u001b[39melse\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_generate(prompts, stop\u001b[39m=\u001b[39mstop)\n\u001b[1;32m 188\u001b[0m )\n\u001b[1;32m 189\u001b[0m \u001b[39mexcept\u001b[39;00m (\u001b[39mKeyboardInterrupt\u001b[39;00m, \u001b[39mException\u001b[39;00m) \u001b[39mas\u001b[39;00m e:\n\u001b[1;32m 190\u001b[0m run_manager\u001b[39m.\u001b[39mon_llm_error(e)\n",
105
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/langchain/llms/openai.py:315\u001b[0m, in \u001b[0;36mBaseOpenAI._generate\u001b[0;34m(self, prompts, stop, run_manager)\u001b[0m\n\u001b[1;32m 313\u001b[0m choices\u001b[39m.\u001b[39mextend(response[\u001b[39m\"\u001b[39m\u001b[39mchoices\u001b[39m\u001b[39m\"\u001b[39m])\n\u001b[1;32m 314\u001b[0m \u001b[39melse\u001b[39;00m:\n\u001b[0;32m--> 315\u001b[0m response \u001b[39m=\u001b[39m completion_with_retry(\u001b[39mself\u001b[39;49m, prompt\u001b[39m=\u001b[39;49m_prompts, \u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mparams)\n\u001b[1;32m 316\u001b[0m choices\u001b[39m.\u001b[39mextend(response[\u001b[39m\"\u001b[39m\u001b[39mchoices\u001b[39m\u001b[39m\"\u001b[39m])\n\u001b[1;32m 317\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mnot\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mstreaming:\n\u001b[1;32m 318\u001b[0m \u001b[39m# Can't update token usage if streaming\u001b[39;00m\n",
106
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/langchain/llms/openai.py:106\u001b[0m, in \u001b[0;36mcompletion_with_retry\u001b[0;34m(llm, **kwargs)\u001b[0m\n\u001b[1;32m 102\u001b[0m \u001b[39m@retry_decorator\u001b[39m\n\u001b[1;32m 103\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39m_completion_with_retry\u001b[39m(\u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs: Any) \u001b[39m-\u001b[39m\u001b[39m>\u001b[39m Any:\n\u001b[1;32m 104\u001b[0m \u001b[39mreturn\u001b[39;00m llm\u001b[39m.\u001b[39mclient\u001b[39m.\u001b[39mcreate(\u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs)\n\u001b[0;32m--> 106\u001b[0m \u001b[39mreturn\u001b[39;00m _completion_with_retry(\u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mkwargs)\n",
107
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/tenacity/__init__.py:289\u001b[0m, in \u001b[0;36mBaseRetrying.wraps.<locals>.wrapped_f\u001b[0;34m(*args, **kw)\u001b[0m\n\u001b[1;32m 287\u001b[0m \u001b[39m@functools\u001b[39m\u001b[39m.\u001b[39mwraps(f)\n\u001b[1;32m 288\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mwrapped_f\u001b[39m(\u001b[39m*\u001b[39margs: t\u001b[39m.\u001b[39mAny, \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkw: t\u001b[39m.\u001b[39mAny) \u001b[39m-\u001b[39m\u001b[39m>\u001b[39m t\u001b[39m.\u001b[39mAny:\n\u001b[0;32m--> 289\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39;49m(f, \u001b[39m*\u001b[39;49margs, \u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mkw)\n",
108
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/tenacity/__init__.py:379\u001b[0m, in \u001b[0;36mRetrying.__call__\u001b[0;34m(self, fn, *args, **kwargs)\u001b[0m\n\u001b[1;32m 377\u001b[0m retry_state \u001b[39m=\u001b[39m RetryCallState(retry_object\u001b[39m=\u001b[39m\u001b[39mself\u001b[39m, fn\u001b[39m=\u001b[39mfn, args\u001b[39m=\u001b[39margs, kwargs\u001b[39m=\u001b[39mkwargs)\n\u001b[1;32m 378\u001b[0m \u001b[39mwhile\u001b[39;00m \u001b[39mTrue\u001b[39;00m:\n\u001b[0;32m--> 379\u001b[0m do \u001b[39m=\u001b[39m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49miter(retry_state\u001b[39m=\u001b[39;49mretry_state)\n\u001b[1;32m 380\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39misinstance\u001b[39m(do, DoAttempt):\n\u001b[1;32m 381\u001b[0m \u001b[39mtry\u001b[39;00m:\n",
109
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/tenacity/__init__.py:325\u001b[0m, in \u001b[0;36mBaseRetrying.iter\u001b[0;34m(self, retry_state)\u001b[0m\n\u001b[1;32m 323\u001b[0m retry_exc \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mretry_error_cls(fut)\n\u001b[1;32m 324\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mreraise:\n\u001b[0;32m--> 325\u001b[0m \u001b[39mraise\u001b[39;00m retry_exc\u001b[39m.\u001b[39;49mreraise()\n\u001b[1;32m 326\u001b[0m \u001b[39mraise\u001b[39;00m retry_exc \u001b[39mfrom\u001b[39;00m \u001b[39mfut\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mexception\u001b[39;00m()\n\u001b[1;32m 328\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mwait:\n",
110
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/tenacity/__init__.py:158\u001b[0m, in \u001b[0;36mRetryError.reraise\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 156\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mreraise\u001b[39m(\u001b[39mself\u001b[39m) \u001b[39m-\u001b[39m\u001b[39m>\u001b[39m t\u001b[39m.\u001b[39mNoReturn:\n\u001b[1;32m 157\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mlast_attempt\u001b[39m.\u001b[39mfailed:\n\u001b[0;32m--> 158\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mlast_attempt\u001b[39m.\u001b[39;49mresult()\n\u001b[1;32m 159\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mself\u001b[39m\n",
111
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/concurrent/futures/_base.py:451\u001b[0m, in \u001b[0;36mFuture.result\u001b[0;34m(self, timeout)\u001b[0m\n\u001b[1;32m 449\u001b[0m \u001b[39mraise\u001b[39;00m CancelledError()\n\u001b[1;32m 450\u001b[0m \u001b[39melif\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_state \u001b[39m==\u001b[39m FINISHED:\n\u001b[0;32m--> 451\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m__get_result()\n\u001b[1;32m 453\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_condition\u001b[39m.\u001b[39mwait(timeout)\n\u001b[1;32m 455\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_state \u001b[39min\u001b[39;00m [CANCELLED, CANCELLED_AND_NOTIFIED]:\n",
112
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/concurrent/futures/_base.py:403\u001b[0m, in \u001b[0;36mFuture.__get_result\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 401\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_exception:\n\u001b[1;32m 402\u001b[0m \u001b[39mtry\u001b[39;00m:\n\u001b[0;32m--> 403\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_exception\n\u001b[1;32m 404\u001b[0m \u001b[39mfinally\u001b[39;00m:\n\u001b[1;32m 405\u001b[0m \u001b[39m# Break a reference cycle with the exception in self._exception\u001b[39;00m\n\u001b[1;32m 406\u001b[0m \u001b[39mself\u001b[39m \u001b[39m=\u001b[39m \u001b[39mNone\u001b[39;00m\n",
113
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/tenacity/__init__.py:382\u001b[0m, in \u001b[0;36mRetrying.__call__\u001b[0;34m(self, fn, *args, **kwargs)\u001b[0m\n\u001b[1;32m 380\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39misinstance\u001b[39m(do, DoAttempt):\n\u001b[1;32m 381\u001b[0m \u001b[39mtry\u001b[39;00m:\n\u001b[0;32m--> 382\u001b[0m result \u001b[39m=\u001b[39m fn(\u001b[39m*\u001b[39;49margs, \u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mkwargs)\n\u001b[1;32m 383\u001b[0m \u001b[39mexcept\u001b[39;00m \u001b[39mBaseException\u001b[39;00m: \u001b[39m# noqa: B902\u001b[39;00m\n\u001b[1;32m 384\u001b[0m retry_state\u001b[39m.\u001b[39mset_exception(sys\u001b[39m.\u001b[39mexc_info()) \u001b[39m# type: ignore[arg-type]\u001b[39;00m\n",
114
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/langchain/llms/openai.py:104\u001b[0m, in \u001b[0;36mcompletion_with_retry.<locals>._completion_with_retry\u001b[0;34m(**kwargs)\u001b[0m\n\u001b[1;32m 102\u001b[0m \u001b[39m@retry_decorator\u001b[39m\n\u001b[1;32m 103\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39m_completion_with_retry\u001b[39m(\u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs: Any) \u001b[39m-\u001b[39m\u001b[39m>\u001b[39m Any:\n\u001b[0;32m--> 104\u001b[0m \u001b[39mreturn\u001b[39;00m llm\u001b[39m.\u001b[39;49mclient\u001b[39m.\u001b[39;49mcreate(\u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mkwargs)\n",
115
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/openai/api_resources/completion.py:25\u001b[0m, in \u001b[0;36mCompletion.create\u001b[0;34m(cls, *args, **kwargs)\u001b[0m\n\u001b[1;32m 23\u001b[0m \u001b[39mwhile\u001b[39;00m \u001b[39mTrue\u001b[39;00m:\n\u001b[1;32m 24\u001b[0m \u001b[39mtry\u001b[39;00m:\n\u001b[0;32m---> 25\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39msuper\u001b[39;49m()\u001b[39m.\u001b[39;49mcreate(\u001b[39m*\u001b[39;49margs, \u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mkwargs)\n\u001b[1;32m 26\u001b[0m \u001b[39mexcept\u001b[39;00m TryAgain \u001b[39mas\u001b[39;00m e:\n\u001b[1;32m 27\u001b[0m \u001b[39mif\u001b[39;00m timeout \u001b[39mis\u001b[39;00m \u001b[39mnot\u001b[39;00m \u001b[39mNone\u001b[39;00m \u001b[39mand\u001b[39;00m time\u001b[39m.\u001b[39mtime() \u001b[39m>\u001b[39m start \u001b[39m+\u001b[39m timeout:\n",
116
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/openai/api_resources/abstract/engine_api_resource.py:153\u001b[0m, in \u001b[0;36mEngineAPIResource.create\u001b[0;34m(cls, api_key, api_base, api_type, request_id, api_version, organization, **params)\u001b[0m\n\u001b[1;32m 127\u001b[0m \u001b[39m@classmethod\u001b[39m\n\u001b[1;32m 128\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mcreate\u001b[39m(\n\u001b[1;32m 129\u001b[0m \u001b[39mcls\u001b[39m,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 136\u001b[0m \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mparams,\n\u001b[1;32m 137\u001b[0m ):\n\u001b[1;32m 138\u001b[0m (\n\u001b[1;32m 139\u001b[0m deployment_id,\n\u001b[1;32m 140\u001b[0m engine,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 150\u001b[0m api_key, api_base, api_type, api_version, organization, \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mparams\n\u001b[1;32m 151\u001b[0m )\n\u001b[0;32m--> 153\u001b[0m response, _, api_key \u001b[39m=\u001b[39m requestor\u001b[39m.\u001b[39;49mrequest(\n\u001b[1;32m 154\u001b[0m \u001b[39m\"\u001b[39;49m\u001b[39mpost\u001b[39;49m\u001b[39m\"\u001b[39;49m,\n\u001b[1;32m 155\u001b[0m url,\n\u001b[1;32m 156\u001b[0m params\u001b[39m=\u001b[39;49mparams,\n\u001b[1;32m 157\u001b[0m headers\u001b[39m=\u001b[39;49mheaders,\n\u001b[1;32m 158\u001b[0m stream\u001b[39m=\u001b[39;49mstream,\n\u001b[1;32m 159\u001b[0m request_id\u001b[39m=\u001b[39;49mrequest_id,\n\u001b[1;32m 160\u001b[0m request_timeout\u001b[39m=\u001b[39;49mrequest_timeout,\n\u001b[1;32m 161\u001b[0m )\n\u001b[1;32m 163\u001b[0m \u001b[39mif\u001b[39;00m stream:\n\u001b[1;32m 164\u001b[0m \u001b[39m# must be an iterator\u001b[39;00m\n\u001b[1;32m 165\u001b[0m \u001b[39massert\u001b[39;00m \u001b[39mnot\u001b[39;00m \u001b[39misinstance\u001b[39m(response, OpenAIResponse)\n",
117
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/openai/api_requestor.py:230\u001b[0m, in \u001b[0;36mAPIRequestor.request\u001b[0;34m(self, method, url, params, headers, files, stream, request_id, request_timeout)\u001b[0m\n\u001b[1;32m 209\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mrequest\u001b[39m(\n\u001b[1;32m 210\u001b[0m \u001b[39mself\u001b[39m,\n\u001b[1;32m 211\u001b[0m method,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 218\u001b[0m request_timeout: Optional[Union[\u001b[39mfloat\u001b[39m, Tuple[\u001b[39mfloat\u001b[39m, \u001b[39mfloat\u001b[39m]]] \u001b[39m=\u001b[39m \u001b[39mNone\u001b[39;00m,\n\u001b[1;32m 219\u001b[0m ) \u001b[39m-\u001b[39m\u001b[39m>\u001b[39m Tuple[Union[OpenAIResponse, Iterator[OpenAIResponse]], \u001b[39mbool\u001b[39m, \u001b[39mstr\u001b[39m]:\n\u001b[1;32m 220\u001b[0m result \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mrequest_raw(\n\u001b[1;32m 221\u001b[0m method\u001b[39m.\u001b[39mlower(),\n\u001b[1;32m 222\u001b[0m url,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 228\u001b[0m request_timeout\u001b[39m=\u001b[39mrequest_timeout,\n\u001b[1;32m 229\u001b[0m )\n\u001b[0;32m--> 230\u001b[0m resp, got_stream \u001b[39m=\u001b[39m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_interpret_response(result, stream)\n\u001b[1;32m 231\u001b[0m \u001b[39mreturn\u001b[39;00m resp, got_stream, \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mapi_key\n",
118
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/openai/api_requestor.py:624\u001b[0m, in \u001b[0;36mAPIRequestor._interpret_response\u001b[0;34m(self, result, stream)\u001b[0m\n\u001b[1;32m 616\u001b[0m \u001b[39mreturn\u001b[39;00m (\n\u001b[1;32m 617\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_interpret_response_line(\n\u001b[1;32m 618\u001b[0m line, result\u001b[39m.\u001b[39mstatus_code, result\u001b[39m.\u001b[39mheaders, stream\u001b[39m=\u001b[39m\u001b[39mTrue\u001b[39;00m\n\u001b[1;32m 619\u001b[0m )\n\u001b[1;32m 620\u001b[0m \u001b[39mfor\u001b[39;00m line \u001b[39min\u001b[39;00m parse_stream(result\u001b[39m.\u001b[39miter_lines())\n\u001b[1;32m 621\u001b[0m ), \u001b[39mTrue\u001b[39;00m\n\u001b[1;32m 622\u001b[0m \u001b[39melse\u001b[39;00m:\n\u001b[1;32m 623\u001b[0m \u001b[39mreturn\u001b[39;00m (\n\u001b[0;32m--> 624\u001b[0m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_interpret_response_line(\n\u001b[1;32m 625\u001b[0m result\u001b[39m.\u001b[39;49mcontent\u001b[39m.\u001b[39;49mdecode(\u001b[39m\"\u001b[39;49m\u001b[39mutf-8\u001b[39;49m\u001b[39m\"\u001b[39;49m),\n\u001b[1;32m 626\u001b[0m result\u001b[39m.\u001b[39;49mstatus_code,\n\u001b[1;32m 627\u001b[0m result\u001b[39m.\u001b[39;49mheaders,\n\u001b[1;32m 628\u001b[0m stream\u001b[39m=\u001b[39;49m\u001b[39mFalse\u001b[39;49;00m,\n\u001b[1;32m 629\u001b[0m ),\n\u001b[1;32m 630\u001b[0m \u001b[39mFalse\u001b[39;00m,\n\u001b[1;32m 631\u001b[0m )\n",
119
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/openai/api_requestor.py:687\u001b[0m, in \u001b[0;36mAPIRequestor._interpret_response_line\u001b[0;34m(self, rbody, rcode, rheaders, stream)\u001b[0m\n\u001b[1;32m 685\u001b[0m stream_error \u001b[39m=\u001b[39m stream \u001b[39mand\u001b[39;00m \u001b[39m\"\u001b[39m\u001b[39merror\u001b[39m\u001b[39m\"\u001b[39m \u001b[39min\u001b[39;00m resp\u001b[39m.\u001b[39mdata\n\u001b[1;32m 686\u001b[0m \u001b[39mif\u001b[39;00m stream_error \u001b[39mor\u001b[39;00m \u001b[39mnot\u001b[39;00m \u001b[39m200\u001b[39m \u001b[39m<\u001b[39m\u001b[39m=\u001b[39m rcode \u001b[39m<\u001b[39m \u001b[39m300\u001b[39m:\n\u001b[0;32m--> 687\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mhandle_error_response(\n\u001b[1;32m 688\u001b[0m rbody, rcode, resp\u001b[39m.\u001b[39mdata, rheaders, stream_error\u001b[39m=\u001b[39mstream_error\n\u001b[1;32m 689\u001b[0m )\n\u001b[1;32m 690\u001b[0m \u001b[39mreturn\u001b[39;00m resp\n",
120
+ "\u001b[0;31mAPIError\u001b[0m: parse endPoint error,please check path {\"error\":{\"code\":\"path not support\",\"message\":\"parse endPoint error,please check path\",\"param\":\"\",\"type\":\"invalid_request_error\"}} 556 {'error': {'code': 'path not support', 'message': 'parse endPoint error,please check path', 'param': '', 'type': 'invalid_request_error'}} {'Date': 'Mon, 26 Jun 2023 08:40:20 GMT', 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': '130', 'Connection': 'keep-alive', 'X-Zyb-Trace-Id': 'b24a6bb210f6b0cb', 'tracecode': '24204725470171513035062616'}"
121
+ ]
122
  }
123
  ],
124
  "source": [
 
127
  },
128
  {
129
  "cell_type": "code",
130
+ "execution_count": null,
131
  "metadata": {},
132
  "outputs": [],
133
  "source": [
 
136
  },
137
  {
138
  "cell_type": "code",
139
+ "execution_count": null,
140
  "metadata": {},
141
  "outputs": [
142
  {
chain_pal.ipynb CHANGED
@@ -2,26 +2,27 @@
2
  "cells": [
3
  {
4
  "cell_type": "code",
5
- "execution_count": 62,
6
  "metadata": {},
7
  "outputs": [],
8
  "source": [
9
  "from langchain.chains import PALChain\n",
10
- "from langchain import OpenAI"
 
11
  ]
12
  },
13
  {
14
  "cell_type": "code",
15
- "execution_count": 63,
16
  "metadata": {},
17
  "outputs": [],
18
  "source": [
19
- "llm = OpenAI(temperature=0, max_tokens=512)"
20
  ]
21
  },
22
  {
23
  "cell_type": "code",
24
- "execution_count": 64,
25
  "metadata": {},
26
  "outputs": [],
27
  "source": [
@@ -30,7 +31,7 @@
30
  },
31
  {
32
  "cell_type": "code",
33
- "execution_count": 65,
34
  "metadata": {},
35
  "outputs": [
36
  {
@@ -41,13 +42,11 @@
41
  "\n",
42
  "\u001b[1m> Entering new PALChain chain...\u001b[0m\n",
43
  "\u001b[32;1m\u001b[1;3mdef solution():\n",
44
- " \"\"\"在操场上活动的男生有67人,女生有31人,男、女生有同样多的人离开操场后,还在操场的男生人数是女生人数的4倍,离开操场的男、女生一共有多少人?\"\"\"\n",
45
- " boys_initial = 67\n",
46
- " girls_initial = 31\n",
47
- " boys_left = boys_initial - (girls_initial * 4)\n",
48
- " total_left = boys_left + girls_initial\n",
49
- " result = total_left\n",
50
  " return result\u001b[0m\n",
 
 
51
  "\n",
52
  "\u001b[1m> Finished chain.\u001b[0m\n"
53
  ]
@@ -55,23 +54,24 @@
55
  {
56
  "data": {
57
  "text/plain": [
58
- "'-26'"
59
  ]
60
  },
61
- "execution_count": 65,
62
  "metadata": {},
63
  "output_type": "execute_result"
64
  }
65
  ],
66
  "source": [
67
- "question = \"在操场上活动的男生有67人,女生有31人,男、女生有同样多的人离开操场后,还在操场的男生人数是女生人数的4倍,离开操场的男、女生一共有多少人?\"\n",
68
  "# question = \"同学们进行广播操比赛,全班正好排成相等的6行。小红排在第二行,从头数,她站在第5个位置,从后数她站在第3个位置,这个班共有( )人\"\n",
 
69
  "pal_chain.run(question)"
70
  ]
71
  },
72
  {
73
  "cell_type": "code",
74
- "execution_count": 1,
75
  "metadata": {},
76
  "outputs": [],
77
  "source": [
@@ -81,7 +81,7 @@
81
  },
82
  {
83
  "cell_type": "code",
84
- "execution_count": 2,
85
  "metadata": {},
86
  "outputs": [],
87
  "source": [
@@ -90,16 +90,24 @@
90
  },
91
  {
92
  "cell_type": "code",
93
- "execution_count": 10,
94
  "metadata": {},
95
  "outputs": [
 
 
 
 
 
 
 
 
96
  {
97
  "data": {
98
  "text/plain": [
99
  "'2\\n'"
100
  ]
101
  },
102
- "execution_count": 10,
103
  "metadata": {},
104
  "output_type": "execute_result"
105
  }
@@ -125,7 +133,7 @@
125
  "name": "python",
126
  "nbconvert_exporter": "python",
127
  "pygments_lexer": "ipython3",
128
- "version": "3.11.3"
129
  },
130
  "orig_nbformat": 4
131
  },
 
2
  "cells": [
3
  {
4
  "cell_type": "code",
5
+ "execution_count": 1,
6
  "metadata": {},
7
  "outputs": [],
8
  "source": [
9
  "from langchain.chains import PALChain\n",
10
+ "from langchain import OpenAI\n",
11
+ "from langchain.chat_models import ChatOpenAI"
12
  ]
13
  },
14
  {
15
  "cell_type": "code",
16
+ "execution_count": 2,
17
  "metadata": {},
18
  "outputs": [],
19
  "source": [
20
+ "llm = ChatOpenAI(temperature=0, max_tokens=512)"
21
  ]
22
  },
23
  {
24
  "cell_type": "code",
25
+ "execution_count": 3,
26
  "metadata": {},
27
  "outputs": [],
28
  "source": [
 
31
  },
32
  {
33
  "cell_type": "code",
34
+ "execution_count": 4,
35
  "metadata": {},
36
  "outputs": [
37
  {
 
42
  "\n",
43
  "\u001b[1m> Entering new PALChain chain...\u001b[0m\n",
44
  "\u001b[32;1m\u001b[1;3mdef solution():\n",
45
+ " \"\"\"145 ÷ 5 × 6\"\"\"\n",
46
+ " result = 145 / 5 * 6\n",
 
 
 
 
47
  " return result\u001b[0m\n",
48
+ "174.0\n",
49
+ "\n",
50
  "\n",
51
  "\u001b[1m> Finished chain.\u001b[0m\n"
52
  ]
 
54
  {
55
  "data": {
56
  "text/plain": [
57
+ "'174.0'"
58
  ]
59
  },
60
+ "execution_count": 4,
61
  "metadata": {},
62
  "output_type": "execute_result"
63
  }
64
  ],
65
  "source": [
66
+ "# question = \"在操场上活动的男生有67人,女生有31人,男、女生有同样多的人离开操场后,还在操场的男生人数是女生人数的4倍,离开操场的男、女生一共有多少人?\"\n",
67
  "# question = \"同学们进行广播操比赛,全班正好排成相等的6行。小红排在第二行,从头数,她站在第5个位置,从后数她站在第3个位置,这个班共有( )人\"\n",
68
+ "question = \"145 ÷ 5 × 6\"\n",
69
  "pal_chain.run(question)"
70
  ]
71
  },
72
  {
73
  "cell_type": "code",
74
+ "execution_count": 5,
75
  "metadata": {},
76
  "outputs": [],
77
  "source": [
 
81
  },
82
  {
83
  "cell_type": "code",
84
+ "execution_count": 6,
85
  "metadata": {},
86
  "outputs": [],
87
  "source": [
 
90
  },
91
  {
92
  "cell_type": "code",
93
+ "execution_count": 7,
94
  "metadata": {},
95
  "outputs": [
96
+ {
97
+ "name": "stdout",
98
+ "output_type": "stream",
99
+ "text": [
100
+ "2\n",
101
+ "\n"
102
+ ]
103
+ },
104
  {
105
  "data": {
106
  "text/plain": [
107
  "'2\\n'"
108
  ]
109
  },
110
+ "execution_count": 7,
111
  "metadata": {},
112
  "output_type": "execute_result"
113
  }
 
133
  "name": "python",
134
  "nbconvert_exporter": "python",
135
  "pygments_lexer": "ipython3",
136
+ "version": "3.10.11"
137
  },
138
  "orig_nbformat": 4
139
  },
custom_prompt_template.ipynb CHANGED
@@ -94,7 +94,7 @@
94
  "name": "python",
95
  "nbconvert_exporter": "python",
96
  "pygments_lexer": "ipython3",
97
- "version": "3.10.10"
98
  },
99
  "orig_nbformat": 4
100
  },
 
94
  "name": "python",
95
  "nbconvert_exporter": "python",
96
  "pygments_lexer": "ipython3",
97
+ "version": "3.10.11"
98
  },
99
  "orig_nbformat": 4
100
  },
data/pure-bash-bible.md ADDED
@@ -0,0 +1,2200 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <p align="center"><b>NEW: <a href="https://github.com/dylanaraps/pure-sh-bible">pure sh bible (📖 A collection of pure POSIX sh alternatives to external processes).</a></b></p>
2
+
3
+ <br>
4
+
5
+ <p align="center"><img src="https://raw.githubusercontent.com/odb/official-bash-logo/master/assets/Logos/Icons/PNG/512x512.png" width="200px"></p>
6
+ <h1 align="center">pure bash bible</h1> <p
7
+ align="center">A collection of pure bash alternatives to external
8
+ processes.</p>
9
+
10
+ <p align="center"> <a
11
+ href="https://travis-ci.com/dylanaraps/pure-bash-bible"><img
12
+ src="https://travis-ci.com/dylanaraps/pure-bash-bible.svg?branch=master"></a>
13
+ <a href="./LICENSE.md"><img
14
+ src="https://img.shields.io/badge/license-MIT-blue.svg"></a>
15
+ </p>
16
+
17
+ <br>
18
+
19
+ <a href="https://leanpub.com/bash/">
20
+ <img src="https://s3.amazonaws.com/titlepages.leanpub.com/bash/hero" width="40%" align="right">
21
+ </a>
22
+
23
+ The goal of this book is to document commonly-known and lesser-known methods of doing various tasks using only built-in `bash` features. Using the snippets from this bible can help remove unneeded dependencies from scripts and in most cases make them faster. I came across these tips and discovered a few while developing [neofetch](https://github.com/dylanaraps/neofetch), [pxltrm](https://github.com/dylanaraps/pxltrm) and other smaller projects.
24
+
25
+ The snippets below are linted using `shellcheck` and tests have been written where applicable. Want to contribute? Read the [CONTRIBUTING.md](https://github.com/dylanaraps/pure-bash-bible/blob/master/CONTRIBUTING.md). It outlines how the unit tests work and what is required when adding snippets to the bible.
26
+
27
+ See something incorrectly described, buggy or outright wrong? Open an issue or send a pull request. If the bible is missing something, open an issue and a solution will be found.
28
+
29
+ <br>
30
+ <p align="center"><b>This book is also available to purchase on leanpub. https://leanpub.com/bash</b></p>
31
+ <p align="center"><b>Or you can buy me a coffee.</b>
32
+ <a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=V7QNJNKS3WYVS"><img src="https://img.shields.io/badge/don-paypal-yellow.svg"></a> <a href="https://www.patreon.com/dyla"><img src="https://img.shields.io/badge/don-patreon-yellow.svg"> </a><a href="https://liberapay.com/2211/"><img src="https://img.shields.io/badge/don-liberapay-yellow.svg"></a>
33
+ </p>
34
+
35
+ <br>
36
+
37
+ # Table of Contents
38
+
39
+ <!-- vim-markdown-toc GFM -->
40
+
41
+ * [FOREWORD](#foreword)
42
+ * [STRINGS](#strings)
43
+ * [Trim leading and trailing white-space from string](#trim-leading-and-trailing-white-space-from-string)
44
+ * [Trim all white-space from string and truncate spaces](#trim-all-white-space-from-string-and-truncate-spaces)
45
+ * [Use regex on a string](#use-regex-on-a-string)
46
+ * [Split a string on a delimiter](#split-a-string-on-a-delimiter)
47
+ * [Change a string to lowercase](#change-a-string-to-lowercase)
48
+ * [Change a string to uppercase](#change-a-string-to-uppercase)
49
+ * [Reverse a string case](#reverse-a-string-case)
50
+ * [Trim quotes from a string](#trim-quotes-from-a-string)
51
+ * [Strip all instances of pattern from string](#strip-all-instances-of-pattern-from-string)
52
+ * [Strip first occurrence of pattern from string](#strip-first-occurrence-of-pattern-from-string)
53
+ * [Strip pattern from start of string](#strip-pattern-from-start-of-string)
54
+ * [Strip pattern from end of string](#strip-pattern-from-end-of-string)
55
+ * [Percent-encode a string](#percent-encode-a-string)
56
+ * [Decode a percent-encoded string](#decode-a-percent-encoded-string)
57
+ * [Check if string contains a sub-string](#check-if-string-contains-a-sub-string)
58
+ * [Check if string starts with sub-string](#check-if-string-starts-with-sub-string)
59
+ * [Check if string ends with sub-string](#check-if-string-ends-with-sub-string)
60
+ * [ARRAYS](#arrays)
61
+ * [Reverse an array](#reverse-an-array)
62
+ * [Remove duplicate array elements](#remove-duplicate-array-elements)
63
+ * [Random array element](#random-array-element)
64
+ * [Cycle through an array](#cycle-through-an-array)
65
+ * [Toggle between two values](#toggle-between-two-values)
66
+ * [LOOPS](#loops)
67
+ * [Loop over a range of numbers](#loop-over-a-range-of-numbers)
68
+ * [Loop over a variable range of numbers](#loop-over-a-variable-range-of-numbers)
69
+ * [Loop over an array](#loop-over-an-array)
70
+ * [Loop over an array with an index](#loop-over-an-array-with-an-index)
71
+ * [Loop over the contents of a file](#loop-over-the-contents-of-a-file)
72
+ * [Loop over files and directories](#loop-over-files-and-directories)
73
+ * [FILE HANDLING](#file-handling)
74
+ * [Read a file to a string](#read-a-file-to-a-string)
75
+ * [Read a file to an array (*by line*)](#read-a-file-to-an-array-by-line)
76
+ * [Get the first N lines of a file](#get-the-first-n-lines-of-a-file)
77
+ * [Get the last N lines of a file](#get-the-last-n-lines-of-a-file)
78
+ * [Get the number of lines in a file](#get-the-number-of-lines-in-a-file)
79
+ * [Count files or directories in directory](#count-files-or-directories-in-directory)
80
+ * [Create an empty file](#create-an-empty-file)
81
+ * [Extract lines between two markers](#extract-lines-between-two-markers)
82
+ * [FILE PATHS](#file-paths)
83
+ * [Get the directory name of a file path](#get-the-directory-name-of-a-file-path)
84
+ * [Get the base-name of a file path](#get-the-base-name-of-a-file-path)
85
+ * [VARIABLES](#variables)
86
+ * [Assign and access a variable using a variable](#assign-and-access-a-variable-using-a-variable)
87
+ * [Name a variable based on another variable](#name-a-variable-based-on-another-variable)
88
+ * [ESCAPE SEQUENCES](#escape-sequences)
89
+ * [Text Colors](#text-colors)
90
+ * [Text Attributes](#text-attributes)
91
+ * [Cursor Movement](#cursor-movement)
92
+ * [Erasing Text](#erasing-text)
93
+ * [PARAMETER EXPANSION](#parameter-expansion)
94
+ * [Indirection](#indirection)
95
+ * [Replacement](#replacement)
96
+ * [Length](#length)
97
+ * [Expansion](#expansion)
98
+ * [Case Modification](#case-modification)
99
+ * [Default Value](#default-value)
100
+ * [BRACE EXPANSION](#brace-expansion)
101
+ * [Ranges](#ranges)
102
+ * [String Lists](#string-lists)
103
+ * [CONDITIONAL EXPRESSIONS](#conditional-expressions)
104
+ * [File Conditionals](#file-conditionals)
105
+ * [File Comparisons](#file-comparisons)
106
+ * [Variable Conditionals](#variable-conditionals)
107
+ * [Variable Comparisons](#variable-comparisons)
108
+ * [ARITHMETIC OPERATORS](#arithmetic-operators)
109
+ * [Assignment](#assignment)
110
+ * [Arithmetic](#arithmetic)
111
+ * [Bitwise](#bitwise)
112
+ * [Logical](#logical)
113
+ * [Miscellaneous](#miscellaneous)
114
+ * [ARITHMETIC](#arithmetic-1)
115
+ * [Simpler syntax to set variables](#simpler-syntax-to-set-variables)
116
+ * [Ternary Tests](#ternary-tests)
117
+ * [TRAPS](#traps)
118
+ * [Do something on script exit](#do-something-on-script-exit)
119
+ * [Ignore terminal interrupt (CTRL+C, SIGINT)](#ignore-terminal-interrupt-ctrlc-sigint)
120
+ * [React to window resize](#react-to-window-resize)
121
+ * [Do something before every command](#do-something-before-every-command)
122
+ * [Do something when a shell function or a sourced file finishes executing](#do-something-when-a-shell-function-or-a-sourced-file-finishes-executing)
123
+ * [PERFORMANCE](#performance)
124
+ * [Disable Unicode](#disable-unicode)
125
+ * [OBSOLETE SYNTAX](#obsolete-syntax)
126
+ * [Shebang](#shebang)
127
+ * [Command Substitution](#command-substitution)
128
+ * [Function Declaration](#function-declaration)
129
+ * [INTERNAL VARIABLES](#internal-variables)
130
+ * [Get the location to the `bash` binary](#get-the-location-to-the-bash-binary)
131
+ * [Get the version of the current running `bash` process](#get-the-version-of-the-current-running-bash-process)
132
+ * [Open the user's preferred text editor](#open-the-users-preferred-text-editor)
133
+ * [Get the name of the current function](#get-the-name-of-the-current-function)
134
+ * [Get the host-name of the system](#get-the-host-name-of-the-system)
135
+ * [Get the architecture of the Operating System](#get-the-architecture-of-the-operating-system)
136
+ * [Get the name of the Operating System / Kernel](#get-the-name-of-the-operating-system--kernel)
137
+ * [Get the current working directory](#get-the-current-working-directory)
138
+ * [Get the number of seconds the script has been running](#get-the-number-of-seconds-the-script-has-been-running)
139
+ * [Get a pseudorandom integer](#get-a-pseudorandom-integer)
140
+ * [INFORMATION ABOUT THE TERMINAL](#information-about-the-terminal)
141
+ * [Get the terminal size in lines and columns (*from a script*)](#get-the-terminal-size-in-lines-and-columns-from-a-script)
142
+ * [Get the terminal size in pixels](#get-the-terminal-size-in-pixels)
143
+ * [Get the current cursor position](#get-the-current-cursor-position)
144
+ * [CONVERSION](#conversion)
145
+ * [Convert a hex color to RGB](#convert-a-hex-color-to-rgb)
146
+ * [Convert an RGB color to hex](#convert-an-rgb-color-to-hex)
147
+ * [CODE GOLF](#code-golf)
148
+ * [Shorter `for` loop syntax](#shorter-for-loop-syntax)
149
+ * [Shorter infinite loops](#shorter-infinite-loops)
150
+ * [Shorter function declaration](#shorter-function-declaration)
151
+ * [Shorter `if` syntax](#shorter-if-syntax)
152
+ * [Simpler `case` statement to set variable](#simpler-case-statement-to-set-variable)
153
+ * [OTHER](#other)
154
+ * [Use `read` as an alternative to the `sleep` command](#use-read-as-an-alternative-to-the-sleep-command)
155
+ * [Check if a program is in the user's PATH](#check-if-a-program-is-in-the-users-path)
156
+ * [Get the current date using `strftime`](#get-the-current-date-using-strftime)
157
+ * [Get the username of the current user](#get-the-username-of-the-current-user)
158
+ * [Generate a UUID V4](#generate-a-uuid-v4)
159
+ * [Progress bars](#progress-bars)
160
+ * [Get the list of functions in a script](#get-the-list-of-functions-in-a-script)
161
+ * [Bypass shell aliases](#bypass-shell-aliases)
162
+ * [Bypass shell functions](#bypass-shell-functions)
163
+ * [Run a command in the background](#run-a-command-in-the-background)
164
+ * [Capture function return without command substitution](#capture-the-return-value-of-a-function-without-command-substitution)
165
+ * [AFTERWORD](#afterword)
166
+
167
+ <!-- vim-markdown-toc -->
168
+
169
+ <br>
170
+
171
+ <!-- CHAPTER START -->
172
+ # FOREWORD
173
+
174
+ A collection of pure `bash` alternatives to external processes and programs. The `bash` scripting language is more powerful than people realise and most tasks can be accomplished without depending on external programs.
175
+
176
+ Calling an external process in `bash` is expensive and excessive use will cause a noticeable slowdown. Scripts and programs written using built-in methods (*where applicable*) will be faster, require fewer dependencies and afford a better understanding of the language itself.
177
+
178
+ The contents of this book provide a reference for solving problems encountered when writing programs and scripts in `bash`. Examples are in function formats showcasing how to incorporate these solutions into code.
179
+
180
+ <!-- CHAPTER END -->
181
+
182
+ <!-- CHAPTER START -->
183
+ # STRINGS
184
+
185
+ ## Trim leading and trailing white-space from string
186
+
187
+ This is an alternative to `sed`, `awk`, `perl` and other tools. The
188
+ function below works by finding all leading and trailing white-space and
189
+ removing it from the start and end of the string. The `:` built-in is used in place of a temporary variable.
190
+
191
+ **Example Function:**
192
+
193
+ ```sh
194
+ trim_string() {
195
+ # Usage: trim_string " example string "
196
+ : "${1#"${1%%[![:space:]]*}"}"
197
+ : "${_%"${_##*[![:space:]]}"}"
198
+ printf '%s\n' "$_"
199
+ }
200
+ ```
201
+
202
+ **Example Usage:**
203
+
204
+ ```shell
205
+ $ trim_string " Hello, World "
206
+ Hello, World
207
+
208
+ $ name=" John Black "
209
+ $ trim_string "$name"
210
+ John Black
211
+ ```
212
+
213
+
214
+ ## Trim all white-space from string and truncate spaces
215
+
216
+ This is an alternative to `sed`, `awk`, `perl` and other tools. The
217
+ function below works by abusing word splitting to create a new string
218
+ without leading/trailing white-space and with truncated spaces.
219
+
220
+ **Example Function:**
221
+
222
+ ```sh
223
+ # shellcheck disable=SC2086,SC2048
224
+ trim_all() {
225
+ # Usage: trim_all " example string "
226
+ set -f
227
+ set -- $*
228
+ printf '%s\n' "$*"
229
+ set +f
230
+ }
231
+ ```
232
+
233
+ **Example Usage:**
234
+
235
+ ```shell
236
+ $ trim_all " Hello, World "
237
+ Hello, World
238
+
239
+ $ name=" John Black is my name. "
240
+ $ trim_all "$name"
241
+ John Black is my name.
242
+ ```
243
+
244
+ ## Use regex on a string
245
+
246
+ The result of `bash`'s regex matching can be used to replace `sed` for a
247
+ large number of use-cases.
248
+
249
+ **CAVEAT**: This is one of the few platform dependent `bash` features.
250
+ `bash` will use whatever regex engine is installed on the user's system.
251
+ Stick to POSIX regex features if aiming for compatibility.
252
+
253
+ **CAVEAT**: This example only prints the first matching group. When using
254
+ multiple capture groups some modification is needed.
255
+
256
+ **Example Function:**
257
+
258
+ ```sh
259
+ regex() {
260
+ # Usage: regex "string" "regex"
261
+ [[ $1 =~ $2 ]] && printf '%s\n' "${BASH_REMATCH[1]}"
262
+ }
263
+ ```
264
+
265
+ **Example Usage:**
266
+
267
+ ```shell
268
+ $ # Trim leading white-space.
269
+ $ regex ' hello' '^\s*(.*)'
270
+ hello
271
+
272
+ $ # Validate a hex color.
273
+ $ regex "#FFFFFF" '^(#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3}))$'
274
+ #FFFFFF
275
+
276
+ $ # Validate a hex color (invalid).
277
+ $ regex "red" '^(#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3}))$'
278
+ # no output (invalid)
279
+ ```
280
+
281
+ **Example Usage in script:**
282
+
283
+ ```shell
284
+ is_hex_color() {
285
+ if [[ $1 =~ ^(#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3}))$ ]]; then
286
+ printf '%s\n' "${BASH_REMATCH[1]}"
287
+ else
288
+ printf '%s\n' "error: $1 is an invalid color."
289
+ return 1
290
+ fi
291
+ }
292
+
293
+ read -r color
294
+ is_hex_color "$color" || color="#FFFFFF"
295
+
296
+ # Do stuff.
297
+ ```
298
+
299
+
300
+ ## Split a string on a delimiter
301
+
302
+ **CAVEAT:** Requires `bash` 4+
303
+
304
+ This is an alternative to `cut`, `awk` and other tools.
305
+
306
+ **Example Function:**
307
+
308
+ ```sh
309
+ split() {
310
+ # Usage: split "string" "delimiter"
311
+ IFS=$'\n' read -d "" -ra arr <<< "${1//$2/$'\n'}"
312
+ printf '%s\n' "${arr[@]}"
313
+ }
314
+ ```
315
+
316
+ **Example Usage:**
317
+
318
+ ```shell
319
+ $ split "apples,oranges,pears,grapes" ","
320
+ apples
321
+ oranges
322
+ pears
323
+ grapes
324
+
325
+ $ split "1, 2, 3, 4, 5" ", "
326
+ 1
327
+ 2
328
+ 3
329
+ 4
330
+ 5
331
+
332
+ # Multi char delimiters work too!
333
+ $ split "hello---world---my---name---is---john" "---"
334
+ hello
335
+ world
336
+ my
337
+ name
338
+ is
339
+ john
340
+ ```
341
+
342
+ ## Change a string to lowercase
343
+
344
+ **CAVEAT:** Requires `bash` 4+
345
+
346
+ **Example Function:**
347
+
348
+ ```sh
349
+ lower() {
350
+ # Usage: lower "string"
351
+ printf '%s\n' "${1,,}"
352
+ }
353
+ ```
354
+
355
+ **Example Usage:**
356
+
357
+ ```shell
358
+ $ lower "HELLO"
359
+ hello
360
+
361
+ $ lower "HeLlO"
362
+ hello
363
+
364
+ $ lower "hello"
365
+ hello
366
+ ```
367
+
368
+ ## Change a string to uppercase
369
+
370
+ **CAVEAT:** Requires `bash` 4+
371
+
372
+ **Example Function:**
373
+
374
+ ```sh
375
+ upper() {
376
+ # Usage: upper "string"
377
+ printf '%s\n' "${1^^}"
378
+ }
379
+ ```
380
+
381
+ **Example Usage:**
382
+
383
+ ```shell
384
+ $ upper "hello"
385
+ HELLO
386
+
387
+ $ upper "HeLlO"
388
+ HELLO
389
+
390
+ $ upper "HELLO"
391
+ HELLO
392
+ ```
393
+
394
+ ## Reverse a string case
395
+
396
+ **CAVEAT:** Requires `bash` 4+
397
+
398
+ **Example Function:**
399
+
400
+ ```sh
401
+ reverse_case() {
402
+ # Usage: reverse_case "string"
403
+ printf '%s\n' "${1~~}"
404
+ }
405
+ ```
406
+
407
+ **Example Usage:**
408
+
409
+ ```shell
410
+ $ reverse_case "hello"
411
+ HELLO
412
+
413
+ $ reverse_case "HeLlO"
414
+ hElLo
415
+
416
+ $ reverse_case "HELLO"
417
+ hello
418
+ ```
419
+
420
+ ## Trim quotes from a string
421
+
422
+ **Example Function:**
423
+
424
+ ```sh
425
+ trim_quotes() {
426
+ # Usage: trim_quotes "string"
427
+ : "${1//\'}"
428
+ printf '%s\n' "${_//\"}"
429
+ }
430
+ ```
431
+
432
+ **Example Usage:**
433
+
434
+ ```shell
435
+ $ var="'Hello', \"World\""
436
+ $ trim_quotes "$var"
437
+ Hello, World
438
+ ```
439
+
440
+ ## Strip all instances of pattern from string
441
+
442
+ **Example Function:**
443
+
444
+ ```sh
445
+ strip_all() {
446
+ # Usage: strip_all "string" "pattern"
447
+ printf '%s\n' "${1//$2}"
448
+ }
449
+ ```
450
+
451
+ **Example Usage:**
452
+
453
+ ```shell
454
+ $ strip_all "The Quick Brown Fox" "[aeiou]"
455
+ Th Qck Brwn Fx
456
+
457
+ $ strip_all "The Quick Brown Fox" "[[:space:]]"
458
+ TheQuickBrownFox
459
+
460
+ $ strip_all "The Quick Brown Fox" "Quick "
461
+ The Brown Fox
462
+ ```
463
+
464
+ ## Strip first occurrence of pattern from string
465
+
466
+ **Example Function:**
467
+
468
+ ```sh
469
+ strip() {
470
+ # Usage: strip "string" "pattern"
471
+ printf '%s\n' "${1/$2}"
472
+ }
473
+ ```
474
+
475
+ **Example Usage:**
476
+
477
+ ```shell
478
+ $ strip "The Quick Brown Fox" "[aeiou]"
479
+ Th Quick Brown Fox
480
+
481
+ $ strip "The Quick Brown Fox" "[[:space:]]"
482
+ TheQuick Brown Fox
483
+ ```
484
+
485
+ ## Strip pattern from start of string
486
+
487
+ **Example Function:**
488
+
489
+ ```sh
490
+ lstrip() {
491
+ # Usage: lstrip "string" "pattern"
492
+ printf '%s\n' "${1##$2}"
493
+ }
494
+ ```
495
+
496
+ **Example Usage:**
497
+
498
+ ```shell
499
+ $ lstrip "The Quick Brown Fox" "The "
500
+ Quick Brown Fox
501
+ ```
502
+
503
+ ## Strip pattern from end of string
504
+
505
+ **Example Function:**
506
+
507
+ ```sh
508
+ rstrip() {
509
+ # Usage: rstrip "string" "pattern"
510
+ printf '%s\n' "${1%%$2}"
511
+ }
512
+ ```
513
+
514
+ **Example Usage:**
515
+
516
+ ```shell
517
+ $ rstrip "The Quick Brown Fox" " Fox"
518
+ The Quick Brown
519
+ ```
520
+
521
+ ## Percent-encode a string
522
+
523
+ **Example Function:**
524
+
525
+ ```sh
526
+ urlencode() {
527
+ # Usage: urlencode "string"
528
+ local LC_ALL=C
529
+ for (( i = 0; i < ${#1}; i++ )); do
530
+ : "${1:i:1}"
531
+ case "$_" in
532
+ [a-zA-Z0-9.~_-])
533
+ printf '%s' "$_"
534
+ ;;
535
+
536
+ *)
537
+ printf '%%%02X' "'$_"
538
+ ;;
539
+ esac
540
+ done
541
+ printf '\n'
542
+ }
543
+ ```
544
+
545
+ **Example Usage:**
546
+
547
+ ```shell
548
+ $ urlencode "https://github.com/dylanaraps/pure-bash-bible"
549
+ https%3A%2F%2Fgithub.com%2Fdylanaraps%2Fpure-bash-bible
550
+ ```
551
+
552
+ ## Decode a percent-encoded string
553
+
554
+ **Example Function:**
555
+
556
+ ```sh
557
+ urldecode() {
558
+ # Usage: urldecode "string"
559
+ : "${1//+/ }"
560
+ printf '%b\n' "${_//%/\\x}"
561
+ }
562
+ ```
563
+
564
+ **Example Usage:**
565
+
566
+ ```shell
567
+ $ urldecode "https%3A%2F%2Fgithub.com%2Fdylanaraps%2Fpure-bash-bible"
568
+ https://github.com/dylanaraps/pure-bash-bible
569
+ ```
570
+
571
+ ## Check if string contains a sub-string
572
+
573
+ **Using a test:**
574
+
575
+ ```shell
576
+ if [[ $var == *sub_string* ]]; then
577
+ printf '%s\n' "sub_string is in var."
578
+ fi
579
+
580
+ # Inverse (substring not in string).
581
+ if [[ $var != *sub_string* ]]; then
582
+ printf '%s\n' "sub_string is not in var."
583
+ fi
584
+
585
+ # This works for arrays too!
586
+ if [[ ${arr[*]} == *sub_string* ]]; then
587
+ printf '%s\n' "sub_string is in array."
588
+ fi
589
+ ```
590
+
591
+ **Using a case statement:**
592
+
593
+ ```shell
594
+ case "$var" in
595
+ *sub_string*)
596
+ # Do stuff
597
+ ;;
598
+
599
+ *sub_string2*)
600
+ # Do more stuff
601
+ ;;
602
+
603
+ *)
604
+ # Else
605
+ ;;
606
+ esac
607
+ ```
608
+
609
+ ## Check if string starts with sub-string
610
+
611
+ ```shell
612
+ if [[ $var == sub_string* ]]; then
613
+ printf '%s\n' "var starts with sub_string."
614
+ fi
615
+
616
+ # Inverse (var does not start with sub_string).
617
+ if [[ $var != sub_string* ]]; then
618
+ printf '%s\n' "var does not start with sub_string."
619
+ fi
620
+ ```
621
+
622
+ ## Check if string ends with sub-string
623
+
624
+ ```shell
625
+ if [[ $var == *sub_string ]]; then
626
+ printf '%s\n' "var ends with sub_string."
627
+ fi
628
+
629
+ # Inverse (var does not end with sub_string).
630
+ if [[ $var != *sub_string ]]; then
631
+ printf '%s\n' "var does not end with sub_string."
632
+ fi
633
+ ```
634
+
635
+ <!-- CHAPTER END -->
636
+
637
+ <!-- CHAPTER START -->
638
+ # ARRAYS
639
+
640
+ ## Reverse an array
641
+
642
+ Enabling `extdebug` allows access to the `BASH_ARGV` array which stores
643
+ the current function’s arguments in reverse.
644
+
645
+ **CAVEAT**: Requires `shopt -s compat44` in `bash` 5.0+.
646
+
647
+ **Example Function:**
648
+
649
+ ```sh
650
+ reverse_array() {
651
+ # Usage: reverse_array "array"
652
+ shopt -s extdebug
653
+ f()(printf '%s\n' "${BASH_ARGV[@]}"); f "$@"
654
+ shopt -u extdebug
655
+ }
656
+ ```
657
+
658
+ **Example Usage:**
659
+
660
+ ```shell
661
+ $ reverse_array 1 2 3 4 5
662
+ 5
663
+ 4
664
+ 3
665
+ 2
666
+ 1
667
+
668
+ $ arr=(red blue green)
669
+ $ reverse_array "${arr[@]}"
670
+ green
671
+ blue
672
+ red
673
+ ```
674
+
675
+ ## Remove duplicate array elements
676
+
677
+ Create a temporary associative array. When setting associative array
678
+ values and a duplicate assignment occurs, bash overwrites the key. This
679
+ allows us to effectively remove array duplicates.
680
+
681
+ **CAVEAT:** Requires `bash` 4+
682
+
683
+ **CAVEAT:** List order may not stay the same.
684
+
685
+ **Example Function:**
686
+
687
+ ```sh
688
+ remove_array_dups() {
689
+ # Usage: remove_array_dups "array"
690
+ declare -A tmp_array
691
+
692
+ for i in "$@"; do
693
+ [[ $i ]] && IFS=" " tmp_array["${i:- }"]=1
694
+ done
695
+
696
+ printf '%s\n' "${!tmp_array[@]}"
697
+ }
698
+ ```
699
+
700
+ **Example Usage:**
701
+
702
+ ```shell
703
+ $ remove_array_dups 1 1 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5 5
704
+ 1
705
+ 2
706
+ 3
707
+ 4
708
+ 5
709
+
710
+ $ arr=(red red green blue blue)
711
+ $ remove_array_dups "${arr[@]}"
712
+ red
713
+ green
714
+ blue
715
+ ```
716
+
717
+ ## Random array element
718
+
719
+ **Example Function:**
720
+
721
+ ```sh
722
+ random_array_element() {
723
+ # Usage: random_array_element "array"
724
+ local arr=("$@")
725
+ printf '%s\n' "${arr[RANDOM % $#]}"
726
+ }
727
+ ```
728
+
729
+ **Example Usage:**
730
+
731
+ ```shell
732
+ $ array=(red green blue yellow brown)
733
+ $ random_array_element "${array[@]}"
734
+ yellow
735
+
736
+ # Multiple arguments can also be passed.
737
+ $ random_array_element 1 2 3 4 5 6 7
738
+ 3
739
+ ```
740
+
741
+ ## Cycle through an array
742
+
743
+ Each time the `printf` is called, the next array element is printed. When
744
+ the print hits the last array element it starts from the first element
745
+ again.
746
+
747
+ ```sh
748
+ arr=(a b c d)
749
+
750
+ cycle() {
751
+ printf '%s ' "${arr[${i:=0}]}"
752
+ ((i=i>=${#arr[@]}-1?0:++i))
753
+ }
754
+ ```
755
+
756
+
757
+ ## Toggle between two values
758
+
759
+ This works the same as above, this is just a different use case.
760
+
761
+ ```sh
762
+ arr=(true false)
763
+
764
+ cycle() {
765
+ printf '%s ' "${arr[${i:=0}]}"
766
+ ((i=i>=${#arr[@]}-1?0:++i))
767
+ }
768
+ ```
769
+
770
+ <!-- CHAPTER END -->
771
+
772
+ <!-- CHAPTER START -->
773
+ # LOOPS
774
+
775
+ ## Loop over a range of numbers
776
+
777
+ Alternative to `seq`.
778
+
779
+ ```shell
780
+ # Loop from 0-100 (no variable support).
781
+ for i in {0..100}; do
782
+ printf '%s\n' "$i"
783
+ done
784
+ ```
785
+
786
+ ## Loop over a variable range of numbers
787
+
788
+ Alternative to `seq`.
789
+
790
+ ```shell
791
+ # Loop from 0-VAR.
792
+ VAR=50
793
+ for ((i=0;i<=VAR;i++)); do
794
+ printf '%s\n' "$i"
795
+ done
796
+ ```
797
+
798
+ ## Loop over an array
799
+
800
+ ```shell
801
+ arr=(apples oranges tomatoes)
802
+
803
+ # Just elements.
804
+ for element in "${arr[@]}"; do
805
+ printf '%s\n' "$element"
806
+ done
807
+ ```
808
+
809
+ ## Loop over an array with an index
810
+
811
+ ```shell
812
+ arr=(apples oranges tomatoes)
813
+
814
+ # Elements and index.
815
+ for i in "${!arr[@]}"; do
816
+ printf '%s\n' "${arr[i]}"
817
+ done
818
+
819
+ # Alternative method.
820
+ for ((i=0;i<${#arr[@]};i++)); do
821
+ printf '%s\n' "${arr[i]}"
822
+ done
823
+ ```
824
+
825
+ ## Loop over the contents of a file
826
+
827
+ ```shell
828
+ while read -r line; do
829
+ printf '%s\n' "$line"
830
+ done < "file"
831
+ ```
832
+
833
+ ## Loop over files and directories
834
+
835
+ Don’t use `ls`.
836
+
837
+ ```shell
838
+ # Greedy example.
839
+ for file in *; do
840
+ printf '%s\n' "$file"
841
+ done
842
+
843
+ # PNG files in dir.
844
+ for file in ~/Pictures/*.png; do
845
+ printf '%s\n' "$file"
846
+ done
847
+
848
+ # Iterate over directories.
849
+ for dir in ~/Downloads/*/; do
850
+ printf '%s\n' "$dir"
851
+ done
852
+
853
+ # Brace Expansion.
854
+ for file in /path/to/parentdir/{file1,file2,subdir/file3}; do
855
+ printf '%s\n' "$file"
856
+ done
857
+
858
+ # Iterate recursively.
859
+ shopt -s globstar
860
+ for file in ~/Pictures/**/*; do
861
+ printf '%s\n' "$file"
862
+ done
863
+ shopt -u globstar
864
+ ```
865
+
866
+ <!-- CHAPTER END -->
867
+
868
+ <!-- CHAPTER START -->
869
+ # FILE HANDLING
870
+
871
+ **CAVEAT:** `bash` does not handle binary data properly in versions `< 4.4`.
872
+
873
+ ## Read a file to a string
874
+
875
+ Alternative to the `cat` command.
876
+
877
+ ```shell
878
+ file_data="$(<"file")"
879
+ ```
880
+
881
+ ## Read a file to an array (*by line*)
882
+
883
+ Alternative to the `cat` command.
884
+
885
+ ```shell
886
+ # Bash <4 (discarding empty lines).
887
+ IFS=$'\n' read -d "" -ra file_data < "file"
888
+
889
+ # Bash <4 (preserving empty lines).
890
+ while read -r line; do
891
+ file_data+=("$line")
892
+ done < "file"
893
+
894
+ # Bash 4+
895
+ mapfile -t file_data < "file"
896
+ ```
897
+
898
+ ## Get the first N lines of a file
899
+
900
+ Alternative to the `head` command.
901
+
902
+ **CAVEAT:** Requires `bash` 4+
903
+
904
+ **Example Function:**
905
+
906
+ ```sh
907
+ head() {
908
+ # Usage: head "n" "file"
909
+ mapfile -tn "$1" line < "$2"
910
+ printf '%s\n' "${line[@]}"
911
+ }
912
+ ```
913
+
914
+ **Example Usage:**
915
+
916
+ ```shell
917
+ $ head 2 ~/.bashrc
918
+ # Prompt
919
+ PS1='➜ '
920
+
921
+ $ head 1 ~/.bashrc
922
+ # Prompt
923
+ ```
924
+
925
+ ## Get the last N lines of a file
926
+
927
+ Alternative to the `tail` command.
928
+
929
+ **CAVEAT:** Requires `bash` 4+
930
+
931
+ **Example Function:**
932
+
933
+ ```sh
934
+ tail() {
935
+ # Usage: tail "n" "file"
936
+ mapfile -tn 0 line < "$2"
937
+ printf '%s\n' "${line[@]: -$1}"
938
+ }
939
+ ```
940
+
941
+ **Example Usage:**
942
+
943
+ ```shell
944
+ $ tail 2 ~/.bashrc
945
+ # Enable tmux.
946
+ # [[ -z "$TMUX" ]] && exec tmux
947
+
948
+ $ tail 1 ~/.bashrc
949
+ # [[ -z "$TMUX" ]] && exec tmux
950
+ ```
951
+
952
+ ## Get the number of lines in a file
953
+
954
+ Alternative to `wc -l`.
955
+
956
+ **Example Function (bash 4):**
957
+
958
+ ```sh
959
+ lines() {
960
+ # Usage: lines "file"
961
+ mapfile -tn 0 lines < "$1"
962
+ printf '%s\n' "${#lines[@]}"
963
+ }
964
+ ```
965
+
966
+ **Example Function (bash 3):**
967
+
968
+ This method uses less memory than the `mapfile` method and works in `bash` 3 but it is slower for bigger files.
969
+
970
+ ```sh
971
+ lines_loop() {
972
+ # Usage: lines_loop "file"
973
+ count=0
974
+ while IFS= read -r _; do
975
+ ((count++))
976
+ done < "$1"
977
+ printf '%s\n' "$count"
978
+ }
979
+ ```
980
+
981
+ **Example Usage:**
982
+
983
+ ```shell
984
+ $ lines ~/.bashrc
985
+ 48
986
+
987
+ $ lines_loop ~/.bashrc
988
+ 48
989
+ ```
990
+
991
+ ## Count files or directories in directory
992
+
993
+ This works by passing the output of the glob to the function and then counting the number of arguments.
994
+
995
+ **Example Function:**
996
+
997
+ ```sh
998
+ count() {
999
+ # Usage: count /path/to/dir/*
1000
+ # count /path/to/dir/*/
1001
+ printf '%s\n' "$#"
1002
+ }
1003
+ ```
1004
+
1005
+ **Example Usage:**
1006
+
1007
+ ```shell
1008
+ # Count all files in dir.
1009
+ $ count ~/Downloads/*
1010
+ 232
1011
+
1012
+ # Count all dirs in dir.
1013
+ $ count ~/Downloads/*/
1014
+ 45
1015
+
1016
+ # Count all jpg files in dir.
1017
+ $ count ~/Pictures/*.jpg
1018
+ 64
1019
+ ```
1020
+
1021
+ ## Create an empty file
1022
+
1023
+ Alternative to `touch`.
1024
+
1025
+ ```shell
1026
+ # Shortest.
1027
+ >file
1028
+
1029
+ # Longer alternatives:
1030
+ :>file
1031
+ echo -n >file
1032
+ printf '' >file
1033
+ ```
1034
+
1035
+ ## Extract lines between two markers
1036
+
1037
+ **Example Function:**
1038
+
1039
+ ```sh
1040
+ extract() {
1041
+ # Usage: extract file "opening marker" "closing marker"
1042
+ while IFS=$'\n' read -r line; do
1043
+ [[ $extract && $line != "$3" ]] &&
1044
+ printf '%s\n' "$line"
1045
+
1046
+ [[ $line == "$2" ]] && extract=1
1047
+ [[ $line == "$3" ]] && extract=
1048
+ done < "$1"
1049
+ }
1050
+ ```
1051
+
1052
+ **Example Usage:**
1053
+
1054
+ ```shell
1055
+ # Extract code blocks from MarkDown file.
1056
+ $ extract ~/projects/pure-bash/README.md '```sh' '```'
1057
+ # Output here...
1058
+ ```
1059
+
1060
+ <!-- CHAPTER END -->
1061
+
1062
+ <!-- CHAPTER START -->
1063
+ # FILE PATHS
1064
+
1065
+ ## Get the directory name of a file path
1066
+
1067
+ Alternative to the `dirname` command.
1068
+
1069
+ **Example Function:**
1070
+
1071
+ ```sh
1072
+ dirname() {
1073
+ # Usage: dirname "path"
1074
+ local tmp=${1:-.}
1075
+
1076
+ [[ $tmp != *[!/]* ]] && {
1077
+ printf '/\n'
1078
+ return
1079
+ }
1080
+
1081
+ tmp=${tmp%%"${tmp##*[!/]}"}
1082
+
1083
+ [[ $tmp != */* ]] && {
1084
+ printf '.\n'
1085
+ return
1086
+ }
1087
+
1088
+ tmp=${tmp%/*}
1089
+ tmp=${tmp%%"${tmp##*[!/]}"}
1090
+
1091
+ printf '%s\n' "${tmp:-/}"
1092
+ }
1093
+ ```
1094
+
1095
+ **Example Usage:**
1096
+
1097
+ ```shell
1098
+ $ dirname ~/Pictures/Wallpapers/1.jpg
1099
+ /home/black/Pictures/Wallpapers
1100
+
1101
+ $ dirname ~/Pictures/Downloads/
1102
+ /home/black/Pictures
1103
+ ```
1104
+
1105
+ ## Get the base-name of a file path
1106
+
1107
+ Alternative to the `basename` command.
1108
+
1109
+ **Example Function:**
1110
+
1111
+ ```sh
1112
+ basename() {
1113
+ # Usage: basename "path" ["suffix"]
1114
+ local tmp
1115
+
1116
+ tmp=${1%"${1##*[!/]}"}
1117
+ tmp=${tmp##*/}
1118
+ tmp=${tmp%"${2/"$tmp"}"}
1119
+
1120
+ printf '%s\n' "${tmp:-/}"
1121
+ }
1122
+ ```
1123
+
1124
+ **Example Usage:**
1125
+
1126
+ ```shell
1127
+ $ basename ~/Pictures/Wallpapers/1.jpg
1128
+ 1.jpg
1129
+
1130
+ $ basename ~/Pictures/Wallpapers/1.jpg .jpg
1131
+ 1
1132
+
1133
+ $ basename ~/Pictures/Downloads/
1134
+ Downloads
1135
+ ```
1136
+
1137
+ <!-- CHAPTER END -->
1138
+
1139
+ <!-- CHAPTER START -->
1140
+ # VARIABLES
1141
+
1142
+ ## Assign and access a variable using a variable
1143
+
1144
+ ```shell
1145
+ $ hello_world="value"
1146
+
1147
+ # Create the variable name.
1148
+ $ var="world"
1149
+ $ ref="hello_$var"
1150
+
1151
+ # Print the value of the variable name stored in 'hello_$var'.
1152
+ $ printf '%s\n' "${!ref}"
1153
+ value
1154
+ ```
1155
+
1156
+ Alternatively, on `bash` 4.3+:
1157
+
1158
+ ```shell
1159
+ $ hello_world="value"
1160
+ $ var="world"
1161
+
1162
+ # Declare a nameref.
1163
+ $ declare -n ref=hello_$var
1164
+
1165
+ $ printf '%s\n' "$ref"
1166
+ value
1167
+ ```
1168
+
1169
+ ## Name a variable based on another variable
1170
+
1171
+ ```shell
1172
+ $ var="world"
1173
+ $ declare "hello_$var=value"
1174
+ $ printf '%s\n' "$hello_world"
1175
+ value
1176
+ ```
1177
+
1178
+ <!-- CHAPTER END -->
1179
+
1180
+ <!-- CHAPTER START -->
1181
+ # ESCAPE SEQUENCES
1182
+
1183
+ Contrary to popular belief, there is no issue in utilizing raw escape sequences. Using `tput` abstracts the same ANSI sequences as if printed manually. Worse still, `tput` is not actually portable. There are a number of `tput` variants each with different commands and syntaxes (*try `tput setaf 3` on a FreeBSD system*). Raw sequences are fine.
1184
+
1185
+ ## Text Colors
1186
+
1187
+ **NOTE:** Sequences requiring RGB values only work in True-Color Terminal Emulators.
1188
+
1189
+ | Sequence | What does it do? | Value |
1190
+ | -------- | ---------------- | ----- |
1191
+ | `\e[38;5;<NUM>m` | Set text foreground color. | `0-255`
1192
+ | `\e[48;5;<NUM>m` | Set text background color. | `0-255`
1193
+ | `\e[38;2;<R>;<G>;<B>m` | Set text foreground color to RGB color. | `R`, `G`, `B`
1194
+ | `\e[48;2;<R>;<G>;<B>m` | Set text background color to RGB color. | `R`, `G`, `B`
1195
+
1196
+ ## Text Attributes
1197
+
1198
+ **NOTE:** Prepend 2 to any code below to turn it's effect off
1199
+ (examples: 21=bold text off, 22=faint text off, 23=italic text off).
1200
+
1201
+ | Sequence | What does it do? |
1202
+ | -------- | ---------------- |
1203
+ | `\e[m` | Reset text formatting and colors. |
1204
+ | `\e[1m` | Bold text. |
1205
+ | `\e[2m` | Faint text. |
1206
+ | `\e[3m` | Italic text. |
1207
+ | `\e[4m` | Underline text. |
1208
+ | `\e[5m` | Blinking text. |
1209
+ | `\e[7m` | Highlighted text. |
1210
+ | `\e[8m` | Hidden text. |
1211
+ | `\e[9m` | Strike-through text. |
1212
+
1213
+
1214
+ ## Cursor Movement
1215
+
1216
+ | Sequence | What does it do? | Value |
1217
+ | -------- | ---------------- | ----- |
1218
+ | `\e[<LINE>;<COLUMN>H` | Move cursor to absolute position. | `line`, `column`
1219
+ | `\e[H` | Move cursor to home position (`0,0`). |
1220
+ | `\e[<NUM>A` | Move cursor up N lines. | `num`
1221
+ | `\e[<NUM>B` | Move cursor down N lines. | `num`
1222
+ | `\e[<NUM>C` | Move cursor right N columns. | `num`
1223
+ | `\e[<NUM>D` | Move cursor left N columns. | `num`
1224
+ | `\e[s` | Save cursor position. |
1225
+ | `\e[u` | Restore cursor position. |
1226
+
1227
+
1228
+ ## Erasing Text
1229
+
1230
+ | Sequence | What does it do? |
1231
+ | -------- | ---------------- |
1232
+ | `\e[K` | Erase from cursor position to end of line.
1233
+ | `\e[1K` | Erase from cursor position to start of line.
1234
+ | `\e[2K` | Erase the entire current line.
1235
+ | `\e[J` | Erase from the current line to the bottom of the screen.
1236
+ | `\e[1J` | Erase from the current line to the top of the screen.
1237
+ | `\e[2J` | Clear the screen.
1238
+ | `\e[2J\e[H` | Clear the screen and move cursor to `0,0`.
1239
+
1240
+
1241
+ <!-- CHAPTER END -->
1242
+
1243
+ <!-- CHAPTER START -->
1244
+ # PARAMETER EXPANSION
1245
+
1246
+ ## Indirection
1247
+
1248
+ | Parameter | What does it do? |
1249
+ | --------- | ---------------- |
1250
+ | `${!VAR}` | Access a variable based on the value of `VAR`.
1251
+ | `${!VAR*}` | Expand to `IFS` separated list of variable names starting with `VAR`. |
1252
+ | `${!VAR@}` | Expand to `IFS` separated list of variable names starting with `VAR`. If double-quoted, each variable name expands to a separate word. |
1253
+
1254
+
1255
+ ## Replacement
1256
+
1257
+ | Parameter | What does it do? |
1258
+ | --------- | ---------------- |
1259
+ | `${VAR#PATTERN}` | Remove shortest match of pattern from start of string. |
1260
+ | `${VAR##PATTERN}` | Remove longest match of pattern from start of string. |
1261
+ | `${VAR%PATTERN}` | Remove shortest match of pattern from end of string. |
1262
+ | `${VAR%%PATTERN}` | Remove longest match of pattern from end of string. |
1263
+ | `${VAR/PATTERN/REPLACE}` | Replace first match with string.
1264
+ | `${VAR//PATTERN/REPLACE}` | Replace all matches with string.
1265
+ | `${VAR/PATTERN}` | Remove first match.
1266
+ | `${VAR//PATTERN}` | Remove all matches.
1267
+
1268
+ ## Length
1269
+
1270
+ | Parameter | What does it do? |
1271
+ | --------- | ---------------- |
1272
+ | `${#VAR}` | Length of var in characters.
1273
+ | `${#ARR[@]}` | Length of array in elements.
1274
+
1275
+ ## Expansion
1276
+
1277
+ | Parameter | What does it do? |
1278
+ | --------- | ---------------- |
1279
+ | `${VAR:OFFSET}` | Remove first `N` chars from variable.
1280
+ | `${VAR:OFFSET:LENGTH}` | Get substring from `N` character to `N` character. <br> (`${VAR:10:10}`: Get sub-string from char `10` to char `20`)
1281
+ | `${VAR:: OFFSET}` | Get first `N` chars from variable.
1282
+ | `${VAR:: -OFFSET}` | Remove last `N` chars from variable.
1283
+ | `${VAR: -OFFSET}` | Get last `N` chars from variable.
1284
+ | `${VAR:OFFSET:-OFFSET}` | Cut first `N` chars and last `N` chars. | `bash 4.2+` |
1285
+
1286
+ ## Case Modification
1287
+
1288
+ | Parameter | What does it do? | CAVEAT |
1289
+ | --------- | ---------------- | ------ |
1290
+ | `${VAR^}` | Uppercase first character. | `bash 4+` |
1291
+ | `${VAR^^}` | Uppercase all characters. | `bash 4+` |
1292
+ | `${VAR,}` | Lowercase first character. | `bash 4+` |
1293
+ | `${VAR,,}` | Lowercase all characters. | `bash 4+` |
1294
+ | `${VAR~}` | Reverse case of first character. | `bash 4+` |
1295
+ | `${VAR~~}` | Reverse case of all characters. | `bash 4+` |
1296
+
1297
+
1298
+ ## Default Value
1299
+
1300
+ | Parameter | What does it do? |
1301
+ | --------- | ---------------- |
1302
+ | `${VAR:-STRING}` | If `VAR` is empty or unset, use `STRING` as its value.
1303
+ | `${VAR-STRING}` | If `VAR` is unset, use `STRING` as its value.
1304
+ | `${VAR:=STRING}` | If `VAR` is empty or unset, set the value of `VAR` to `STRING`.
1305
+ | `${VAR=STRING}` | If `VAR` is unset, set the value of `VAR` to `STRING`.
1306
+ | `${VAR:+STRING}` | If `VAR` is not empty, use `STRING` as its value.
1307
+ | `${VAR+STRING}` | If `VAR` is set, use `STRING` as its value.
1308
+ | `${VAR:?STRING}` | Display an error if empty or unset.
1309
+ | `${VAR?STRING}` | Display an error if unset.
1310
+
1311
+
1312
+ <!-- CHAPTER END -->
1313
+
1314
+ <!-- CHAPTER START -->
1315
+ # BRACE EXPANSION
1316
+
1317
+ ## Ranges
1318
+
1319
+ ```shell
1320
+ # Syntax: {<START>..<END>}
1321
+
1322
+ # Print numbers 1-100.
1323
+ echo {1..100}
1324
+
1325
+ # Print range of floats.
1326
+ echo 1.{1..9}
1327
+
1328
+ # Print chars a-z.
1329
+ echo {a..z}
1330
+ echo {A..Z}
1331
+
1332
+ # Nesting.
1333
+ echo {A..Z}{0..9}
1334
+
1335
+ # Print zero-padded numbers.
1336
+ # CAVEAT: bash 4+
1337
+ echo {01..100}
1338
+
1339
+ # Change increment amount.
1340
+ # Syntax: {<START>..<END>..<INCREMENT>}
1341
+ # CAVEAT: bash 4+
1342
+ echo {1..10..2} # Increment by 2.
1343
+ ```
1344
+
1345
+ ## String Lists
1346
+
1347
+ ```shell
1348
+ echo {apples,oranges,pears,grapes}
1349
+
1350
+ # Example Usage:
1351
+ # Remove dirs Movies, Music and ISOS from ~/Downloads/.
1352
+ rm -rf ~/Downloads/{Movies,Music,ISOS}
1353
+ ```
1354
+
1355
+ <!-- CHAPTER END -->
1356
+
1357
+
1358
+ <!-- CHAPTER START -->
1359
+
1360
+ # CONDITIONAL EXPRESSIONS
1361
+
1362
+ ## File Conditionals
1363
+
1364
+ | Expression | Value | What does it do? |
1365
+ | ---------- | ------ | ---------------- |
1366
+ | `-a` | `file` | If file exists.
1367
+ | `-b` | `file` | If file exists and is a block special file.
1368
+ | `-c` | `file` | If file exists and is a character special file.
1369
+ | `-d` | `file` | If file exists and is a directory.
1370
+ | `-e` | `file` | If file exists.
1371
+ | `-f` | `file` | If file exists and is a regular file.
1372
+ | `-g` | `file` | If file exists and its set-group-id bit is set.
1373
+ | `-h` | `file` | If file exists and is a symbolic link.
1374
+ | `-k` | `file` | If file exists and its sticky-bit is set
1375
+ | `-p` | `file` | If file exists and is a named pipe (*FIFO*).
1376
+ | `-r` | `file` | If file exists and is readable.
1377
+ | `-s` | `file` | If file exists and its size is greater than zero.
1378
+ | `-t` | `fd` | If file descriptor is open and refers to a terminal.
1379
+ | `-u` | `file` | If file exists and its set-user-id bit is set.
1380
+ | `-w` | `file` | If file exists and is writable.
1381
+ | `-x` | `file` | If file exists and is executable.
1382
+ | `-G` | `file` | If file exists and is owned by the effective group ID.
1383
+ | `-L` | `file` | If file exists and is a symbolic link.
1384
+ | `-N` | `file` | If file exists and has been modified since last read.
1385
+ | `-O` | `file` | If file exists and is owned by the effective user ID.
1386
+ | `-S` | `file` | If file exists and is a socket.
1387
+
1388
+ ## File Comparisons
1389
+
1390
+ | Expression | What does it do? |
1391
+ | ---------- | ---------------- |
1392
+ | `file -ef file2` | If both files refer to the same inode and device numbers.
1393
+ | `file -nt file2` | If `file` is newer than `file2` (*uses modification time*) or `file` exists and `file2` does not.
1394
+ | `file -ot file2` | If `file` is older than `file2` (*uses modification time*) or `file2` exists and `file` does not.
1395
+
1396
+ ## Variable Conditionals
1397
+
1398
+ | Expression | Value | What does it do? |
1399
+ | ---------- | ----- | ---------------- |
1400
+ | `-o` | `opt` | If shell option is enabled.
1401
+ | `-v` | `var` | If variable has a value assigned.
1402
+ | `-R` | `var` | If variable is a name reference.
1403
+ | `-z` | `var` | If the length of string is zero.
1404
+ | `-n` | `var` | If the length of string is non-zero.
1405
+
1406
+ ## Variable Comparisons
1407
+
1408
+ | Expression | What does it do? |
1409
+ | ---------- | ---------------- |
1410
+ | `var = var2` | Equal to.
1411
+ | `var == var2` | Equal to (*synonym for `=`*).
1412
+ | `var != var2` | Not equal to.
1413
+ | `var < var2` | Less than (*in ASCII alphabetical order.*)
1414
+ | `var > var2` | Greater than (*in ASCII alphabetical order.*)
1415
+
1416
+ <!-- CHAPTER END -->
1417
+
1418
+ <!-- CHAPTER START -->
1419
+
1420
+ # ARITHMETIC OPERATORS
1421
+
1422
+ ## Assignment
1423
+
1424
+ | Operators | What does it do? |
1425
+ | --------- | ---------------- |
1426
+ | `=` | Initialize or change the value of a variable.
1427
+
1428
+ ## Arithmetic
1429
+
1430
+ | Operators | What does it do? |
1431
+ | --------- | ---------------- |
1432
+ | `+` | Addition
1433
+ | `-` | Subtraction
1434
+ | `*` | Multiplication
1435
+ | `/` | Division
1436
+ | `**` | Exponentiation
1437
+ | `%` | Modulo
1438
+ | `+=` | Plus-Equal (*Increment a variable.*)
1439
+ | `-=` | Minus-Equal (*Decrement a variable.*)
1440
+ | `*=` | Times-Equal (*Multiply a variable.*)
1441
+ | `/=` | Slash-Equal (*Divide a variable.*)
1442
+ | `%=` | Mod-Equal (*Remainder of dividing a variable.*)
1443
+
1444
+ ## Bitwise
1445
+
1446
+ | Operators | What does it do? |
1447
+ | --------- | ---------------- |
1448
+ | `<<` | Bitwise Left Shift
1449
+ | `<<=` | Left-Shift-Equal
1450
+ | `>>` | Bitwise Right Shift
1451
+ | `>>=` | Right-Shift-Equal
1452
+ | `&` | Bitwise AND
1453
+ | `&=` | Bitwise AND-Equal
1454
+ | `\|` | Bitwise OR
1455
+ | `\|=` | Bitwise OR-Equal
1456
+ | `~` | Bitwise NOT
1457
+ | `^` | Bitwise XOR
1458
+ | `^=` | Bitwise XOR-Equal
1459
+
1460
+ ## Logical
1461
+
1462
+ | Operators | What does it do? |
1463
+ | --------- | ---------------- |
1464
+ | `!` | NOT
1465
+ | `&&` | AND
1466
+ | `\|\|` | OR
1467
+
1468
+ ## Miscellaneous
1469
+
1470
+ | Operators | What does it do? | Example |
1471
+ | --------- | ---------------- | ------- |
1472
+ | `,` | Comma Separator | `((a=1,b=2,c=3))`
1473
+
1474
+
1475
+ <!-- CHAPTER END -->
1476
+
1477
+ <!-- CHAPTER START -->
1478
+ # ARITHMETIC
1479
+
1480
+ ## Simpler syntax to set variables
1481
+
1482
+ ```shell
1483
+ # Simple math
1484
+ ((var=1+2))
1485
+
1486
+ # Decrement/Increment variable
1487
+ ((var++))
1488
+ ((var--))
1489
+ ((var+=1))
1490
+ ((var-=1))
1491
+
1492
+ # Using variables
1493
+ ((var=var2*arr[2]))
1494
+ ```
1495
+
1496
+ ## Ternary Tests
1497
+
1498
+ ```shell
1499
+ # Set the value of var to var2 if var2 is greater than var.
1500
+ # var: variable to set.
1501
+ # var2>var: Condition to test.
1502
+ # ?var2: If the test succeeds.
1503
+ # :var: If the test fails.
1504
+ ((var=var2>var?var2:var))
1505
+ ```
1506
+
1507
+ <!-- CHAPTER END -->
1508
+
1509
+ <!-- CHAPTER START -->
1510
+ # TRAPS
1511
+
1512
+ Traps allow a script to execute code on various signals. In [pxltrm](https://github.com/dylanaraps/pxltrm) (*a pixel art editor written in bash*) traps are used to redraw the user interface on window resize. Another use case is cleaning up temporary files on script exit.
1513
+
1514
+ Traps should be added near the start of scripts so any early errors are also caught.
1515
+
1516
+ **NOTE:** For a full list of signals, see `trap -l`.
1517
+
1518
+
1519
+ ## Do something on script exit
1520
+
1521
+ ```shell
1522
+ # Clear screen on script exit.
1523
+ trap 'printf \\e[2J\\e[H\\e[m' EXIT
1524
+ ```
1525
+
1526
+ ## Ignore terminal interrupt (CTRL+C, SIGINT)
1527
+
1528
+ ```shell
1529
+ trap '' INT
1530
+ ```
1531
+
1532
+ ## React to window resize
1533
+
1534
+ ```shell
1535
+ # Call a function on window resize.
1536
+ trap 'code_here' SIGWINCH
1537
+ ```
1538
+
1539
+ ## Do something before every command
1540
+
1541
+ ```shell
1542
+ trap 'code_here' DEBUG
1543
+ ```
1544
+
1545
+ ## Do something when a shell function or a sourced file finishes executing
1546
+
1547
+ ```shell
1548
+ trap 'code_here' RETURN
1549
+ ```
1550
+
1551
+ <!-- CHAPTER END -->
1552
+
1553
+ <!-- CHAPTER START -->
1554
+ # PERFORMANCE
1555
+
1556
+ ## Disable Unicode
1557
+
1558
+ If unicode is not required, it can be disabled for a performance increase. Results may vary however there have been noticeable improvements in [neofetch](https://github.com/dylanaraps/neofetch) and other programs.
1559
+
1560
+ ```shell
1561
+ # Disable unicode.
1562
+ LC_ALL=C
1563
+ LANG=C
1564
+ ```
1565
+
1566
+ <!-- CHAPTER END -->
1567
+
1568
+ <!-- CHAPTER START -->
1569
+ # OBSOLETE SYNTAX
1570
+
1571
+ ## Shebang
1572
+
1573
+ Use `#!/usr/bin/env bash` instead of `#!/bin/bash`.
1574
+
1575
+ - The former searches the user's `PATH` to find the `bash` binary.
1576
+ - The latter assumes it is always installed to `/bin/` which can cause issues.
1577
+
1578
+ **NOTE**: There are times when one may have a good reason for using `#!/bin/bash` or another direct path to the binary.
1579
+
1580
+
1581
+ ```shell
1582
+ # Right:
1583
+
1584
+ #!/usr/bin/env bash
1585
+
1586
+ # Less right:
1587
+
1588
+ #!/bin/bash
1589
+ ```
1590
+
1591
+ ## Command Substitution
1592
+
1593
+ Use `$()` instead of `` ` ` ``.
1594
+
1595
+ ```shell
1596
+ # Right.
1597
+ var="$(command)"
1598
+
1599
+ # Wrong.
1600
+ var=`command`
1601
+
1602
+ # $() can easily be nested whereas `` cannot.
1603
+ var="$(command "$(command)")"
1604
+ ```
1605
+
1606
+ ## Function Declaration
1607
+
1608
+ Do not use the `function` keyword, it reduces compatibility with older versions of `bash`.
1609
+
1610
+ ```shell
1611
+ # Right.
1612
+ do_something() {
1613
+ # ...
1614
+ }
1615
+
1616
+ # Wrong.
1617
+ function do_something() {
1618
+ # ...
1619
+ }
1620
+ ```
1621
+
1622
+ <!-- CHAPTER END -->
1623
+
1624
+ <!-- CHAPTER START -->
1625
+ # INTERNAL VARIABLES
1626
+
1627
+ ## Get the location to the `bash` binary
1628
+
1629
+ ```shell
1630
+ "$BASH"
1631
+ ```
1632
+
1633
+ ## Get the version of the current running `bash` process
1634
+
1635
+ ```shell
1636
+ # As a string.
1637
+ "$BASH_VERSION"
1638
+
1639
+ # As an array.
1640
+ "${BASH_VERSINFO[@]}"
1641
+ ```
1642
+
1643
+ ## Open the user's preferred text editor
1644
+
1645
+ ```shell
1646
+ "$EDITOR" "$file"
1647
+
1648
+ # NOTE: This variable may be empty, set a fallback value.
1649
+ "${EDITOR:-vi}" "$file"
1650
+ ```
1651
+
1652
+ ## Get the name of the current function
1653
+
1654
+ ```shell
1655
+ # Current function.
1656
+ "${FUNCNAME[0]}"
1657
+
1658
+ # Parent function.
1659
+ "${FUNCNAME[1]}"
1660
+
1661
+ # So on and so forth.
1662
+ "${FUNCNAME[2]}"
1663
+ "${FUNCNAME[3]}"
1664
+
1665
+ # All functions including parents.
1666
+ "${FUNCNAME[@]}"
1667
+ ```
1668
+
1669
+ ## Get the host-name of the system
1670
+
1671
+ ```shell
1672
+ "$HOSTNAME"
1673
+
1674
+ # NOTE: This variable may be empty.
1675
+ # Optionally set a fallback to the hostname command.
1676
+ "${HOSTNAME:-$(hostname)}"
1677
+ ```
1678
+
1679
+ ## Get the architecture of the Operating System
1680
+
1681
+ ```shell
1682
+ "$HOSTTYPE"
1683
+ ```
1684
+
1685
+ ## Get the name of the Operating System / Kernel
1686
+
1687
+ This can be used to add conditional support for different Operating
1688
+ Systems without needing to call `uname`.
1689
+
1690
+ ```shell
1691
+ "$OSTYPE"
1692
+ ```
1693
+
1694
+ ## Get the current working directory
1695
+
1696
+ This is an alternative to the `pwd` built-in.
1697
+
1698
+ ```shell
1699
+ "$PWD"
1700
+ ```
1701
+
1702
+ ## Get the number of seconds the script has been running
1703
+
1704
+ ```shell
1705
+ "$SECONDS"
1706
+ ```
1707
+
1708
+ ## Get a pseudorandom integer
1709
+
1710
+ Each time `$RANDOM` is used, a different integer between `0` and `32767` is returned. This variable should not be used for anything related to security (*this includes encryption keys etc*).
1711
+
1712
+
1713
+ ```shell
1714
+ "$RANDOM"
1715
+ ```
1716
+
1717
+ <!-- CHAPTER END -->
1718
+
1719
+ <!-- CHAPTER START -->
1720
+ # INFORMATION ABOUT THE TERMINAL
1721
+
1722
+ ## Get the terminal size in lines and columns (*from a script*)
1723
+
1724
+ This is handy when writing scripts in pure bash and `stty`/`tput` can’t be
1725
+ called.
1726
+
1727
+ **Example Function:**
1728
+
1729
+ ```sh
1730
+ get_term_size() {
1731
+ # Usage: get_term_size
1732
+
1733
+ # (:;:) is a micro sleep to ensure the variables are
1734
+ # exported immediately.
1735
+ shopt -s checkwinsize; (:;:)
1736
+ printf '%s\n' "$LINES $COLUMNS"
1737
+ }
1738
+ ```
1739
+
1740
+ **Example Usage:**
1741
+
1742
+ ```shell
1743
+ # Output: LINES COLUMNS
1744
+ $ get_term_size
1745
+ 15 55
1746
+ ```
1747
+
1748
+ ## Get the terminal size in pixels
1749
+
1750
+ **CAVEAT**: This does not work in some terminal emulators.
1751
+
1752
+ **Example Function:**
1753
+
1754
+ ```sh
1755
+ get_window_size() {
1756
+ # Usage: get_window_size
1757
+ printf '%b' "${TMUX:+\\ePtmux;\\e}\\e[14t${TMUX:+\\e\\\\}"
1758
+ IFS=';t' read -d t -t 0.05 -sra term_size
1759
+ printf '%s\n' "${term_size[1]}x${term_size[2]}"
1760
+ }
1761
+ ```
1762
+
1763
+ **Example Usage:**
1764
+
1765
+ ```shell
1766
+ # Output: WIDTHxHEIGHT
1767
+ $ get_window_size
1768
+ 1200x800
1769
+
1770
+ # Output (fail):
1771
+ $ get_window_size
1772
+ x
1773
+ ```
1774
+
1775
+ ## Get the current cursor position
1776
+
1777
+ This is useful when creating a TUI in pure bash.
1778
+
1779
+ **Example Function:**
1780
+
1781
+ ```sh
1782
+ get_cursor_pos() {
1783
+ # Usage: get_cursor_pos
1784
+ IFS='[;' read -p $'\e[6n' -d R -rs _ y x _
1785
+ printf '%s\n' "$x $y"
1786
+ }
1787
+ ```
1788
+
1789
+ **Example Usage:**
1790
+
1791
+ ```shell
1792
+ # Output: X Y
1793
+ $ get_cursor_pos
1794
+ 1 8
1795
+ ```
1796
+
1797
+ <!-- CHAPTER END -->
1798
+
1799
+ <!-- CHAPTER START -->
1800
+ # CONVERSION
1801
+
1802
+ ## Convert a hex color to RGB
1803
+
1804
+ **Example Function:**
1805
+
1806
+ ```sh
1807
+ hex_to_rgb() {
1808
+ # Usage: hex_to_rgb "#FFFFFF"
1809
+ # hex_to_rgb "000000"
1810
+ : "${1/\#}"
1811
+ ((r=16#${_:0:2},g=16#${_:2:2},b=16#${_:4:2}))
1812
+ printf '%s\n' "$r $g $b"
1813
+ }
1814
+ ```
1815
+
1816
+ **Example Usage:**
1817
+
1818
+ ```shell
1819
+ $ hex_to_rgb "#FFFFFF"
1820
+ 255 255 255
1821
+ ```
1822
+
1823
+
1824
+ ## Convert an RGB color to hex
1825
+
1826
+ **Example Function:**
1827
+
1828
+ ```sh
1829
+ rgb_to_hex() {
1830
+ # Usage: rgb_to_hex "r" "g" "b"
1831
+ printf '#%02x%02x%02x\n' "$1" "$2" "$3"
1832
+ }
1833
+ ```
1834
+
1835
+ **Example Usage:**
1836
+
1837
+ ```shell
1838
+ $ rgb_to_hex "255" "255" "255"
1839
+ #FFFFFF
1840
+ ```
1841
+
1842
+
1843
+ # CODE GOLF
1844
+
1845
+ ## Shorter `for` loop syntax
1846
+
1847
+ ```shell
1848
+ # Tiny C Style.
1849
+ for((;i++<10;)){ echo "$i";}
1850
+
1851
+ # Undocumented method.
1852
+ for i in {1..10};{ echo "$i";}
1853
+
1854
+ # Expansion.
1855
+ for i in {1..10}; do echo "$i"; done
1856
+
1857
+ # C Style.
1858
+ for((i=0;i<=10;i++)); do echo "$i"; done
1859
+ ```
1860
+
1861
+ ## Shorter infinite loops
1862
+
1863
+ ```shell
1864
+ # Normal method
1865
+ while :; do echo hi; done
1866
+
1867
+ # Shorter
1868
+ for((;;)){ echo hi;}
1869
+ ```
1870
+
1871
+ ## Shorter function declaration
1872
+
1873
+ ```shell
1874
+ # Normal method
1875
+ f(){ echo hi;}
1876
+
1877
+ # Using a subshell
1878
+ f()(echo hi)
1879
+
1880
+ # Using arithmetic
1881
+ # This can be used to assign integer values.
1882
+ # Example: f a=1
1883
+ # f a++
1884
+ f()(($1))
1885
+
1886
+ # Using tests, loops etc.
1887
+ # NOTE: ‘while’, ‘until’, ‘case’, ‘(())’, ‘[[]]’ can also be used.
1888
+ f()if true; then echo "$1"; fi
1889
+ f()for i in "$@"; do echo "$i"; done
1890
+ ```
1891
+
1892
+ ## Shorter `if` syntax
1893
+
1894
+ ```shell
1895
+ # One line
1896
+ # Note: The 3rd statement may run when the 1st is true
1897
+ [[ $var == hello ]] && echo hi || echo bye
1898
+ [[ $var == hello ]] && { echo hi; echo there; } || echo bye
1899
+
1900
+ # Multi line (no else, single statement)
1901
+ # Note: The exit status may not be the same as with an if statement
1902
+ [[ $var == hello ]] &&
1903
+ echo hi
1904
+
1905
+ # Multi line (no else)
1906
+ [[ $var == hello ]] && {
1907
+ echo hi
1908
+ # ...
1909
+ }
1910
+ ```
1911
+
1912
+ ## Simpler `case` statement to set variable
1913
+
1914
+ The `:` built-in can be used to avoid repeating `variable=` in a case statement. The `$_` variable stores the last argument of the last command. `:` always succeeds so it can be used to store the variable value.
1915
+
1916
+ ```shell
1917
+ # Modified snippet from Neofetch.
1918
+ case "$OSTYPE" in
1919
+ "darwin"*)
1920
+ : "MacOS"
1921
+ ;;
1922
+
1923
+ "linux"*)
1924
+ : "Linux"
1925
+ ;;
1926
+
1927
+ *"bsd"* | "dragonfly" | "bitrig")
1928
+ : "BSD"
1929
+ ;;
1930
+
1931
+ "cygwin" | "msys" | "win32")
1932
+ : "Windows"
1933
+ ;;
1934
+
1935
+ *)
1936
+ printf '%s\n' "Unknown OS detected, aborting..." >&2
1937
+ exit 1
1938
+ ;;
1939
+ esac
1940
+
1941
+ # Finally, set the variable.
1942
+ os="$_"
1943
+ ```
1944
+
1945
+ <!-- CHAPTER END -->
1946
+
1947
+ <!-- CHAPTER START -->
1948
+ # OTHER
1949
+
1950
+ ## Use `read` as an alternative to the `sleep` command
1951
+
1952
+ Surprisingly, `sleep` is an external command and not a `bash` built-in.
1953
+
1954
+ **CAVEAT:** Requires `bash` 4+
1955
+
1956
+ **Example Function:**
1957
+
1958
+ ```sh
1959
+ read_sleep() {
1960
+ # Usage: read_sleep 1
1961
+ # read_sleep 0.2
1962
+ read -rt "$1" <> <(:) || :
1963
+ }
1964
+ ```
1965
+
1966
+ **Example Usage:**
1967
+
1968
+ ```shell
1969
+ read_sleep 1
1970
+ read_sleep 0.1
1971
+ read_sleep 30
1972
+ ```
1973
+
1974
+ For performance-critical situations, where it is not economic to open and close an excessive number of file descriptors, the allocation of a file descriptor may be done only once for all invocations of `read`:
1975
+
1976
+ (See the generic original implementation at https://blog.dhampir.no/content/sleeping-without-a-subprocess-in-bash-and-how-to-sleep-forever)
1977
+
1978
+ ```shell
1979
+ exec {sleep_fd}<> <(:)
1980
+ while some_quick_test; do
1981
+ # equivalent of sleep 0.001
1982
+ read -t 0.001 -u $sleep_fd
1983
+ done
1984
+ ```
1985
+
1986
+ ## Check if a program is in the user's PATH
1987
+
1988
+ ```shell
1989
+ # There are 3 ways to do this and either one can be used.
1990
+ type -p executable_name &>/dev/null
1991
+ hash executable_name &>/dev/null
1992
+ command -v executable_name &>/dev/null
1993
+
1994
+ # As a test.
1995
+ if type -p executable_name &>/dev/null; then
1996
+ # Program is in PATH.
1997
+ fi
1998
+
1999
+ # Inverse.
2000
+ if ! type -p executable_name &>/dev/null; then
2001
+ # Program is not in PATH.
2002
+ fi
2003
+
2004
+ # Example (Exit early if program is not installed).
2005
+ if ! type -p convert &>/dev/null; then
2006
+ printf '%s\n' "error: convert is not installed, exiting..."
2007
+ exit 1
2008
+ fi
2009
+ ```
2010
+
2011
+ ## Get the current date using `strftime`
2012
+
2013
+ Bash’s `printf` has a built-in method of getting the date which can be used in place of the `date` command.
2014
+
2015
+ **CAVEAT:** Requires `bash` 4+
2016
+
2017
+ **Example Function:**
2018
+
2019
+ ```sh
2020
+ date() {
2021
+ # Usage: date "format"
2022
+ # See: 'man strftime' for format.
2023
+ printf "%($1)T\\n" "-1"
2024
+ }
2025
+ ```
2026
+
2027
+ **Example Usage:**
2028
+
2029
+ ```shell
2030
+ # Using above function.
2031
+ $ date "%a %d %b - %l:%M %p"
2032
+ Fri 15 Jun - 10:00 AM
2033
+
2034
+ # Using printf directly.
2035
+ $ printf '%(%a %d %b - %l:%M %p)T\n' "-1"
2036
+ Fri 15 Jun - 10:00 AM
2037
+
2038
+ # Assigning a variable using printf.
2039
+ $ printf -v date '%(%a %d %b - %l:%M %p)T\n' '-1'
2040
+ $ printf '%s\n' "$date"
2041
+ Fri 15 Jun - 10:00 AM
2042
+ ```
2043
+
2044
+ ## Get the username of the current user
2045
+
2046
+ **CAVEAT:** Requires `bash` 4.4+
2047
+
2048
+ ```shell
2049
+ $ : \\u
2050
+ # Expand the parameter as if it were a prompt string.
2051
+ $ printf '%s\n' "${_@P}"
2052
+ black
2053
+ ```
2054
+
2055
+ ## Generate a UUID V4
2056
+
2057
+ **CAVEAT**: The generated value is not cryptographically secure.
2058
+
2059
+ **Example Function:**
2060
+
2061
+ ```sh
2062
+ uuid() {
2063
+ # Usage: uuid
2064
+ C="89ab"
2065
+
2066
+ for ((N=0;N<16;++N)); do
2067
+ B="$((RANDOM%256))"
2068
+
2069
+ case "$N" in
2070
+ 6) printf '4%x' "$((B%16))" ;;
2071
+ 8) printf '%c%x' "${C:$RANDOM%${#C}:1}" "$((B%16))" ;;
2072
+
2073
+ 3|5|7|9)
2074
+ printf '%02x-' "$B"
2075
+ ;;
2076
+
2077
+ *)
2078
+ printf '%02x' "$B"
2079
+ ;;
2080
+ esac
2081
+ done
2082
+
2083
+ printf '\n'
2084
+ }
2085
+ ```
2086
+
2087
+ **Example Usage:**
2088
+
2089
+ ```shell
2090
+ $ uuid
2091
+ d5b6c731-1310-4c24-9fe3-55d556d44374
2092
+ ```
2093
+
2094
+ ## Progress bars
2095
+
2096
+ This is a simple way of drawing progress bars without needing a for loop
2097
+ in the function itself.
2098
+
2099
+ **Example Function:**
2100
+
2101
+ ```sh
2102
+ bar() {
2103
+ # Usage: bar 1 10
2104
+ # ^----- Elapsed Percentage (0-100).
2105
+ # ^-- Total length in chars.
2106
+ ((elapsed=$1*$2/100))
2107
+
2108
+ # Create the bar with spaces.
2109
+ printf -v prog "%${elapsed}s"
2110
+ printf -v total "%$(($2-elapsed))s"
2111
+
2112
+ printf '%s\r' "[${prog// /-}${total}]"
2113
+ }
2114
+ ```
2115
+
2116
+ **Example Usage:**
2117
+
2118
+ ```shell
2119
+ for ((i=0;i<=100;i++)); do
2120
+ # Pure bash micro sleeps (for the example).
2121
+ (:;:) && (:;:) && (:;:) && (:;:) && (:;:)
2122
+
2123
+ # Print the bar.
2124
+ bar "$i" "10"
2125
+ done
2126
+
2127
+ printf '\n'
2128
+ ```
2129
+
2130
+ ## Get the list of functions in a script
2131
+
2132
+ ```sh
2133
+ get_functions() {
2134
+ # Usage: get_functions
2135
+ IFS=$'\n' read -d "" -ra functions < <(declare -F)
2136
+ printf '%s\n' "${functions[@]//declare -f }"
2137
+ }
2138
+ ```
2139
+
2140
+ ## Bypass shell aliases
2141
+
2142
+ ```shell
2143
+ # alias
2144
+ ls
2145
+
2146
+ # command
2147
+ # shellcheck disable=SC1001
2148
+ \ls
2149
+ ```
2150
+
2151
+ ## Bypass shell functions
2152
+
2153
+ ```shell
2154
+ # function
2155
+ ls
2156
+
2157
+ # command
2158
+ command ls
2159
+ ```
2160
+
2161
+ ## Run a command in the background
2162
+
2163
+ This will run the given command and keep it running, even after the terminal or SSH connection is terminated. All output is ignored.
2164
+
2165
+ ```sh
2166
+ bkr() {
2167
+ (nohup "$@" &>/dev/null &)
2168
+ }
2169
+
2170
+ bkr ./some_script.sh # some_script.sh is now running in the background
2171
+ ```
2172
+
2173
+ ## Capture the return value of a function without command substitution
2174
+
2175
+ **CAVEAT:** Requires `bash` 4+
2176
+
2177
+ This uses local namerefs to avoid using `var=$(some_func)` style command substitution for function output capture.
2178
+
2179
+ ```sh
2180
+ to_upper() {
2181
+ local -n ptr=${1}
2182
+
2183
+ ptr=${ptr^^}
2184
+ }
2185
+
2186
+ foo="bar"
2187
+ to_upper foo
2188
+ printf "%s\n" "${foo}" # BAR
2189
+ ```
2190
+
2191
+ <!-- CHAPTER END -->
2192
+
2193
+ # AFTERWORD
2194
+
2195
+ Thanks for reading! If this bible helped you in any way and you'd like to give back, consider donating. Donations give me the time to make this the best resource possible. Can't donate? That's OK, star the repo and share it with your friends!
2196
+
2197
+ <a href="https://www.patreon.com/dyla"><img src="https://img.shields.io/badge/donate-patreon-yellow.svg"></a>
2198
+
2199
+
2200
+ Rock on. 🤘
llms_serialization.ipynb CHANGED
@@ -2,7 +2,7 @@
2
  "cells": [
3
  {
4
  "cell_type": "code",
5
- "execution_count": 31,
6
  "metadata": {},
7
  "outputs": [],
8
  "source": [
@@ -15,20 +15,9 @@
15
  },
16
  {
17
  "cell_type": "code",
18
- "execution_count": 32,
19
  "metadata": {},
20
- "outputs": [
21
- {
22
- "data": {
23
- "text/plain": [
24
- "'\\n\\n今天是2020年7月5日(星期日)。'"
25
- ]
26
- },
27
- "execution_count": 32,
28
- "metadata": {},
29
- "output_type": "execute_result"
30
- }
31
- ],
32
  "source": [
33
  "llm = load_llm(\"./txt/llm.json\")\n",
34
  "llm(\"今天的日期?\")"
@@ -36,37 +25,9 @@
36
  },
37
  {
38
  "cell_type": "code",
39
- "execution_count": 33,
40
  "metadata": {},
41
- "outputs": [
42
- {
43
- "name": "stdout",
44
- "output_type": "stream",
45
- "text": [
46
- "\n",
47
- "\n",
48
- "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
49
- "\u001b[32;1m\u001b[1;3m I need to figure out the date today\n",
50
- "Action: Search\n",
51
- "Action Input: 今天的日期\u001b[0m\n",
52
- "Observation: \u001b[36;1m\u001b[1;3m随着Today'sDate365,迅速得到当前日期。 无论你有一台电脑,平板电脑或智能手机,当前日期会立即出现。 今天的日期是:. 周一; 2023 四月17; 2023/04/17 ...\u001b[0m\n",
53
- "Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n",
54
- "Final Answer: 2023年4月17日\u001b[0m\n",
55
- "\n",
56
- "\u001b[1m> Finished chain.\u001b[0m\n"
57
- ]
58
- },
59
- {
60
- "data": {
61
- "text/plain": [
62
- "'2023年4月17日'"
63
- ]
64
- },
65
- "execution_count": 33,
66
- "metadata": {},
67
- "output_type": "execute_result"
68
- }
69
- ],
70
  "source": [
71
  "tools = load_tools([\"serpapi\", \"llm-math\"], llm=llm)\n",
72
  "agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)\n",
@@ -90,7 +51,7 @@
90
  "name": "python",
91
  "nbconvert_exporter": "python",
92
  "pygments_lexer": "ipython3",
93
- "version": "3.10.10"
94
  },
95
  "orig_nbformat": 4
96
  },
 
2
  "cells": [
3
  {
4
  "cell_type": "code",
5
+ "execution_count": null,
6
  "metadata": {},
7
  "outputs": [],
8
  "source": [
 
15
  },
16
  {
17
  "cell_type": "code",
18
+ "execution_count": null,
19
  "metadata": {},
20
+ "outputs": [],
 
 
 
 
 
 
 
 
 
 
 
21
  "source": [
22
  "llm = load_llm(\"./txt/llm.json\")\n",
23
  "llm(\"今天的日期?\")"
 
25
  },
26
  {
27
  "cell_type": "code",
28
+ "execution_count": null,
29
  "metadata": {},
30
+ "outputs": [],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  "source": [
32
  "tools = load_tools([\"serpapi\", \"llm-math\"], llm=llm)\n",
33
  "agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)\n",
 
51
  "name": "python",
52
  "nbconvert_exporter": "python",
53
  "pygments_lexer": "ipython3",
54
+ "version": "3.10.11"
55
  },
56
  "orig_nbformat": 4
57
  },
redis/test_string.ipynb CHANGED
@@ -102,7 +102,7 @@
102
  "name": "python",
103
  "nbconvert_exporter": "python",
104
  "pygments_lexer": "ipython3",
105
- "version": "3.10.10"
106
  },
107
  "orig_nbformat": 4
108
  },
 
102
  "name": "python",
103
  "nbconvert_exporter": "python",
104
  "pygments_lexer": "ipython3",
105
+ "version": "3.10.11"
106
  },
107
  "orig_nbformat": 4
108
  },
redis/test_user_redis.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import redis
2
+ from flask import Flask, request, jsonify
3
+
4
+ app = Flask(__name__)
5
+ db = redis.Redis(host='localhost', port=6379, db=0)
6
+
7
+
8
+ @app.route('/register', methods=['POST'])
9
+ def register():
10
+ data = request.get_json()
11
+ username = data['username']
12
+ password = data['password']
13
+
14
+ # 检查用户名是否已存在
15
+ if db.exists(username):
16
+ return jsonify({'message': 'Username already exists'}), 400
17
+
18
+ # 在 Redis 中保存用户名和密码
19
+ db.hset(username, mapping={'password': password})
20
+
21
+ return jsonify({'message': 'User registered successfully'}), 201
22
+
23
+
24
+ @app.route('/login', methods=['POST'])
25
+ def login():
26
+ data = request.get_json()
27
+ username = data['username']
28
+ password = data['password']
29
+
30
+ # 获取保存在 Redis 中的密码
31
+ saved_password = db.hget(username, 'password')
32
+
33
+ # 检查用户名和密码是否匹配
34
+ if saved_password is None or saved_password.decode() != password:
35
+ return jsonify({'message': 'Invalid username or password'}), 401
36
+
37
+ return jsonify({'message': 'Login successful'})
38
+
39
+
40
+ if __name__ == '__main__':
41
+ app.run(debug=True)
test_book.ipynb ADDED
@@ -0,0 +1,164 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "metadata": {},
7
+ "outputs": [],
8
+ "source": [
9
+ "from langchain.embeddings import OpenAIEmbeddings\n",
10
+ "from langchain.chat_models import ChatOpenAI\n",
11
+ "from langchain.text_splitter import CharacterTextSplitter,RecursiveCharacterTextSplitter\n",
12
+ "from langchain.vectorstores import FAISS\n",
13
+ "from langchain.document_loaders import TextLoader\n",
14
+ "from langchain.document_loaders import UnstructuredMarkdownLoader\n",
15
+ "import faiss\n",
16
+ "import pickle\n",
17
+ "from langchain.chains.question_answering import load_qa_chain"
18
+ ]
19
+ },
20
+ {
21
+ "cell_type": "code",
22
+ "execution_count": 2,
23
+ "metadata": {},
24
+ "outputs": [
25
+ {
26
+ "name": "stdout",
27
+ "output_type": "stream",
28
+ "text": [
29
+ "You have 1 document(s) in your data\n",
30
+ "There are 31888 characters in your document\n"
31
+ ]
32
+ }
33
+ ],
34
+ "source": [
35
+ "loader = UnstructuredMarkdownLoader('./data/pure-bash-bible.md')\n",
36
+ "data = loader.load() # 加载文件数据\n",
37
+ "print(f'You have {len(data)} document(s) in your data')\n",
38
+ "print(f'There are {len(data[0].page_content)} characters in your document')"
39
+ ]
40
+ },
41
+ {
42
+ "cell_type": "code",
43
+ "execution_count": 3,
44
+ "metadata": {},
45
+ "outputs": [
46
+ {
47
+ "name": "stdout",
48
+ "output_type": "stream",
49
+ "text": [
50
+ "Now you have 103 documents\n"
51
+ ]
52
+ }
53
+ ],
54
+ "source": [
55
+ "text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=200)\n",
56
+ "docs = text_splitter.split_documents(data)\n",
57
+ "\n",
58
+ "print(f'Now you have {len(docs)} documents')"
59
+ ]
60
+ },
61
+ {
62
+ "cell_type": "code",
63
+ "execution_count": 4,
64
+ "metadata": {},
65
+ "outputs": [
66
+ {
67
+ "name": "stderr",
68
+ "output_type": "stream",
69
+ "text": [
70
+ "Retrying langchain.embeddings.openai.embed_with_retry.<locals>._embed_with_retry in 4.0 seconds as it raised APIError: parse model error or model not allow ,please check model {\"error\":{\"code\":\"model not support\",\"message\":\"parse model error or model not allow ,please check model\",\"param\":\"\",\"type\":\"invalid_request_error\"}} 551 {'error': {'code': 'model not support', 'message': 'parse model error or model not allow ,please check model', 'param': '', 'type': 'invalid_request_error'}} {'Date': 'Mon, 26 Jun 2023 03:55:04 GMT', 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': '149', 'Connection': 'keep-alive', 'X-Zyb-Trace-Id': '7a009973924277d0', 'tracecode': '33046944670171513031062611'}.\n",
71
+ "Retrying langchain.embeddings.openai.embed_with_retry.<locals>._embed_with_retry in 4.0 seconds as it raised APIError: parse model error or model not allow ,please check model {\"error\":{\"code\":\"model not support\",\"message\":\"parse model error or model not allow ,please check model\",\"param\":\"\",\"type\":\"invalid_request_error\"}} 551 {'error': {'code': 'model not support', 'message': 'parse model error or model not allow ,please check model', 'param': '', 'type': 'invalid_request_error'}} {'Date': 'Mon, 26 Jun 2023 03:55:09 GMT', 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': '149', 'Connection': 'keep-alive', 'X-Zyb-Trace-Id': '1f29c7fd4e373d98', 'tracecode': '33092031480171513035062611'}.\n",
72
+ "Retrying langchain.embeddings.openai.embed_with_retry.<locals>._embed_with_retry in 4.0 seconds as it raised APIError: parse model error or model not allow ,please check model {\"error\":{\"code\":\"model not support\",\"message\":\"parse model error or model not allow ,please check model\",\"param\":\"\",\"type\":\"invalid_request_error\"}} 551 {'error': {'code': 'model not support', 'message': 'parse model error or model not allow ,please check model', 'param': '', 'type': 'invalid_request_error'}} {'Date': 'Mon, 26 Jun 2023 03:55:13 GMT', 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': '149', 'Connection': 'keep-alive', 'X-Zyb-Trace-Id': '405289e269fff16a', 'tracecode': '33134649520171513027062611'}.\n",
73
+ "Retrying langchain.embeddings.openai.embed_with_retry.<locals>._embed_with_retry in 8.0 seconds as it raised APIError: parse model error or model not allow ,please check model {\"error\":{\"code\":\"model not support\",\"message\":\"parse model error or model not allow ,please check model\",\"param\":\"\",\"type\":\"invalid_request_error\"}} 551 {'error': {'code': 'model not support', 'message': 'parse model error or model not allow ,please check model', 'param': '', 'type': 'invalid_request_error'}} {'Date': 'Mon, 26 Jun 2023 03:55:18 GMT', 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': '149', 'Connection': 'keep-alive', 'X-Zyb-Trace-Id': '12202061a6884896', 'tracecode': '33189355200171513031062611'}.\n",
74
+ "Retrying langchain.embeddings.openai.embed_with_retry.<locals>._embed_with_retry in 10.0 seconds as it raised APIError: parse model error or model not allow ,please check model {\"error\":{\"code\":\"model not support\",\"message\":\"parse model error or model not allow ,please check model\",\"param\":\"\",\"type\":\"invalid_request_error\"}} 551 {'error': {'code': 'model not support', 'message': 'parse model error or model not allow ,please check model', 'param': '', 'type': 'invalid_request_error'}} {'Date': 'Mon, 26 Jun 2023 03:55:29 GMT', 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': '149', 'Connection': 'keep-alive', 'X-Zyb-Trace-Id': '906d832c90ec61fa', 'tracecode': '33293200460171513031062611'}.\n"
75
+ ]
76
+ },
77
+ {
78
+ "ename": "APIError",
79
+ "evalue": "parse model error or model not allow ,please check model {\"error\":{\"code\":\"model not support\",\"message\":\"parse model error or model not allow ,please check model\",\"param\":\"\",\"type\":\"invalid_request_error\"}} 551 {'error': {'code': 'model not support', 'message': 'parse model error or model not allow ,please check model', 'param': '', 'type': 'invalid_request_error'}} {'Date': 'Mon, 26 Jun 2023 03:55:40 GMT', 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': '149', 'Connection': 'keep-alive', 'X-Zyb-Trace-Id': '1779bca4ff664108', 'tracecode': '33404372040171513031062611'}",
80
+ "output_type": "error",
81
+ "traceback": [
82
+ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
83
+ "\u001b[0;31mAPIError\u001b[0m Traceback (most recent call last)",
84
+ "Cell \u001b[0;32mIn[4], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m embeddings \u001b[39m=\u001b[39m OpenAIEmbeddings()\n\u001b[0;32m----> 2\u001b[0m store \u001b[39m=\u001b[39m FAISS\u001b[39m.\u001b[39;49mfrom_documents(docs, embeddings)\n\u001b[1;32m 3\u001b[0m faiss\u001b[39m.\u001b[39mwrite_index(store\u001b[39m.\u001b[39mindex, \u001b[39m\"\u001b[39m\u001b[39mlangchain-pure-base-bible.index\u001b[39m\u001b[39m\"\u001b[39m)\n\u001b[1;32m 4\u001b[0m store\u001b[39m.\u001b[39mindex \u001b[39m=\u001b[39m \u001b[39mNone\u001b[39;00m\n",
85
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/langchain/vectorstores/base.py:296\u001b[0m, in \u001b[0;36mVectorStore.from_documents\u001b[0;34m(cls, documents, embedding, **kwargs)\u001b[0m\n\u001b[1;32m 294\u001b[0m texts \u001b[39m=\u001b[39m [d\u001b[39m.\u001b[39mpage_content \u001b[39mfor\u001b[39;00m d \u001b[39min\u001b[39;00m documents]\n\u001b[1;32m 295\u001b[0m metadatas \u001b[39m=\u001b[39m [d\u001b[39m.\u001b[39mmetadata \u001b[39mfor\u001b[39;00m d \u001b[39min\u001b[39;00m documents]\n\u001b[0;32m--> 296\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mcls\u001b[39;49m\u001b[39m.\u001b[39;49mfrom_texts(texts, embedding, metadatas\u001b[39m=\u001b[39;49mmetadatas, \u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mkwargs)\n",
86
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/langchain/vectorstores/faiss.py:384\u001b[0m, in \u001b[0;36mFAISS.from_texts\u001b[0;34m(cls, texts, embedding, metadatas, **kwargs)\u001b[0m\n\u001b[1;32m 359\u001b[0m \u001b[39m@classmethod\u001b[39m\n\u001b[1;32m 360\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mfrom_texts\u001b[39m(\n\u001b[1;32m 361\u001b[0m \u001b[39mcls\u001b[39m,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 365\u001b[0m \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs: Any,\n\u001b[1;32m 366\u001b[0m ) \u001b[39m-\u001b[39m\u001b[39m>\u001b[39m FAISS:\n\u001b[1;32m 367\u001b[0m \u001b[39m \u001b[39m\u001b[39m\"\"\"Construct FAISS wrapper from raw documents.\u001b[39;00m\n\u001b[1;32m 368\u001b[0m \n\u001b[1;32m 369\u001b[0m \u001b[39m This is a user friendly interface that:\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 382\u001b[0m \u001b[39m faiss = FAISS.from_texts(texts, embeddings)\u001b[39;00m\n\u001b[1;32m 383\u001b[0m \u001b[39m \"\"\"\u001b[39;00m\n\u001b[0;32m--> 384\u001b[0m embeddings \u001b[39m=\u001b[39m embedding\u001b[39m.\u001b[39;49membed_documents(texts)\n\u001b[1;32m 385\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mcls\u001b[39m\u001b[39m.\u001b[39m__from(\n\u001b[1;32m 386\u001b[0m texts,\n\u001b[1;32m 387\u001b[0m embeddings,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 390\u001b[0m \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs,\n\u001b[1;32m 391\u001b[0m )\n",
87
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/langchain/embeddings/openai.py:288\u001b[0m, in \u001b[0;36mOpenAIEmbeddings.embed_documents\u001b[0;34m(self, texts, chunk_size)\u001b[0m\n\u001b[1;32m 276\u001b[0m \u001b[39m\u001b[39m\u001b[39m\"\"\"Call out to OpenAI's embedding endpoint for embedding search docs.\u001b[39;00m\n\u001b[1;32m 277\u001b[0m \n\u001b[1;32m 278\u001b[0m \u001b[39mArgs:\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 284\u001b[0m \u001b[39m List of embeddings, one for each text.\u001b[39;00m\n\u001b[1;32m 285\u001b[0m \u001b[39m\"\"\"\u001b[39;00m\n\u001b[1;32m 286\u001b[0m \u001b[39m# NOTE: to keep things simple, we assume the list may contain texts longer\u001b[39;00m\n\u001b[1;32m 287\u001b[0m \u001b[39m# than the maximum context and use length-safe embedding function.\u001b[39;00m\n\u001b[0;32m--> 288\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_get_len_safe_embeddings(texts, engine\u001b[39m=\u001b[39;49m\u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mdeployment)\n",
88
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/langchain/embeddings/openai.py:215\u001b[0m, in \u001b[0;36mOpenAIEmbeddings._get_len_safe_embeddings\u001b[0;34m(self, texts, engine, chunk_size)\u001b[0m\n\u001b[1;32m 213\u001b[0m _chunk_size \u001b[39m=\u001b[39m chunk_size \u001b[39mor\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mchunk_size\n\u001b[1;32m 214\u001b[0m \u001b[39mfor\u001b[39;00m i \u001b[39min\u001b[39;00m \u001b[39mrange\u001b[39m(\u001b[39m0\u001b[39m, \u001b[39mlen\u001b[39m(tokens), _chunk_size):\n\u001b[0;32m--> 215\u001b[0m response \u001b[39m=\u001b[39m embed_with_retry(\n\u001b[1;32m 216\u001b[0m \u001b[39mself\u001b[39;49m,\n\u001b[1;32m 217\u001b[0m \u001b[39minput\u001b[39;49m\u001b[39m=\u001b[39;49mtokens[i : i \u001b[39m+\u001b[39;49m _chunk_size],\n\u001b[1;32m 218\u001b[0m engine\u001b[39m=\u001b[39;49m\u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mdeployment,\n\u001b[1;32m 219\u001b[0m request_timeout\u001b[39m=\u001b[39;49m\u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mrequest_timeout,\n\u001b[1;32m 220\u001b[0m headers\u001b[39m=\u001b[39;49m\u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mheaders,\n\u001b[1;32m 221\u001b[0m )\n\u001b[1;32m 222\u001b[0m batched_embeddings \u001b[39m+\u001b[39m\u001b[39m=\u001b[39m [r[\u001b[39m\"\u001b[39m\u001b[39membedding\u001b[39m\u001b[39m\"\u001b[39m] \u001b[39mfor\u001b[39;00m r \u001b[39min\u001b[39;00m response[\u001b[39m\"\u001b[39m\u001b[39mdata\u001b[39m\u001b[39m\"\u001b[39m]]\n\u001b[1;32m 224\u001b[0m results: List[List[List[\u001b[39mfloat\u001b[39m]]] \u001b[39m=\u001b[39m [[] \u001b[39mfor\u001b[39;00m _ \u001b[39min\u001b[39;00m \u001b[39mrange\u001b[39m(\u001b[39mlen\u001b[39m(texts))]\n",
89
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/langchain/embeddings/openai.py:63\u001b[0m, in \u001b[0;36membed_with_retry\u001b[0;34m(embeddings, **kwargs)\u001b[0m\n\u001b[1;32m 59\u001b[0m \u001b[39m@retry_decorator\u001b[39m\n\u001b[1;32m 60\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39m_embed_with_retry\u001b[39m(\u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs: Any) \u001b[39m-\u001b[39m\u001b[39m>\u001b[39m Any:\n\u001b[1;32m 61\u001b[0m \u001b[39mreturn\u001b[39;00m embeddings\u001b[39m.\u001b[39mclient\u001b[39m.\u001b[39mcreate(\u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs)\n\u001b[0;32m---> 63\u001b[0m \u001b[39mreturn\u001b[39;00m _embed_with_retry(\u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mkwargs)\n",
90
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/tenacity/__init__.py:289\u001b[0m, in \u001b[0;36mBaseRetrying.wraps.<locals>.wrapped_f\u001b[0;34m(*args, **kw)\u001b[0m\n\u001b[1;32m 287\u001b[0m \u001b[39m@functools\u001b[39m\u001b[39m.\u001b[39mwraps(f)\n\u001b[1;32m 288\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mwrapped_f\u001b[39m(\u001b[39m*\u001b[39margs: t\u001b[39m.\u001b[39mAny, \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkw: t\u001b[39m.\u001b[39mAny) \u001b[39m-\u001b[39m\u001b[39m>\u001b[39m t\u001b[39m.\u001b[39mAny:\n\u001b[0;32m--> 289\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39;49m(f, \u001b[39m*\u001b[39;49margs, \u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mkw)\n",
91
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/tenacity/__init__.py:379\u001b[0m, in \u001b[0;36mRetrying.__call__\u001b[0;34m(self, fn, *args, **kwargs)\u001b[0m\n\u001b[1;32m 377\u001b[0m retry_state \u001b[39m=\u001b[39m RetryCallState(retry_object\u001b[39m=\u001b[39m\u001b[39mself\u001b[39m, fn\u001b[39m=\u001b[39mfn, args\u001b[39m=\u001b[39margs, kwargs\u001b[39m=\u001b[39mkwargs)\n\u001b[1;32m 378\u001b[0m \u001b[39mwhile\u001b[39;00m \u001b[39mTrue\u001b[39;00m:\n\u001b[0;32m--> 379\u001b[0m do \u001b[39m=\u001b[39m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49miter(retry_state\u001b[39m=\u001b[39;49mretry_state)\n\u001b[1;32m 380\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39misinstance\u001b[39m(do, DoAttempt):\n\u001b[1;32m 381\u001b[0m \u001b[39mtry\u001b[39;00m:\n",
92
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/tenacity/__init__.py:325\u001b[0m, in \u001b[0;36mBaseRetrying.iter\u001b[0;34m(self, retry_state)\u001b[0m\n\u001b[1;32m 323\u001b[0m retry_exc \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mretry_error_cls(fut)\n\u001b[1;32m 324\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mreraise:\n\u001b[0;32m--> 325\u001b[0m \u001b[39mraise\u001b[39;00m retry_exc\u001b[39m.\u001b[39;49mreraise()\n\u001b[1;32m 326\u001b[0m \u001b[39mraise\u001b[39;00m retry_exc \u001b[39mfrom\u001b[39;00m \u001b[39mfut\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mexception\u001b[39;00m()\n\u001b[1;32m 328\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mwait:\n",
93
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/tenacity/__init__.py:158\u001b[0m, in \u001b[0;36mRetryError.reraise\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 156\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mreraise\u001b[39m(\u001b[39mself\u001b[39m) \u001b[39m-\u001b[39m\u001b[39m>\u001b[39m t\u001b[39m.\u001b[39mNoReturn:\n\u001b[1;32m 157\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mlast_attempt\u001b[39m.\u001b[39mfailed:\n\u001b[0;32m--> 158\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mlast_attempt\u001b[39m.\u001b[39;49mresult()\n\u001b[1;32m 159\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mself\u001b[39m\n",
94
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/concurrent/futures/_base.py:451\u001b[0m, in \u001b[0;36mFuture.result\u001b[0;34m(self, timeout)\u001b[0m\n\u001b[1;32m 449\u001b[0m \u001b[39mraise\u001b[39;00m CancelledError()\n\u001b[1;32m 450\u001b[0m \u001b[39melif\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_state \u001b[39m==\u001b[39m FINISHED:\n\u001b[0;32m--> 451\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m__get_result()\n\u001b[1;32m 453\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_condition\u001b[39m.\u001b[39mwait(timeout)\n\u001b[1;32m 455\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_state \u001b[39min\u001b[39;00m [CANCELLED, CANCELLED_AND_NOTIFIED]:\n",
95
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/concurrent/futures/_base.py:403\u001b[0m, in \u001b[0;36mFuture.__get_result\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 401\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_exception:\n\u001b[1;32m 402\u001b[0m \u001b[39mtry\u001b[39;00m:\n\u001b[0;32m--> 403\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_exception\n\u001b[1;32m 404\u001b[0m \u001b[39mfinally\u001b[39;00m:\n\u001b[1;32m 405\u001b[0m \u001b[39m# Break a reference cycle with the exception in self._exception\u001b[39;00m\n\u001b[1;32m 406\u001b[0m \u001b[39mself\u001b[39m \u001b[39m=\u001b[39m \u001b[39mNone\u001b[39;00m\n",
96
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/tenacity/__init__.py:382\u001b[0m, in \u001b[0;36mRetrying.__call__\u001b[0;34m(self, fn, *args, **kwargs)\u001b[0m\n\u001b[1;32m 380\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39misinstance\u001b[39m(do, DoAttempt):\n\u001b[1;32m 381\u001b[0m \u001b[39mtry\u001b[39;00m:\n\u001b[0;32m--> 382\u001b[0m result \u001b[39m=\u001b[39m fn(\u001b[39m*\u001b[39;49margs, \u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mkwargs)\n\u001b[1;32m 383\u001b[0m \u001b[39mexcept\u001b[39;00m \u001b[39mBaseException\u001b[39;00m: \u001b[39m# noqa: B902\u001b[39;00m\n\u001b[1;32m 384\u001b[0m retry_state\u001b[39m.\u001b[39mset_exception(sys\u001b[39m.\u001b[39mexc_info()) \u001b[39m# type: ignore[arg-type]\u001b[39;00m\n",
97
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/langchain/embeddings/openai.py:61\u001b[0m, in \u001b[0;36membed_with_retry.<locals>._embed_with_retry\u001b[0;34m(**kwargs)\u001b[0m\n\u001b[1;32m 59\u001b[0m \u001b[39m@retry_decorator\u001b[39m\n\u001b[1;32m 60\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39m_embed_with_retry\u001b[39m(\u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs: Any) \u001b[39m-\u001b[39m\u001b[39m>\u001b[39m Any:\n\u001b[0;32m---> 61\u001b[0m \u001b[39mreturn\u001b[39;00m embeddings\u001b[39m.\u001b[39;49mclient\u001b[39m.\u001b[39;49mcreate(\u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mkwargs)\n",
98
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/openai/api_resources/embedding.py:33\u001b[0m, in \u001b[0;36mEmbedding.create\u001b[0;34m(cls, *args, **kwargs)\u001b[0m\n\u001b[1;32m 31\u001b[0m \u001b[39mwhile\u001b[39;00m \u001b[39mTrue\u001b[39;00m:\n\u001b[1;32m 32\u001b[0m \u001b[39mtry\u001b[39;00m:\n\u001b[0;32m---> 33\u001b[0m response \u001b[39m=\u001b[39m \u001b[39msuper\u001b[39;49m()\u001b[39m.\u001b[39;49mcreate(\u001b[39m*\u001b[39;49margs, \u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mkwargs)\n\u001b[1;32m 35\u001b[0m \u001b[39m# If a user specifies base64, we'll just return the encoded string.\u001b[39;00m\n\u001b[1;32m 36\u001b[0m \u001b[39m# This is only for the default case.\u001b[39;00m\n\u001b[1;32m 37\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mnot\u001b[39;00m user_provided_encoding_format:\n",
99
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/openai/api_resources/abstract/engine_api_resource.py:153\u001b[0m, in \u001b[0;36mEngineAPIResource.create\u001b[0;34m(cls, api_key, api_base, api_type, request_id, api_version, organization, **params)\u001b[0m\n\u001b[1;32m 127\u001b[0m \u001b[39m@classmethod\u001b[39m\n\u001b[1;32m 128\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mcreate\u001b[39m(\n\u001b[1;32m 129\u001b[0m \u001b[39mcls\u001b[39m,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 136\u001b[0m \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mparams,\n\u001b[1;32m 137\u001b[0m ):\n\u001b[1;32m 138\u001b[0m (\n\u001b[1;32m 139\u001b[0m deployment_id,\n\u001b[1;32m 140\u001b[0m engine,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 150\u001b[0m api_key, api_base, api_type, api_version, organization, \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mparams\n\u001b[1;32m 151\u001b[0m )\n\u001b[0;32m--> 153\u001b[0m response, _, api_key \u001b[39m=\u001b[39m requestor\u001b[39m.\u001b[39;49mrequest(\n\u001b[1;32m 154\u001b[0m \u001b[39m\"\u001b[39;49m\u001b[39mpost\u001b[39;49m\u001b[39m\"\u001b[39;49m,\n\u001b[1;32m 155\u001b[0m url,\n\u001b[1;32m 156\u001b[0m params\u001b[39m=\u001b[39;49mparams,\n\u001b[1;32m 157\u001b[0m headers\u001b[39m=\u001b[39;49mheaders,\n\u001b[1;32m 158\u001b[0m stream\u001b[39m=\u001b[39;49mstream,\n\u001b[1;32m 159\u001b[0m request_id\u001b[39m=\u001b[39;49mrequest_id,\n\u001b[1;32m 160\u001b[0m request_timeout\u001b[39m=\u001b[39;49mrequest_timeout,\n\u001b[1;32m 161\u001b[0m )\n\u001b[1;32m 163\u001b[0m \u001b[39mif\u001b[39;00m stream:\n\u001b[1;32m 164\u001b[0m \u001b[39m# must be an iterator\u001b[39;00m\n\u001b[1;32m 165\u001b[0m \u001b[39massert\u001b[39;00m \u001b[39mnot\u001b[39;00m \u001b[39misinstance\u001b[39m(response, OpenAIResponse)\n",
100
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/openai/api_requestor.py:230\u001b[0m, in \u001b[0;36mAPIRequestor.request\u001b[0;34m(self, method, url, params, headers, files, stream, request_id, request_timeout)\u001b[0m\n\u001b[1;32m 209\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mrequest\u001b[39m(\n\u001b[1;32m 210\u001b[0m \u001b[39mself\u001b[39m,\n\u001b[1;32m 211\u001b[0m method,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 218\u001b[0m request_timeout: Optional[Union[\u001b[39mfloat\u001b[39m, Tuple[\u001b[39mfloat\u001b[39m, \u001b[39mfloat\u001b[39m]]] \u001b[39m=\u001b[39m \u001b[39mNone\u001b[39;00m,\n\u001b[1;32m 219\u001b[0m ) \u001b[39m-\u001b[39m\u001b[39m>\u001b[39m Tuple[Union[OpenAIResponse, Iterator[OpenAIResponse]], \u001b[39mbool\u001b[39m, \u001b[39mstr\u001b[39m]:\n\u001b[1;32m 220\u001b[0m result \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mrequest_raw(\n\u001b[1;32m 221\u001b[0m method\u001b[39m.\u001b[39mlower(),\n\u001b[1;32m 222\u001b[0m url,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 228\u001b[0m request_timeout\u001b[39m=\u001b[39mrequest_timeout,\n\u001b[1;32m 229\u001b[0m )\n\u001b[0;32m--> 230\u001b[0m resp, got_stream \u001b[39m=\u001b[39m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_interpret_response(result, stream)\n\u001b[1;32m 231\u001b[0m \u001b[39mreturn\u001b[39;00m resp, got_stream, \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mapi_key\n",
101
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/openai/api_requestor.py:624\u001b[0m, in \u001b[0;36mAPIRequestor._interpret_response\u001b[0;34m(self, result, stream)\u001b[0m\n\u001b[1;32m 616\u001b[0m \u001b[39mreturn\u001b[39;00m (\n\u001b[1;32m 617\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_interpret_response_line(\n\u001b[1;32m 618\u001b[0m line, result\u001b[39m.\u001b[39mstatus_code, result\u001b[39m.\u001b[39mheaders, stream\u001b[39m=\u001b[39m\u001b[39mTrue\u001b[39;00m\n\u001b[1;32m 619\u001b[0m )\n\u001b[1;32m 620\u001b[0m \u001b[39mfor\u001b[39;00m line \u001b[39min\u001b[39;00m parse_stream(result\u001b[39m.\u001b[39miter_lines())\n\u001b[1;32m 621\u001b[0m ), \u001b[39mTrue\u001b[39;00m\n\u001b[1;32m 622\u001b[0m \u001b[39melse\u001b[39;00m:\n\u001b[1;32m 623\u001b[0m \u001b[39mreturn\u001b[39;00m (\n\u001b[0;32m--> 624\u001b[0m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_interpret_response_line(\n\u001b[1;32m 625\u001b[0m result\u001b[39m.\u001b[39;49mcontent\u001b[39m.\u001b[39;49mdecode(\u001b[39m\"\u001b[39;49m\u001b[39mutf-8\u001b[39;49m\u001b[39m\"\u001b[39;49m),\n\u001b[1;32m 626\u001b[0m result\u001b[39m.\u001b[39;49mstatus_code,\n\u001b[1;32m 627\u001b[0m result\u001b[39m.\u001b[39;49mheaders,\n\u001b[1;32m 628\u001b[0m stream\u001b[39m=\u001b[39;49m\u001b[39mFalse\u001b[39;49;00m,\n\u001b[1;32m 629\u001b[0m ),\n\u001b[1;32m 630\u001b[0m \u001b[39mFalse\u001b[39;00m,\n\u001b[1;32m 631\u001b[0m )\n",
102
+ "File \u001b[0;32m~/anaconda3/lib/python3.10/site-packages/openai/api_requestor.py:687\u001b[0m, in \u001b[0;36mAPIRequestor._interpret_response_line\u001b[0;34m(self, rbody, rcode, rheaders, stream)\u001b[0m\n\u001b[1;32m 685\u001b[0m stream_error \u001b[39m=\u001b[39m stream \u001b[39mand\u001b[39;00m \u001b[39m\"\u001b[39m\u001b[39merror\u001b[39m\u001b[39m\"\u001b[39m \u001b[39min\u001b[39;00m resp\u001b[39m.\u001b[39mdata\n\u001b[1;32m 686\u001b[0m \u001b[39mif\u001b[39;00m stream_error \u001b[39mor\u001b[39;00m \u001b[39mnot\u001b[39;00m \u001b[39m200\u001b[39m \u001b[39m<\u001b[39m\u001b[39m=\u001b[39m rcode \u001b[39m<\u001b[39m \u001b[39m300\u001b[39m:\n\u001b[0;32m--> 687\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mhandle_error_response(\n\u001b[1;32m 688\u001b[0m rbody, rcode, resp\u001b[39m.\u001b[39mdata, rheaders, stream_error\u001b[39m=\u001b[39mstream_error\n\u001b[1;32m 689\u001b[0m )\n\u001b[1;32m 690\u001b[0m \u001b[39mreturn\u001b[39;00m resp\n",
103
+ "\u001b[0;31mAPIError\u001b[0m: parse model error or model not allow ,please check model {\"error\":{\"code\":\"model not support\",\"message\":\"parse model error or model not allow ,please check model\",\"param\":\"\",\"type\":\"invalid_request_error\"}} 551 {'error': {'code': 'model not support', 'message': 'parse model error or model not allow ,please check model', 'param': '', 'type': 'invalid_request_error'}} {'Date': 'Mon, 26 Jun 2023 03:55:40 GMT', 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': '149', 'Connection': 'keep-alive', 'X-Zyb-Trace-Id': '1779bca4ff664108', 'tracecode': '33404372040171513031062611'}"
104
+ ]
105
+ }
106
+ ],
107
+ "source": [
108
+ "embeddings = OpenAIEmbeddings()\n",
109
+ "store = FAISS.from_documents(docs, embeddings)\n",
110
+ "faiss.write_index(store.index, \"langchain-pure-base-bible.index\")\n",
111
+ "store.index = None\n",
112
+ "\n",
113
+ "with open(\"faiss-book.pkl\", \"wb\") as f:\n",
114
+ " pickle.dump(store, f)"
115
+ ]
116
+ },
117
+ {
118
+ "cell_type": "code",
119
+ "execution_count": null,
120
+ "metadata": {},
121
+ "outputs": [],
122
+ "source": [
123
+ "# 先加载前面训练好的语义索引\n",
124
+ "index = faiss.read_index(\"langchain-pure-base-bible.index\")\n",
125
+ "with open(\"faiss-book.pkl\", \"rb\") as f:\n",
126
+ " docsearch = pickle.load(f)\n",
127
+ "docsearch.index = index\n",
128
+ "\n",
129
+ "llm = ChatOpenAI(temperature=0, verbose=True)\n",
130
+ "chain = load_qa_chain(llm, chain_type=\"stuff\")\n",
131
+ "\n",
132
+ "# 调用OpenAI根据前面切割好的文档上下文回答问题\n",
133
+ "query_list = [\"可以给一下shell的示例用法的代码么?\", \"for循环该如何使用?\", \"如何检查一个命令是否在用户的PATH中\"]\n",
134
+ "for query in query_list:\n",
135
+ " print(\"问题:\", query)\n",
136
+ " docs = docsearch.similarity_search(query, include_metadata=True)\n",
137
+ " r = chain.run(input_documents=docs, question=query)\n",
138
+ " print(\"结果:\", r.encode('utf-8').decode('utf-8'))"
139
+ ]
140
+ }
141
+ ],
142
+ "metadata": {
143
+ "kernelspec": {
144
+ "display_name": "base",
145
+ "language": "python",
146
+ "name": "python3"
147
+ },
148
+ "language_info": {
149
+ "codemirror_mode": {
150
+ "name": "ipython",
151
+ "version": 3
152
+ },
153
+ "file_extension": ".py",
154
+ "mimetype": "text/x-python",
155
+ "name": "python",
156
+ "nbconvert_exporter": "python",
157
+ "pygments_lexer": "ipython3",
158
+ "version": "3.10.10"
159
+ },
160
+ "orig_nbformat": 4
161
+ },
162
+ "nbformat": 4,
163
+ "nbformat_minor": 2
164
+ }