远兮 commited on
Commit
fdb9357
·
1 Parent(s): 2ef1d57
chain_input_tool_schema.ipynb ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 10,
6
+ "metadata": {},
7
+ "outputs": [],
8
+ "source": [
9
+ "from typing import Any, Dict\n",
10
+ "\n",
11
+ "from langchain.agents import AgentType, initialize_agent\n",
12
+ "from langchain.llms import OpenAI\n",
13
+ "from langchain.tools.requests.tool import RequestsGetTool, TextRequestsWrapper\n",
14
+ "from pydantic import BaseModel, Field, root_validator"
15
+ ]
16
+ },
17
+ {
18
+ "cell_type": "code",
19
+ "execution_count": 11,
20
+ "metadata": {},
21
+ "outputs": [],
22
+ "source": [
23
+ "llm = OpenAI(temperature=0)"
24
+ ]
25
+ },
26
+ {
27
+ "cell_type": "code",
28
+ "execution_count": 12,
29
+ "metadata": {},
30
+ "outputs": [],
31
+ "source": [
32
+ "import tldextract\n",
33
+ "\n",
34
+ "_APPROVED_DOMAINS = {\n",
35
+ " \"langchain\",\n",
36
+ " \"wikipedia\",\n",
37
+ "}\n",
38
+ "\n",
39
+ "class ToolInputSchema(BaseModel):\n",
40
+ "\n",
41
+ " url: str = Field(...)\n",
42
+ " \n",
43
+ " @root_validator\n",
44
+ " def validate_query(cls, values: Dict[str, Any]) -> Dict:\n",
45
+ " url = values[\"url\"]\n",
46
+ " domain = tldextract.extract(url).domain\n",
47
+ " if domain not in _APPROVED_DOMAINS:\n",
48
+ " raise ValueError(f\"Domain {domain} is not on the approved list:\"\n",
49
+ " f\" {sorted(_APPROVED_DOMAINS)}\")\n",
50
+ " return values\n",
51
+ " \n",
52
+ "tool = RequestsGetTool(args_schema=ToolInputSchema, requests_wrapper=TextRequestsWrapper())\n",
53
+ "# tool = RequestsGetTool( requests_wrapper=TextRequestsWrapper())"
54
+ ]
55
+ },
56
+ {
57
+ "cell_type": "code",
58
+ "execution_count": 13,
59
+ "metadata": {},
60
+ "outputs": [],
61
+ "source": [
62
+ "agent = initialize_agent([tool], llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=False)"
63
+ ]
64
+ },
65
+ {
66
+ "cell_type": "code",
67
+ "execution_count": 14,
68
+ "metadata": {},
69
+ "outputs": [
70
+ {
71
+ "name": "stdout",
72
+ "output_type": "stream",
73
+ "text": [
74
+ "The main title on langchain.com is \"LANG CHAIN 🦜️🔗 Official Home Page\".\n"
75
+ ]
76
+ }
77
+ ],
78
+ "source": [
79
+ "# This will succeed, since there aren't any arguments that will be triggered during validation\n",
80
+ "answer = agent.run(\"What's the main title on langchain.com?\")\n",
81
+ "print(answer)"
82
+ ]
83
+ }
84
+ ],
85
+ "metadata": {
86
+ "kernelspec": {
87
+ "display_name": "base",
88
+ "language": "python",
89
+ "name": "python3"
90
+ },
91
+ "language_info": {
92
+ "codemirror_mode": {
93
+ "name": "ipython",
94
+ "version": 3
95
+ },
96
+ "file_extension": ".py",
97
+ "mimetype": "text/x-python",
98
+ "name": "python",
99
+ "nbconvert_exporter": "python",
100
+ "pygments_lexer": "ipython3",
101
+ "version": "3.10.10"
102
+ },
103
+ "orig_nbformat": 4
104
+ },
105
+ "nbformat": 4,
106
+ "nbformat_minor": 2
107
+ }
chain_pal.ipynb ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
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": [
28
+ "pal_chain = PALChain.from_math_prompt(llm, verbose=True)"
29
+ ]
30
+ },
31
+ {
32
+ "cell_type": "code",
33
+ "execution_count": 65,
34
+ "metadata": {},
35
+ "outputs": [
36
+ {
37
+ "name": "stdout",
38
+ "output_type": "stream",
39
+ "text": [
40
+ "\n",
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
+ ]
54
+ },
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
+ "metadata": {
74
+ "kernelspec": {
75
+ "display_name": "base",
76
+ "language": "python",
77
+ "name": "python3"
78
+ },
79
+ "language_info": {
80
+ "codemirror_mode": {
81
+ "name": "ipython",
82
+ "version": 3
83
+ },
84
+ "file_extension": ".py",
85
+ "mimetype": "text/x-python",
86
+ "name": "python",
87
+ "nbconvert_exporter": "python",
88
+ "pygments_lexer": "ipython3",
89
+ "version": "3.11.3"
90
+ },
91
+ "orig_nbformat": 4
92
+ },
93
+ "nbformat": 4,
94
+ "nbformat_minor": 2
95
+ }
prompt_load.ipynb CHANGED
@@ -134,7 +134,7 @@
134
  "name": "python",
135
  "nbconvert_exporter": "python",
136
  "pygments_lexer": "ipython3",
137
- "version": "3.11.3"
138
  },
139
  "orig_nbformat": 4
140
  },
 
134
  "name": "python",
135
  "nbconvert_exporter": "python",
136
  "pygments_lexer": "ipython3",
137
+ "version": "3.10.10"
138
  },
139
  "orig_nbformat": 4
140
  },