GVKun编程网logo

如何在chatGPT Python API中启用上下文管理?

1

如果您对如何在chatGPTPythonAPI中启用上下文管理?感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解如何在chatGPTPythonAPI中启用上下文管理?的各种细节,此外还有关于C

如果您对如何在chatGPT Python API中启用上下文管理?感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解如何在chatGPT Python API中启用上下文管理?的各种细节,此外还有关于CMake 不断从 cygwin python 中获取 Python,如何从 Windows 安装的 Python 中获取、Core Python | 2 - Core Python: Getting Started | 2.5 - Modularity | 2.5.5 - The Python Execution Mod、Error: Can‘t find Python executable “python“, you can set the PYTHON env variable、ERROR: Command "python setup.py egg_info" python-nss的实用技巧。

本文目录一览:

如何在chatGPT Python API中启用上下文管理?

如何在chatGPT Python API中启用上下文管理?

官方案例:

# Note: you need to be using OpenAI Python v0.27.0 for the code below to work
import openai
 
openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Who won the world series in 2020?"},
        {"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
        {"role": "user", "content": "Where was it played?"}
    ]
)
登录后复制

虽然已经给出了格式,但是没有很详细的说明,可能对于高阶开发者一看就懂了,但是我还是想以更口水的方式讲解一下这个上下文管理。

先看一下我一个简单的代码(还没有启用上下文管理):

import openai
 
openai.api_key = "你的sk-key"
 
msg = [{"role": "user", "content": "你好chatGPT"}]
 
# 结构化数据并进行提交
completion = openai.ChatCompletion.create(
                # max_tokens = inf # 默认inf 最大令牌数
                presence_penalty = 1, # 惩罚机制,-2.0 到 2.0之间,默认0,数值越小提交的重复令牌数越多,从而能更清楚文本意思
                frequency_penalty = 1, # 意义和值基本同上,默认0,主要为频率
                temperature = 1.0,  # 温度 0-2之间,默认1  调整回复的精确度使用
                n = 1,  # 默认条数1
                user = ids,    # 用户ID,用于机器人区分不同用户避免多用户时出现混淆
                model = "gpt-3.5-turbo",    # 这里注意openai官方有很多个模型
                messages = msg
            )
 
value = completion.choices[0].message.content    # chatGPT返回的数据
登录后复制

这是一个最基本的结构,其中参数model和messages是必须要有的两个形参。

加入上下文管理的代码:

立即学习“Python免费学习笔记(深入)”;

import openai
 
openai.api_key = "你的sk-key"
 
msg = [{"role": "system", "content": "你的名字叫玖河AI,你是一个插件,你的开发者是玖河."},
        {"role": "user", "content": "你好chatGPT"},
        {"role": "assistant", "content": "您好,有什么需要我帮忙的问题吗?"},
        {"role": "user", "content": "我的名字叫高启强,我的妹妹叫高启兰,我们是兄妹关系。记住了吗?"}
        {"role": "assistant", "content": "好的,您叫高启强,您的妹妹叫高启兰,是亲兄妹关系。谢谢您提供信息让我更了解你们~"},
        {"role": "user", "content": "你现在在哪里?"},
        {"role": "assistant", "content": "作为一款智能Ai助手,我并没有实际的位置。我只是在云端中运行,在等待用户输入指令时保持睡眠状态。"},
        {"role": "user", "content": "我的妹妹是谁?"},
        {"role": "assistant", "content": "您之前告诉我,您的妹妹叫高启兰。"},
        {"role": "user", "content": "你的名字叫什么?"},
        {"role": "assistant", "content": "我的名字叫玖河AI是一个叫玖河的开发者开发的插件"}
        ]
 
# 结构化数据并进行提交
completion = openai.ChatCompletion.create(
                # max_tokens = inf # 默认inf 最大令牌数
                presence_penalty = 1, # 惩罚机制,-2.0 到 2.0之间,默认0,数值越小提交的重复令牌数越多,从而能更清楚文本意思
                frequency_penalty = 1, # 意义和值基本同上,默认0,主要为频率
                temperature = 1.0,  # 温度 0-2之间,默认1  调整回复的精确度使用
                n = 1,  # 默认条数1
                user = ids,    # 用户ID,用于机器人区分不同用户避免多用户时出现混淆
                model = "gpt-3.5-turbo",    # 这里注意openai官方有很多个模型
                messages = msg
            )
 
