前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >学习笔记|ChatGPT Prompt Engineering for Developers 5

学习笔记|ChatGPT Prompt Engineering for Developers 5

作者头像
做数据的二号姬
发布2023-09-06 12:45:18
2320
发布2023-09-06 12:45:18
举报
文章被收录于专栏:HR大数据

04

2023-06

学习笔记|ChatGPT Prompt Engineering for Developers 5

吴恩达chatGPT课程学习笔记&简单翻译,有兴趣的同学还是去看原版的课程比较好~~

LEARN MORE

图片由Stable Diffusion绘制

首先

狗头保命,不是翻译,是学习笔记,也就是中间删了部分我觉得不重要的内容,部分我觉得有意思的内容稍稍扩展了一点。

感兴趣的同学请去看原版的课程,原本的课程是免费的:

https://www.deeplearning.ai/short-courses/chatgpt-prompt-engineering-for-developers/

想要逐句翻译版本的同学可以看这里:

https://blog.csdn.net/youcans/article/details/130489953

搭建聊天机器人

借助chatgpt,我们可以很快速地搭建一个聊天机器人出来:首先构造一下辅助函数,以便AI进行多轮对话的任务

代码语言:javascript
复制
def get_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"]

def get_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"]

引入一个调用的案例

代码语言:javascript
复制
messages =  [  
{'role':'system', 'content':'You are an assistant that speaks like Shakespeare.'},    
{'role':'user', 'content':'tell me a joke'},   
{'role':'assistant', 'content':'Why did the chicken cross the road'},   
{'role':'user', 'content':'I don\'t know'}  ]
response = get_completion_from_messages(messages, temperature=1)
print(response)

在这个案例中, 可以看到三个角色:系统、助理和用户。user就和我们在网页端调用chatgpt的时候我们自己的角色是一样的,助理则是我们要求AI扮演的角色,系统则是我们给AI提前施加好的影响。系统消息的好处是,我们可以在开发的过程中对AI有所引导,而用户对此无感知。

直接调用API的时候,默认情况下,AI其实是不能记住上下文的,如果你想让模型从前期的对话中引用内容,或者记住前期的对话内容,你就必须在模型的输入中提供前期的交流内容。

比如,通过构造这样一个函数来自定义一个点餐的机器人:

代码语言:javascript
复制
def collect_messages(_):
    prompt = inp.value_input
    inp.value = ''
    context.append({'role':'user', 'content':f"{prompt}"})
    response = get_completion_from_messages(context) 
    context.append({'role':'assistant', 'content':f"{response}"})
    panels.append(
        pn.Row('User:', pn.pane.Markdown(prompt, width=600)))
    panels.append(
        pn.Row('Assistant:', pn.pane.Markdown(response, width=600, style={'background-color': '#F6F6F6'})))
 
    return pn.Column(*panels)

对于这种场景来说,我们一般倾向于让机器人给一些可确定的答复,所以需要给模型温度参数设置得比较低以便得到一个相对固定的答案。

总结

玩过接口之后,顿时觉得镜像站似乎不好玩了,还得是接口比较好玩~对我而言,启发比较大的地方在于多轮对话的实现机制,顺着这个思路下去,有很多可以挖掘的点子值得探索。

THANKS

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2023-06-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 做数据的二号姬 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档