探索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,使用起来真的好简单的。
来源于网络

安装openai

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类接口参数与返回数据结构不完全一致。

您可能感兴趣的与本文相关的镜像

Qwen3-8B

Qwen3-8B

文本生成
Qwen3

Qwen3 是 Qwen 系列中的最新一代大型语言模型,提供了一整套密集型和专家混合(MoE)模型。基于广泛的训练,Qwen3 在推理、指令执行、代理能力和多语言支持方面取得了突破性进展

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

「已注销」

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值