一步步教你怎样使用 chatgpt 原版!
ChatGPT是一种开源且强大的语言生成模型,它在NLP利用中非常受欢迎。它也提供了一系列功能,使得我们能够更好地创建和部署NLP系统。下面,我们将一步步教你怎样使用ChatGPT原版!
第一步:安装ChatGPT
在你使用ChatGPT前,需要先安装它。你可以到ChatGPT的官方网站上下载它的源代码。下载后,你需要安装一些依赖包,这些包包括Numpy、Pytorch、Transformers等等。如果你使用的是Linux或Mac OS系统,你可以打开终端,并使用以下命令在控制台中安装这些包:
`pip install numpy pytorch transformers`
如果你使用的是Windows系统,则可以在终端(CMD)中安装这些包:
`python -m pip install numpy pytorch transformers`
安装完成后,你可以验证你的安装会不会成功。使用以下代码导入ChatGPT:
```
from transformers import ChatTokenizer, ChatGPT
tokenizer = ChatTokenizer.from_pretrained('microsoft/DialoGPT-medium')
model = ChatGPT.from_pretrained('microsoft/DialoGPT-medium')
```
接下来,创建一个简单的键入摹拟会话:
```
# Let's chat for 5 lines
for step in range(5):
# encode the new user input, add the eos_token and return a tensor in Pytorch
new_user_input_ids = tokenizer.encode(input(">> User:") + tokenizer.eos_token, return_tensors='pt')
# append the new user input tokens to the chat history
bot_input_ids = torch.cat([chat_history_ids, new_user_input_ids], dim=⑴) if step > 0 else new_user_input_ids
# generated a response while limiting the total chat history to 1000 tokens,
chat_history_ids = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id)
# pretty print last ouput tokens from bot
print("ChatGPT: {}".format(tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[⑴]:][0], skip_special_tokens=True)))
```
如果这段代码正常运行并输出了相应的结果,则你已成功安装了ChatGPT!
第二步:开始使用ChatGPT
ChatGPT有很多区别的用处,例如生成对话、生成段落、生成文章等等。当你需要使用ChatGPT时,需要根据你的需求选择相应的模型和Tokenizer。这里我们以生成对话为例。
你需要导入ChatTokenizer和ChatGPT:
```
from transformers import ChatTokenizer, ChatGPT
tokenizer = ChatTokenizer.from_pretrained('microsoft/DialoGPT-medium')
model = ChatGPT.from_pretrained('microsoft/DialoGPT-medium')
```
然后,你需要创建一个输入的历史记录。这个历史记录其实就是会话记录,它将被模型用来生成下一句话。
```
# Let's chat for 5 lines
chat_history_ids = torch.tensor([[tokenizer.eos_token_id]])
for step in range(5):
# encode the new user input, add the eos_token and return a tensor in Pytorch
new_user_input_ids = tokenizer.encode(input(">> User:") + tokenizer.eos_token, return_tensors='pt')
# append the new user input tokens to the chat history
bot_input_ids = torch.cat([chat_history_ids, new_user_input_ids], dim=⑴) if step > 0 else new_user_input_ids
# generated a response while limiting the total chat history to 1000 tokens,
chat_history_ids = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id)
# pretty print last ouput tokens from bot
print("ChatGPT: {}".format(tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[⑴]:][0], skip_special_tokens=True)))
```
这段代码的功能是从标准控制台中读取用户输入,并根据用户的输入和 ChatGPT 原版来生成对话。
第三步:优化ChatGPT
默许情况下,ChatGPT有一定的生成问题。它常常会重复使用一样的片断,致使生成过于单调。为了解决这个问题,你可以采取一些优化策略。
一种优化方法是使用top-k采样技术。这类技术可使ChatGPT只选择几率最高的k个词语的一种,从而避免使用相同的片断。 另外一种优化方法是对生成的输出进行评估,并仅选择最好的模型输出。这样可以免因毛病输出而致使低质量的结果。
要使用这类优化技术,你可使用generate()方法中的相应参数。
例如:
```
# set seed to reproduce results. Feel free to change the seed though to get different results
torch.manual_seed(0)
# Let's chat for 5 lines
chat_history_ids = torch.tensor([[tokenizer.eos_token_id]])
for step in range(5):
# encode the new user input, add the eos_token and return a tensor in Pytorch
new_user_input_ids = tokenizer.encode(input(">> User:") + tokenizer.eos_token, return_tensors='pt')
# append the new user input tokens to the chat history
bot_input_ids = torch.cat([chat_history_ids, new_user_input_ids], dim=⑴) if step > 0 else new_user_input_ids
# generated a response while limiting the total chat history to 1000 tokens,
chat_history_ids = model.generate(
bot_input_ids,
max_length=1000,
pad_token_id=tokenizer.eos_token_id,
temperature=0.7, # 采取softargmax技术来选择下一个词
top_k=0, # 取所有词的几率,而不是只取几率前k的
top_p=0.92, # 取累加的几率超过0.92的词语
repetition_penalty=1.0, #避免重复使用相同的片断
do_sample=True, #使用sample方法从词典中采样
num_return_sequences=1, #返回一个序列
)
# pretty print last ouput tokens from bot
print("ChatGPT: {}".format(tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[⑴]:][0], skip_special_tokens=True)))
```
通过这类方式,ChatGPT将采取更复杂的策略来生成输出,从而更好地满足你的需求。
总结
通过这篇文章,你已学会了怎样使用ChatGPT原版来生成对话和其他文本。不但如此,我们还了解了怎样使用区别的策略来优化ChatGPT,并避免出现重复片断。我们希望这篇文章能够为你提供帮助,并帮助你更好地使用ChatGPT进行NLP利用。如果你有任何疑问或问题,请随时在下面发表评论,我们会尽快给你回复!
本文来源于chatgptplus账号购买平台,转载请注明出处:https://chatgpt.guigege.cn/chatgpt/35058.html 咨询请加VX:muhuanidc