OpenAI API continuing conversation in a dialogue

news2024/11/23 3:24:14

题意:在对话中继续使用OpenAI API进行对话

问题背景:

I am playing around with the openAI API and I am trying to continue a conversation. For example:

我正在尝试使用OpenAI API,并试图继续一段对话。例如:

import openai
openai.api_key = mykey
    
prompt= "write me a haiku"
    
response = openai.Completion.create(engine="text-davinci-001",
                                    prompt=prompt,
                                    max_tokens=50)
print(response)

This produces a Haiku in the following format:        这会产生一首以下格式的俳句:

{
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "logprobs": null,
      "text": "\n\n\n\nThis world is\nfull of wonders\nSo much to see and do"
    }
  ],
  "created": 1670379922,
  "id": "cmpl-6KePalYQFhm1cXmwOOJdyKiygSMUq",
  "model": "text-davinci-001",
  "object": "text_completion",
  "usage": {
    "completion_tokens": 17,
    "prompt_tokens": 5,
    "total_tokens": 22
  }
}

Which is great. However, what if I now want to ask to "write me another"? If I use the the openAI playground chat or chatGPT, I am able to continue the conversation. I would like to do this via my python script. I notice I receive an id in response. Can I use this somehow to continue my conversation?

这很棒。但是,如果我现在想要求“再写一首”呢?如果我在OpenAI Playground聊天室或ChatGPT中,我能够继续对话。我想通过我的Python脚本来实现这一点。我注意到我在响应中收到了一个id。我能否以某种方式使用这个id来继续我的对话?

问题解决:

OpenAI Now officially released the "gpt-3.5-turbo" model. Here's some sample code:https://github.com/stancsz/chatgpt

OpenAI 现在已正式发布了“gpt-3.5-turbo”模型。以下是一些示例代码:

and here's the official docs.

这是官方文档

import os
import openai


class ChatApp:
    def __init__(self):
        # Setting the API key to use the OpenAI API
        openai.api_key = os.getenv("OPENAI_API_KEY")
        self.messages = [
            {"role": "system", "content": "You are a coding tutor bot to help user write and optimize python code."},
        ]

    def chat(self, message):
        self.messages.append({"role": "user", "content": message})
        response = openai.ChatCompletion.create(
            model="gpt-3.5-turbo",
            messages=self.messages
        )
        self.messages.append({"role": "assistant", "content": response["choices"][0]["message"].content})
        return response["choices"][0]["message"]

Test Log        测试日志