value = completion.choices[0].message.content    # chatGPT返回的数据
登录后复制

下面启用上下文管理的数据结构和没有启用的数据结构略有不同:

① system 代表系统设定(也就是告诉chatGPT他的角色)

② user 表示用户

③ assistant 表示GPT的回复

有几个点需要跟大家说一下,避免踩坑!

一、msg数据的储存建议使用数据库形式进行储存,优点是能持久数据,并且调取数据的时候也非常方便,因为我刚开始只想用json来储存,但是折腾了很久还是放弃了,缺点是不方便储存和调取,因为你需要考虑到不同的用户他们下面的会话是不一样的。

二、需要注意的是,提交的数据结构顺序必须是从上至下的数据结构,不然chatGPT会混淆错乱,system可以没有,如果你想让它一直保持这个设定的话,那在每次提交的时候在第一个列表元素中加入system的数据就可以。

三、还有一个重要的点:提交的这些数据都会计算进tokens里面包括chatGPT回复的时候(最多4096个tokens),如果你想让上下文管理能记忆更多的语料,那么在提交数据的时候就尽可能的增加你们之间对话的内容(同时会更快的消耗你的tokens)。

四、截止2023年3月14日前:chatGPT的会员价格为20美元/月 ,tokens按量收费。通俗的说就是想手机卡一样,每个月有月租,通话另外计费。chatGPT Plus会员的好处就是速度能更快,并且稳定,白嫖版的也能用,就是速度会慢一些而且不稳定容易挂掉。

以上就是如何在chatGPT Python API中启用上下文管理?的详细内容,更多请关注php中文网其它相关文章!

CMake 不断从 cygwin python 中获取 Python,如何从 Windows 安装的 Python 中获取

CMake 不断从 cygwin python 中获取 Python,如何从 Windows 安装的 Python 中获取

如何解决CMake 不断从 cygwin python 中获取 Python,如何从 Windows 安装的 Python 中获取

我有一个看起来像这样的 CMake 脚本:

  1. find_program(PYTHON_COMMAND NAMES python3 python)

问题是它检测到安装在 Cygwin 安装中的 python。 输出总是:

  1. -- PYTHON_PATH:C:/cygwin64/bin/python3

我希望它取自:

  1. c:\\python36-64\\python

在windows PATH变量中,Cygwin bin在路径的最后一个,windows安装在第一个 但它只检测到 Cygwin python,
怎么改?

Core Python | 2 - Core Python: Getting Started | 2.5 - Modularity | 2.5.5 - The Python Execution Mod

Core Python | 2 - Core Python: Getting Started | 2.5 - Modularity | 2.5.5 - The Python Execution Mod

It's important to understand the Python execution model and precisely when function deFinitions and other important events occur when a module is imported or executed. Here, we show execution of our Python module as it's imported in a graphical debugging environment. We step through the top‑level statements in the module. What's important to realize here is that the def used for the fetch_words function isn't merely a declaration. It's actually a statement, which when executed in sequence with the other top‑level model scope code, causes the code within the function to be bound to the name of the function. When modules are imported or run, all of the top‑level statements are run, and this is the means by which the function within the module namespace are defined. We are sometimes asked about the difference between Python modules, Python scripts, and Python programs. Any .py file constitutes a Python module. But as we've seen, modules can be written for convenient import, convenient execution, or using the if dunder name = dunder main idiom, both. We strongly recommend making even simple scripts importable since it eases development and testing so much if you can access your code from within the REPL. Likewise, even modules, which are only ever meant to be imported in production settings, benefit from having executable test code. For this reason, nearly all modules we create have this form of defining one or more importable functions with a postscript to facilitate execution. Whether you consider a module to be a Python script or Python program is a matter of context and usage. It's certainly wrong to consider Python to be merely a scripting tool, in the vein of Windows batch files or UNIX Shell scripts, as many large and complex applications are built exclusively with python.

 

  • def不仅仅是一个declaration声明,更是一条statement语句。它将其中的python代码于函数名绑定在一起
  • 一个py文件就是一个模块,这个模块包含类或函数。你写python,要尽量将代码包装成函数和类,方便各种import
  • 一个py文件也可看作是一个脚本,在系统命令行中运行
  • python不仅仅是脚本语言,很多大型程序都是用python构建的

Error: Can‘t find Python executable “python“, you can set the PYTHON env variable

