2020 年 2 月 11 日 35 分読んだ
推奨事項: NSDT シーン デザイナーを 使用して、3D シーンをすばやく構築します。
ChatGPT は現時点で印象的な偉業を達成できることが知られています。このテクニックを自分のプロジェクトで使用するアイデアを持っている人も多いでしょう。ただし、ChatGPT には現在公式 API がないことに注意してください。非公式 API を使用すると問題が発生する可能性があります。
現在、API を使用するには、アクセス トークンとクラウドフレア トークンを手動で取得する必要があります。さらに、これらのトークンは 2 時間ごとに手動で変更する必要があります。
1、チャットGPT
ChatGPT は設計に GPT-3 モデルを使用し、それに基づいて新しいモデルを開発しました。したがって、新しいモデルの出力は GPT-3 と同様になる傾向があります。この記事の執筆時点では、ChatGPT では text-davinci-002-render モデルが新しいモデルとして使用されていますが、現在は一般公開されていません。
ChatGPT は画期的なものではないかもしれませんが、既存のテクノロジーを活用した新しいインターフェイスを提供します。強力なヒントと効率的なメモリ ウィンドウを利用します。したがって、ChatGPT の非公式 API をハッキングする代わりに、LLM チェーンのアプローチを使用してその機能を複製できます。
2、ラングチェーン
Langchain は、LLM チェーンの標準インターフェイス、他のツールとの広範な統合、一般的なアプリケーションのエンドツーエンド チェーンを提供する新しい Python パッケージです。
LangChain は 4 つの主要な領域を支援するように設計されており、以下に複雑さの順に示します。
LLM とヒント
鎖
演技
メモリー
langchain の詳細については、こちらの公式ドキュメントをご覧ください。
3. インストール
langchain パッケージを使用するには、pypi からインストールできます。
pip install langchain
langchain から最新の更新を取得するには、このインストール方法を使用できます。
pip install "git+https://github.com/hwchase17/langchain.git"
インストール オプションの詳細については、こちらをご覧ください。
4. サンプルプロジェクト
ChatGPT でできることはたくさんありますが、楽しいことの 1 つは、生徒の課題に対する Q&A を作成することです。そこで今回は、BrainlyのAI版を作成してみます。
ChatGPT から得られるものは次のとおりです。
ラングチェーンのヒントは次のとおりです。
from langchain.llms import OpenAI
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
from langchain.chains import SimpleSequentialChain
llm = OpenAI(temperature=.7)
template = """You are a teacher in physics for High School student. Given the text of question, it is your job to write a answer that question with example.
Question: {text}
Answer:
"""
prompt_template = PromptTemplate(input_variables=["text"], template=template)
answer_chain = LLMChain(llm=llm, prompt=prompt_template)
answer = answer_chain.run("What is the formula for Gravitational Potential Energy (GPE)?")
print(answer)
langchain を使用して GPT-3 から得られる結果は次のとおりです。
The formula for Gravitational Potential Energy (GPE) is GPE = mgh, where m is the mass of an object, g is the acceleration due to gravity, and h is the height of the object. For example, if an object with a mass of 10 kg is at a height of 5 meters, then the GPE would be GPE = 10 x 9.8 x 5 = 490 Joules.
5. チャットボット
AI のようなチャットボットを作成する必要がある場合は、langchain のメモリを使用できます。その方法の例を次に示します。
from langchain.chains.conversation.memory import ConversationBufferMemory
from langchain import OpenAI, LLMChain, PromptTemplate
template = """You are a teacher in physics for High School student. Given the text of question, it is your job to write a answer that question with example.
{chat_history}
Human: {question}
AI:
"""
prompt_template = PromptTemplate(input_variables=["chat_history","question"], template=template)
memory = ConversationBufferMemory(memory_key="chat_history")
llm_chain = LLMChain(
llm=OpenAI(),
prompt=prompt_template,
verbose=True,
memory=memory,
)
llm_chain.predict(question="What is the formula for Gravitational Potential Energy (GPE)?")
result = llm_chain.predict(question="What is Joules?")
print(result)
結果は次のようになります。
$ python3 memory.py
> Entering new LLMChain chain...
Prompt after formatting:
You are a teacher in physics for High School student. Given the text of question, it is your job to write a answer that question with example.
Human: What is the formula for Gravitational Potential Energy (GPE)?
AI:
> Finished LLMChain chain.
> Entering new LLMChain chain...
Prompt after formatting:
You are a teacher in physics for High School student. Given the text of question, it is your job to write a answer that question with example.
Human: What is the formula for Gravitational Potential Energy (GPE)?
AI:
The formula for Gravitational Potential Energy (GPE) is GPE = mgh, where m is the mass of the object, g is the acceleration due to gravity, and h is the height of the object.
For example, if an object has a mass of 10 kg and is at a height of 5 meters, then the gravitational potential energy of the object is GPE = 10 kg x 9.8 m/s2 x 5 m = 490 Joules.
Human: What is Joules?
AI:
> Finished LLMChain chain.
Joules (J) is the SI unit of energy. It is defined as the amount of energy required to move an object of one kilogram at a speed of one meter per second. It is also equal to the work done when a force of one Newton is applied to an object and moved one meter in the direction of the force.
6. 結論
ChatGPT は GPT-3 に基づいたチャットボットですが、現時点では公式 API はありません。LangChain を使用すると、開発者は非公式 API を使用せずに、チャットボットや質問応答システムの作成など、ChatGPT の機能を複製できます。
LangChain は、一般的なアプリケーションに標準インターフェイス、多数の統合、エンドツーエンド チェーンを提供します。pypi からインストールでき、詳細については公式ドキュメントを参照してください。