環境:本地使用ollama部署的deepseek-r1:1.5b模型
本文示例包含:
- [1] 非LCEL的調用方法
- [2] LCEL的調用方法
- [3] prompt template的簡單使用,除了PromptTemplate模板,還有一些其它模板,可去查看官網
- [4] 輸出:json格式、pydantic對象、字符串格式[一次輸出]、字符串格式[流式輸出]。除了這些還有其它的輸出方式比如xml等,可去官網查看
示例代碼:
from pydantic import BaseModel, Field
from langchain_community.llms import Ollama
from langchain.prompts import PromptTemplate
from langchain.schema.runnable import RunnablePassthroughclass Add(BaseModel):a: int = Field(description="第一個整數")b: int = Field(description="第二個整數")result: int = Field(description="兩個整數的求和結果")# 初始化LLM
llm = Ollama(model="deepseek-r1:1.5b",base_url="http://localhost:11434",temperature=0
)# 1 非LCEL 輸出json
print("============1 非LCEL 輸出json===========")
from langchain_core.output_parsers import JsonOutputParser
json_parser = JsonOutputParser(pydantic_object=Add)
prompt_template = PromptTemplate(template="計算兩個整數的和。\n用戶輸入:{query}\n{format_instructions}",input_variables=["query"],partial_variables={"format_instructions": json_parser.get_format_instructions()}
)
query = "5 加 3 等于幾"
prompt = prompt_template.format_prompt(query=query)
output = llm.invoke(prompt)
print(json_parser.invoke(output))# 2 非LCEL 輸出pydantic對象
print("============2 非LCEL 輸出pydantic對象===========")
from langchain_core.output_parsers import PydanticOutputParser
pydantic_parser = PydanticOutputParser(pydantic_object=Add)
prompt_template = PromptTemplate(template="計算兩個整數的和。\n用戶輸入:{query}\n{format_instructions}",input_variables=["query"],partial_variables={"format_instructions": pydantic_parser.get_format_instructions()}
)
query = "5 加 3 等于幾"
prompt = prompt_template.format_prompt(query=query)
output = llm.invoke(prompt)
res = pydantic_parser.invoke(output)
print(res, "|", res.a, res.b, res.result)# 3 LCEL 字符串流式輸出 帶有json字符串
print("============3 LCEL 字符串流式輸出 帶有json字符串===========")
from langchain.schema.output_parser import StrOutputParser
from langchain_core.output_parsers import JsonOutputParser
json_parser = JsonOutputParser(pydantic_object=Add)
prompt_template = PromptTemplate(template="計算兩個整數的和。\n用戶輸入:{query}\n{format_instructions}",input_variables=["query"],partial_variables={"format_instructions": json_parser.get_format_instructions()}
)
chain = {"query": RunnablePassthrough()} | prompt_template | llm | StrOutputParser()
for chunk in chain.stream("5 加 3 等于幾"):print(chunk, end="")
# output = chain.invoke("5 加 3 等于幾")
# print(output)# 4 LCEL 字符串流式輸出 不帶有json字符串
print("============4 LCEL 字符串流式輸出 不帶有json字符串===========")
from langchain.schema.output_parser import StrOutputParser
prompt_template = PromptTemplate(template="計算兩個整數的和。\n用戶輸入:{query}\n",input_variables=["query"],
)
chain = {"query": RunnablePassthrough()} | prompt_template | llm | StrOutputParser()
for chunk in chain.stream("5 加 3 等于幾"):print(chunk, end="")
print() # 為了流式輸出完后換行而已
# output = chain.invoke("5 加 3 等于幾")
# print(output)# 5 LCEL 輸出json
print("============5 LCEL 輸出json===========")
from langchain_core.output_parsers import JsonOutputParser
json_parser = JsonOutputParser(pydantic_object=Add)
prompt_template = PromptTemplate(template="計算兩個整數的和。\n用戶輸入:{query}\n{format_instructions}",input_variables=["query"],partial_variables={"format_instructions": json_parser.get_format_instructions()}
)
chain = {"query": RunnablePassthrough()} | prompt_template | llm | json_parser
output = chain.invoke("5 加 3 等于幾")
print(output)# 6 LCEL 輸出pydantic對象
print("============6 LCEL 輸出pydantic對象===========")
from langchain_core.output_parsers import PydanticOutputParser
pydantic_parser = PydanticOutputParser(pydantic_object=Add)
prompt_template = PromptTemplate(template="計算兩個整數的和。\n用戶輸入:{query}\n{format_instructions}",input_variables=["query"],partial_variables={"format_instructions": pydantic_parser.get_format_instructions()}
)
chain = {"query": RunnablePassthrough()} | prompt_template | llm | pydantic_parser
output = chain.invoke("5 加 3 等于幾")
print(output, "|", output.a, output.b, output.result)
結果輸出:
============1 非LCEL 輸出json===========
{'a': 5, 'b': 3, 'result': 8}
============2 非LCEL 輸出pydantic對象===========
a=5 b=3 result=8 | 5 3 8
============3 LCEL 字符串流式輸出 帶有json字符串===========
<think>
嗯,用戶讓我計算兩個整數的和。輸入是5加3等于幾。首先,我需要確認這兩個數字是否正確輸入了。看起來沒問題,都是整數。接下來,我要按照用戶的要求,生成一個JSON對象,并且確保它符合指定的格式。根據輸出 schema,我需要定義三個屬性:a、b和result。每個屬性都有特定的描述和類型。對于a,應該是第一個整數5;b是第二個整數3;result則是它們的和8。這樣填寫的話,整個結構就完整了。然后,我要檢查一下JSON格式是否正確。確保逗號的位置沒有錯誤,特別是分隔符部分。同時,每個屬性的名稱也必須準確無誤地對應到輸入中的變量名。最后,確認輸出結果是否符合要求,比如類型是否都是整數,以及結構是否正確。這樣用戶就能得到一個正確的JSON對象作為輸出了。
</think>要計算兩個整數的和,請按照以下步驟操作:1. 輸入第一個整數:5
2. 輸入第二個整數:3
3. 計算它們的和:5 + 3 = 8根據要求,生成的JSON對象應如下:```json
{"a": 5,"b": 3,"result": 8
}
```請將上述內容作為輸出。============4 LCEL 字符串流式輸出 不帶有json字符串===========
<think>
首先,我需要明確用戶的要求。用戶希望計算兩個整數的和,并且提供一個簡短的輸出。接下來,我會將輸入的兩個整數分別提取出來。這里給出的是5和3。然后,我會進行加法運算:5加上3等于8。最后,我會將結果以清晰的方式呈現給用戶。
</think>要計算兩個整數的和,按照以下步驟進行:1. **輸入兩個整數**:- 輸入的第一個整數是 \( 5 \)- 輸入的第二個整數是 \( 3 \)2. **進行加法運算**:\[5 + 3 = 8\]3. **輸出結果**:\boxed{8}
============5 LCEL 輸出json===========
{'a': 5, 'b': 3, 'result': 8}
============6 LCEL 輸出pydantic對象===========
a=5 b=3 result=8 | 5 3 8Process finished with exit code 0