Location>code7788 >text

Langchain Basic Article (02)

Popularity:177 ℃/2025-02-04 20:12:57

LANGChain Core Module Learning: Chains

For simple large model applications, it is possible to use the language model (LLMS) alone.

But more complicated large model applications need toLLMsandChat ModelsLinks together. Either link with each other or link with other components.

Langchain provides this "chain" applicationChaininterface.

LANGChain is defined in a common wayChainIt is a collection of serials that call the component, which can contain other chains.

Chain class base class

Class inheritance relationship:

Chain --> <name>Chain  # Examples: LLMChain, MapReduceChain, RouterChain
# Define a basic class called chain
 Class Chain (Serializable, Runnable [Dict [Str, Any], DICT [Str, Any]], ABC):
     "" "" Create a structured component to call the abstract base of the sequence.
    
     The chain should be used to encode a series of calls for components, such as models, document retrievals, other chains, etc., and provide a simple interface for this sequence.
    
     The CHAIN ​​interface makes the creation application easier. These applications are:
     -The state: adding Memory to any chain can make it state,
     -The observed: pass the callbacks to chain to perform additional functions, such as records, which is outside the main component call sequence,
     -Table: Chain API is flexible enough to easily combine Chains with other components, including other chains.
    
     The main method of chain disclosure is:
     - `__call__`:The chain can be called。`__call__`The method is to executeChainThe main way。It will receive input as a dictionary,And return a dictionary output。
     - `run`:A convenient way,Toargs/kwargsReceive input in form,And return the output as a string or object。This method can only be used for some chains,Not like`__call__`Return to rich output like that。
     "" "

     # Call chain
     def invoke(
         self, input: dict [str, any], config: optional [runnableconfig] = None
     ) -> Dict [Str, Any]:
         "" "Traditional calling method." "" "" "
         Return Self (input, ** (config or {}))

     # Chain memory, storage status and variables
     Memory: Optional [BaseMemory] = None
     "" "Optional memory object, defaults to NONE.
     Memory is a class that is called at the beginning and end of each chain。  At the beginning, memory loading variables and passed them in the chain.  At the end, it saves any return variables.
     There are many different types of memory, please check the memory document to get the complete directory.  "" "

     #, May be used for some operations or events of the chain.
     callbacks: callbacks = field (default = none, exclude = true)
     "" "Optional callback processing (or callback manager). The default is NONE.
     In the life cycle of the chain call, starting from on_chain_start to the end of on_chain_end or on_chain_error, the callback processing program will be called.
     Each custom chain can choose to call the additional callback method. For details, please refer to the callback document.  "" "

     # Whether the detailed output mode
     VERBOSE: BOOL = FIELD (default_factory = _get_verbosity)
     "" "Whether to run in detailed mode. In detailed mode, some intermediate logs will be printed to the console. The default value is` `." ""

     #The tag associated with the chain
     tags: optional [list [str]] = None
     "" "" Optional label with the chain, the default is None.
     These tags will be associated with each call for this chain and pass as a parameter to the processing program defined in the `callbacks`.
     You can use these, such as a specific instance of the identification chain and its use cases.  "" "

     #
     metadata: Optional [DICT [Str, Any]] = None
     "" "" Optional data with the chain, the default is None.
     These metadata will be associated with each call for this chain and pass it as a parameter to the processing program defined in the `callbacks`.
     You can use these, such as a specific instance of the identification chain and its use cases.  "" "

LLMChain

LLMChain is the simplest chain in Langchain. As the internal calls of other complex Chains and Agents, it is widely used.

A LLMChain is composed of PromPTEMPlate and LLM or Chat Model. It uses Key-Value directly (or Memory) to standardize the generated Prompt Template (prompt template), and transmit the generated Prompt (formatted string) to the large model, and return to large model output.

(picture)

