Skip to content

Commit 384110d

Browse files
committed
add bind tools & document loader
1 parent bee9d27 commit 384110d

File tree

4 files changed

+450
-3
lines changed

4 files changed

+450
-3
lines changed
Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"## Rannable 对象绑定函数"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": 19,
13+
"metadata": {},
14+
"outputs": [],
15+
"source": [
16+
"from pprint import pprint\n",
17+
"from pydantic import BaseModel, Field\n",
18+
"from langchain_core.prompts import PromptTemplate\n",
19+
"from chat_model_client import get_model\n",
20+
"from langchain_core.output_parsers import PydanticOutputParser\n",
21+
"from langchain_core.runnables import (\n",
22+
" RunnablePassthrough\n",
23+
")"
24+
]
25+
},
26+
{
27+
"cell_type": "markdown",
28+
"metadata": {},
29+
"source": [
30+
"1. Tools声明"
31+
]
32+
},
33+
{
34+
"cell_type": "code",
35+
"execution_count": 20,
36+
"metadata": {},
37+
"outputs": [],
38+
"source": [
39+
"tools = [\n",
40+
" {\n",
41+
" \"type\": \"function\",\n",
42+
" \"function\": {\n",
43+
" \"name\": \"solver\",\n",
44+
" \"description\": \"表述并解一个方程\",\n",
45+
" \"parameters\": {\n",
46+
" \"type\": \"object\",\n",
47+
" \"properties\": {\n",
48+
" \"equation\": {\n",
49+
" \"type\": \"string\",\n",
50+
" \"description\": \"待解的数学表达式\"\n",
51+
" },\n",
52+
" \"solution\": {\n",
53+
" \"type\": \"string\",\n",
54+
" \"description\": \"方程的解\"\n",
55+
" }\n",
56+
" },\n",
57+
" \"required\": [\"equation\", \"solution\"]\n",
58+
" }\n",
59+
" }\n",
60+
" \n",
61+
" }\n",
62+
"]"
63+
]
64+
},
65+
{
66+
"cell_type": "markdown",
67+
"metadata": {},
68+
"source": [
69+
"2. 声明返回结构"
70+
]
71+
},
72+
{
73+
"cell_type": "code",
74+
"execution_count": 21,
75+
"metadata": {},
76+
"outputs": [],
77+
"source": [
78+
"class Solver(BaseModel):\n",
79+
" equation: str = Field(\n",
80+
" description=\"数学表达式\"\n",
81+
" ),\n",
82+
" solution: str = Field(\n",
83+
" description=\"方程的解\"\n",
84+
" )"
85+
]
86+
},
87+
{
88+
"cell_type": "markdown",
89+
"metadata": {},
90+
"source": [
91+
"2. 绑定tools"
92+
]
93+
},
94+
{
95+
"cell_type": "code",
96+
"execution_count": 22,
97+
"metadata": {},
98+
"outputs": [
99+
{
100+
"name": "stderr",
101+
"output_type": "stream",
102+
"text": [
103+
"/Users/shaon/anaconda3/envs/wrenv3.10/lib/python3.10/site-packages/pydantic/json_schema.py:2191: PydanticJsonSchemaWarning: Default value (FieldInfo(annotation=NoneType, required=True, description='数学表达式'),) is not JSON serializable; excluding default from JSON schema [non-serializable-default]\n",
104+
" warnings.warn(message, PydanticJsonSchemaWarning)\n"
105+
]
106+
},
107+
{
108+
"data": {
109+
"text/plain": [
110+
"RunnableBinding(bound=ChatOpenAI(client=<openai.resources.chat.completions.Completions object at 0x136366d40>, async_client=<openai.resources.chat.completions.AsyncCompletions object at 0x136384730>, root_client=<openai.OpenAI object at 0x136367250>, root_async_client=<openai.AsyncOpenAI object at 0x136366dd0>, model_name='deepseek-chat', temperature=0.0, model_kwargs={}, openai_api_key=SecretStr('**********'), openai_api_base='https://api.deepseek.com', max_tokens=2000), kwargs={'tools': [{'type': 'function', 'function': {'name': 'solver', 'description': '表述并解一个方程', 'parameters': {'type': 'object', 'properties': {'equation': {'type': 'string', 'description': '待解的数学表达式'}, 'solution': {'type': 'string', 'description': '方程的解'}}, 'required': ['equation', 'solution']}}}]}, config={}, config_factories=[])"
111+
]
112+
},
113+
"execution_count": 22,
114+
"metadata": {},
115+
"output_type": "execute_result"
116+
}
117+
],
118+
"source": [
119+
"query = \"x的3次方加上11等于75\"\n",
120+
"parser = PydanticOutputParser(pydantic_object=Solver)\n",
121+
"prompt = PromptTemplate(\n",
122+
" template=\"你是一个乐于助人的助手。写出下面的方程,然后求解。\\n{format_instructions}\\n{query}\\n如果输出的是代码块,请不要包括首尾的```符号\",\n",
123+
" input_variables=[\"query\"],\n",
124+
" partial_variables={\"format_instructions\": parser.get_format_instructions()},\n",
125+
")\n",
126+
"\n",
127+
"# prompt = ChatPromptTemplate([\n",
128+
"# (\"system\", \"你是一个乐于助人的助手。写出下面的方程,然后求解\"),\n",
129+
"# (\"human\", \"{input}\")\n",
130+
"# ])\n",
131+
"\n",
132+
"llm = get_model('deepseek')\n",
133+
"llm.bind_tools(tools=tools)"
134+
]
135+
},
136+
{
137+
"cell_type": "markdown",
138+
"metadata": {},
139+
"source": [
140+
"3. 构建并执行runnable"
141+
]
142+
},
143+
{
144+
"cell_type": "code",
145+
"execution_count": 23,
146+
"metadata": {},
147+
"outputs": [
148+
{
149+
"name": "stdout",
150+
"output_type": "stream",
151+
"text": [
152+
"Solver(equation='x^3 + 11 = 75', solution='x = 4')\n"
153+
]
154+
}
155+
],
156+
"source": [
157+
"\n",
158+
"chain = {\"query\": RunnablePassthrough()} | prompt | llm | parser\n",
159+
"output = chain.invoke({\"query\": query})\n",
160+
"\n",
161+
"pprint(output)"
162+
]
163+
},
164+
{
165+
"cell_type": "markdown",
166+
"metadata": {},
167+
"source": [
168+
"4. 链路检查"
169+
]
170+
},
171+
{
172+
"cell_type": "code",
173+
"execution_count": 24,
174+
"metadata": {},
175+
"outputs": [
176+
{
177+
"name": "stdout",
178+
"output_type": "stream",
179+
"text": [
180+
"+----------------------+ \n",
181+
"| Parallel<query>Input | \n",
182+
"+----------------------+ \n",
183+
" * \n",
184+
" * \n",
185+
" * \n",
186+
" +-------------+ \n",
187+
" | Passthrough | \n",
188+
" +-------------+ \n",
189+
" * \n",
190+
" * \n",
191+
" * \n",
192+
" +----------------+ \n",
193+
" | PromptTemplate | \n",
194+
" +----------------+ \n",
195+
" * \n",
196+
" * \n",
197+
" * \n",
198+
" +------------+ \n",
199+
" | ChatOpenAI | \n",
200+
" +------------+ \n",
201+
" * \n",
202+
" * \n",
203+
" * \n",
204+
"+----------------------+ \n",
205+
"| PydanticOutputParser | \n",
206+
"+----------------------+ \n",
207+
" * \n",
208+
" * \n",
209+
" * \n",
210+
" +--------+ \n",
211+
" | Solver | \n",
212+
" +--------+ \n",
213+
"None\n"
214+
]
215+
}
216+
],
217+
"source": [
218+
"pprint(chain.get_graph().print_ascii())"
219+
]
220+
}
221+
],
222+
"metadata": {
223+
"kernelspec": {
224+
"display_name": "wrenv3.10",
225+
"language": "python",
226+
"name": "python3"
227+
},
228+
"language_info": {
229+
"codemirror_mode": {
230+
"name": "ipython",
231+
"version": 3
232+
},
233+
"file_extension": ".py",
234+
"mimetype": "text/x-python",
235+
"name": "python",
236+
"nbconvert_exporter": "python",
237+
"pygments_lexer": "ipython3",
238+
"version": "3.10.16"
239+
}
240+
},
241+
"nbformat": 4,
242+
"nbformat_minor": 2
243+
}

langchain/7.document_loader_from_arxiv.ipynb

Lines changed: 195 additions & 0 deletions
Large diffs are not rendered by default.

langchain/chat_model_client.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,14 @@ def get_llama():
4545
base_url=env_config['OLLAMA_BASE_URL'],
4646
temperature=0, # 设置成0最稳定;structured generation中稳定最重要
4747
max_tokens=2000,
48-
frequency_penalty=0,
49-
presence_penalty=0,
50-
top_p=1.0
48+
model_kwargs={
49+
'frequency_penalty':0,
50+
'presence_penalty':0,
51+
'top_p':1.0,
52+
}
53+
# frequency_penalty=0,
54+
# presence_penalty=0,
55+
# top_p=1.0
5156
)
5257
return llm
5358

requirement.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ langsmith==0.2.10
1010
langchain-ollama==0.2.2
1111
langchain-openai=0.3.1
1212

13+
## tools
14+
arxiv==2.1.3
15+
pymupdf==1.25.2
16+
1317
## parser
1418
defusedxml==0.7.1
1519
markdownify==0.14.1

0 commit comments

Comments
 (0)