見出し画像

LangChainとChatGPTで文章のファクトチェックを行う

LangChainを使って文章のファクトチェックを行うデモがあったので、どんな形でファクトチェックを行っているのか見てみました。

仕組み

  • LLMに質問する

  • 質問した内容について最初の答えを生成する

  • その答えに至った前提が何であったかを自らに問う

  • これらの仮定が真であるかどうかを順次判断する

  • 新しい情報を取り入れた上で、質問に対する新しい答えを生成する

実行してみる

まず必要なライブラリをインポートします。

from langchain.llms import OpenAIChat
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
from langchain.chains import SimpleSequentialChain

問いを設定します。
「経済的に裕福な家庭ほどその子供たちの教育水準は高く、将来的に有利になるでしょうか?」

q = "Will the more financially well-off a family is, the more educated its children will be and the better off they will be in the future?"

推論のチェーンを設定します。

llm = OpenAIChat(temperature=0.7)
template = """{question}\n\n"""
prompt_template = PromptTemplate(input_variables=["question"], template=template)
question_chain = LLMChain(llm=llm, prompt=prompt_template)

template = """Here is a statement:
{statement}
Make a bullet point list of the assumptions you made when producing the above statement.\n\n"""
prompt_template = PromptTemplate(input_variables=["statement"], template=template)
assumptions_chain = LLMChain(llm=llm, prompt=prompt_template)

template = """Here is a bullet point list of assertions:
{assertions}
For each assertion, determine whether it is true or false. If it is false, explain why.\n\n"""
prompt_template = PromptTemplate(input_variables=["assertions"], template=template)
fact_checker_chain = LLMChain(llm=llm, prompt=prompt_template)

template = """In light of the above facts, how would you answer the question '{}'""".format(q)
template = """{facts}\n""" + template
prompt_template = PromptTemplate(input_variables=["facts"], template=template)
answer_chain = LLMChain(llm=llm, prompt=prompt_template)

overall_chain = SimpleSequentialChain(chains=[question_chain, assumptions_chain, fact_checker_chain, answer_chain], verbose=True)

以下のコードで推論を実行します。

print(q)
overall_chain.run(q)

結果

Will the more financially well-off a family is, the more educated its children will be and the better off they will be in the future?


> Entering new SimpleSequentialChain chain...


There is a correlation between a family's financial status and the education level of their children, but it is not always a direct cause-and-effect relationship. While wealthier families have more access to resources, such as private schools and tutors, that can lead to better educational opportunities, there are other factors that contribute to academic success, such as parental involvement, motivation, and natural ability. Additionally, financial stability does not always guarantee future success, as factors such as personal choices and external circumstances can also play a significant role. Therefore, while financial stability can contribute to educational and career success, it is not the only determining factor.


- There is a relationship between a family's financial status and their children's education level.
- Wealthier families have more access to resources that can lead to better educational opportunities.
- Parental involvement, motivation, and natural ability also play a significant role in academic success.
- Financial stability does not guarantee future success.
- Personal choices and external circumstances can also impact educational and career success.
- Financial stability is not the only determining factor in academic and career success.


1. True: Various studies have shown that a family's financial status correlates with their children's educational level.
2. True: Wealthier families have more resources, such as private tutors, better schools, and technology, that can lead to better educational opportunities for their children.
3. True: Parental involvement, motivation, and natural ability are crucial factors in academic success, regardless of a family's financial status.
4. True: Financial stability does not guarantee future success. Many wealthy individuals have failed in their careers or personal lives.
5. True: Personal choices and external circumstances, such as health issues, can also impact educational and career success, regardless of financial status.
6. True: Financial stability is not the only determining factor in academic and career success. Other factors, such as hard work, dedication, and networking, also play important roles.


Based on the facts presented above, it is likely that the more financially well-off a family is, the more educational opportunities their children will have and the higher their likelihood of success. However, financial stability is not the only determining factor in academic and career success, and other factors such as parental involvement, motivation, natural ability, personal choices, and external circumstances can also impact a child's educational and career trajectory. Therefore, while financial stability can be a significant advantage, it is not a guarantee of success.

> Finished chain.

DeepLで日本語訳したもの↓

家庭の経済状況と子どもの教育レベルには相関関係がありますが、必ずしも直接的な因果関係があるわけではありません。裕福な家庭ほど、私立学校や家庭教師など、より良い教育機会につながるリソースを利用できる一方で、親の関与、モチベーション、生まれ持った能力など、学業の成功に貢献する他の要因もあります。さらに、個人の選択や外的環境などの要因も重要な役割を果たすため、経済的安定が必ずしも将来の成功を保証するとは限りません。したがって、経済的安定は教育やキャリアの成功に貢献しますが、それだけが決定要因ではありません。


- 家庭の経済状況と子どもの教育レベルには関係があります。
- 裕福な家庭は、より良い教育機会につながる資源をより多く利用することができます。
- 親の関与、やる気、生まれつきの能力も学業成就に大きな役割を果たします。
- 経済的に安定しているからといって、将来の成功が保証されるわけではありません。
- 個人の選択や外的環境も、教育やキャリアの成功に影響を与える可能性があります。
- 経済的安定性だけが学業やキャリアの成功を決定する要因ではない。


1. 真:様々な研究により、家族の経済状態は子供の教育水準と相関があることが示されている。
2. 真:裕福な家庭は、家庭教師、より良い学校、技術など、子供により良い教育の機会を与えることができる資源を持っている。
3. 真:親の関与、モチベーション、生まれつきの能力は、家庭の経済状態にかかわらず、学業の成功に欠かせない要素である。
4. 真:経済的安定は将来の成功を保証するものではない。多くの富裕層が、キャリアや私生活で失敗している。
5. 真:個人の選択と健康問題などの外的環境は、経済状況にかかわらず、教育やキャリアの成功に影響を与える可能性があります。
6. 真:学業やキャリアの成功の決め手は、経済的安定だけではありません。努力、献身、ネットワークなど、他の要素も重要な役割を果たす。


上記の事実を踏まえると、経済的に裕福な家庭であればあるほど、その子供たちはより多くの教育機会を得ることができ、成功する可能性が高くなると思われます。しかし、経済的安定性だけが学業やキャリアの成功を決定する要因ではなく、親の関与、モチベーション、生まれ持った能力、個人の選択、外的環境などの他の要因も、子供の教育やキャリアの軌跡に影響を与える可能性があります。したがって、経済的安定は大きなアドバンテージとなり得ますが、成功を保証するものではありません。

真か偽の判定条件がLLMに因っていますが、検証タスクに対してファインチューニングしたLLMを利用するか、情報の検索結果もチェーンさせることでよりファクトチェックらしい動きにさせることができそうです。

この記事が気に入ったらサポートをしてみませんか?