Các dạng móc xích LangChain (phần 3)

Xích chuỗi: kết hợp nhiều mắt xích

Xích chuỗi cho phép bạn kết hợp nhiều mắt xích cùng với nhau vào thành một chuỗi. Mỗi bước trong xích lấy đầu ra từ
bước trước và sử dụng nó như đầu vào cho bước tiếp theo. Cái này đặc biệt hữu dụng khi bạn muốn xử lí dữ liệu qua
một chuỗi các chuyển dạng, như tóm tắt một tài liệu, rút thông tin then chốt, và sau đó khởi tạo cái nhìn bên trong
dựa trên text rút ra đó.
Thực thi code ví dụ

from langchain.chains import SequentialChain, LLMChain
from langchain.prompts import ChatPromptTemplate
from langchain_openai import AzureOpenAI
# Initialize the LLM (Azure OpenAI in this case)
llm = AzureOpenAI(deployment_name=”<Azure deployment name>”, model_name=”<Model name>”)
# Step 1: Summarize the customer feedback
template1 = “Summarize the following customer feedback:\n{feedback}”
prompt1 = ChatPromptTemplate.from_template(template1)
chain_1 = LLMChain(llm=llm, prompt=prompt1, output_key=”feedback_summary”)
# Step 2: Identify key issues from the feedback summary
template2 = “Identify the key issues or complaints from this feedback summary:\n{feedback_summary}”
prompt2 = ChatPromptTemplate.from_template(template2)
chain_2 = LLMChain(llm=llm, prompt=prompt2, output_key=”key_issues”)
# Step 3: Generate improvement suggestions based on the key issues
template3 = “Based on these key issues, suggest improvements to the product:\n{key_issues}”
prompt3 = ChatPromptTemplate.from_template(template3)
chain_3 = LLMChain(llm=llm, prompt=prompt3, output_key=”improvement_suggestions”)
# Create the Sequential Chain
seq_chain = SequentialChain(chains=[chain_1, chain_2, chain_3], input_variables=[‘feedback’], output_variables=[‘feedback_summary’, ‘key_issues’, ‘improvement_suggestions’], verbose=True)
# Example Customer Feedback
customer_feedback = ”’
I have been using this smartphone for about 6 months now, and while it performs well in terms of speed,
I am facing significant issues with battery life. The battery drains very quickly, especially when using apps with high performance requirements.
Also, the camera quality does not match what was advertised. I expected better photo quality, particularly in low-light conditions.
On the positive side, I appreciate the build quality and the design, but the battery and camera need major improvements.
”’
# Run the Sequential Chain
results = seq_chain.invoke({‘feedback’: customer_feedback})
# Print the final improvement suggestions
print(results[‘improvement_suggestions’])
1. Manufacturers should focus on optimizing battery life and improving energy efficiency to meet customer demands.
2. Software integration should be enhanced for better performance and user experience.
3. Further exploration of foldable screens could provide competitive differentiation as the technology gains mainstream acceptance.
4. Emphasizing sustainability and environmentally friendly practices can attract eco-conscious customers and improve brand reputation.

Code này minh họa một xử lí chuỗi phản hồi thông tin khách hàng sử dụng LangChain và Azure OpenAI. Nó tạo một xích
3 bước.
Giải thích mỗi mắt xích
+ Mắt xích 1 (Tóm tắt text): Lấy tài liệu đầu vào và khởi tạo một summary về nó. Đầu ra được lưu giữ như một summary.
+ Mắt xích 2 (Nhận dạng các chủ đề then chốt) : Lấy summary khởi tạo bới mắt xích 1 và nhận dạng các topics hay themes
quan trọng nhất thảo luận trong tài liệu. Đầu ra được lưu giữ như key_topics.
+ Mắt xích 3 (Khởi tạo cái nhìn bên trong thực tế): Lấy key_topics từ mắt xích 2 và khởi tạo cái nhìn bên trong hay gợi ý
thực tế dựa trên các topics đó. Đầu ra được lưu giữ như insights.
Xích chuỗi cũng cho phép chúng ta kết hợp nhiều LLM chains theo phong cách khôn ngoan, với mỗi mắt xích xây dựng
trên các kết quả của cái trước. Cái này tạo một cách thức mạnh mẽ và linh động để xử lí các dòng làm việc phức tạp.

Chia sẻ