how are you? {
  "content": "I'm just a computer program, so I don't have feelings, but I'm functioning properly and ready to help you with any coding questions you have!",
  "role": "assistant"
}
I want to know how to learn python quickly? {
  "content": "There are several ways to learn Python quickly. Here are some tips that may help:\n\n1. Start with the basics: Before jumping into complex topics, ensure that you have a strong foundation in the basics of Python programming such as variables, data types, variable assignment, loops, functions, etc. You can find many resources online to learn these basics.\n\n2. Practice coding regularly: Regular practice is the key to learn Python quickly. You can start by solving small coding challenges or writing small programs on a daily basis.\n\n3. Use interactive platforms: There are several interactive platforms available online, such as Codecademy, HackerRank, and DataCamp, that can help you learn and practice Python.\n\n4. Learn important libraries: Python has a wide range of powerful libraries that can save you time and effort in programming. Examples include NumPy, Pandas, Matplotlib, Scikit-learn, etc. Learning these libraries can help you accelerate your learning and improve your productivity.\n\n5. Participate in coding communities: Participating in online coding communities can help you learn more quickly by interacting with other developers and getting feedback on your code. You can join forums or groups on social media platforms like Reddit and Facebook.\n\nRemember that learning any programming language takes time and effort. However, with consistent practice and a commitment to improving your skills, you can become proficient in Python in a relatively short amount of time.",
  "role": "assistant"
}
What are the best resources for this? {
  "content": "There are many resources available for learning Python quickly. Here are some of the best ones:\n\n1. Coursera: Coursera offers a wide range of Python courses that are designed for beginners and those with some programming experience. These courses cover everything from basic programming concepts to advanced topics like machine learning and data analysis.\n\n2. Codecademy: Codecademy is an interactive learning platform that offers free and paid Python courses. It provides hands-on coding experience and covers the essential Python skills for beginners.\n\n3. Udemy: Udemy is an online learning platform that offers a wide variety of Python courses. It provides both free and paid courses, covering various Python skills and applications, from web development to data science.\n\n4. Automate the Boring Stuff with Python: This is a free online book that covers the basics of Python programming and includes practical examples and projects that you can work on, such as web scraping and automating tasks.\n\n5. Python for Everybody: Python for Everybody is a free course offered by the University of Michigan. It covers the basics of Python programming and is suitable for beginners with no prior programming experience.\n\n6. Python documentation: The official Python documentation is an excellent resource for learning Python. It provides clear and detailed explanations of Python concepts and syntax, as well as examples and code snippets.\n\n7. Python community: The Python community is very active and supportive. You can find help, tutorials, and examples online through various forums, social media groups, and developer communities like Stack Overflow and GitHub.\n\nThese resources can help you learn Python quickly and efficiently, but remember that consistent practice and hands-on experience are key to becoming proficient in Python.",
  "role": "assistant"
}
what should I learn first? {
  "content": "If you are new to programming, you should start with the basic concepts of Python. The following are some of the key concepts to learn first:\n\n1. Variables and Data types: Learn how to create variables and assign values to them. Python has different types of data, such as numbers, strings, and lists.\n\n2. Control Structures: Learn how to control program flow using if/else statements, loops (for and while loops), and basic logic concepts.\n\n3. Functions: Learn how to create functions and pass arguments to them. Functions allow you to segment your code into modular pieces that can be reused throughout your program.\n\n4. Input and Output: Learn how to accept input from users and output results to the screen or file.\n\n5. File handling: Learn how to read/write data from/to files.\n\n6. Libraries: Learn about built-in Python libraries, such as math and random, and how to import external libraries for more advanced functionality.\n\nOnce you have mastered the basics, you can move on to more advanced topics like object-oriented programming, web development, data science, and machine learning.",
  "role": "assistant"
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1973849.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

<数据集>航拍车辆识别数据集<目标检测>

数据集格式:VOCYOLO格式 图片数量:6787张 标注数量(xml文件个数):6787 标注数量(txt文件个数):6787 标注类别数:3 标注类别名称:[Car, Truck, Bus] 序号类别名称图片数框数1Car6494791332Truck250576…

【从零开始一步步学习VSOA开发】快速体验SylixOS

快速体验SylixOS 安装完毕RealEvo-IDE 后,同时也安装了RealEvo-Simulator。RealEvo-Simulator 是一个虚拟运行环境,可以模拟各种体系结构并在其上运行 SylixOS。相比于物理板卡,在 RealEvo-Simulator 进行运行调测更加的方便快捷且成本低廉。…

2024年中国数据中台行业研究报告

数据中台丨研究报告 核心摘要: 数据中台是企业数字化建设的重要构成,其通过整合企业基础设施和数据能力,实现数据资产化和服务复用,降低运营成本,支撑业务创新。受宏观经济影响,部分企业减少了对数据中台等…

java中InputStream, OutputStream 的用法

java针对文件的操作有两种1.文件系统操作 File类指定的路径可以使一个不存在的路径。2,文件内容操作,流对象。 流也分为两种1)字节流(二进制文件):以字节为基本单位读写的使用InputStream()和Ou…

什么是kafka的重平衡机制?

背景 kafka重平衡的主要发生在消费者端,重平衡的目的,主要是为了均衡消费者消费kafka的消息而设计的,对于动态加入消费者,减少消费者,以及消息分区变化这些场景中,若不设计消费者重平衡,容易出…

第十五章 数据管理成熟度评估

定义: 能力成熟度评估: 是是一种基于能力成熟度模型框架的能力提升方案,描述了数据管理能力初始状态发展到最优化的过程。 数据管理成熟度评估:(Data Management Maturity Assessment, DMMA)可用于全面评估…

MySQL——数据表的基本操作(一)创建数据表

数据库创建成功后,就需要创建数据表。所谓创建数据表指的是在已存在的数据库中建立新表。需要注意的是,在操作数据表之前,应该使用 “ USE 数据库名 ” 指定操作是在哪个数据库中进行,否则会抛出 “ No database selected ” 错误。创建数据表…

大路灯护眼灯有必要买吗?五款护眼大路灯推荐

大路灯护眼灯有必要买吗?许多消费者对护眼大路灯的了解不够,总是被不专业产品“耍”得团团转。就比如市面上很多声称用了眼睛就不近视的产品,实际上它们毫无专业技术沉淀,还疏于调校光线稳定性、光线均匀度等上百项核心参数&#…

