owned this note
owned this note
Published
Linked with GitHub
### [AI / ML領域相關學習筆記入口頁面](https://hackmd.io/@YungHuiHsu/BySsb5dfp)
### [Deeplearning.ai GenAI/LLM系列課程筆記](https://learn.deeplearning.ai/)
#### [Large Language Models with Semantic Search。大型語言模型與語義搜索 ](https://hackmd.io/@YungHuiHsu/rku-vjhZT)
#### [Finetuning Large Language Models。微調大型語言模型](https://hackmd.io/@YungHuiHsu/HJ6AT8XG6)
#### [LangChain for LLM Application Development](https://hackmd.io/1r4pzdfFRwOIRrhtF9iFKQ)
---
[LangChain for LLM Application Development](https://www.youtube.com/watch?v=jFo_gDOOusk) LangChain教學、系列課程筆記
- [Models, Prompts and Output Parsers](https://hackmd.io/1r4pzdfFRwOIRrhtF9iFKQ)
- [Memory](https://hackmd.io/@YungHuiHsu/Hy120mR23)
- [Chains](https://hackmd.io/@YungHuiHsu/SJJvZ-ya2)
- [Question-and-Answer](https://hackmd.io/@YungHuiHsu/BJ10qunzp)
- [Evaluation](https://hackmd.io/@YungHuiHsu/Hkg0SgazT)
- [Agents](https://hackmd.io/@YungHuiHsu/rkBMDgRM6)
![](https://hackmd.io/_uploads/r1WTGXRhn.png =400x)
source : [LangChain.dart](https://pub.dev/packages/langchain)
---
:::info
整體內容是好的,但授課技巧、程式碼範例跟簡報內容的組織欠佳
如果直接跟著上課順序先看程式碼大概會很亂,建議影片內容先看後面的概念說明在看程式碼會比較好理解
以下筆記根據個人理解重新組織架構(有點頭痛)
:::
---
# [LangChain - Models, Prompts and Output Parsers](https://learn.deeplearning.ai/langchain/lesson/2/models,-prompts-and-parsers)
## Outline
* Direct API calls to OpenAI
* API calls through LangChain:
* Prompts:建輸入以傳遞給模型的方式
* Models:構成核心的大型語言模型 (LLM)
* Output parsers:處理模型輸出,將其轉化為更結構化的格式,以便後續處理
LangChain提供簡化操作的抽象,使重複使用模型、提示模型、解析輸出變得容易
![](https://hackmd.io/_uploads/H1nN87R3n.png =1200x)
- **demo**:
- 使用 **ChatOpenAI** 和 **LangChain** 提供的模板來建立和重用提示模板
- 利用提示模板和輸出解析器來處理複雜的LLM應用,如從客戶評論中提取信息並轉換成JSON格式
![](https://hackmd.io/_uploads/ryNnjy6fT.png =300x)
## Models, Prompts and Output Parsers
這節課探討如何使用LangChain工具來簡化和強化與大型語言模型的互動
### 1. 內容概述
本章主要在介紹Model I/O的部分,包含
- 模型(Models)、提示(Prompts)和解析器(Parsers)的概念
- **模型**:語言模型的基礎。
- **提示**:創建輸入以傳遞給模型的方式。
- **解析器**:將模型的輸出解析為更結構化的格式。
- 利用LangChain簡化和抽象化這些操作。
更詳盡的介紹見[官方文件-model_io](https://python.langchain.com/docs/modules/model_io/)
### 2. 啟動代碼
- 包括:導入OS、OpenAI,並加載OpenAI秘密金鑰。
```python!
from langchain.chat_models import ChatOpenAI
# To control the randomness and creativity of the generated
# text by an LLM, use temperature = 0.0
chat = ChatOpenAI(temperature=0.0)
chat
```
### 3. 語言翻譯示例
- 使用LLM將顧客郵件從「海盜英語」翻譯為禮貌的美式英語。
- 介紹如何使用LangChain更便捷地生成翻譯提示。
### 4. 提示模板
- 能夠重新使用和抽象化長而詳細的提示。
- LangChain提供了一些常用操作的內置提示,如摘要、問答、連接SQL數據庫或API。
#### 範例:語氣調換
```python=!
template_string = """Translate the text \
that is delimited by triple backticks \
into a style that is {style}. \
text: ```{text}```
"""
from langchain.prompts import ChatPromptTemplate
prompt_template = ChatPromptTemplate.from_template(template_string)
prompt_template.messages[0].prompt
# PromptTemplate(input_variables=['style', 'text'], output_parser=None, partial_variables={}, template='Translate the text that is delimited by triple backticks into a style that is {style}. text: ```{text}```\n', template_format='f-string', validate_template=True)
prompt_template.messages[0].prompt.input_variables
# ['style', 'text']
```
在`customer_style`指定想要的語氣
```python!
customer_style = """American English \
in a calm and respectful tone
"""
customer_email = """
Arrr, I be fuming that me blender lid \
flew off and splattered me kitchen walls \
with smoothie! And to make matters worse, \
the warranty don't cover the cost of \
cleaning up me kitchen. I need yer help \
right now, matey!
"""
customer_messages = prompt_template.format_messages(
style=customer_style,
text=customer_email)
print(type(customer_messages))
# <class 'list'>
print(type(customer_messages[0]))
# <class 'langchain.schema.HumanMessage'>
print(customer_messages[0])
# content="Translate the text that is delimited by triple backticks into a style that is American English in a calm and respectful tone\n. text: ```\nArrr, I be fuming that me blender lid flew off and splattered me kitchen walls with smoothie! And to make matters worse, the warranty don't cover the cost of cleaning up me kitchen. I need yer help right now, matey!\n```\n" additional_kwargs={} example=False
# Call the LLM to translate to the style of the customer message
customer_response = chat(customer_messages)
print(customer_response.content)
```
將客戶信件從“英式海盜(English Pirate)”口吻轉換為“平和恭敬的美式英語(American English in a calm and respectful tone)”的語氣
```text=!
I'm really frustrated that my blender lid flew off and made a mess of my kitchen walls with smoothie! And to make things even worse, the warranty doesn't cover the cost of cleaning up my kitchen. I could really use your help right now, my friend!
```
接著把客服回覆的語氣也做調換
```python=
service_reply = """Hey there customer, \
the warranty does not cover \
cleaning expenses for your kitchen \
because it's your fault that \
you misused your blender \
by forgetting to put the lid on before \
starting the blender. \
Tough luck! See ya!
"""
service_style_pirate = """\
a polite tone \
that speaks in English Pirate\
"""
service_messages = prompt_template.format_messages(
style=service_style_pirate,
text=service_reply)
print(service_messages[0].content)
service_response = chat(service_messages)
print(service_response.content)
```
原本客服正式的回覆語氣變成靠北的8+9語氣:joy_cat:
```text=!
Ahoy there, matey! I regret to inform ye that the warranty be not coverin' the costs o' cleanin' yer galley, as 'tis yer own fault fer misusin' yer blender by forgettin' to secure the lid afore startin' it. Aye, tough luck, me heartie! Fare thee well!
```
### 5. 解析輸出
- 使用LangChain解析LLM的JSON格式輸出。
- 示例:從產品評論中提取資訊,然後將輸出格式化為JSON格式。
以下顧客留言為非結構化的自然語言,要如何提取為結構化或量化的資訊呢?
```python=!
customer_review = """\
This leaf blower is pretty amazing. It has four settings:\
candle blower, gentle breeze, windy city, and tornado. \
It arrived in two days, just in time for my wife's \
anniversary present. \
I think my wife liked it so much she was speechless. \
So far I've been the only one using it, and I've been \
using it every other morning to clear the leaves on our lawn. \
It's slightly more expensive than the other leaf blowers \
out there, but I think it's worth it for the extra features.
"""
```
- 透過Langchain的提示模板
```python=
review_template = """\
For the following text, extract the following information:
gift: Was the item purchased as a gift for someone else? \
Answer True if yes, False if not or unknown.
delivery_days: How many days did it take for the product \
to arrive? If this information is not found, output -1.
price_value: Extract any sentences about the value or price,\
and output them as a comma separated Python list.
Format the output as JSON with the following keys:
gift
delivery_days
price_value
text: {text}
"""
```
我們希望可以透過提示模板,讓LLM幫我們從客戶留言中提取出結構化的輸出,例如類似下面json或dict格式,方便串接後面的分析
```text=!
{
"gift": False,
"delivery_days": 5,
"price_value": "pretty affordable!"
}
```
使用提示模板可以幫助我們把大量重覆的任務以模組化的方式進行
```python=!
from langchain.prompts import ChatPromptTemplate
prompt_template = ChatPromptTemplate.from_template(review_template)
print(prompt_template)
messages = prompt_template.format_messages(text=customer_review)
```
#### 直接使用openAI的API
產出的結果看似為json結構實為`str`格式,不利後續解析
```pythpn=
chat = ChatOpenAI(temperature=0.0)
response = chat(messages)
print(response.content)
#{
# "gift": false,
# "delivery_days": 2,
# "price_value": ["It's slightly more expensive than the other leaf #blowers out there, but I think it's worth it for the extra features."]
#}
type(response.content)
# "str"
```
使用`dict`的`get`方法會得到錯誤回報
```python=!
# You will get an error by running this line of code
# because'gift' is not a dictionary
# 'gift' is a string
response.content.get('gift')
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[65], line 4
1 # You will get an error by running this line of code
2 # because'gift' is not a dictionary
3 # 'gift' is a string
----> 4 response.content.get('gift')
AttributeError: 'str' object has no attribute 'get'
```
#### 使用LangChain的解析器
透過定義響應模式、建立結構化輸出解析器、建立評論模板,並最後格式化消息,實現了一個流程,用於從客戶評論中提取和結構化特定資訊。這種方法允許將文本資料轉化為結構化資料,以便於分析和處理
- 本節程式碼流程圖
```mermaid
flowchart TD
A[Define Response Schemas] -->|gift| B[Create Structured Output Parser]
A -->|delivery_days| B
A -->|price_value| B
B --> C[Get Format Instructions]
C --> D[Create Review Template]
D --> E[Format Messages with Chat Prompt Template]
classDef schema fill:#f9f,stroke:#333,stroke-width:2px;
class A schema;
class B,C,D,E default;
classDef default fill:#f0ffff,stroke:#333,stroke-width:2px;
```
- 建立響應模式
分別為 "gift", "delivery_days", 和 "price_value" 建立三個響應模式(ResponseSchema)。將我們想要的三種輸出結果,使用`ResponseSchema`封裝
- 每個模式都有一個名稱和描述,描述指定了如何解析和提取特定的資訊
- `name`
- `description` : 輸入要模型執行的工作
```python!
from langchain.output_parsers import ResponseSchema
from langchain.output_parsers import StructuredOutputParser
gift_schema = ResponseSchema(name="gift",
description="Was the item purchased\
as a gift for someone else? \
Answer True if yes,\
False if not or unknown.")
delivery_days_schema = ResponseSchema(name="delivery_days",
description="How many days\
did it take for the product\
to arrive? If this \
information is not found,\
output -1.")
price_value_schema = ResponseSchema(name="price_value",
description="Extract any\
sentences about the value or \
price, and output them as a \
comma separated Python list.")
```
- 建立結構化輸出解析器
- 組合響應模式
- 將之前建立的三個響應模式組合成一個列表 `response_schemas`
- 建立結構化輸出解析器
- 使用 `StructuredOutputParser` 和 `response_schemas` 建立一個結構化的輸出解析器 `output_parser`
```python!
response_schemas = [gift_schema,
delivery_days_schema,
price_value_schema]
output_parser = StructuredOutputParser.from_response_schemas(response_schemas)
```
- 建立評論模板
- 獲取格式化指示:
- 使用 output_parser.get_format_instructions() 獲取格式化的指示。
```python!
format_instructions = output_parser.get_format_instructions()
print(format_instructions)
# The output should be a markdown code snippet formatted in the following schema, including the leading and trailing "\`\`\`json" and "\`\`\`":
# ```json
# {
# "gift": string // Was the item purchased as a gift for someone else? Answer True if yes, False if not or unknown.
# "delivery_days": string // How many days did it take for the product to arrive? If this information is not found, output -1.
# "price_value": string // Extract any sentences about the value or price, and output them as a comma separated Python list.
# }
```
- 建立評論模板:
* 建立一個包含指示和輸入文本區域的評論模板 review_template_2
* 將剛剛設定的`format_instructions`套入模板中
```python=
review_template_2 = """\
For the following text, extract the following information:
gift: Was the item purchased as a gift for someone else? \
Answer True if yes, False if not or unknown.
delivery_days: How many days did it take for the product\
to arrive? If this information is not found, output -1.
price_value: Extract any sentences about the value or price,\
and output them as a comma separated Python list.
text: {text}
{format_instructions}
"""
```
- 格式化消息
檢視經過`StructuredOutputParser`解析LLM輸出的結果,資料格式已經轉為python的dict,而且也能用`get`方法取值
```python=
prompt = ChatPromptTemplate.from_template(template=review_template_2)
messages = prompt.format_messages(text=customer_review,
format_instructions=format_instructions)
```
```python=!
response = chat(messages)
print(response.content)
# ```json
# {
# "gift": false,
# "delivery_days": "2",
# "price_value": "It's slightly more expensive than the other leaf blowers out there, but I think it's worth it for the extra features."
# }
# ```
output_dict = output_parser.parse(response.content)
print(output_dict)
# {'gift': False,
# 'delivery_days': '2',
# 'price_value': "It's slightly more expensive than the other leaf blowers out there, but I think it's worth it for the extra features."}
type(output_dict)
# dict
output_dict.get('delivery_days')
# '2'
```
---
##
### AutoGen
微軟在2023.08推出的智能代理框架,使用上看來比Langchain更簡單易用
- [github](https://github.com/microsoft/autogen)
![image](https://hackmd.io/_uploads/rkFs55LIa.png)
![image](https://hackmd.io/_uploads/S1fA55LI6.png)
#### [2023.10。01coder。AutoGen + LangChain + ChromaDB = Super AI Agents
有一系列LLM部署架構相關的教學
:::info
AutoGen doesn't support connecting to various external data sources natively. This is exactly where LangChain can come into play.
:::