ChatGPT Prompting development practice

Chapter 7 ChatGPT Prompting Development Practice

 Application of prompting in LangChain framework

This section tells you about Prompt Engineering, which is mainly based on industrial-level source code and specific projects. Whether you are an engineer or a person without a technical background, everyone has heard of prompt words or used them frequently. In short, when we use OpenAI, we use its API or ChatGPT to communicate with it using prompt words. As shown in Figure 7-1, this is the ChatGPT interface. The author generally uses the GPT-4 large model.

9e61032d3ed5517af93f06b2b8a8c8dd.png

Figure 7- 1 ChatGPT interface

Any information you enter here is called a prompt from the perspective of the model, but industrial-grade prompts are not the same as those commonly understood by everyone. Let’s first take a look at what industrial-grade prompts look like. We have prepared many specific examples for you, as shown in Figure 7-2. These examples basically come from DeepLearning.AI. From the perspective of prompts, these examples are very systematic and very valuable. For example, the chains-of-thought mentioned here is obviously a very important one. Important example. Gavin's WeChat account: NLP_Matrix_Space

c4bdfddab5378e7295626209328e98c0.png

Figure 7- 2 Prompt word case

Now the most recognized large-scale application development framework in open source, industry and academia is LangChain, which involves the use of a large number of prompt words. The prompt words have been reviewed and improved by many people, as well as the practice of the industry or the practice of academic research. If you download the source code of LangChain, you will find that LangChain basically has prompt words (Prompt) under different functions, as shown in Figure 7-3.

60a4a2eb461c7330dcaf36b8ff3f6f84.png

Figure 7- 3 LangChain function directory

To give a simple example, look at any directory, such as retrieval_qa, base.py can be considered as a basic API, and the core part is its prompt word (prompt), as shown in Figure 7-4.

37f166e7d71978a5f30a57fde6547518.png

Figure 7- 4 Prompt words for retrieval_qa

The prompt in prompt.py will have a template (template), described as "Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer." ("Use the following pieces of context to answer the question. If you don't know the answer, just say you don't know, Don't try to make up the answer.")

