Objective: # 概念阐释 语言模型分为2种,一种是base LLM,预测语言模型,它是[[自我监督学习]]的应用,能够预测接下来的内容;另一种,也就是我们现在使用的,instruction LLM,指令调用语言模型,它能够按照指令回答问题。 训练从base LLM开始 -> 培训 -> 微调 -> 遵循指令 -> RLHF 从人类反馈中学习增强 # 实例 - 基础型 LLM 例如给'The cat is',模型预测下一个单词是“sitting”,“sleeping”。 - 指令型LLM 例如给出的指令是“写一篇关于人工智能的文字”。指令型LLM可以生成一篇文章。 但它需要更加具体清晰的指令。假设你在和一个极为聪明却不了解你的人对话,你需要它完成一些任务,比如“写一篇关于图灵的文章”。这不是一个具体清晰的指令,写关于图灵的什么?学术研究?生平?用哪种基调?一篇报导还是朋友圈?如果需要让它参考一些资料还需要告诉它你要去阅读下哪些相关资料。 # 相关内容 ## 原则1: 提供具体、清晰的指令 清晰≠简短: 长的提示语提供了更好的上下文和清晰度 #### 1. 使用分隔符: quotes:""" triple backticks: 反引号 triple dashes:--- angle brackets:< > XML tags: `<tag></tags>` ```python text = f""" You should express what you want a model to do by \ providing instructions that are as clear and \ specific as you can possibly make them. \ This will guide the model towards the desired output, \ and reduce the chances of receiving irrelevant \ or incorrect responses. Don't confuse writing a \ clear prompt with writing a short prompt. \ In many cases, longer prompts provide more clarity \ and context for the model, which can lead to \ more detailed and relevant outputs. """ prompt = f""" Summarize the text delimited by triple backticks \ into a single sentence. --- {text} //{}括起来的是引用的内容 --- """ response = get_completion(prompt) print(response) ``` - *这里面提到的根据3个反引号中的内容进行总结,反引号={text}=f"""You should..., 在chatGPT中应该是不需要* #### 2. 结构化输出: - 生成JSON, HTML 格式的文件:生成三个编造的书名及其作者和类型的列表。 使用以下键以 JSON 格式提供它们:book_id、书名、作者、流派。 ```python prompt = f""" Generate a list of three made-up book titles along \ with their authors and genres. Provide them in JSON format with the following keys: book_id, title, author, genre. """ response = get_completion(prompt) print(response) ``` ``` [ { "book_id": "001", "book_title": "The Shadow in the Woods", "author": "Sophie Davis", "genre": "Thriller" }, { "book_id": "002", "book_title": "The Last Rose", "author": "David Jameson", "genre": "Romance" }, { "book_id": "003", "book_title": "The Lost City of Zinj", "author": "Michael Johnson", "genre": "Adventure" }] ``` #### 3. 让模型在进行任务前检查某些条件是否满足 - 有指令的案例:如何制作一杯茶的过程(给出的内容是否满足“提供了一些指令”这个条件,如果满足,以某种格式给出指令,如果没有返回没有步骤。) ```python text_1 = f""" Making a cup of tea is easy! First, you need to get some \ water boiling. While that's happening, \ grab a cup and put a tea bag in it. Once the water is \ hot enough, just pour it over the tea bag. \ Let it sit for a bit so the tea can steep. After a \ few minutes, take out the tea bag. If you \ like, you can add some sugar or milk to taste. \ And that's it! You've got yourself a delicious \ cup of tea to enjoy. """ prompt = f""" You will be provided with text delimited by triple quotes. 您将获得由三重引号分隔的文本。 If it contains a sequence of instructions,如果它包含一系列指令, \ re-write those instructions in the following format:按照以下格式重写这些指令: Step 1 - ... Step 2 - … … Step N - … If the text does not contain a sequence of instructions, \ 如果文本不包含指令序列, then simply write \"No steps provided.\"然后简单地写下“没有提供步骤。” \"\"\"{text_1}\"\"\" """ response = get_completion(prompt) print("Completion for Text 1:") print(response) ``` - 没指令的案例:描述了一个阳光明媚的日子 ```python text_2 = f""" The sun is shining brightly today, and the birds are \ singing. It's a beautiful day to go for a \ walk in the park. The flowers are blooming, and the \ trees are swaying gently in the breeze. People \ are out and about, enjoying the lovely weather. \ Some are having picnics, while others are playing \ games or simply relaxing on the grass. It's a \ perfect day to spend time outdoors and appreciate the \ beauty of nature. """ prompt = f""" You will be provided with text delimited by triple quotes. If it contains a sequence of instructions, \ re-write those instructions in the following format: Step 1 - ... Step 2 - … … Step N - … If the text does not contain a sequence of instructions, \ then simply write \"No steps provided.\" \"\"\"{text_2}\"\"\" """ response = get_completion(prompt) print("Completion for Text 2:") print(response) ``` **迁移案例**: - 产品操作手册 #### 4. 少量的示例提示 类似于创建了一种character,模仿某种回复风格 ```python prompt = f""" Your task is to answer in a consistent style.你的任务是以一致的风格回答。 <child>: Teach me about patience. <grandparent>: The river that carves the deepest \ valley flows from a modest spring; the \ grandest symphony originates from a single note; \ the most intricate tapestry begins with a solitary thread. <child>: Teach me about resilience. """ response = get_completion(prompt) print(response) ``` ## 原则2 给模型足够的“思考”时间 #### 1. 指定完成任务所需的步骤 把一个大的任务拆解成小任务 ```python text = f""" In a charming village, siblings Jack and Jill set out on \ a quest to fetch water from a hilltop \ well. As they climbed, singing joyfully, misfortune \ struck—Jack tripped on a stone and tumbled \ down the hill, with Jill following suit. \ Though slightly battered, the pair returned home to \ comforting embraces. Despite the mishap, \ their adventurous spirits remained undimmed, and they \ continued exploring with delight. """ # example 1 prompt_1 = f""" Perform the following actions: 1 - Summarize the following text delimited by triple \ 总结反引号中的text backticks with 1 sentence. 2 - Translate the summary into French. 3 - List each name in the French summary. 4 - Output a json object that contains the following \ keys: french_summary, num_names. Separate your answers with line breaks. Text: --- {text} --- """ response = get_completion(prompt_1) print("Completion for prompt 1:") print(response) ``` **迁移案例**: - 2w2h模型 - 总结性prompt ``` 首先概述本文,然后用列要点的形式总结主要思想,然后提炼出最重要的5个关键词,最后提出启发思考的3个好问题。每一步之间回车分段,不要不分段,不便于阅读。Response in chinese. 用以下格式输出 - <总结> - <主要思想> - - 。。。 ``` 指引模型使用具体的格式 ```python prompt_2 = f""" Your task is to perform the following actions: 1 - Summarize the following text delimited by <> with 1 sentence. 2 - Translate the summary into French. 3 - List each name in the French summary. 4 - Output a json object that contains the following keys: french_summary, num_names. Use the following format: Text: <text to summarize> Summary: <summary> Translation: <summary translation> Names: <list of names in Italian summary> Output JSON: <json with summary and num_names> Text: <{text}> """ response = get_completion(prompt_2) print("\nCompletion for prompt 2:") print(response) ``` #### 2. 要求模型在得出结论前先自行处理问题 - 在判断学生的答案之前,指引模型先给出自己的答案 ```python prompt = f""" Your task is to determine if the student's solution \ is correct or not. To solve the problem do the following: - First, work out your own solution to the problem. - Then compare your solution to the student's solution \ and evaluate if the student's solution is correct or not. Don't decide if the student's solution is correct until you have done the problem yourself. Use the following format: Question: --- question here --- Student's solution: --- student's solution here --- Actual solution: --- steps to work out the solution and your solution here --- Is the student's solution the same as actual solution \ just calculated: --- yes or no --- Student grade: --- correct or incorrect --- Question: --- I'm building a solar power installation and I need help \ working out the financials. - Land costs $100 / square foot - I can buy solar panels for $250 / square foot - I negotiated a contract for maintenance that will cost \ me a flat $100k per year, and an additional $10 / square \ foot What is the total cost for the first year of operations \ as a function of the number of square feet. --- Student's solution: --- Let x be the size of the installation in square feet. Costs: 1. Land cost: 100x 2. Solar panel cost: 250x 3. Maintenance cost: 100,000 + 100x Total cost: 100x + 250x + 100,000 + 100x = 450x + 100,000 --- Actual solution: """ response = get_completion(prompt) print(response) ``` #### 为减少幻觉现象,建议让模型先从文本中找到相关引文,然后基于这些引文回答问题。 - 基于文本生成答案 - 从文中找出引文 - 要求模型引用这些引文回答问题 # 参考资料 https://learn.deeplearning.ai/chatgpt-prompt-eng/lesson/2/guidelines