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