38. 生成モデルについて
Transformerの構造③ • デコーダ
→ Attention層で重み付けして来たものに対して、最
終的にどういう出力がいいか判断して、出力する。
Self-Attention層(マスクあり)
入力文章内の照応関係(類似度や重要度)を獲得する層。
Positional Encoding層
単語の位置情報を埋め込む層。
Source-Target-Attention層
異なる文章同士の照応関係(類似度や重要度)を獲得する層。
出典:”Attention Is All You Need”
38
39. 生成モデルについて
“Attention Is All You Need”
• Attention層自体は、すでに2015年あたりに登場。
→ RNNの「あまりにも長文になると精度が下がってしまう問題」を
解決するために使われてはいた。
→ リカレント層を取り除くという発想には至らなかった。
• リカレント層がなくてもAttention層だけで十分じゃないか!
→ “Attention Is All You Need”
39
Transformer
→ リカレント層を取り除いて、Attention層 でモデルを構成
92. OpenAI APIを使ってみよう
Pythonで実装する ④APIリクエストを送って動作確認
• OpenAI公式HPに載っているサンプルコードを試してみる。
→ チャット補完(文法修正)を試してみる。
import os
import openai
# APIキーを設定
openai.organization = "[会社アカウントのOrganization ID]"
openai.api_key = os.getenv("OPENAI_API_KEY")
# 応答を取得するためのリクエストデータを定義
request_data = {
"model": "gpt-3.5-turbo",
"messages": [
{"role": "system", "content": "You will be provided with statements, and your task is to convert them to standard English."},
{"role": "user", "content": "She no went to the marcket"}
],
"temperature": 0,
"max_tokens": 256
}
# チャット補完APIにリクエストを送って応答を取得
response = openai.ChatCompletion.create(**request_data)
# メッセージの内容のみを表示
print(response['choices'][0]['message']['content'])
“ She no went to the marcket .”
↑
・リクエスト
「この文を標準的な英語の文に直してほしい」
← Openaiのチャット補完APIにリクエストを送って、
応答を取得
92