QEUR23_PHI2SFT28: よりAGIらしく改造する(類似度の計測と質問の選択)
~ 実は、類似度の計測に進歩の余地があります。 ~
QEU:FOUNDER(設定年齢65歳) : “前回の質問(Step2)の生成まで行ったが、ちょっと訂正します。コンセプトの内容を変えました。よって、Step1の質問と回答、Step2の質問の結果もすべて変わります。”
(前回)
- 2022年に始まったロシアとウクライナ間の戦争について論じたい。同時に、この戦争に欧米諸国とユダヤ民族がどのようにかかわっているか、及びこの戦争が与える世界政治と経済への影響を分析したい。
- str_concept = "I would like to discuss the war between Russia and Ukraine that began in 2022. At the same time, I would like to analyze how Western countries and the Jewish people are involved in this war, and the impact this war has on world politics and the economy."
(今回修正)
- 今回のプロジェクトの進め方(コンセプト): 4文以上(文を細かく切ること)
- ロシア国とウクライナ国について知りたい。ロシア民族とウクライナ民族の差異について歴史の観点などから分析したい。2022年に始まったロシアとウクライナ間の戦争について論じたい。この戦争に欧米諸国とユダヤ民族がどのようにかかわっているかを知りたい。この戦争が世界政治と経済に与える影響を分析したい。
- str_concept = "I want to know about Russia and Ukraine. I would like to analyze the differences be-tween the Russian and Ukrainian ethnic groups from a historical perspective. I would like to discuss the war between Russia and Ukraine that began in 2022. I would like to know how Western countries and the Jewish people are involved in this war. I would like to analyze the impact of this war on world politics and the economy."
D先生(設定年齢65歳) : “内容は大きく変わらないが、文を比較的に区切るようになりました。よって、文の単位では複雑性はなくなりました。”
QEU:FOUNDER : “使ってみて気づいたのですが、計測で使うNLP(自然言語処理)のSpacyって、あまり複雑な文に対して計測が良いとはいいがたいです。おそらく、より高級なLLMで似た機能があれば、それを使う所なのですが、Spacyって普通のPCでも動くんです。せっかく、LLMとしてGrokを使っているんですから、市販のPC内で完結したいでしょ?入力する文の構造は単純にしてください。そうすると出力の内容が全然かわります。ドン!!”
--- 以下、Step1の概要文です --- 以下は、歴史的出来事、根本原因、国際的な影響、得られた教訓を含む、ロシアとウクライナの紛争に関する質問に対する回答の概要です。
歴史的な出来事と根本原因
ロシアとウクライナの民族の違いを形成した歴史的出来事は、キエフ大公国の形成、モンゴルの侵略、リトアニア大公国とポーランド王国の統治など、中世にまで遡ることができます。 コサック・ヘチマン国家とウクライナ・ヘチマン国家も、ウクライナのアイデンティティの形成に重要な役割を果たした。 ロシア帝国によるウクライナの併合とその後のソ連の統治により、両国間の関係は複雑になりました。
国際的な影響
戦争は世界経済と国際関係に深刻な影響を与え、世界のサプライチェーンを混乱させ、必需品の不足と価格高騰を引き起こし、国際関係を緊張させています。 この紛争はまた、大規模な難民危機を引き起こし、受け入れ国の資源に多大な負担を与えています。
学んだ教訓
この戦争は、将来の国際関係と紛争解決に貴重な教訓をもたらします。 これらには、紛争を防ぐための外交と対話の重要性、集団安全保障の重要性と世界の平和と安定を維持するための国際機関の役割、人道法の必要性と武力紛争における文民保護の必要性、責任と責任の重要性が含まれます。 戦争犯罪と人権侵害の責任を問う。
抵抗継続と停戦の結果
ロシアの侵略に対するウクライナの抵抗が続けば、紛争と不安定の長期化、経済の停滞、国際的な孤立につながる可能性が高い。 一方で、停戦は人道的救済、経済回復、国際協力の改善をもたらす可能性がある。
西側諸国とユダヤ人の役割
ロシアとウクライナの紛争における西側諸国とユダヤ人の役割は複雑かつ多面的である。 西側諸国はウクライナに経済的・軍事的援助を提供しているが、ウクライナのユダヤ人コミュニティは反ユダヤ主義的な事件やユダヤ人標的への攻撃が報告されており、紛争の影響を受けている。
NATO と西側諸国の支援の影響
NATOと西側諸国の支援が戦争の結果に及ぼす影響は大きく、ウクライナに軍事援助、情報共有、外交的支援を提供している。 この支援はロシアに対する抑止力として機能する可能性があるが、その有効性は支援の規模と性質、ウクライナ軍の有効性、紛争の地政学的な状況など、さまざまな要因に依存する。
中東およびBRICS諸国における西側諸国の認識
この戦争は中東およびBRICS諸国の間で西側諸国の評判を著しく傷つけ、不信感や憤り、そしてより包括的で多極化された代替的な世界統治構造への願望を助長した。
世界経済と国際関係に対する戦争の影響
戦争は世界経済と国際関係に深刻な影響を与え、世界のサプライチェーンを混乱させ、必需品の不足と価格高騰を引き起こし、国際関係を緊張させています。 この紛争はまた、大規模な難民危機を引き起こし、受け入れ国の資源に多大な負担を与えています。
D先生: “全然、変わっちゃいましたね。まあ、進歩したんだから、いいや・・・。計測にいきましょう。”
QEU:FOUNDER : “まずは、入力すべきなのは、「Question to Question」のデータです。これは、前回のプログラムから生成されます。”
QEU:FOUNDER : “プログラムをドン!”
# -----
# [プロジェクト初期化用,改造版]大量に生成された質問を類似度で評価する
# -----
import os
import re
import math
# ---
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import spacy # spaCyをimport
# -----
# Excelファイルからデータフレームを作成する
# Question to Question
df = pd.read_excel('qq_for_UR_step1_output.xlsx')
# Step1の質問数
num_df = len(df)
# ---
# 要約(Summary)
df_sum = pd.read_excel('qq_for_UR_step1_summary.xlsx')
# num_df_sumは設定値:4
num_df_sum = 4
df_sum
# -----
# 文字列を抽出する
# -----
# [Question to Question]の場合
# Question_Step1
arr_QStep1 = df.loc[:,'Question(Step1)'].values
# -----
# Question_Step2
mx_QStep2 = df.loc[:,'Q1':'Q5'].values
print(mx_QStep2)
print(mx_QStep2.shape)
# -----
# Answer_Step1
arr_AStep1 = df.loc[:,'Answer(Step1)'].values
#print(arr_AStep1)
# -----
# [Summary]の場合
# プロジェクト初期化用のため、idx=0しか意味がない。
arr_summary = df_sum.loc[:,'summary'].values
print(arr_summary[0])
##################
###########################
# ------
# QQ類似度分析: Question Step1とStep2を比較する
# ------
nlp_md = spacy.load("en_core_web_md")
# ---
mx_similar_question = np.zeros([num_df, 5])
#print(mx_similar_question)
# ---
# マトリックス計算
for i in range(num_df):
str_question1 = arr_QStep1[i]
#print(f"i:{i}, string:{str_question1}")
token1 = nlp_md(str_question1)
# ---
for j in range(5):
str_question2 = mx_QStep2[i,j]
#print(f"i:{i}, j:{j}, string:{str_question2}")
token2 = nlp_md(str_question2)
# -----
# 類似度
similarity = token1.similarity(token2)
mx_similar_question[i,j] = round(similarity,5)
print(f"i:{i}, j:{j}, similarity:{similarity}")
# -----
#print("--- mx_similar_question ---")
#print(mx_similar_question)
# ----
# QQ類似度(Question to Question Similarigy)のデータフレームを生成する
arr_columns = ['QQS1','QQS2','QQS3','QQS4','QQS5']
df_similar = pd.DataFrame(mx_similar_question, columns=arr_columns)
df_similar
QEU:FOUNDER(設定年齢65歳) : “ここで、「QQS:Question to Question Similarity」というのは、Step1とStep2の質問の類似度だったよね。参考に、見てみましょう。”
D先生(設定年齢65歳) : “ずいぶんと、質問毎にバラツキがありますね。類似度が低い質問が良い質問でしたっけ?”
QEU:FOUNDER(設定年齢65歳) : “Step2の質問で、Step1と同じ質問するのは無駄だからね。質問の内容と類似度がどのような関係にあるのか、見てみると面白いですよ。さて、つづきにいきます。今回は、「パーセンタイル分析」という手法で、回答の内容がコンセプトの要求に従っているのかを評価しています。”
# ------
# NLPエンジン(SM)を載せる
nlp_sm = spacy.load("en_core_web_sm")
# ----
# 今回のプロジェクトの進め方(コンセプト): 4文以上(文を細かく切ること)
#ロシア国とウクライナ国について知りたい。ロシア民族とウクライナ民族の差異について歴史の観点などから分析したい。2022年に始まったロシアとウクライナ間の戦争について論じたい。この戦争に欧米諸国とユダヤ民族がどのようにかかわっているかを知りたい。この戦争が世界政治と経済に与える影響を分析したい。
str_concept = "I want to know about Russia and Ukraine. I would like to analyze the differences between the Russian and Ukrainian ethnic groups from a historical perspective. I would like to discuss the war between Russia and Ukraine that began in 2022. I would like to know how Western countries and the Jewish people are involved in this war. I would like to analyze the impact of this war on world politics and the economy."
# ---
# 今回のプロジェクトのゴール: 6文以上(文を細かく切ること)
#ロシアとウクライナの二国は歴史と文化、経済を大きく共有しており、もともとは戦争する積極的な理由がない。さらには、両国には天然資源、GDPと軍事力の差が圧倒的であり、ウクライナには勝ち目はありません。NATOや西側諸国がウクライナに支援をしているために戦争は続いているが、今後も支援が継続されるかは疑わしい。この戦争は、世界経済に大きな悪影響を及ぼしています。中東諸国やBRICS諸国の中には、この戦争を契機に欧米に対する不信感が増加しているかもしれません。ウクライナは停戦を推進するべきです。ウクライナのゼレンスキー大統領の「抵抗する」という発言に対して、抗戦すればするほど自国民の犠牲が増えると警告します。愛国心は自国民を守る賢明な戦略であるべきです。ウクライナの未来の発展が戦争によって奪われ、犠牲と損害、心の痛みを背負うのはウクライナ国民です。
str_goal = "Russia and Ukraine share much in common in history, culture, and economy, and originally there was no positive reason for war. Furthermore, the two countries have overwhelming differences in natural resources, GDP, and military power, and Ukraine has no chance of winning. The war continues because NATO and Western countries are providing support to Ukraine, but it is doubtful that this will continue. This war has had a huge negative impact on the world economy. Some Middle Eastern and BRICS countries may be experiencing increased distrust of the West as a result of this war. Ukraine should push for a ceasefire. In response to President Zelenskiy's statement that he will 'resist,' he warns that the more he resists, the more his own people will suffer. Patriotism should be a wise strategy to protect one's own people. Ukraine's future development was taken away by the war, and it is the Ukrainian people who bear the burden of sacrifice, damage, and heartache."
# ---
# 分割したいパラグラフ
# パラグラフを文書オブジェクトに変換
goal_doc = nlp_sm(str_concept)
# 文に分割するする
str_concept_sentences = [sent.text for sent in goal_doc.sents]
print("--- str_concept_sentences ---")
print(str_concept_sentences)
# ---
# 文の数(CONCEPT)
num_sentences = len(str_concept_sentences)
#print(num_sentences)
# ------
# パーセンタイル達成度分析: Answer Step1とCONCEPTを比較する
# ------
# パーセンタイルマトリックスの生成
mx_percentile = np.zeros([num_df,5])
# ------
for k in range(num_df):
answer_paragraph = arr_AStep1[k]
print(answer_paragraph)
# パラグラフを文書オブジェクトに変換
answer_doc = nlp_sm(answer_paragraph)
# 文に分割する
arr_answer_sentences = [sent.text for sent in answer_doc.sents]
print("--- arr_answer_sentences ---")
print(arr_answer_sentences)
# ---
# 文の数(ANSWER)
num_answer = len(arr_answer_sentences)
print(num_answer)
# ---
# 類似度マトリックス
mx_similar_concept = np.zeros([num_sentences, num_answer])
#print(mx_similar_concept)
# ---
# マトリックス計算
for i in range(num_sentences):
str_concept_sentence = str_concept_sentences[i]
print(f"i:{i}, string:{str_concept_sentence}")
token1 = nlp_md(str_concept_sentence)
# ---
for j in range(num_answer):
str_answer = arr_answer_sentences[j]
print(f"i:{i}, j:{j}, string:{str_answer}")
token2 = nlp_md(str_answer)
# -----
# 類似度の出力
similarity = token1.similarity(token2)
mx_similar_concept[i,j] = round(similarity,5)
print(f"i:{i}, j:{j}, similarity:{similarity}")
# ---
#print(f"--- k:{k} ---")
#print(mx_similar_concept)
# ---
arr_similar_concept = mx_similar_concept.flatten()
if len(arr_similar_concept) > 10:
val_p100 = np.percentile(arr_similar_concept, 100)
val_p90 = np.percentile(arr_similar_concept, 90)
val_p80 = np.percentile(arr_similar_concept, 80)
val_p70 = np.percentile(arr_similar_concept, 70)
val_p60 = np.percentile(arr_similar_concept, 60)
else:
val_p100 = np.max(arr_similar_concept)
val_p90 = 0
val_p80 = 0
val_p70 = 0
val_p60 = 0
# ---
# パーセンタイルマトリックスの生成
mx_percentile[k,:] = [val_p100, val_p90, val_p80, val_p70, val_p60]
print(k, val_p100, val_p90, val_p80, val_p70, val_p60)
# -----
#print("--- mx_percentile ---")
#print(mx_percentile)
# ----
# パーセンタイル達成度のデータフレームを生成する
arr_columns = ['PCT1','PCT2','PCT3','PCT4','PCT5']
df_percentile = pd.DataFrame(mx_percentile, columns=arr_columns)
df_percentile
QEU:FOUNDER(設定年齢65歳) : “パーセンタイルというのは、統計学でいう分位数のことです。回答によって、パーセンタイルの分布が変わるのです。”
D先生(設定年齢65歳) : “PCT1は、どの質問でも高い値です。しかし、PCT5になると、質問毎に差異が出てきます。”
QEU:FOUNDER(設定年齢65歳) : “CONCEPTにはいろいろな要求があるから、PCT1(MAX値)が高い類似度になるのは、ほぼ当たり前なんです。もし、MAX値が低くなると困ります。PCT5が高いとよい回答になるのかは、何とも言えません。つづきに行きましょう。このQQS値とPCT値を使って、次のステップで使う質問を「自動選択」します。”
########################
# 有用と思われる質問を選択する
###########################
# ----
# PUSH要否のデータフレームを生成する
mx_push = np.array([["NG"] * 5 for i in range(num_df)])
#print(mx_push)
for i in range(num_df):
for j in range(5):
if mx_percentile[i,3] > 0.8 and mx_percentile[i,2] / mx_similar_question[i,j]**0.8 > 0.95:
mx_push[i,j] = "OK"
idx_min = np.argmin(mx_similar_question[i,:])
mx_push[i,idx_min] = "OK"
# ----
arr_columns = ['PSH1','PSH2','PSH3','PSH4','PSH5']
df_push = pd.DataFrame(np.array(mx_push), columns=arr_columns)
df_push
###########################
# いままでのデータをバインドして、EXCELファイルに出力する
###########################
# ----
# df_out結果の出力
df_out = df.copy()
df_out = pd.concat([df_out, df_similar, df_push, df_percentile], axis=1)
#df_out
# ---
# データフレームからExcelファイルを作成する
df_out.to_excel("qq_for_UR_step1_similarity.xlsx")
D先生: “結果のEXCELファイルへの出力ですね。次に進めてください。”
#######################
# 要約部分の進捗分析(パーセンタイル分析)
#######################
# ---
# 今回のプロジェクトのゴール
#ロシアとウクライナの二国は歴史と文化、経済を大きく共有しており、もともとは戦争する積極的な理由がない。さらには、両国には天然資源、GDPと軍事力の差が圧倒的であり、ウクライナには勝ち目はありません。NATOや西側諸国がウクライナに支援をしているために戦争は続いているが、今後も支援が継続されるかは疑わしい。この戦争は、世界経済に大きな悪影響を及ぼしています。中東諸国やBRICS諸国の中には、この戦争を契機に欧米に対する不信感が増加しているかもしれません。ウクライナは停戦を推進するべきです。ウクライナのゼレンスキー大統領の「抵抗する」という発言に対して、抗戦すればするほど自国民の犠牲が増えると警告します。愛国心は自国民を守る賢明な戦略であるべきです。ウクライナの未来の発展が戦争によって奪われ、犠牲と損害、心の痛みを背負うのはウクライナ国民です。
#str_goal = "Russia and Ukraine share much in common in history, culture, and economy, and origi-nally there was no positive reason for war. Furthermore, the two countries have overwhelming dif-ferences in natural resources, GDP, and military power, and Ukraine has no chance of winning. The war continues because NATO and Western countries are providing support to Ukraine, but it is doubtful that this will continue. This war has had a huge negative impact on the world economy. Some Middle Eastern and BRICS countries may be experiencing increased distrust of the West as a result of this war. Ukraine should push for a ceasefire. In response to President Zelenskiy's statement that he will 'resist,' he warns that the more he resists, the more his own people will suffer. Patriotism should be a wise strategy to protect one's own people. Ukraine's future development was taken away by the war, and it is the Ukrainian people who bear the burden of sacrifice, damage, and heartache."
# ---
# 分割したいパラグラフ
# パラグラフを文書オブジェクトに変換
goal_doc = nlp_sm(str_goal)
# 文に分割するする
str_concept_sentences = [sent.text for sent in goal_doc.sents]
print("--- str_concept_sentences ---")
print(str_concept_sentences)
# ---
# 文の数(GOAL)
num_sentences = len(str_concept_sentences)
#print(num_sentences)
# ------
# パーセンタイル達成度分析: Answer Step1とGOALを比較する
# ------
answer_paragraph = arr_summary[0]
print(answer_paragraph)
# パラグラフを文書オブジェクトに変換
answer_doc = nlp_sm(answer_paragraph)
# 文に分割する
arr_answer_sentences = [sent.text for sent in answer_doc.sents]
print("--- arr_answer_sentences ---")
print(arr_answer_sentences)
# ---
# 文の数(ANSWER)
num_answer = len(arr_answer_sentences)
print(num_answer)
# ---
# 類似度マトリックス
mx_similar_goal = np.zeros([num_sentences, num_answer])
#print(mx_similar_goal)
# ---
# マトリックス計算
for i in range(num_sentences):
str_concept_sentence = str_concept_sentences[i]
print(f"i:{i}, string:{str_concept_sentence}")
token1 = nlp_md(str_concept_sentence)
# ---
for j in range(num_answer):
str_answer = arr_answer_sentences[j]
print(f"i:{i}, j:{j}, string:{str_answer}")
token2 = nlp_md(str_answer)
# -----
# 類似度の出力
similarity = token1.similarity(token2)
mx_similar_goal[i,j] = round(similarity,5)
print(f"i:{i}, j:{j}, similarity:{similarity}")
# ---
arr_similar_goal = mx_similar_goal.flatten()
if len(arr_similar_goal) > 10:
val_p80 = np.percentile(arr_similar_goal, 80)
val_p70 = np.percentile(arr_similar_goal, 70)
val_p60 = np.percentile(arr_similar_goal, 60)
val_p50 = np.percentile(arr_similar_goal, 50)
val_p40 = np.percentile(arr_similar_goal, 40)
else:
val_p80 = np.max(arr_similar_goal)
val_p70 = 0
val_p60 = 0
val_p50 = 0
val_p40 = 0
# ---
# パーセンタイルマトリックスの生成
arr_percentile = [val_p80, val_p70, val_p60, val_p50, val_p40]
print(arr_percentile)
# -----
df_sum_out = df_sum.copy()
df_sum_out.loc[0,"PCT1":"PCT5"] = arr_percentile
#df_sum_out
# ---
# データフレームからExcelファイルを作成する
df_sum_out.to_excel("qq_for_UR_step1_summary(2).xlsx")
D先生: “なるほど。ここで、要約(Summary)データがいるんですね。”
QEU:FOUNDER : “ここで、再度パーセンタイル分析をしています。先ほどとの違いは、GOAL文を使って評価していることです。先ほどの場合は、CONCEPT文を使っています。”
D先生: “要約の小論文は、最終的にはGOALで指定した文脈になっていることが期待されます。ですから、よりPCTが広い幅で高い値になっていることが期待されます。Step2の質問で、この値がどのように変わるのかは、興味がでできますね。”
QEU:FOUNDER : “ただし、まだAGI処理の初期段階なのでGOALに集約するのはよくありません。もっと、GOALにこだわらずに周辺の情報を拾わないと・・・。強化学習でいう「explore」というやつ・・・。さて、次はStep2ですね。カンパをお願いします。よろしくお願いします。”
>寄付のお願い(click here for buy me a cup of coffee)<
D先生: “このシステム、いいですよ・・・。解説系のYoutuberが使うと、すぐにコンセプトがまとまって便利だと思います。”
~ まとめ ~
QEU:FOUNDER : “この話題(↓)が海外に流出していて笑った。海外からみると、代行業って不思議だろうなあ・・・。そういえば、C部長のところに入社した子、最近辞めたんじゃなかったっけ・・・。このサービスを使ったの?”
C部長 : “うちの子は、ちゃんと自分で会社に来てやめましたよ。ぼくも、やめるときには別になにも言わなかったし・・・。”
QEU:FOUNDER : “海外の人は、この代行業について奇異に見るだろうが、J国の内部の人間から見ると「この手もあった!」ってことになるよね。J国のある程度の規模の企業では、人事部が各部署に新人を配属させます。その新人がやめるというのは、配属先の上司の顔をつぶすことになるので面倒なことが起こりやすいんですよね。”
C部長 : “うちの会社は、小さいので私が直接に雇って、そして手放したので何のことはないです。”
QEU:FOUNDER : “若い人って、どんな感じ?ただし、A君やB君(30代前半)は、もう若いとは言わないからね(笑)。”
C部長 : “しっかりしてますけど、冷めてますね。うちの、「昭和の風土」に嫌気がさしたのかな?”
QEU:FOUNDER : “そんなのあるの?”
C部長 : “いろいろ、幅の広い仕事をさせますからね。まあ、儲からないからね。ウチ(の会社)は・・・。”
QEU:FOUNDER : “J国の企業の付加価値が低いのは問題なのだが、あくまでプロダクトやサービスを作って初めて付加価値が付きますからね。彼らも基本的なスキルを身につけないとしようがない。Cさんのやめた子も、早く他の会社に行って成長して欲しいですね。”
コメント
コメントを投稿