当前位置: 首页 > news >正文

OpenAI API continuing conversation in a dialogue

题意:在对话中继续使用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 = mykeyprompt= "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 openaiclass ChatApp:def __init__(self):# Setting the API key to use the OpenAI APIopenai.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.lryc.cn/news/414250.html

相关文章:

  • 6.前端怎么做一个验证码和JWT,使用mockjs模拟后端
  • Python酷库之旅-第三方库Pandas(064)
  • MATLAB基础操作(二)
  • win10 繁体简体字切换
  • R语言统计分析——描述性统计
  • 为什么需要合成数据进行机器学习
  • 传统CS网络的新生——基于2G网络的远程灌溉实现
  • EasyAR_稀疏空间图
  • 设计模式 - Singleton pattern 单例模式
  • 显示学习5(基于树莓派Pico) -- 彩色LCD的驱动
  • ros vscode配置gdb调试
  • C 环境设置
  • Linux-ubuntu操作系统装机步骤
  • 马尔科夫毯:信息屏障与状态独立性的守护者
  • Pandas的30个高频函数使用介绍
  • 1. protobuf学习
  • Java面试题:SpringBean的生命周期
  • 50 IRF检测MAD-BFD
  • SpringSecurity-1(认证和授权+SpringSecurity入门案例+自定义认证+数据库认证)
  • Java高级
  • python实现图像分割算法3
  • 解密XXE漏洞:原理剖析、复现与代码审计实战
  • Spring Boot集成Resilience4J实现限流/重试/隔离
  • 谷粒商城实战笔记-119~121-全文检索-ElasticSearch-mapping
  • Java 并发编程:Java 线程池的介绍与使用
  • ubuntu上安装HBase伪分布式-2024年08月04日
  • Mojo的特征与参数(参数化部分)详解
  • C++数组、vector求最大值最小值及其下标
  • 内网安全:多种横向移动方式
  • 搭建 STM32 网关服务器的全流程:集成嵌入式 C++、TCP/IP 通信、Flash 存储及 JWT 认证(含代码示例)