自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

利用 GPT-3 構(gòu)建適合公司業(yè)務(wù)的聊天機器人

人工智能
GPT-3 是一種強大的大型語言生成模型,最近火到無邊無際的Chatgpt就是基于GPT-3上fine-tuning的,我們也可以對GPT-3進行fine-tuning,以構(gòu)建適合我們自己業(yè)務(wù)的聊天機器人。

背景

聊天機器人或客服助手是AI工具,希望通過互聯(lián)網(wǎng)上的文本或語音與用戶的交付,實現(xiàn)業(yè)務(wù)價值。聊天機器人的發(fā)展在這幾年間迅速進步,從最初的基于簡單邏輯的機器人到現(xiàn)在基于自然語言理解(NLU)的人工智能。對于后者,構(gòu)建此類聊天機器人時最常用的框架或庫包括國外的RASA、Dialogflow和Amazon Lex等,以及國內(nèi)大廠百度、科大訊飛等。這些框架可以集成自然語言處理(NLP)和NLU來處理輸入文本、分類意圖并觸發(fā)正確的操作以生成響應(yīng)。

隨著大型語言模型(LLM)的出現(xiàn),我們可以直接使用這些模型構(gòu)建功能齊全的聊天機器人。其中一個著名的LLM例子是來自O(shè)penAI的生成Generative Pre-trained Transformer 3 (GPT-3:chatgpt就是基于gpt fine-tuning及加入人類反饋模型的),它可以通過使用對話或會話數(shù)據(jù)來fine-tuning模型,生成類似于自然對話的文本。這種能力使其成為構(gòu)建自定義聊天機器人的最佳選擇。

今天我們來聊如何通過fine-tuning GPT-3模型來構(gòu)建滿足屬于我們自己的簡單會話聊天機器人。

通常,我們希望在自己的業(yè)務(wù)對話示例的數(shù)據(jù)集上fine-tuning模型,例如客戶服務(wù)的對話記錄、聊天日志或電影中的字幕。fine-tuning過程調(diào)整模型的參數(shù),讓它更好地適應(yīng)這些會話數(shù)據(jù),從而使聊天機器人更擅長理解和回復(fù)用戶輸入。

要fine-tuningGPT-3,我們可以使用Hugging Face的Transformers庫,該庫提供了預(yù)訓(xùn)練模型和fine-tuning工具。該庫提供了幾種不同大小和較多能力的GPT-3模型。模型越大,可以處理的數(shù)據(jù)就越多,精度也可能越高。但是,為了簡單起見,我們這次使用的是OpenAI接口,可通過編寫少量的代碼來實現(xiàn)fine-tuning。

接下來就是我們使用OpenAI GPT-3 來實現(xiàn)fine-tuning,可從這獲取數(shù)據(jù)集,抱歉我又用國外數(shù)據(jù)集了,國內(nèi)真的很少這類已經(jīng)處理好的數(shù)據(jù)集。

1、創(chuàng)建Open API密匙

創(chuàng)建帳戶非常簡單,可以使用打開這個鏈接就可以完成。我們可以通過openai key訪問 OpenAI 上的模型。創(chuàng)建API 密鑰步驟如下:

  • 登錄到您的帳戶
  • 轉(zhuǎn)到頁面的右上角,然后單擊帳戶名,下拉列表,然后單擊“查看 API 密鑰”

  • 單擊“創(chuàng)建新密鑰”,記得馬上復(fù)制生成的密鑰,切記,并保存好,不然無法再次查看它。

2、準(zhǔn)備數(shù)據(jù)

我們已經(jīng)創(chuàng)建了api密匙,那么我們可以開始準(zhǔn)備fine-tuning模型的數(shù)據(jù),在這可以查看數(shù)據(jù)集。

第一步:

安裝 OpenAI 庫pip install openai

安裝后,我們就可以加載數(shù)據(jù)了:

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列中,并將相應(yīng)的答案加載到Human列中。我們還需要創(chuàng)建一個環(huán)境變量.env文件來保存OPENAI_API_KEY