Error: Can‘t find Python executable “python“, you can set the PYTHON env variable

在启动vue项目的时候,安装node.js组件node-sass过程中报错了,错误提示如下
Error: Can’t find Python executable “python”, you can set the PYTHON env variable

由错误提示可知:Node.js 在安装模块组件node-sass的时候,node.js缺少Visual Studio2015 Build Tools相关的组件和python的环境,如果安装了vs2015组件的小伙伴们就不用安装Visual Studio2015 Build Tools相应的组件,只用安装python2.7即可解决缺少的python组件的问题。

欲安装python2.7,请至python官网:www.python.org 下载,然后配置好python的环境变量即可。

不过博主我并不推荐上述的解决方案,因为对于程序员来说,效率第一,上述的问题一个命令就可以轻松解决你所遇到的麻烦,前面说了那么多,无非就是想告诉在看本篇博客的同仁们放下浮躁的心,遇到问题首先不是急着去解决问题,而是分析为什么会这样,然后才能水到聚成的去找到解决问题的方法。

运行下面这个命令即可解决你们遇到的Error问题

npm install --global --production windows-build-tools

:上面讲述了一堆就是为了讲述此命令是干嘛的,上面已经描述很详细了,就不再赘述了,该操作与上述的一堆操作无异,效果却是一样的。

然后运气不好的小伙伴可能接着会遇到一个坑,那就是执行了:npm install --global --production windows-build-tools这个命令的人细心点会发现执行到一半就卡住了,这个卡住了没有红字重点提示,而且下方还有英文在等待中,粗心的小伙伴可能以为是命令执行完了,组件安装好了,其实不然,我这边已经解决了,就无法复现了,具体点就是中文的提示,提示我们由于有类似组件在运行或者下载导致无法继续下载安装组件了。稳妥点的解决办法是,将电脑重启,将底层正在运行的模块干掉,待电脑重启后再执行npm install --global --production windows-build-tools这条命令即可,博主我就是这样解决的,稳稳的幸福就会浮现在你面前如下图所示,你的可能和我不一样,因为我已经跑成功过一次了,没有你的那么多细节的log打印。
在这里插入图片描述

然后就是在你的项目下shift+鼠标右击你的项目运行npm run dev即可启动vue项目了。

ERROR: Command

ERROR: Command "python setup.py egg_info" python-nss

[root@localhost ~]# pip install python-nss

DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won''t be maintained after that date. A future version of pip will drop support for Python 2.7.
Looking in indexes: http://pypi.douban.com/simple
Collecting python-nss
  Downloading http://pypi.doubanio.com/packages/6b/29/629098e34951c358b1f04f13a70b3590eb0cf2df817d945bd05c4169d71b/python-nss-1.0.1.tar.bz2 (222kB)
     |████████████████████████████████| 225kB 31kB/s 
    ERROR: Complete output from command python setup.py egg_info:
    ERROR: Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-install-JGnrT5/python-nss/setup.py", line 409, in <module>
        sys.exit(main(sys.argv))
      File "/tmp/pip-install-JGnrT5/python-nss/setup.py", line 333, in main
        nss_include_dir  = find_include_dir([''nss3'', ''nss''],   [''nss.h'',  ''pk11pub.h''], include_roots=include_roots)
      File "/tmp/pip-install-JGnrT5/python-nss/setup.py", line 94, in find_include_dir
        raise ValueError("unable to locate include directory containing header files %s" % include_files)

    ValueError: unable to locate include directory containing header files [''nss.h'', ''pk11pub.h'']

ERROR: Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-JGnrT5/python-nss/


查看错误日志缺少头文件

进入python-nss官网,写着To build python-nss you the C language header files and libraries for both NSPR and NSS will need to be installed. This is system and distribution specific, as such we cannot give you explicit instructions. On Linux typically these packages are called:

  • nss-devel
  • nspr-devel

yum install nss-devel -y

yum install nspr-devel -y

关于如何在chatGPT Python API中启用上下文管理?的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于CMake 不断从 cygwin python 中获取 Python,如何从 Windows 安装的 Python 中获取、Core Python | 2 - Core Python: Getting Started | 2.5 - Modularity | 2.5.5 - The Python Execution Mod、Error: Can‘t find Python executable “python“, you can set the PYTHON env variable、ERROR: Command "python setup.py egg_info" python-nss的相关知识,请在本站寻找。

本文标签: