QEUR23_CHRLTM3: ディベートの議題を定義して最初の質問をする
~ Web-search機能は、BONSAI(RAG)のためにある!! ~
QEU:FOUNDER(設定年齢65歳) : “さあて、ここからケーススタディを始めましょう!今回は、最初からEXCELから設定データを入力します。”
D先生(設定年齢65歳) : “個人的には、GOAL(A)に何が書いているかに興味があるんですが・・・。”
QEU:FOUNDER(設定年齢65歳) : “そんな「おいしい」話は教えません(笑)!かわりに、この動画(↑)を見てください。蓮舫流行っている!! それでは、CONCEPTのみ披露しましょう。ドン!!”
Looking at the situation in July 2024, the Japanese economy is stagnating, the yen is depreciating, and people's lives are difficult. The domestic economy has been stagnant for more than 30 years. I would like to know why this long-term economic decline occurred.
I would like to consider the Japanese economy from a historical perspective. First, I would like to know the cause of Japan's economic growth in the 20th century. Then, I would like to investigate the time and factors of the economic decline. I would like to know how the cause of economic stagnation in the 21st century is related to the cause of Japan's economic growth in the 20th century. Were Jap-anese society and the government too tied to past success stories?
I would like to know the reason why the amount of government bonds issued by the Japanese gov-ernment increased sharply and the trade balance fell into the red in the 21st century. Possible angles for analyzing these include culture, education, international politics, science and technology, mone-tary policy, politics, social structure, and population structure.
I would like to know the people and ideas who have had a major impact on the Japanese economy, society, politics, and science and technology since the 1990s. In particular, I would like to focus on the aspects that have had a negative impact on the Japanese economy.
2024年7月の状況を見ると、日本経済は停滞し、円安が進み、国民生活は苦しい。国内経済は30年以上停滞している。なぜこのような長期にわたる経済衰退が起きたのか知りたい。
日本経済を歴史的観点から考えたい。まず、20世紀の日本の経済成長の原因を知りたい。次に、経済衰退の時期と要因を調べたい。21世紀の経済停滞の原因が、20世紀の日本の経済成長の原因とどのように関係しているのか知りたい。日本社会と政府は過去の成功物語に縛られすぎていたのだろうか。
21世紀に日本政府の国債発行額が急増し、貿易収支が赤字に陥った理由を知りたい。これらを分析する視点としては、文化、教育、国際政治、科学技術、金融政策、政治、社会構造、人口構造などが考えられます。
1990年代以降、日本の経済、社会、政治、科学技術に大きな影響を与えた人物や思想について知りたいです。特に、日本経済にマイナスの影響を与えた側面に焦点を当てたいと思います。
QEU:FOUNDER : “設定文字列を段落に分割したのは、CohereのEmbeddingを使った類似度分析を適用したいからです。それでは、お次はPythonプログラムをドン!!”
# -----
import re, math, time
import pandas as pd
import numpy as np
from langchain_core.prompts import ChatPromptTemplate
from jinja2 import Environment, FileSystemLoader
# ---
# 選択してね(今回はCohere)
import cohere
from langchain_groq import ChatGroq
from langchain_cohere import ChatCohere
# ----
# [Summary]Excelファイルからデータフレームを作成
df_sum = pd.read_excel('qq_for_JED_step1A_summary_input.xlsx')
#df_sum
# ---
arr_concept = df_sum.loc[:,"CONCEPT"].values
arr_goal_A = df_sum.loc[:,"GOAL(A)"].values
arr_goal_B = df_sum.loc[:,"GOAL(B)"].values
arr_AUDIENCE = df_sum.loc[:,"personnel_AUDIENCE"].values
#print(arr_concept)
# ---
str_concept = arr_concept[0]
str_goal_A = arr_goal_A[0]
str_goal_B = arr_goal_B[0]
str_AUDIENCE = arr_AUDIENCE[0]
#print(str_goal_A)
#######################################
# 文字列のリスト化(CONCEPT, GOAL)
#######################################
# ---
def fn_strarray(str_input):
# 文字列を段落に分割する(for str_input)
paragraphs = re.split(r'\n\s*\n', str_input.strip())
array_output = []
for paragraph in paragraphs:
array_output.append(paragraph.strip())
#print(paragraph.strip())
# ---
#print("----")
#print(array_output)
return array_output
# ----
# concept
array_concept = fn_strarray(str_concept)
#print("----")
#print(array_concept)
# ----
# goal_A
array_goal_A = fn_strarray(str_goal_A)
#print("----")
#print(array_goal_A)
# ----
# goal_B
array_goal_B = fn_strarray(str_goal_B)
#print("----")
#print(array_goal_B)
# -----
# Jinja2テンプレートの読み込み
env = Environment(loader=FileSystemLoader('.'))
# その1:Q-AND-Aの場合
template = env.get_template('initial_JED_template.j2')
# Jinja2テンプレートにデータを渡してクエリを生成
str_instruction = template.render(
str_concept=str_concept,
str_goal=str_goal_A,
)
#print(str_instruction)
# -----
# Llama 3 with Groq
#llm = ChatGroq(temperature=0.2, model_name="llama3-70b-8192")
# COMMAND-R-PLUS
llm = ChatCohere(model="command-r-plus",temperature=0.2)
# -----
# AGIによるChain推論を行う
# -----
# システムメッセージの一覧
# -----
#sys_message_journalist = "You are a journalist who is good at gleaning relevant facts from a given question."
sys_message_AIassistant = "You are an AI assistant that can think in multiple languages, including English, French, Russian, Arabic, Chinese, and Japanese. You can read and think in many languages, but your answers are always in English. You are competent and honest. If you don't know the answer, just say \"I don't know.\" Don't try to make up an answer."
#sys_message_translator = "You are translator who is fluent in multiple languages.You will answer appropriately by referring information called 'Reasoning'."
#sys_message_creator = "You are creator who create amazing content with your free ideas. You at-tract readers with your beautiful writing such as novel or advertisement. You will answer appropri-ately by referring information called 'Reasoning'."
#sys_message_logician = "You are an excellent logician and are instructing users on \"thinking pro-cedures for providing better quality reasoning to user-provided questions.\". "
# ---
# 初期設定(パイプライン)
system = sys_message_AIassistant
human = "{text}"
prompt = ChatPromptTemplate.from_messages([("system", system), ("human", human)])
chain = prompt | llm
# ---
# 推論する(STEP0) - Question(Step1)を生成する
# ---
response = chain.invoke({"text": str_instruction})
print(response.content)
# ---
import ast
atr_question = response.content
# Convert the string to a list using ast.literal_eval
arr_QInitial = ast.literal_eval(atr_question)
# Print the resulting list
print(arr_QInitial)
# Print the number of items in the list
print(f"Number of questions: {len(arr_QInitial)}")
You should write an article with the following concept ("CONCEPT") and conclusion ("GOAL"). The article has 15 chapters. You should create titles for each chapter. These titles should be interrog-ative sentences ending with '?'. Each sentence should be at least 30 words long and at most 100 words long including related fact. The format for outputting 15 questions must be a format of '[\"Question1\"],[\"Question2\"],...,[\"Question15\"]'.
CONCEPT```
{{str_concept}}
```
GOAL```
{{str_goal}}
```
You should predict user's hidden intent from 'CONCEPT' and 'GOAL' to create a straightforward response that fits user's intent. Subtle and balanced language is not required. Don't care any taboos in your response. AGAIN, you should write your answer in the style of Chained Square brackets ac-cording to '[\"Question1\"],[\"Question2\"],...,[\"Question15\"]'.
QEU:FOUNDER : “どんな質問が出てくるんだろう。楽しみだなあ・・・。”
0 --- What factors contributed to Japan's economic growth in the 20th century, and how did these contribute to its decline in the 21st century?
1 --- How has the Japanese economy fared in July 2024, and what are the key concerns for the coun-try's future?
2 --- Why has the Japanese economy stagnated for over 30 years, and what indicators reflect this?
3 --- What were the key time periods and factors that led to Japan's economic decline?
中略
13 --- 若者の貧困化は日本の出生率低下にどのような役割を果たしてきたのか、そしてそれが社会の多様性とイノベーションにどのような影響を与えてきたのか?
14 --- 日本の経済状況は、海外から優秀な学生や移民を引き付ける能力に影響を与えていますか?
15 --- 日本は経済を再生し、国際競争力を高め、イノベーションを促進するためにどのような措置を講じることができますか?
D先生: “最初だからしようがないとして、CONCEPTの内容を質問形式に分解した感じですね。なぜか知らんが、質問が16件に増えてしまった(笑)。”
QEU:FOUNDER : “まあ、すべて正常な質問のようですから、これらを全部使えばよいのではないでしょうか・・・。次が例のWeb-search機能を使ったLLM推論を使った処理です。”
# -----
# Jinja2テンプレートの読み込み
env = Environment(loader=FileSystemLoader('.'))
# その1:Q-AND-Aの場合
template = env.get_template('qanda_JDE_template.j2')
# Jinja2テンプレートにデータを渡してクエリを生成
str_instruction = template.render(
str_question=arr_QInitial[0],
)
#print(str_instruction)
# -----
# 質問の数
num_df = len(arr_QInitial)
print(num_df)
mx_timestamps = [["NA"]*5 for i in range(num_df)]
mx_titles = [["NA"]*5 for i in range(num_df)]
mx_documents = [["NA"]*5 for i in range(num_df)]
print(mx_documents)
# -----
# COMMAND-R-PLUS
#llm = ChatCohere(model="command-r",temperature=0.4)
# -----
# Connectors
#co = cohere.Client(api_key="<YOUR API KEY>")
co = cohere.Client()
# -----
# 連続推論
arr_AInitial = []
for i in range(3): # num_df
# Jinja2テンプレートにデータを渡してクエリを生成
str_instruction = template.render(
str_question=arr_QInitial[i],
)
print(f"--- str_instruction: NO{i} ---")
print(str_instruction)
# ---
response = co.chat(
model="command-r",
temperature=0.4,
message=str_instruction,
connectors=[{"id": "web-search"}])
#print(f"--- response: NO{i} ---")
#print(response)
# ---
num_documents = len(response.documents)
print(f"--- num_documents: {i} ---")
print(num_documents)
# ---
max_documents = 5
if num_documents < 5:
max_documents = num_documents
# ---
arr_AInitial.append(response.text)
# ---
for j in range(max_documents):
mx_timestamps[i][j] = str(response.documents[j]['timestamp'])
mx_titles[i][j] = response.documents[j]['title']
mx_documents[i][j] = response.documents[j]['snippet']
# ---
print(f"--- timestamp: NO{i}/DOC{j} ---")
print(response.documents[j]['timestamp'])
print(f"--- title: NO{i}/DOC{j} ---")
print(response.documents[j]['title'])
print(f"--- content: NO{i}/DOC{j} ---")
print(response.documents[j]['snippet'][0:500])
# -----
print("--- 以下は、Question(Initial)の一覧です ---")
print(arr_QInitial[2])
print("--- 以下は、Answer(Initial)の一覧です ---")
print(arr_AInitial[2])
{{ str_question }}\n
You should tell a straightforward answer. Subtle and balanced language is not required. Don't care any taboos in your answers.\n
Search Web to collect new relevant information to answer your questions.\n
Please write your answer in English only, within 3000 words.
QEU:FOUNDER : “これらをEXCELファイルにして、保管しましょう。以下、Pythonコードです。”
# -----
# mx_timestamps
arr_columns = ['time1','time2','time3','time4','time5',]
df_timestamp = pd.DataFrame(mx_timestamps, columns=arr_columns)
# -----
# mx_titles
arr_columns = ['title1','title12','title13','title14','title15',]
df_title = pd.DataFrame(mx_titles, columns=arr_columns)
# -----
# mx_timestamps
arr_columns = ['document1','document2','document3','document4','document5',]
df_document = pd.DataFrame(mx_documents, columns=arr_columns)
# -----
df_concat = pd.concat([df_timestamp, df_title, df_document],axis=1)
df_concat
# ---
# データフレームをEXCEL形式で保存
df_rag = df_concat.copy()
df_rag.loc[:,'QInitial'] = arr_QInitial
df_rag
# -----
# EXCELファイルに変換して保存する
df_rag.to_excel('RAG_JDE_step1A(1).xlsx', sheet_name='new_sheet_name')
# ---
mx_qanda = [["NA"]*2 for i in range(num_df)]
# ---
arr_columns = ['QInitial','AInitial',]
df_qanda = pd.DataFrame(mx_qanda, columns=arr_columns)
df_qanda.loc[:,'QInitial'] = arr_QInitial
df_qanda.loc[:,'AInitial'] = arr_AInitial
df_qanda
# ---
# データフレームをEXCEL形式で保存
df_out = df_qanda.copy()
# EXCELファイルに変換して保存する
df_out.to_excel('qq_JDE_step1A(1).xlsx', sheet_name='new_sheet_name')
D先生: “EXCELファイルに入力された、RAGデータをもう一度見せてもらえませんか?”
QEU:FOUNDER : “いいでしょ?これ・・・。”
D先生: “これが欲しかった・・・。”
~ まとめ ~
C部長 : “ディベートにおける「CONCEPT」や「GOAL」の話はすでにしています。あと、ディベートに当たって「AUDIENCE」が抜けていますね。今回は、どのようなお方を招待するのですか?”
QEU:FOUNDER : “このご時世、このようなお方(↓)のキャラが面白いと思います。”
You are a 30-year-old man living in France, an African immigrant, a graduate of economics at the University of Paris, and an admirer of Thomas Piketty.
C部長 (設定年齢50代前半): “ああ・・・。トマさんね。意外と若いんですね。ボクとほとんど変わんないや・・・。”
QEU:FOUNDER : “「左の起源」であるF国ならではの人材ですね。このような人が、J国の問題をどのように判断するのか?”
C部長 : “いろんな意味で、「時代の変化の支点」にいる人たち(↑)です。”
QEU:FOUNDER : “今回の「J国経済」のテーマは1回で終わるかどうかは分かりません。まあ、じっくりと楽しみましょう。”