探索AI交互:Python与ChatGPT的完美结合!

04-06 1158阅读 0评论

大家好!我是爱摸鱼的小鸿,人生苦短,我用Python!关注我,收看技术干货。

探索AI交互:Python与ChatGPT的完美结合!,探索AI交互:Python与ChatGPT的完美结合!,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,使用,我们,ChatGPT,第1张
(图片来源网络,侵删)

随着人工智能的迅速发展,AI交互正成为技术领域的一大亮点。在这个过程中,Python编程语言和ChatGPT模型的结合展现出强大的潜力,为创造性、智能的对话系统带来了新的可能性。本文将探讨如何将Python与ChatGPT完美结合,为AI交互带来全新的体验。

作者:Maker陈,本文字数:4.3k,阅读时长≈5分钟

目录

    • 一、ChatGPT简介
    • 二、Python与ChatGPT关系
    • 三、GPT各模型概览
    • 四、ChatGPT使用准备
      • 4.1 注册openAI账号
      • 4.2 获取API-Key
      • 4.3 安装openAI提供的库
      • 五、Python与ChatGPT的融合
      • 六、ChatGPT创造或赋予应用智能
      • 七、作者Info

        一、ChatGPT简介

        ChatGPT是由OpenAI推出的一种基于GPT(Generative Pre-trained Transformer)架构的语言模型,用于生成自然语言文本。GPT是一种预训练模型,意味着它在大规模文本数据上进行了预训练,从而学到了语言的模式和结构。

        ChatGPT在训练时强调了对话数据,以更好地适应对话上下文的生成,被广泛用于各种自然语言处理任务,包括对话系统、内容生成、语言翻译等。

        然而,由于它是基于大量预训练数据的通用模型,它在某些情况下可能会生成不准确或不合适的回复(就是说如果你问的问题它不会,它就会根据以往的经验胡编),因此在特定应用中需要谨慎使用

        二、Python与ChatGPT关系

        在ChatGPT的开发中,Python被用于实现算法、处理数据、构建神经网络模型等方面。同时,深度学习框架,如TensorFlow和PyTorch,也是使用Python编写的,它们为构建和训练神经网络提供了便捷的工具。

        探索AI交互:Python与ChatGPT的完美结合!,探索AI交互:Python与ChatGPT的完美结合!,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,使用,我们,ChatGPT,第2张
        (图片来源网络,侵删)

        因此,可以说ChatGPT的主要实现语言是Python,但在整个系统中可能还涉及其他语言或工具。

        由于ChatGPT的出现,为Python和人工智能带火了一波,未来的很多职业会被AI所取代,只能说还没入坑Python的赶紧冲鸭~

        三、GPT各模型概览

        截止目前为止,openAI为GPT共创造出了9个大模型,我们从最开始的介绍:

        • GPT-3:一组能够理解和生成自然语言的模型,这是最原始的; GPT-base:一组无需遵循指令即可理解并生成自然语言或代码的模型;
        • Moderation:可以检测文本是否敏感或不安全的微调模型; Embeddings:一组组可以将文本转换为数字形式的模型;
        • Whisper:可以将音频转换为文本的模型; TTS:一组可以将文本转换为听起来自然的语音的模型;
        • DALL·E:可以在自然语言提示下生成和编辑图像的模型;
        • GPT-3.5 and GPT-3.5 Turbo:一组改进 GPT-3 的模型,可以理解并生成自然语言或代码;
        • GPT-4 and GPT-4 Turbo:一组改进 GPT-3.5的模型,可以理解并生成自然语言或代码。

          其中GPT-3.5 系列中功能最强大且最具成本效益的型号是gpt-3.5-turbo,而GPT-4是一个大型多模态模型(接受文本或图像输入并输出文本),比之前的任何模型更能准确地解决难题,这是openAI最智能和最大的模型,还可以使用DALL·E模型进行图像生成和编辑,以及高级的数据分析等。

          目前个人的项目和公司的项目也在使用gpt-3.5-turbo完成NLP任务,比较推荐,但也可根据自己的需求来定。

          四、ChatGPT使用准备

          4.1 注册openAI账号

          要想使用ChatGPT,首先要注册一个openAI的账号,官网如下:

          探索AI交互:Python与ChatGPT的完美结合!,探索AI交互:Python与ChatGPT的完美结合!,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,使用,我们,ChatGPT,第3张
          (图片来源网络,侵删)
          https://openai.com
          

          然后学会科学上网,拥有一个能够接收openAI验证的国际邮箱,比如Gmail、Outlook等,其次要有一个接收openAI短信验证码的国外手机号,没有的话可以购买用于一次性接受短信服务的虚拟手机号也行

          4.2 获取API-Key

          注册成功之后,这一步就比较简单了,直接自动生成属于你的API-Key:

          https://platform.openai.com/api-keys
          

          获取完成

          4.3 安装openAI提供的库

          pip install openai
          

          okay,打开AI世界的钥匙已经获取,接下来就是如何使用了。

          五、Python与ChatGPT的融合

          此处以gpt-3.5-turbo模型为例,向大家展示ChatGPT强大的自然语言处理能力,一个简单的示例代码如下:

          import openai
          openai.api_key = 'your api-key'
          response = openai.ChatCompletion.create(
              model="gpt-3.5-turbo",
              messages=[
                  {"role": "system", "content": "You are a helpful assistant."},
                  {"role": "user", "content": "Is Python the best programming language in the world?"}
              ]
          )
          print(response)
          

          这里传递了你的api-key之后,直接就可以调用相关的接口创建对话,上面的两个参数是必需的,model传入你要使用的模型,messages传入的是一个字典列表,这里我们作为user角色询问ChatGPT:Python是世界上最好的编程语言?看一下输出:

          {
            "id": "chatcmpl-8LWai4W3jE9yHnmgPn8KcwBxvq7mu",
            "object": "chat.completion",
            "created": 1700140860,
            "model": "gpt-3.5-turbo-0613",
            "choices": [
              {
                "index": 0,
                "message": {
                  "role": "assistant",
                  "content": "As an AI assistant, I don't have personal opinions. However, Python is a highly versatile and powerful programming language that is widely used in various fields such as web development, data analysis, artificial intelligence, and more. It is known for its simplicity, readability, and vast range of libraries and frameworks. However, the \"best\" programming language ultimately depends on the specific needs and requirements of your project. There is no definitive answer as different programming languages excel in different areas."
                },
                "finish_reason": "stop"
              }
            ],
            "usage": {
              "prompt_tokens": 27,
              "completion_tokens": 95,
              "total_tokens": 122
            }
          }
          

          返回的是一个JSON格式,若我们只要ChatGPT的回复内容,其他字段不需要,可以改一下输出格式:

          print(response['choices'][0]['message']['content'])
          

          这样最终返回的就只是ChatGPT的回复内容了:

          As an AI assistant, I don't have personal opinions. However, Python is a highly versatile and powerful programming language that is widely used in various fields such as web development, data analysis, artificial intelligence, and more. It is known for its simplicity, readability, and vast range of libraries and frameworks. However, the \"best\" programming language ultimately depends on the specific needs and requirements of your project. There is no definitive answer as different programming languages excel in different areas.
          

          在上面的代码,我们会注意到messages字典列表里有个role字段,在ChatGPT会话中通常有三种角色:system、user、assistant,你可能也注意到了在上面的回话中返回的role是assistant,表示助理

          因为ChatGPT是生成式预训练模型,它对过去的对话没有记忆能力,我们这时需要给出一些提示,让它能根据上下文对话能更准确的回复,例如下面的例子:

          import openai
          openai.api_key = 'your api-key'
          response = openai.ChatCompletion.create(
              model="gpt-3.5-turbo",
              messages=[
                  {"role": "system", "content": "You are a helpful assistant."},
                  {"role": "user", "content": "Is Python the best programming language in the world?"},
                  {"role": "assistant", "content": "As an AI assistant, I don't have personal opinions. However, Python is a highly versatile and powerful programming language that is widely used in various fields such as web development, data analysis, artificial intelligence, and more. It is known for its simplicity, readability, and vast range of libraries and frameworks. However, the \"best\" programming language ultimately depends on the specific needs and requirements of your project. There is no definitive answer as different programming languages excel in different areas."},
                  {"role": "user", "content": "What are the advantages of the python programming language?"}
              ]
          )
          message = response['choices'][0]['message']['content']
          print(message)
          

          我们先给system这个role给出一些提示:你是一个有用的助理;然后将我们上次对话的内容传给user和assistant角色,最后让它根据上次的对话内容,我们再问它一个问题:Python编程语言有什么优势?


          回复如下:

          Python has several advantages that contribute to its popularity:
          1. Readability and simplicity: Python emphasizes code readability with its clean and well-structured syntax, making it easier to understand and write code quickly.
          2. Broad range of libraries and frameworks: Python has a vast ecosystem with numerous built-in libraries and frameworks that simplify development tasks. These libraries offer functionalities for various purposes, such as data analysis (NumPy, Pandas), web development (Django, Flask), and scientific computing (SciPy).
          3. Cross-platform compatibility: Python is compatible with most major operating systems, making it easy to write code that can run on multiple platforms without modifications.
          4. Productivity and efficiency: Python's simplicity and extensive libraries enable faster development and reduce the time spent on repetitive tasks. It has a shorter learning curve, which makes it an efficient language to pick up for beginners.
          5. Community and support: Python has a large and active community of developers who contribute to its growth and provide support. This means you can find a wealth of resources, tutorials, and help when needed.
          6. Integration capabilities: Python easily integrates with other programming languages like C++, Java, and C#, allowing developers to leverage existing codebases and libraries.
          7. Versatility: Python can be used for various purposes, including web development, data analysis, machine learning, scripting, and automation. It is a versatile language suitable for a wide range of applications.
          These advantages make Python a preferred choice for many developers and organizations.
          

          中文翻译如下:

          Python 的流行有以下几个优点:
          1.可读性和简单性:Python以其干净、结构良好的语法强调代码的可读性,使人更容易理解和快速编写代码。
          2. 广泛的库和框架:Python拥有庞大的生态系统,拥有众多内置的库和框架,可以简化开发任务。 这些库提供用于各种目的的功能,例如数据分析(NumPy、Pandas)、Web 开发(Django、Flask)和科学计算(SciPy)。
          3.跨平台兼容性:Python与大多数主要操作系统兼容,可以轻松编写无需修改即可在多个平台上运行的代码。
          4.生产力和效率:Python的简单性和广泛的库可以加快开发速度并减少重复任务所花费的时间。 它的学习曲线较短,这使其成为初学者可以有效学习的语言。
          5. 社区和支持:Python 拥有一个庞大且活跃的开发者社区,他们为其发展做出贡献并提供支持。 这意味着您可以在需要时找到丰富的资源、教程和帮助。
          6. 集成能力:Python 可以轻松地与其他编程语言(如 C++、Java 和 C#)集成,从而允许开发人员利用现有的代码库和库。
          7. 多功能性:Python 可用于多种用途,包括 Web 开发、数据分析、机器学习、脚本编写和自动化。 它是一种适用于多种应用的多功能语言。
          这些优点使 Python 成为许多开发人员和组织的首选。
          

          如果我们不需要编程的方式嵌入到项目里面,只是日常的答复,提一些问题可以在openAI官网上直接可视化进行操作

          如果是与assistant聊天,需要手动创建一个小助理:

          https://platform.openai.com/assistants
          

          创建之后还可以手动选择模型,比如gpt-3.5-turbo-16k、gpt-3.5-turbo-1106等

          直接与ChatGPT聊天:

          https://chat.openai.com
          

          需要注意的是ChatGPT的请求速率是有限制的,需要根据自己的需求调整

          六、ChatGPT创造或赋予应用智能

          ChatGPT可以用于创造各种应用,为应用赋予自然语言处理和生成的能力。以下是一些可能的应用场景:

          • 聊天机器人:利用ChatGPT可以构建智能聊天机器人,与用户进行自然而流畅的对话。这种应用可以用于客户服务、虚拟助手等场景。

          • 智能助手:ChatGPT可以用于创建个人助手,帮助用户回答问题、提供建议,甚至执行一些简单的任务,如设置提醒、查询信息等。

          • 教育应用:将ChatGPT集成到教育应用中,帮助学生解答问题、提供解释,提供个性化的学习支持。

          • 创意写作助手:ChatGPT可以用于生成创意性的文本,为写作者提供灵感、扩展想法或生成故事情节。

          • 内容生成:ChatGPT可用于自动生成各种类型的内容,包括文章、新闻摘要、评论等。

          • 语言翻译:ChatGPT可以用于构建实时语言翻译工具,帮助用户进行跨语言交流。

          • 编程助手:ChatGPT可以用于协助程序员编写代码,回答技术问题,提供编程建议等。

          • 游戏中的虚拟角色:将ChatGPT整合到游戏中,创造更智能、交互性更强的虚拟角色。

            当然,不仅是ChatGPT模型,其他GPT模型也可应用到各领域中

            在使用ChatGPT时,需要注意它是一个生成式预训练模型,可能会产生不准确或不符合预期的输出。因此,在特定应用中,需要谨慎处理模型的输出,确保其符合应用场景的要求。此外,对于一些关键应用领域,可能需要额外的模型调优或结合其他技术来提高性能。

            七、作者Info

            Author:小鸿的摸鱼日常,Goal:让编程更有趣!

            专注于算法、爬虫,Web开发,数据分析、自然语言处理,AI等,期待你的关注,让我们一起成长、一起Coding!

            版权说明:本文禁止抄袭、转载,侵权必究!


免责声明
本网站所收集的部分公开资料来源于AI生成和互联网,转载的目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。
文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

发表评论

快捷回复: 表情:
评论列表 (暂无评论,1158人围观)

还没有评论,来说两句吧...

目录[+]