openai模型(二) -- 基本库

openai模型学习笔记(二)

  • 了解openai库的基本调用并运行
  • 思维链
  • 更好的使用chatgpt和prompt优化

学习内容:

一:
1.past1

// past1
import openai
import os
openai.api_key = os.getenv('OPENAI_API_KEY')
def get_completion(prompt,model="gpt-3.5-turbo"):
    messages = [{'role': "user","content": prompt}]
    response = openai.ChatCompletion.create(
        model=model,
        messages=messages,
        temperature=0,
    )
    return response.choices[0].message["content"]
instruction = "提取输入信息中的收件人姓名、地址和电话"
input_text = "帮我寄给王先生,地址是北京市朝阳区亮马桥外交办公大楼,电话13012345678"
prompt = f"{instruction} 输入信息:{input_text}"
response = get_completion(prompt)
print(response)

2.past2
给prompt加入了指示词,输入和输出的相关要求

// past2
import openai
import os
openai.api_key = os.getenv('OPENAI_API_KEY')
def get_completion(prompt,model="gpt-3.5-turbo"):
    messages = [{'role': "user","content": prompt}]
    response = openai.ChatCompletion.create(
        model=model,
        messages=messages,
        temperature=0,
    )
    return response.choices[0].message["content"]
instruction = "提取输入信息中的收件人姓名、地址和电话"
input_text = "帮我寄给王先生,地址是北京市朝阳区亮马桥外交办公大楼,电话13012345678"
output_format = "以JSON格式输出"
prompt = f"{instruction} 输入信息:{input_text}"
response = get_completion(prompt)
print(response)

3.past3
再次加入对话历史

// past3
import openai
import os
openai.api_key = os.getenv('OPENAI_API_KEY')
def get_completion(prompt,model="gpt-4"):
    messages = [{'role': "user","content": prompt}]
    response = openai.ChatCompletion.create(
        model=model,
        messages=messages,
        temperature=0,
    )
    return response.choices[0].message["content"]
instruction = "d对话场景是针对话费套餐信息的回答。根据给定的对话历史和最后一轮的用户输入,判断用户的话题是否与场景相关。"
context = ("客服:有什么可以帮您?"
           "用户:查一下我的套餐还剩多少钱"
           "客服:您好,您当前余额为0元。请问是否需要帮您充值?")
input_text = "不用"
output_format = "以JSON格式输出。'relevance'字段表示话题相关性,取值为bool类型。"
prompt = f"{instruction} 之前的对话历史:{context} 用户输入:{input_text} {output_format}"
response = get_completion(prompt)
print(response)

4.past4
为更精准的回答我们的要求,给与例子

// past4
import openai
import os
openai.api_key = os.getenv('OPENAI_API_KEY')
def get_completion(prompt,model="gpt-4"):
    messages = [{'role': "user","content": prompt}]
    response = openai.ChatCompletion.create(
        model=model,
        messages=messages,
        temperature=0,
    )
    return response.choices[0].message["content"]
instruction = ("从用户输出中抽取用户问询的城市"
               "城市别称替换成标准名称")
examples = ("北京天气怎么样:北京市"
            "三亚冷吗:三亚市"
            "江城下雨嘛:武汉市")
input_text = "羊城今天穿毛衣热吗"
output_format = "只输出城市名"
prompt_no_example = f"{instruction} 例如:{examples} {input_text} {output_format}"
response = get_completion(prompt_no_example)
print(response)

二.思维链
一步一步的进行分析,不容易出错(analyze the task step by step)

// past5
import openai
import os
openai.api_key = os.getenv('OPENAI_API_KEY')
def get_completion(prompt,model="gpt-4"):
    messages = [{'role': "user","content": prompt}]
    response = openai.ChatCompletion.create(
        model=model,
        messages=messages,
        temperature=0,
    )
    return response.choices[0].message["content"]
instruction = ("根据输入:analyze the task step by step"
               "计算下一次任务运行的日期,生成Linux shell 命令")
context = "当前日期:2023-07-08 星期六"
input_text = "每隔五天重启httpd服务,遇到星期四延后一天执行"
output_format = "最终输入JSON,‘command’代表任务指令,‘date’代表任务执行日期"
prompt= f"{context} 输入 {input_text} {instruction}  {output_format}"
response = get_completion(prompt)
print(response)

对要完成的项目,提高回答的准确,prompt提示:

一步一步的进行analyze the task step by step
不要说一些非口语化,像个人交流no comments,no ackonwledges
让chatgpt生成问题

  1. I want you to become my Expert prompt Creator, Your goal is to help me craft the best possible promptfor my needs, The prompt you provide should be written from the perspective of me making the request toChatGpT, Consider in your prompt creation that this prompt will be entered into an interface for ChatGpTThe process is as follows:1. You will generate the following sections:
    Prompt:fprovide the best possible prompt according to my request)
    Critique: {provide a concise paragraph on how to improve the prompt. Be very critical in your response}
    Questions :
    fask any questions pertaining to what additional information is needed from me toimprove the prompt(max of 3). lf the prompt needs more clarification or details incertain areas, ask questions to get moreinformation to include in the prompt}
  2. I will provide my answers to your response which you will then incorporate into your next responseusing the same format, We will continue this iterative process with me providing additional informationto you and you updating the prompt until the prompt is perfected.Remember, the prompt we are creatingshould be written from the perspective of me making a request to ChatGPT, Think carefully and use yourimagination to create an amazing prompt for me.You’re first response should only be a ereetine to the user and to ask what the promot should be about

更多详细见 https://www.chainbrainai.com/

  • 17
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值