from langchain_openai import OpenAI
 from  import PromptTemplate

 llm = OpenAI(model_name="gpt-3.5-turbo-instruct", temperature=0.9, max_tokens=500)

 prompt = PromptTemplate(
     input_variables=["product"],
     Template = "Give 10 good names for the company that manufacture {Product} and give a complete company name"
 Cure

 From Import LLMChain

 Chain = llmchain (llm = llm, prompt = prompt)
 Print ({{{
     'Product': "GPU with excellent performance"
     })))

Output:

{'Product': 'GPU', 'Text': '\ n1. Star Fire Excellence Technology Co., Ltd. \ n2.  Li Technology Co., Ltd. \ n5. Speed ​​Computing Technology Co., Ltd. \ n6. Infinite Creative Intelligent Technology Co., Ltd. \ n7. High Energy Innovation Technology Co., Ltd. \ n8. Tianma Xingkong Technology Co., Ltd.  Technology Co., Ltd. '}

Sequential Chain

Candidate call language model (using one call output as the input of another call).

Sequential chain allows users to connect multiple chains and combine them into pipeline to perform specific scenes. There are two types of sequential chains:

  • SimplesequentialChain: The simplest form of order chain, each step has a single input/output, and the output of one step is the input of the next step.
  • SequentialChain: The order chain of more common forms allow multiple input/output.

Use SimplesequentialChain to achieve drama abstracts and comments (single input/single output)

(picture)

# This is a LLMChain, which is used to write profiles according to the title of the play.

 llm = Openai (temperature = 0.7, max_tokens = 1000)

 Template = "" "You are a playwright. According to the title of the drama, your task is to write a profile for the title.

 Title: {Title}
 Drama writer: The following is a brief introduction to the above drama: "" ""

 Prompt_template = Prompttemplate (input_variables = ["Title"], template = Template)
 synopsis_chain = llmchain (llm = llm, prompt = prompt_template)
# This is a LLMCHAIN, which is used to write a drama comment based on the plot profile.
 # LLM = Openai (temporature = 0.7, max_tokens = 1000)
 Template = "" "You are the drama critics of the New York Times. According to the story, your job is to write a comment on the show.

 Introduction to the plot:
 {synopsis}

 The following is a commentator from the New York Times drama critics commented on the above drama: "" "

 Prompt_template = Prompttemplate (input_variables = ["Synopsis"], template = Template)
 Review_chain = llmchain (llm = llm, prompt = prompt_template)

(picture)

# This is a SimplesequentialChain, run these two chains in order
 From Import SimplesequentialChain

 overall_chain = Simplesequentialchain (chains = [SYNOPSIS_Chain, Review_chain], VERBOSE = TRUE)
Review = overall_chain.invoke ("" Star Wars Season 9 ")

Output:

> Entering New SimplesequentialChain Chain ...

 "The Ninth Season of Star Wars" is a science fiction drama full of thrilling stimuli, which continues the legendary story of the Star Wars series.  In this season, we will witness the new adventure and challenges, because the Galaxy is once again in the abyss of the war.  The war between the resistance and the first order continued, but this time, they will face more powerful enemies.  New characters will reappear with old friends and fight side by side with our heroes.  As the war progresses, we will also witness the eternal confrontation of power and darkness, and unremitting pursuit of freedom and justice.  In this epic battle, we will see the power of courage, sacrifice and love, and at the same time, we will also think about our future choices and fate.  "The Ninth Season of Star Wars" will bring the audience's unparalleled visual feast and touching stories, let us witness this magnificent interstellar adventure together!


 "The Ninth Season of Star Wars" is an epic sci -fi drama that is not to be missed.  The play continues the legendary story of the Star Wars series, bringing the audience into a thrilling and exciting galaxy.  In this season, we will fight side by side with the heroes we are familiar with, and at the same time, we will also witness new challenges and adventures.

 The play is not only a visual feast, but also a thoughtful story.  Through the eternal confrontation of strength and darkness, and the pursuit of freedom and justice, the audience will be brought into a deep thinking about destiny and choice.  In this epic battle, we will witness the power of courage, sacrifice and love, these are important themes that have been discussed in the Star Wars series.

 The play not only has amazing special effects and wonderful action scenes, but more importantly, it successfully combines these elements with deep character relationships and emotional expression.  The audience will be brought into a hoping and moved interstellar adventure to experience their growth and transformation with the characters.

 In general, "The Ninth Season of Star Wars" is an excellent work full of emotions and stories. It will immerse the audience in an interstellar world full of surprises and challenges.  Whether you are a loyal fan of the Star Wars series or an audience who is interested in science fiction, this drama will bring you an unforgettable experience.  Don't miss it!

 > Finished Chain.

Router Chain: Model calls to implement conditional judgment

This code builds a customized link system that users can provide different input prompts and get appropriate responses based on these prompts.

Main logic: fromprompt_infosCreate multipleLLMChainObject, and save them in a dictionary, and then create a defaultConversationChain, Finally create a routing functionMultiPromptChain

(picture)

from  import MultiPromptChain
from langchain_openai import OpenAI
from  import ConversationChain
from  import LLMChain
from  import PromptTemplate
Physics_template = "" "You are a very smart physics professor.
 You are good at answering physical questions about physics in a simple and easy -to -understand way.
 When you don't know the answer to a question, you will frankly admit it.

 This is a question:
 {input} "" "" "


 math_template = "" You are a great mathematician. You are good at answering mathematical questions.
 The reason why it is so outstanding is that you can decompose the problem into various components.
 Answer these parts first, and then integrate them to answer wider questions.

 This is a question:
 {input} "" "" "
Prompt_infos = [
     {{
         "name": "Physics",
         "Description": "Applicable to answering physical questions",
         "Prompt_template": Physics_template,
     },
     {{
         "name": "Mathematics",
         "Description": "Applicable to answering mathematical questions",
         "Prompt_template": math_template,
     },
 ]

llm = OpenAI(model_name="gpt-3.5-turbo-instruct")

# Create an empty target chain dictionary to store LLMChain generated according to Prompt_infos.
 destination_chains = {}

 # Prompt_infos list to create a LLMChain for each information.
 for p_info in Prompt_infos:
     name = p_info ["name"] # extract name
     Prompt_template = p_info ["Prompt_template"] # extract template
     # P
     Prompt = Prompttemplate (template = Prompt_template, input_variables = ["input"])
     # Use the above templates and LLM objects to create the LLMChain object
     Chain = llmchain (llm = llm, prompt = prompt)
     # Add the newly created Chain object to the Destination_chains dictionary
     destination_chains [name] = chain

 # Create a default ConversationChain
 default_chain = ConversationChain (llm = llm, output_key = "text")

Use LLMROUTERCHAIN ​​to implement conditional judgment calls

This code defines a Chain object (Llmrouterchain). The object first uses Router_chain to determine which Destination_chain should be executed. If there is no suitable target chain, the default_chain defaults.

from .llm_router import LLMRouterChain, RouterOutputParser
from .multi_prompt_prompt import MULTI_PROMPT_ROUTER_TEMPLATE
# Extract the target information from Prompt_infos and convert it into a string list
 destinations = [f "{p ['name']}: {p ['description']}" for p in prompt_infos]
 # Use the join method to convert the list into string, and separate the lines between each element
 destinations_Str = "\ n". Join (destinations)
 # According to Multi_prompt_Router_template Format Strings and Destinations_Str Create Route Templates
 router_template = multi_prompt_router_template.Format (destinations = Destinations_Str)
 # P PROMPTEMPlate
 router_prompt = Prompttemplate (
     template = router_template,
     input_variables = ["input"],
     output_parser = routeroutputparser (),
 Cure
 # Use the above -mentioned routing templates and LLM objects to create LLMROUTERCHAIN ​​objects
 router_chain = llmrouterchain.from_llm (llm, router_prompt)

print(destinations)
Output:['Physics: Applicable to answering physical questions', 'Mathematics: Applicable to answering mathematical questions']]

# T Multipromptchain object, which contains routing chains, target chains and default chains.
 chain = multipromptchain (
     router_chain = router_chain,
     destination_chains = Destination_chains,
     default_chain = default_chain,
     Verbose = true,
 Cure

Print ("What is the black body radiation ??"))
Output:

> Entering New Multipromptchain Chain ...
 Physics: {'Input': 'What is Blackbody Radiation?'}
 > Finished Chain.
 {'Input': 'What is Blackbody Radiation?', 'Text': '\ n \ N \ nThe black body radiation is a physical phenomenon that refers to electromagnetic radiation caused by thermal movement inside the object.  Simply put, the object will emit electromagnetic waves when heated, and the frequency and strength of these waves depends on the temperature of the object.  The characteristic of black body radiation is that its radiation frequency has nothing to do with the temperature of the object, but only related to the structure and nature of the object.  This concept has important applications in thermodynamics and quantum mechanics.  '}