【海贼王航海日志:前端技术探索】HTML你学会了吗?(二)

目录 1 -> HTML常见标签 1.1 -> 表格标签 1.1.1 -> 基本使用 1.1.2 -> 合并单元格 1.2 -> 列表标签 1.3 -> 表单标签 1.3.1 -> form标签 1.3.2 -> input标签 1.4 -> label标签 1.5 -> select标签 1.6 -> textarea标签 1.7 -> …

C++类和对象——中

1. 类的默认成员函数 默认成员函数就是⽤⼾没有显式实现,编译器会⾃动⽣成的成员函数称为默认成员函数。⼀个类,我们不写的情况下编译器会默认⽣成以下6个默认成员函数,需要注意的是这6个中最重要的是前4个,最后两个取地址重载不…

数字孪生赋能智慧城市大脑智建设方案(可编辑65页PPT)

引言:随着科技的飞速发展,智慧城市的建设已成为全球城市发展的新趋势。数字孪生技术作为其中的关键技术之一,正逐步赋能智慧城市大脑的建设,推动城市治理从数字化向智能化、智慧化转型升级。本方案旨在简要介绍数字孪生赋能智慧城…

“七人拼团:共赢与互助的奖励之旅“

在七人拼团策略中,其精心构建的奖励体系无疑是吸引并激励参与者踊跃参与的核心驱动力。接下来,我们将深入剖析该策略中三种核心奖励类型——直推奖、滑落奖与团队奖,并探讨它们如何促进成员间的互助合作机制。 奖励体系深度剖析 直推奖&…

python爬虫实践

两个python程序的小实验(附带源码) 题目1 爬取http://www.gaosan.com/gaokao/196075.html 中国大学排名,并输出。提示:使用requests库获取页面的基本操作获取该页面,运用BeautifulSoup解析该页面绑定对象soup&#x…

股票多因子模型实战之因子行业中性化(附python代码)

原创文章第607篇,专注“AI量化投资、世界运行的规律、个人成长与财富自由"。 今天开始,咱们从ETF、可转债正式转到大家最熟悉、最常见的股票市场。 而且切入的方向,仍然是——“多因子模型”。 01 什么是因子? 因子是什么…

qml教程:qml视频播放组件介绍

更多qml教程,请参考QML入门进阶教程专栏:https://mingshiqiang.blog.csdn.net/category_9951228_2.html 文章目录 MediaPlayerVideoOutput视频播放代码示例增加视频效果图片二值化处理视频帧本篇博客介绍qml如何播放视频,以及视频特效处理。 本篇博客提供的代码全部通过Qt6.…

64位Office API声明语句第121讲(终了)

跟我学VBA,我这里专注VBA, 授人以渔。我98年开始,从源码接触VBA已经20余年了,随着年龄的增长,越来越觉得有必要把这项技能传递给需要这项技术的职场人员。希望职场和数据打交道的朋友,都来学习VBA,利用VBA,起码可以提高…

【Mind+】掌控板入门教程02 趣味相框

光影重叠,时间和回忆定格在在一张张相片里。人们通常会用相框把相片装裱起来,展示在家中或者工作的地方,来回味往日的美好时光。但是一个相框只能放一张照片,怎么才能让它展示不同的照片呢?正好此时我们的手边有一块掌…

Python 爬虫项目实战(一):破解网易云 VIP 免费下载付费歌曲

前言 网络爬虫(Web Crawler),也称为网页蜘蛛(Web Spider)或网页机器人(Web Bot),是一种按照既定规则自动浏览网络并提取信息的程序。爬虫的主要用途包括数据采集、网络索引、内容抓…

World of Warcraft [RETAIL] UI Setting

/*测试魔兽世界正式服,界面UI编辑器,UI布局文本 World of Warcraft [RETAIL] UI Setting开头的:1 39 某种类型 : 序号(默认-1,有多个从0开始计数0,1,2,3 未知类型 未知类型 未知类型 控件类型&#xff1…

【中项】系统集成项目管理工程师-第9章 项目管理概论-9.8项目管理知识领域

前言:系统集成项目管理工程师专业,现分享一些教材知识点。觉得文章还不错的喜欢点赞收藏的同时帮忙点点关注。 软考同样是国家人社部和工信部组织的国家级考试,全称为“全国计算机与软件专业技术资格(水平)考试”&…