本文正在参与「技术视角深入 ChatGPT」征文活动
自然语言处理技术——文本生成
ChatGPT的应用领域越来越广泛,关于文本生成,咱们能够运用Python中的文本生成库来完成。其间,最常用的是基于深度学习的文本生成模型,如循环神经网络(RNN)和长短时记忆网络(LSTM)。
能够运用Python中的文本生成库来生成文本,例如运用OpenAI的GPT-2模型或者运用TensorFlow的Seq2Seq模型。
模型生成文本Python代码示例
以下是一个运用GPT-2模型生成文本的Python代码示例:
import openai
openai.api_key = "YOUR_API_KEY"
prompt = "今天天气怎么样?"
model = "text-davinci-002"
response = openai.Completion.create(
engine=model,
prompt=prompt,
max_tokens=1024,
n=1,
stop=None,
temperature=.5,
)
print(response.choices[].text)
这段代码运用OpenAI的API来调用GPT-2模型生成文本,其间prompt是输入的文本,model是运用的模型,max_tokens是生成的文本长度,temperature是控制生成文本的随机程度。
用pip命令装置openai类库
ChatGPT现已爆火了一段时间了,如今ChatGPT现已开放了自己的API系统,所以咱们自己写一个吧。只要你会Python,运用起来真的好简单的。
装置
pip install openai
测验把ChatGPT应用到NLP具体任务。
利用ChatGPT类接口完成文本翻译
别离运用Chat类接口和Completion类接口来完成。
翻译涉及到的文本和具体代码如下:
# 原始文本
ori_text = """Mikel Arteta has named an unchanged side for tonight’s game against Everton.
The boss has stuck with the same starting line-up that beat Leicester City on Saturday, with Leandro Trossard expected to start up front once again after impressing against the Foxes.
Jorginho makes his fourth successive start in midfield, with Thomas Partey still only fit enough for a spot on the bench.
Sean Dyche meanwhile has made one change to his line-up from Saturday’s 1-0 loss against Aston Villa, with Michael Keane replacing Conor Coady in the heart of defence.
The former Burnley defender has played just 22 minutes in the Premier League this season, and has been out recently with a knee injury but has been recalled by his former manager for this evening’s encounter at Emirates Stadium.
Arsenal: Ramsdale, White, Saliba, Gabriel, Zinchenko, Xhaka, Jorginho, Odegaard, Saka, Martinelli, Trossard.
Subs: Turner, Tierney, Tomiyasu, Holding, Kiwior, Partey, Vieira, Smith Rowe, Nketiah.
Everton: Pickford, Coleman, Tarkowski, Keane, Mykolenko, Gueye, Onana, Doucoure, Iwobi, McNeil, Maupay.
Subs: Begovic, Vinagre, Godfrey, Holgate, Coady, Mina, Davies, Gray, Simms."""
models = ["gpt-3.5-turbo", "text-davinci-003", "text-curie-001", "text-babbage-001", "text-ada-001"]
# 翻译参数,参考https://platform.openai.com/playground/p/default-translate
prompt = "please translate this into Simplified Chinese"
input_str = "\n\n".join([prompt, ori_text])
temperature = 0.3
max_len = 1024
top_p = 1
# 成果存储
res_ls = []
# Chat类接口
t0 = time.time()
result = openai.ChatCompletion.create(model=models[0], max_tokens=max_len, temperature=temperature, top_p=1,
messages=[{"role": "user", "content": input_str}])
t1 = time.time()
print(f"{models[0]}\t{t1-t0:.2f}", flush=True)
res_ls.append(result)
# Completion类接口
for model in models[1:]:
t0 = time.time()
result = openai.Completion.create(model=model, max_tokens=max_len, temperature=temperature, top_p=1, prompt=input_str)
t1 = time.time()
res_ls.append(result)
print(f"{model}\t{t1-t0:.2f}", flush=True)
print(len(res_ls))
这里需求注意的点便是,Chat类接口和Completion类接口参数与返回数据结构不完全共同。
本文正在参与 ✍ 技术视角深入 ChatGPT 征文活动