接下來,我們將數(shù)據(jù)轉(zhuǎn)換為 GPT-3 的標(biāo)準(zhǔn)。根據(jù)文檔,確保數(shù)據(jù)采用JSONL具有兩個鍵的格式,這個很重要:prompt例如completion

{ "prompt" :  "<prompt text>" ,  "completion" :  "<ideal generated text>" } 
{ "prompt" : "<prompt text>" , "completion" : "<ideal generated text>" }

重新構(gòu)造數(shù)據(jù)集以適應(yīng)以上方式,基本是循環(huán)遍歷數(shù)據(jù)框中的每一行,并將文本分配給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回復(fù)。

os.system("openai tools fine_tunes.prepare_data -f 'data/data.jsonl' ")

最后,一個名為的文件data_prepared.jsonl被轉(zhuǎn)儲到目錄中。

3、fun-tuning 模型

要fun-tuning模型,我們只需要運行一行命令:

os .system( "openai api fine_tunes.create -t 'data/data_prepared.jsonl' -m davinci " )

這基本上使用準(zhǔn)備好的數(shù)據(jù)從 OpenAI 訓(xùn)練davinci模型,fine-tuning后的模型將存儲在用戶配置文件下,可以在模型下的右側(cè)面板中找到。

4、模型調(diào)試

我們可以使用多種方法來驗證我們的模型??梢灾苯訌?Python 腳本、OpenAI Playground 來測試,或者使用 Flask 或 FastAPI 等框構(gòu)建 Web 服務(wù)來測試。

我們先構(gòu)建一個簡單的函數(shù)來與此實驗的模型進行交互。

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.\n\n"
"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.\n\n"
"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()

示例響應(yīng):

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

結(jié)論

GPT-3 是一種強大的大型語言生成模型,最近火到無邊無際的chatgpt就是基于GPT-3上fine-tuning的,我們也可以對GPT-3進行fine-tuning,以構(gòu)建適合我們自己業(yè)務(wù)的聊天機器人。fun-tuning過程調(diào)整模型的參數(shù)可以更好地適應(yīng)業(yè)務(wù)對話數(shù)據(jù),讓機器人更善于理解和響應(yīng)業(yè)務(wù)的需求。經(jīng)過fine-tuning的模型可以集成到聊天機器人平臺中以處理用戶交互,還可以為聊天機器人生成客服回復(fù)習(xí)慣與用戶交互。整個實現(xiàn)可以在這里找到,數(shù)據(jù)集可以從這里下載。

責(zé)任編輯:姜華 來源: 今日頭條
相關(guān)推薦

2022-07-05 06:42:01

聊天機器人人工智能

2019-07-03 10:02:47

聊天機器人平臺機器人

2017-08-21 13:31:44

AI聊天機器人facebook

2018-03-23 09:35:54

機器人業(yè)務(wù)成長

2019-03-22 09:00:00

AI人工智能聊天機器人

2019-01-25 16:30:34

機器人機器學(xué)習(xí)人工智能

2023-03-22 17:25:18

ChatGPT人工智能聊天機器人

2020-02-02 09:19:14

聊天機器人機器人智能

2017-03-28 12:21:21

機器人定義

2016-02-16 14:46:33

聊天機器人機器學(xué)習(xí)自然語言

2024-09-30 13:11:09

2017-12-15 08:58:40

測試工具地圖API文檔

2023-09-18 09:00:00

聊天機器人DeepInfraLangChain

2023-05-04 08:00:00

機器人GPT4模型機器學(xué)習(xí)

2019-12-19 16:08:40

人工智能機器人數(shù)據(jù)

2022-07-03 10:23:06

機器人場景個性化

2023-06-29 15:04:21

微軟ChatGPT

2023-12-18 19:05:34

2023-08-23 08:54:59

OpenAIGPT-3.5
點贊
收藏

51CTO技術(shù)棧公眾號