背景聊天机器人或客服助手是ai工具,希望通过互联网上的文本或语音与用户的交付,实现业务价值。聊天机器人的发展在这几年间迅速进步,从最初的基于简单逻辑的机器人到现在基于自然语言理解(nlu)的人工智能。对于后者,构建此类聊天机器人时最常用的框架或库包括国外的rasa、dialogflow和amazon lex等,以及国内大厂百度、科大讯飞等。这些框架可以集成自然语言处理(nlp)和nlu来处理输入文本、分类意图并触发正确的操作以生成响应。
随着大型语言模型(llm)的出现,我们可以直接使用这些模型构建功能齐全的聊天机器人。其中一个著名的llm例子是来自openai的生成generative pre-trained transformer 3 (gpt-3:chatgpt就是基于gpt fine-tuning及加入人类反馈模型的),它可以通过使用对话或会话数据来fine-tuning模型,生成类似于自然对话的文本。这种能力使其成为构建自定义聊天机器人的最佳选择。
今天我们来聊如何通过fine-tuning gpt-3模型来构建满足属于我们自己的简单会话聊天机器人。
通常,我们希望在自己的业务对话示例的数据集上fine-tuning模型,例如客户服务的对话记录、聊天日志或电影中的字幕。fine-tuning过程调整模型的参数,让它更好地适应这些会话数据,从而使聊天机器人更擅长理解和回复用户输入。
要fine-tuninggpt-3,我们可以使用hugging face的transformers库,该库提供了预训练模型和fine-tuning工具。该库提供了几种不同大小和较多能力的gpt-3模型。模型越大,可以处理的数据就越多,精度也可能越高。但是,为了简单起见,我们这次使用的是openai接口,可通过编写少量的代码来实现fine-tuning。
接下来就是我们使用openai gpt-3 来实现fine-tuning,可从这获取数据集,抱歉我又用国外数据集了,国内真的很少这类已经处理好的数据集。
1、创建open api密匙创建帐户非常简单,可以使用打开这个链接就可以完成。我们可以通过openai key访问 openai 上的模型。创建api 密钥步骤如下:
登录到您的帐户转到页面的右上角,然后单击帐户名,下拉列表,然后单击“查看 api 密钥”
单击“创建新密钥”,记得马上复制生成的密钥,切记,并保存好,不然无法再次查看它。
2、准备数据我们已经创建了api密匙,那么我们可以开始准备fine-tuning模型的数据,在这可以查看数据集。
第一步:安装 openai 库pip install openai
安装后,我们就可以加载数据了:
import os
import json
import openai
import pandas as pd
from dotenv import load_dotenv
load_dotenv()
os.environ['openai_api_key'] = os.getenv('openai_key')
openai.api_key = os.getenv('openai_key')
data = pd.read_csv('data/data.csv')
new_df = pd.dataframe({'interview ai': data['text'].iloc[::2].values, 'human': data['text'].iloc[1::2].values})
print(new_df.head(5))
我们将问题加载到interview ai列中,并将相应的答案加载到human列中。我们还需要创建一个环境变量.env文件来保存openai_api_key
接下来,我们将数据转换为 gpt-3 的标准。根据文档,确保数据采用jsonl具有两个键的格式,这个很重要:prompt例如completion
{ prompt : ,completion : }
{ prompt : ,completion : }
重新构造数据集以适应以上方式,基本是循环遍历数据框中的每一行,并将文本分配给human,将interview ai文本分配给完成。
output = []
for index, row in new_df.iterrows():
print(row)
completion = ''
line = {'prompt': row['human'], 'completion': row['interview ai']}
output.append(line)
print(output)
with open('data/data.jsonl', 'w') as outfile:
for i in output:
json.dump(i, outfile)
outfile.write('n')
使用prepare_data命令,这时会在提示时询问一些问题,我们可以提供y或n回复。
os.system(openai tools fine_tunes.prepare_data -f 'data/data.jsonl' )
最后,一个名为的文件data_prepared.jsonl被转储到目录中。
3、fun-tuning 模型要fun-tuning模型,我们只需要运行一行命令:
os .system( openai api fine_tunes.create -t 'data/data_prepared.jsonl' -m davinci )
这基本上使用准备好的数据从 openai 训练davinci模型,fine-tuning后的模型将存储在用户配置文件下,可以在模型下的右侧面板中找到。
4、模型调试我们可以使用多种方法来验证我们的模型。可以直接从 python 脚本、openai playground 来测试,或者使用 flask 或 fastapi 等框构建 web 服务来测试。
我们先构建一个简单的函数来与此实验的模型进行交互。
def generate_response(input_text):
response = openai.completion.create(
engine=davinci:ft-personal-2023-01-25-19-20-17,
prompt=the following is a conversation with dsa an ai assistant.
dsa is an interview bot who is very helpful and knowledgeable in data structure and algorithms.nn
human: hello, who are you?n
dsa: i am dsa, an interview digital assistant. how can i help you today?n
human: {}ndsa:.format(input_text),
temperature=0.9,
max_tokens=150,
top_p=1,
frequency_penalty=0.0,
presence_penalty=0.6,
stop=[n, human:, dsa:]
)
return response.choices[0].text.strip()
output = generate_response(input_text)
print(output)
把它们放在一起。
import os
import json
import openai
import pandas as pd
from dotenv import load_dotenv
load_dotenv()
os.environ['openai_api_key'] = os.getenv('openai_key')
openai.api_key = os.getenv('openai_key')
data = pd.read_csv('data/data.csv')
new_df = pd.dataframe({'interview ai': data['text'].iloc[::2].values, 'human': data['text'].iloc[1::2].values})
print(new_df.head(5))
output = []
for index, row in new_df.iterrows():
print(row)
completion = ''
line = {'prompt': row['human'], 'completion': row['interview ai']}
output.append(line)
print(output)
with open('data/data.jsonl', 'w') as outfile:
for i in output:
json.dump(i, outfile)
outfile.write('n')
os.system(openai tools fine_tunes.prepare_data -f 'data/data.jsonl' )
os.system(openai api fine_tunes.create -t 'data/data_prepared.jsonl' -m davinci )
def generate_response(input_text):
response = openai.completion.create(
engine=davinci:ft-personal-2023-01-25-19-20-17,
prompt=the following is a conversation with dsa an ai assistant.
dsa is an interview bot who is very helpful and knowledgeable in data structure and algorithms.nn
human: hello, who are you?n
dsa: i am dsa, an interview digital assistant. how can i help you today?n
human: {}ndsa:.format(input_text),
temperature=0.9,
max_tokens=150,
top_p=1,
frequency_penalty=0.0,
presence_penalty=0.6,
stop=[n, human:, dsa:]
)
return response.choices[0].text.strip()
示例响应:
input_text = what is breadth first search algorithm
output = generate_response(input_text)
the breadth-first search (bfs) is an algorithm for discovering all the
reachable nodes from a starting point in a computer network graph or tree data
structure
结论gpt-3 是一种强大的大型语言生成模型,最近火到无边无际的chatgpt就是基于gpt-3上fine-tuning的,我们也可以对gpt-3进行fine-tuning,以构建适合我们自己业务的聊天机器人。fun-tuning过程调整模型的参数可以更好地适应业务对话数据,让机器人更善于理解和响应业务的需求。经过fine-tuning的模型可以集成到聊天机器人平台中以处理用户交互,还可以为聊天机器人生成客服回复习惯与用户交互。整个实现可以在这里找到,数据集可以从这里下载。
以上就是使用 gpt-3 构建符合业务需求的企业聊天机器人的详细内容。