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

Định tuyến đa prompt

MultiPromptChain của LangChain cho phép chúng ta chọn động xích phù hợp dựa trên nhập vào người dùng.

from langchain.chains.router.multi_prompt_prompt import MULTI_PROMPT_ROUTER_TEMPLATE
# Join all destinations into a single string
destinations = [f”{p[‘name’]}: {p[‘description’]}” for p in prompt_infos]
destinations_str = “\n”.join(destinations)
# Define the router template using the multi-prompt router
from langchain.prompts import PromptTemplate
router_template = MULTI_PROMPT_ROUTER_TEMPLATE.format(destinations=
destinations_str)
router_prompt = PromptTemplate(template=router_template, input_variables=[“input”])

Đoạn code này minh họa chức năng định tuyến của LangChain sử dụng Multi-prompt Router. Nó lấy một danh sách
các prompts đích đến với các mô tả của chúng, định dạng chúng thành một mẫu định tuyến, và tạo một PromptTemplate
cái có thể chuyển hướng các nhập vào người dùng tới cái xử lí prompt chuyên biệt phù hợp nhất.
Thực thi xích định tuyến
Chúng ta bây giờ có thể định nghĩa xích định tuyến và kết nối các xích đích đến khác nhau vào nó. Chúng ta cũng
định nghĩa một xích mặc định cho các truy vấn không khớp.

from langchain.chains.router.llm_router import LLMRouterChain, RouterOutputParser
# Set up the router chain
router_chain = LLMRouterChain.from_llm(llm, router_prompt)
# Define a default chain for unmatched queries
default_prompt = ChatPromptTemplate.from_template(“{input}”)
default_chain = LLMChain(llm=llm, prompt=default_prompt)
from langchain.chains.router import MultiPromptChain
chain = MultiPromptChain(router_chain=router_chain,
destination_chains=destination_chains,
default_chain=default_chain, verbose=True)
# Run the routing chain for different queries
chain.run(“How do magnets work?”)
Beginner physics: {‘input’: ‘How do magnets work?’}
> Finished chain.
‘\n\nMagnets work by creating a magnetic field around them. This magnetic field is made up of tiny particles called electrons, which have a negative charge. When two magnets are brought close together, the negative electrons in one magnet are attracted to the positive electrons in the other magnet. I hope this explanation helps you understand how magnets work!’
chain.run(“How do Feynman Diagrams work?”)
Advanced physics: {‘input’: ‘How do Feynman Diagrams work?’}
> Finished chain.
‘\n\nFeynman diagrams are graphical representations of mathematical equations that describe the behavior of particles in quantum field theory. They were developed by physicist Richard Feynman. The rules for constructing Feynman diagrams dictate that the total number of incoming lines must equal the total number of outgoing lines at each vertex, and the conservation laws of energy

Code thực thi một hệ thống router chain cái chuyển hướng thông minh các truy vấn tới các xích chuyên biệt phù hợp
dựa trên sự phức tạp. Nó sử dụng LLMRouterChain để phân tích các truy vấn và định tuyến chúng tới hoặc xích vật lí
bắt đầu hoặc nâng cao, với một xích mặc định như fallback. MultiPromptChain phối hợp cơ chế định tuyến này, đảm bảo
các truy vấn nhận các trả lời mức phù hợp.
RunnablePassthrough là một tiện ích đơn giản cái đóng vai trò như một bộ phận đi qua trong một xích. Nó lấy đầu
vào và truyền nó hướng tới bộ phận tiếp theo trong xích mà không điều chỉnh nó. Về cơ bản, nó phục vụ như một cái
giữ chỗ hay cái kết nối trong một chuỗi các hoạt động.

Chia sẻ