很久没用gpt模型,都更新到gpt4了,今天想试试看,上次使用还是gpt-3。
老规矩,先看官方文档:https://platform.openai.com/docs/models
主要模型有:
MODELS | DESCRIPTION |
---|---|
GPT-4 Limited beta | A set of models that improve on GPT-3.5 and can understand as well as generate natural language or code |
GPT-3.5 | A set of models that improve on GPT-3 and can understand as well as generate natural language or code |
DALL·EBeta | A model that can generate and edit images given a natural language prompt |
WhisperBeta | A model that can convert audio into text |
Embeddings | A set of models that can convert text into a numerical form |
Moderation | A fine-tuned model that can detect whether text may be sensitive or unsafe |
GPT-3 | A set of models that can understand and generate natural language |
CodexDeprecated | A set of models that can understand and generate code, including translating natural language to code |
GPT-4系列有:
LATEST MODEL | DESCRIPTION | MAX TOKENS | TRAINING DATA |
---|---|---|---|
gpt-4 | More capable than any GPT-3.5 model, able to do more complex tasks, and optimized for chat. Will be updated with our latest model iteration. | 8,192 tokens | Up to Sep 2021 |
gpt-4-0314 | Snapshot of gpt-4 from March 14th 2023. Unlike gpt-4 , this model will not receive updates, and will only be supported for a three month period ending on June 14th 2023. |
8,192 tokens | Up to Sep 2021 |
gpt-4-32k | Same capabilities as the base gpt-4 mode but with 4x the context length. Will be updated with our latest model iteration. |
32,768 tokens | Up to Sep 2021 |
gpt-4-32k-0314 | Snapshot of gpt-4-32 from March 14th 2023. Unlike gpt-4-32k , this model will not receive updates, and will only be supported for a three month period ending on June 14th 2023. |
32,768 tokens | Up to Sep 2021 |
GPT-3.5系列有:
LATEST MODEL | DESCRIPTION | MAX TOKENS | TRAINING DATA |
---|---|---|---|
gpt-3.5-turbo | Most capable GPT-3.5 model and optimized for chat at 1/10th the cost of text-davinci-003 . Will be updated with our latest model iteration. |
4,096 tokens | Up to Sep 2021 |
gpt-3.5-turbo-0301 | Snapshot of gpt-3.5-turbo from March 1st 2023. Unlike gpt-3.5-turbo , this model will not receive updates, and will only be supported for a three month period ending on June 1st 2023. |
4,096 tokens | Up to Sep 2021 |
text-davinci-003 | Can do any language task with better quality, longer output, and consistent instruction-following than the curie, babbage, or ada models. Also supports inserting completions within text. | 4,097 tokens | Up to Jun 2021 |
text-davinci-002 | Similar capabilities to text-davinci-003 but trained with supervised fine-tuning instead of reinforcement learning |
4,097 tokens | Up to Jun 2021 |
code-davinci-002 | Optimized for code-completion tasks | 8,001 tokens | Up to Jun 2021 |
解决:
原来的urllib3版本:
操作步骤 :
我给我的项目建立了conda虚拟环境(first_env),你根据你的实际情况操作即可,pip也是可以的。
(1)进入虚拟环境
conda activate first_env
(2)查看packages的版本
conda list
(3)移除现在的urllib3
conda remove urllib3
(3)安装低版本urllib3
conda install urllib3=1.25.11
现在已经降级好了:
因为这个虚拟环境下的urllib3是安装openai的时候被安装的,所以刚才移除urllib的时候,相关的package会被安装,现在需要重新安装一下openai(上面,我们已经重新安装了1.25版本的urllib3,这次安装openai的时候,就不会再安装他了)。
conda install openai
现在就可以用了:
注意看,我运行的是以前的程序,那时候我用的是GPT-3
的text-davinci-003模型。
当我换成gpt-3.5或者4会怎样呢?
注意:
GPT-4的模都需要你先加入waitlist,通过之后才能使用哦,在openAI官网相关模型的介绍页面就有加入waitlist的入口。
有人可能没通过gpt-4的waitlist,这里先写个gpt-3.5的python例程。
可以看看我之前的文章,写了可以连续对话的程序,使用的是gpt-3的text-davinci-003模型。
我把原来的代码简单地修改了一下,可以连续对话。
输入是messages原来是prompt:
messages=[
# {"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt},
# {"role": "assistant", "content":self.bot }
],
完整返回:
{
"choices": [
{
"finish_reason": "stop",
"index": 0,
"message": {
"content": "\u8349\u62df\u4e00\u5c01\u7535\u5b50\u90ae\u4ef6\u6216\u5176\u4ed6\u6587\u5b57\u3002",
"role": "assistant"
}
}
],
"created": 1680860435,
"id": "chatcmpl-72cs7EtzxhyTRSyHPpGaLVrZSfaer",
"model": "gpt-3.5-turbo-0301",
"object": "chat.completion",
"usage": {
"completion_tokens": 15,
"prompt_tokens": 70,
"total_tokens": 85
}
}
把message中的content解析出来显示就可以了。
代码:
# -*- coding = utf-8 -*-
# @TIME : 2023-4-7 下午 5:23
# @Author : CQUPTLei
# @File : GPT_3.5_Turbo.py
# @Software : PyCharm
# @Abstract :
import openai
import time
from pathlib import Path
openai.api_key = "你的apikey"
class Chat_bot:
def __init__(self,model):
self.user = "\nYou: "
self.bot = "GPT-3.5-turbo: "
self.model = model
self.question_list = []
self.answer_list = []
self.text = ''
self.turns = []
self.last_result = ''
def dialogue_save(self):
timestamp = time.strftime("%Y%m%d-%H%M-%S", time.localtime()) # 时间戳
file_name = 'output/Chat_' + timestamp + '.md' # 文件名
f = Path(file_name)
f.parent.mkdir(parents=True, exist_ok=True)
with open(file_name, "w", encoding="utf-8") as f:
for q, a in zip(self.question_list, self.answer_list):
f.write(f"You: {q}\nGPT-3.5-turbo: {a}\n\n")
print("对话内容已保存到文件中: " + file_name)
def Generate(self):
print('\n请开始你们的对话,以exit结束。')
while True:
# 用户输入
question = input(self.user)
self.question_list.append(question) # 将问题添加到问题列表中
prompt = self.bot + self.text + self.user + question
# 退出命令
if question == 'exit':
break
else:
try:
response = openai.ChatCompletion.create(
# 模型名称
model= self.model,
messages=[
# {"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt},
# {"role": "assistant", "content":self.bot }
],
)
result = response["choices"][0]["message"]["content"].strip()
print(result)
self.answer_list.append(result) #将回答添加到回答列表中
self.last_result = result
self.turns += [question] + [result]
if len(self.turns) <= 10:
self.text = " ".join(self.turns)
else:
self.text = " ".join(self.turns[-10:])
# 打印异常
except Exception as exc:
print(exc)
# 退出对话后保存
self.dialogue_save()
if __name__ =='__main__':
bot = Chat_bot('gpt-3.5-turbo')
bot.Generate()