The code implementation of prompt.py:

  1. # flake8: me

  2. from langchain.prompts import PromptTemplate

  3. prompt_template = """Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.

  4. {context}

  5. Question: {question}

  6. Helpful Answer:"""

  7. PROMPT = PromptTemplate(

  8.    template=prompt_template, input_variables=["context", "question"]

You can look at the examples again, for example: question_answering, which provides information on prompt templates such as map_reduce_prompt, map_rerank_prompt, refine_prompts, stuff_prompt, etc.

8803f9b3159ef4e1b2578d824ece1618.png

Figure 7- 5 Prompt words for question_answering

The map_reduce_prompt.py code sets a question prompt template in its own specific format, described as: "Use the following portion of a long document to see if any of the text is relevant to answer the question. Return any relevant text verbatim" ("Use the following part of the long document to see if there is any text related to answering the question, and return any relevant text verbatim"), here There is context, there are questions and relevant content.

Code implementation of map_reduce_prompt.py:

  1. question_prompt_template = """Use the following portion of a long document to see if any of the text is relevant to answer the question.

  2. Return any relevant text verbatim.

  3. {context}

  4. Question: {question}

  5. Relevant text, if any:"""

  6. QUESTION_PROMPT = PromptTemplate(

  7.    template=question_prompt_template, input_variables=["context", "question"]

  8. )

  9. system_template = """Use the following portion of a long document to see if any of the text is relevant to answer the question.

  10. Return any relevant text verbatim.

  11. ______________________

  12. {context}"""

  13. messages = [

  14.   SystemMessagePromptTemplate.from_template(system_template),

  15.   HumanMessagePromptTemplate.from_template("{question}"),

  16. ]

  17. CHAT_QUESTION_PROMPT = ChatPromptTemplate.from_messages(messages)

  18. …….

Also have a look at query_constructor, as shown in Figure 7-6.

2bb85f3398019d296a293d4d72970135.png

Figure 7-6 prompt words of query_constructor

In the prompt.py code file, a lot of information about the prompt word is also given, including some descriptions of it.

The code implementation of prompt.py:

  1. ….

  2. EXAMPLES_WITH_LIMIT = [

  3.    {

  4.        "i": 1,

  5.        "data_source": SONG_DATA_SOURCE,

  6.        "user_query": "What are songs by Taylor Swift or Katy Perry about teenage romance under 3 minutes long in the dance pop genre",

  7.        "structured_request": FULL_ANSWER,

  8.    },

  9.    {

  10.        "i": 2,

  11.        "data_source": SONG_DATA_SOURCE,

  12.        "user_query": "What are songs that were not published on Spotify",

  13.        "structured_request": NO_FILTER_ANSWER,

  14.    },

  15.    {

  16.        "i": 3,

  17.        "data_source": SONG_DATA_SOURCE,

  18.        "user_query": "What are three songs about love",

  19.        "structured_request": WITH_LIMIT_ANSWER,

  20.    },

  21. ]

  22. EXAMPLE_PROMPT_TEMPLATE = """\

  23. << Example {i}. >>

  24. Data Source:

  25. {data_source}

  26. User Query:

  27. {user_query}

  28. Structured Request:

  29. {structured_request}

  30. """

  31. EXAMPLE_PROMPT = PromptTemplate(

  32.    input_variables=["i", "data_source", "user_query", "structured_request"],

  33.    template=EXAMPLE_PROMPT_TEMPLATE,

  34. )

  35. ….

However, what we focus on in this section is Agents. As shown in Figure 7-7, there are many different types of agents, and two are particularly important. You must pay attention to them. One is react, and the other is called mrkl. Gavin's WeChat account: NLP_Matrix_Space

c5e1d002b00e0a3f28c50468d91a3332.png

Figure 7- 7 Agent directory of LangChain

If you do industrial-strength, large-scale model-based application development, you generally need a manager, which can be collectively called an agent. When we talk about the ChatGPT plug-in (Plugin), we actually think about things from the perspective of an agent, which is the idea of ​​an agent. gpt-4-0613 and gpt-3.5-turbo-0613 are new models released by OpenAI in June 2023. They can well support function calling (Function calling). The documentation on OpenAI’s official website says a very important sentence: the large model extracts the name and parameters of the function from your information, and passes them to you to mobilize the function. server). Going back to LangChain, LangChain actually includes all the functions above, including the functions of the ChatGPT plug-in, including the function of OpenAI function calling (Function calling). The functions provided by OpenAI can be immediately mapped to LangChain. Therefore, the core problem here is to make it work effectively, rather than you simply input information, and then you get the result, of course, the result may include some steps, or some interactions and so on. However, if you do not have an agent, you can only generate text and cannot perform actions (Action), and if you develop an application, you must implement Action, which is the main role of the agent (Agent). From the perspective of open source software naming, at the beginning of LangChain, the core focus was on Chain, and later on Agent.

As shown in Figure 7-8, it is the directory structure of the mrkl agent. The author of the "MRKL Systems" paper pronounces it as ˈmɪrək(ə)l, which is the same as the pronunciation of miracle.

72da9b733a177a74abda44038de4d89c.png

Figure 7- 8 The directory structure of mrkl

As shown in Figure 7-9, MRKL (Modular Reasoning, Knowledge and Language) is a new type of language understanding system that combines neural network models, external knowledge bases and expert systems, taking into account neural models and symbolic reasoning capabilities, and improves the efficiency of natural language processing.

51d646627ddf53d7631c6218a1d574b2.png

Figure 7-9 Schematic diagram of MRKL architecture

319618c7f7b542496c0f3e6f12b62520.png

7e03534d2b75bcc2d3e617f999793ef0.jpeg

0b40cb681b6c8812c339236c1736ca9d.png

afbcf60a6f46d2cdd8932b0621915259.jpeg

5c84bd024aa0e38767c19b754ca50b46.jpeg

Guess you like

Origin blog.csdn.net/duan_zhihua/article/details/131587727