defget_completion(prompt, model="gpt-3.5-turbo"): messages = [{"role": "user", "content": prompt}] response = openai.ChatCompletion.create( model=model, messages=messages, temperature=0, # this is the degree of randomness of the model's output ) return response.choices[0].message["content"]
text = f""" 内容省略(可添加任意内容) """ prompt = f""" Summarize the text delimited by triple backticks \ into a single sentence. ```{text}``` """
response = get_completion(prompt) print(response)
策略 2:要求模型提供结构化输出
对于开发者来说,要求模型提供诸如 HTML 或 JSON 的格式化输出,有利于构建更加健壮的应用。具体的代码示例如下:
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)
策略 3:让模型进行条件判断
对于较复杂的 Prompt,模型生成结果的时间可能会较长,同时可能浪费大量的 token。为了规避不必要的 API 调用消耗,我们可以在 Prompt 中包含一定的条件判断逻辑,来帮助模型在不满足条件时提前终止运算,直接返回结果。具体的代码示例如下:
text_1 = f""" 如何把大象放进冰箱?\ 首先打开冰箱的门,\ 然后把大象放进去,\ 最后关上冰箱门。 """ 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)
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 love. """
该策略即通过 Prompt 拆解任务步骤降低复杂度,以帮助模型更好地完成任务。具体的代码示例如下(省略了 text 的内容,指定输出格式):
prompt = f""" Your task is to perform the following actions: 1 - Summarize the following text delimited by <> with 1 sentence. 2 - Translate the summary into Chinese. 3 - List each name in the Chinese summary. 4 - Output a json object that contains the following keys: chinese_summary, num_names. Use the following format: Text: <text to summarize> Summary: <summary> Translation: <summary translation> Names: <list of names in Chinese summary> Output JSON: <json with summary and num_names> Text: <{text}> """
response = get_completion(prompt) print("\nCompletion for prompt 2:") print(response)
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)
该代码的输出结果为:
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+10x
Total cost: 100x +250x +100,000+10x =360x +100,000
Is the student's solution the same as actual solution just calculated: No Student grade: Incorrect
prompt = f""" Your task is to generate a short summary of a product \ review from an ecommerce site to give feedback to the \ Shipping deparmtment. Summarize the review below in Chinese, delimited by triple backticks, in at most 30 words, and focusing on any aspects \ that mention shipping and delivery of the product. Review: '''{prod_review}''' """
prompt = f""" Your task is to extract relevant information from \ a product review from an ecommerce site to give \ feedback to the Shipping department. The extracted result \ should be translated into Chinese. From the review below delimited by triple quotes \ extract the information relevant to shipping and \ delivery. Limit to 30 words. Review: '''{prod_review}''' """
prompt = f""" What is the sentiment of the following product review, which is delimited with triple backticks? Give your answer as a single word, either "positive" \ or "negative". Review text: '''{lamp_review}''' """
response = get_completion(prompt) print(response)
识别多种情感
prompt = f""" Identify a list of emotions that the writer of the \ following review is expressing. Include no more than \ five items in the list. Format your answer as a list of \ lower-case words separated by commas. Review text: '''{lamp_review}''' """
response = get_completion(prompt) print(response)
识别特定情感
prompt = f""" Is the writer of the following review expressing anger?\ The review is delimited with triple backticks. \ Give your answer as either yes or no. Review text: '''{lamp_review}''' """ response = get_completion(prompt) print(response)
信息提取
我们可以通过不同的 Prompt 指导 ChatGPT 提取不同维度的信息,例如:
提取标签(实体)信息
prompt = f""" Identify the following items from the review text: - Item purchased by reviewer - Company that made the item The review is delimited with triple backticks. \ Format your response as a JSON object with \ "Item" and "Brand" as the keys. If the information isn't present, use "unknown" \ as the value. Make your response as short as possible. Review text: '''{lamp_review}''' """
response = get_completion(prompt) print(response)
提取主题信息
对于主题提取,ChatGPT 可以直接提取多个主题:
prompt = f""" Determine five topics that are being discussed in the \ following text, which is delimited by triple backticks. Make each item one or two words long. Format your response as a list of items separated by commas. Text sample: '''{story}''' """
prompt = f""" Determine whether each item in the following list of \ topics is a topic in the text below, which is delimited with triple backticks. Give your answer as list with 0 or 1 for each topic.\ List of topics: {", ".join(topic_list)} Text sample: '''{story}''' """
prompt = f""" Translate the following python dictionary from JSON to an HTML \ table with column headers and title: {data_json} """
response = get_completion(prompt) print(response)
文字检查
ChatGPT 还可以用来检查拼写和语法,帮助你改写文字(写文章必备),例如:
普通校对与改写
text = f""" Got this for my daughter for her birthday cuz she keeps taking \ mine from my room. Yes, adults also like pandas too. She takes \ it everywhere with her, and it's super soft and cute. One of the \ ears is a bit lower than the other, and I don't think that was \ designed to be asymmetrical. It's a bit small for what I paid for it \ though. I think there might be other options that are bigger for \ the same price. It arrived a day earlier than expected, so I got \ to play with it myself before I gave it to my daughter. """
prompt = f"proofread and correct this review: ```{text}```" response = get_completion(prompt) print(response)
prompt = f""" proofread and correct this review. Make it more compelling. Ensure it follows APA style guide and targets an advanced reader. Output in markdown format. Text: ```{text}``` """
ChatGPT 可以用来对文字进行扩展(将短文本变成长文本),本节将给出一个使用 ChatGPT 自动回复 email 的示例,并介绍对扩展来说很重要的一个 API 参数:温度(Temperature)
扩展示例
下面的例子给出了基于用户的评价生成自动回复 email 的 Prompt:
prompt = f""" You are a customer service AI assistant. Your task is to send an email reply in Chinese to a valued customer. Given the customer email delimited by ```, \ Generate a reply to thank the customer for their review. If the sentiment is positive or neutral, thank them for \ their review. If the sentiment is negative, apologize and suggest that \ they can reach out to customer service. Make sure to use specific details from the review. Write in a concise and professional tone. Sign the email as `AI customer agent`. Customer review: '''{review}''' Review sentiment: {sentiment} """
response = get_completion(prompt) print(response)
实际上情感可以由 ChatGPT 自动提取,无需指定。此外,为了防止不好的用途,课程中建议明确表明上述内容是由 AI 生成的。
defget_completion_from_messages(messages, model="gpt-3.5-turbo", temperature=0): response = openai.ChatCompletion.create( model=model, messages=messages, temperature=temperature, # this is the degree of randomness of the model's output ) # print(str(response.choices[0].message)) return response.choices[0].message["content"]
基于上述函数,可以手动实现历史消息的传入:
messages = [ {'role':'system', 'content':'You are friendly chatbot.'}, {'role':'user', 'content':'Hi, my name is Isa'}, {'role':'assistant', 'content': "Hi Isa! It's nice to meet you. \ Is there anything I can help you with today?"}, {'role':'user', 'content':'Yes, you can remind me, What is my name?'} ] response = get_completion_from_messages(messages, temperature=1) print(response)
为了实现一个聊天机器人,我们需要支持对于对话历史消息的自动收集,课程中基于 panel 包实现了一个点菜机器人,通过精心设置的 System Prompt 实现了自动化点菜与价格计算,具体的代码此处不作展开。
这里给出一个更加通用的聊天机器人 demo,基于 Streamlit 实现,可以自由设置 System Prompt,同时对于对话历史消息进行了自动收集与上传: