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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pip3 install googletrans==4.0.0-rc1
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from google import genai
|
| 4 |
+
from google.genai import types
|
| 5 |
+
import os
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
from googletrans import Translator
|
| 8 |
+
from pgvector.psycopg import register_vector, Bit
|
| 9 |
+
import psycopg2
|
| 10 |
+
#import cohere
|
| 11 |
+
#import numpy as np
|
| 12 |
+
|
| 13 |
+
load_dotenv(verbose=True)
|
| 14 |
+
#co = cohere.ClientV2(api_key=os.environ.get("COHERE_API_KEY"))
|
| 15 |
+
client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
|
| 16 |
+
|
| 17 |
+
# Embedding function
|
| 18 |
+
#def embed(input, input_type):
|
| 19 |
+
#response = co.embed(texts=input, model='embed-multilingual-v3.0', input_type=input_type, embedding_types=['ubinary'])
|
| 20 |
+
#return [np.unpackbits(np.array(embedding, dtype=np.uint8)) for embedding in response.embeddings.ubinary]
|
| 21 |
+
|
| 22 |
+
# 埋め込み生成関数
|
| 23 |
+
def generate_embeddings(texts):
|
| 24 |
+
# 改行または句点で分割(任意で調整可能)
|
| 25 |
+
if isinstance(texts, str):
|
| 26 |
+
inputs = [t.strip() for t in texts.split("\n") if t.strip()]
|
| 27 |
+
else:
|
| 28 |
+
inputs = texts
|
| 29 |
+
|
| 30 |
+
result = client.models.embed_content(
|
| 31 |
+
model="gemini-embedding-001",
|
| 32 |
+
contents=inputs,
|
| 33 |
+
config=types.EmbedContentConfig(output_dimensionality=1024)
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
# 結果を整形して表示
|
| 37 |
+
formatted = ""
|
| 38 |
+
for i, embedding in enumerate(result.embeddings):
|
| 39 |
+
formatted += f"{embedding.values}"
|
| 40 |
+
return formatted
|
| 41 |
+
|
| 42 |
+
async def generate_content(prompt):
|
| 43 |
+
conn = psycopg2.connect(
|
| 44 |
+
dbname="smair",
|
| 45 |
+
user="smairuser",
|
| 46 |
+
password="smairuser",
|
| 47 |
+
host="www.ryhintl.com",
|
| 48 |
+
port=10629
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
cur = conn.cursor()
|
| 52 |
+
# Embed the query
|
| 53 |
+
#query_embedding = embed([prompt], 'search_query')[0]
|
| 54 |
+
query_embedding = generate_embeddings(prompt)
|
| 55 |
+
|
| 56 |
+
# Convert numpy array to a Python list or tuple
|
| 57 |
+
#query_embedding = query_embedding.tolist() # Convert to list
|
| 58 |
+
|
| 59 |
+
cur.execute(
|
| 60 |
+
'SELECT content, 1 - (embedding <=> %s::vector) AS similarity FROM thinking_rag where (1 - (embedding <=> %s::vector)) <> 0 ORDER BY similarity ASC',
|
| 61 |
+
(query_embedding,query_embedding)
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
results = cur.fetchall()
|
| 65 |
+
vector_resp = [row[0] for row in results]
|
| 66 |
+
|
| 67 |
+
final_prompt = f'''
|
| 68 |
+
{vector_resp}に基づいて{prompt}に対する答えを正確に出力してください。
|
| 69 |
+
'''
|
| 70 |
+
|
| 71 |
+
client = genai.Client(api_key=os.environ["GEMINI_API_KEY2"])
|
| 72 |
+
config = {'thinking_config': {'include_thoughts': True}}
|
| 73 |
+
response = client.models.generate_content(
|
| 74 |
+
model="gemini-2.5-flash",
|
| 75 |
+
#model='gemini-2.0-flash-thinking-exp',
|
| 76 |
+
contents=final_prompt,
|
| 77 |
+
config=config
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
summary_thought = ""
|
| 81 |
+
summary_answer = ''
|
| 82 |
+
|
| 83 |
+
translator = Translator()
|
| 84 |
+
summary_thought = ""
|
| 85 |
+
summary_answer = ""
|
| 86 |
+
|
| 87 |
+
for part in response.candidates[0].content.parts:
|
| 88 |
+
if not part.text:
|
| 89 |
+
continue
|
| 90 |
+
translated_text = await translator.translate(part.text, src='en', dest='ja')
|
| 91 |
+
if part.thought:
|
| 92 |
+
summary_thought += translated_text.text
|
| 93 |
+
else:
|
| 94 |
+
summary_answer += translated_text.text
|
| 95 |
+
|
| 96 |
+
summaries = summary_thought + '\n' + summary_answer
|
| 97 |
+
|
| 98 |
+
return summary_thought,summary_answer
|
| 99 |
+
|
| 100 |
+
# Gradio Blocksの設定
|
| 101 |
+
with gr.Blocks(title="gemini thinking RAG",css="footer {visibility: hidden;} #header {display: flex; justify-content: space-between; align-items: center; font-size: 24px; font-weight: bold;} #logo {width: 50px; height: 50px;} .logout-btn { background-color: #3498db; border-radius: 10px; color: white; padding: 10px 20px; border: none; cursor: pointer; transparent-bg {background-color: transparent; color: black; padding: 10px; border: none;}") as gemini:
|
| 102 |
+
gr.HTML('<div id="header"><span>🛡️ GEMINI THINKING RAG</span><img id="logo" src="https://www.ryhintl.com/images/ryhlogo/ryhlogo.png" width="64" height="64" alt="Logo"></div>')
|
| 103 |
+
gr.Markdown("### gemini thinking RAG - プロンプトで依頼した内容の回答をRAGやWEBを利用して生成します。")
|
| 104 |
+
|
| 105 |
+
with gr.Sidebar(open=False,width=450):
|
| 106 |
+
gr.HTML('''
|
| 107 |
+
<!DOCTYPE html>
|
| 108 |
+
<html lang="ja">
|
| 109 |
+
<head>
|
| 110 |
+
<meta charset="UTF-8">
|
| 111 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 112 |
+
<title>Geminiの思考プロセス</title>
|
| 113 |
+
<style>
|
| 114 |
+
@import url('https://fonts.googleapis.com/css2?family=Noto+Sans+JP:wght@400;700&display=swap');
|
| 115 |
+
|
| 116 |
+
:root {
|
| 117 |
+
--primary-color: #4285F4; /* Googleのブランドカラー */
|
| 118 |
+
--secondary-color: #34A853;
|
| 119 |
+
--background-color: #f8f9fa;
|
| 120 |
+
--card-background: #ffffff;
|
| 121 |
+
--text-color: #333;
|
| 122 |
+
--heading-color: #202124;
|
| 123 |
+
--border-color: #e0e0e0;
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
body {
|
| 127 |
+
font-family: 'Noto Sans JP', sans-serif;
|
| 128 |
+
background-color: var(--background-color);
|
| 129 |
+
color: var(--text-color);
|
| 130 |
+
line-height: 1.8;
|
| 131 |
+
margin: 0;
|
| 132 |
+
padding: 20px;
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
.container {
|
| 136 |
+
max-width: 900px;
|
| 137 |
+
margin: 40px auto;
|
| 138 |
+
padding: 40px;
|
| 139 |
+
background-color: var(--card-background);
|
| 140 |
+
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.05);
|
| 141 |
+
border-radius: 12px;
|
| 142 |
+
border-top: 5px solid var(--primary-color);
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
h1 {
|
| 146 |
+
text-align: center;
|
| 147 |
+
color: var(--heading-color);
|
| 148 |
+
font-weight: 700;
|
| 149 |
+
font-size: 2.2em;
|
| 150 |
+
margin-bottom: 30px;
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
h2 {
|
| 154 |
+
color: var(--primary-color);
|
| 155 |
+
font-weight: 700;
|
| 156 |
+
font-size: 1.5em;
|
| 157 |
+
border-left: 4px solid var(--primary-color);
|
| 158 |
+
padding-left: 15px;
|
| 159 |
+
margin-top: 40px;
|
| 160 |
+
margin-bottom: 20px;
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
p {
|
| 164 |
+
font-size: 1em;
|
| 165 |
+
margin-bottom: 20px;
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
ul {
|
| 169 |
+
list-style: none;
|
| 170 |
+
padding-left: 0;
|
| 171 |
+
}
|
| 172 |
+
|
| 173 |
+
li {
|
| 174 |
+
margin-bottom: 15px;
|
| 175 |
+
position: relative;
|
| 176 |
+
/*padding-left: 30px;*/
|
| 177 |
+
padding-left: 10px;
|
| 178 |
+
}
|
| 179 |
+
|
| 180 |
+
li::before {
|
| 181 |
+
content: '';
|
| 182 |
+
position: absolute;
|
| 183 |
+
left: 0;
|
| 184 |
+
top: 5px;
|
| 185 |
+
width: 10px;
|
| 186 |
+
height: 10px;
|
| 187 |
+
/*background-color: var(--primary-color);*/
|
| 188 |
+
background-color: transparent;
|
| 189 |
+
border-radius: 50%;
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
+
.section-title {
|
| 193 |
+
font-weight: 700;
|
| 194 |
+
color: var(--heading-color);
|
| 195 |
+
display: block;
|
| 196 |
+
margin-bottom: 5px;
|
| 197 |
+
}
|
| 198 |
+
|
| 199 |
+
strong {
|
| 200 |
+
color: var(--heading-color);
|
| 201 |
+
}
|
| 202 |
+
|
| 203 |
+
.summary {
|
| 204 |
+
margin-top: 40px;
|
| 205 |
+
padding: 20px;
|
| 206 |
+
background-color: #e8f0fe;
|
| 207 |
+
border-left: 5px solid var(--primary-color);
|
| 208 |
+
border-radius: 8px;
|
| 209 |
+
}
|
| 210 |
+
</style>
|
| 211 |
+
</head>
|
| 212 |
+
<body>
|
| 213 |
+
|
| 214 |
+
<div class="container">
|
| 215 |
+
<h1>Geminiの思考プロセス</h1>
|
| 216 |
+
<p>Geminiは、Googleが開発した大規模言語モデルであり、その思考プロセスは、単一のAIモデルではなく、複数の「エージェント」が連携して複雑なタスクを解決する**「Multi-Agent Reasoning」**という手法をコアとしています。</p>
|
| 217 |
+
|
| 218 |
+
<h2>Geminiの思考プロセス:Multi-Agent Reasoning</h2>
|
| 219 |
+
<p>このプロセスは、複雑な問題を解くために、複数の専門家が協力する人間のチームに似ています。具体的には、Geminiの思考は以下のステップで構成されています。</p>
|
| 220 |
+
<ul>
|
| 221 |
+
<li>
|
| 222 |
+
<strong>問題の分解(Decomposition):</strong>
|
| 223 |
+
最初に、与えられたタスクを、よりシンプルで管理しやすい小さなサブタスクに分解します。
|
| 224 |
+
</li>
|
| 225 |
+
<li>
|
| 226 |
+
<strong>専門エージェントの割り当て(Agent Assignment):</strong>
|
| 227 |
+
分解された各サブタスクに最適なAIエージェントが割り当てられます。たとえば、データ分析が必要なサブタスクには「データ分析エージェント」、クリエイティブな文章生成が必要なサブタスクには「ライティングエージェント」といった具合です。
|
| 228 |
+
</li>
|
| 229 |
+
<li>
|
| 230 |
+
<strong>並行処理(Parallel Processing):</strong>
|
| 231 |
+
各エージェントは、それぞれのサブタスクを並行して実行します。この並行処理により、タスク全体の完了時間が大幅に短縮されます。
|
| 232 |
+
</li>
|
| 233 |
+
<li>
|
| 234 |
+
<strong>統合と調整(Integration and Refinement):</strong>
|
| 235 |
+
各エージェントが生成した結果は、最後に統合エージェントによって集約されます。この統合エージェントは、結果の矛盾をチェックし、全体として一貫性のある最終的な回答を生成します。
|
| 236 |
+
</li>
|
| 237 |
+
</ul>
|
| 238 |
+
|
| 239 |
+
<h2>「Gemini thinking」のメリット</h2>
|
| 240 |
+
<p>このMulti-Agent Reasoningの手法は、従来のAIモデルにはない以下のようなメリットをもたらします。</p>
|
| 241 |
+
<ul>
|
| 242 |
+
<li>
|
| 243 |
+
<strong>複雑な問題解決能力の向上:</strong>
|
| 244 |
+
複数のエージェントが連携することで、高度な論理的思考やクリエイティブなタスクをより正確にこなすことができます。
|
| 245 |
+
</li>
|
| 246 |
+
<li>
|
| 247 |
+
<strong>推論の透明性の向上:</strong>
|
| 248 |
+
各エージェントの思考プロセスを追跡できるため、従来のブラックボックス問題の一部を解消し、なぜそのような結論に至ったのかをある程度理解しやすくなります。
|
| 249 |
+
</li>
|
| 250 |
+
<li>
|
| 251 |
+
<strong>効率性の向上:</strong>
|
| 252 |
+
サブタスクを並行処理することで、大規模なタスクでも迅速に完了することが可能です。
|
| 253 |
+
</li>
|
| 254 |
+
</ul>
|
| 255 |
+
|
| 256 |
+
<div class="summary">
|
| 257 |
+
<p><strong>まとめ:</strong><br>
|
| 258 |
+
Geminiの「思考」は、単一の巨大なモデルが一気に答えを出すのではなく、複数の専門AIエージェントが協力して問題を解決する、高度に構造化されたプロセスであると理解できます。これにより、より複雑で精度の高いアウトプットが可能になります。</p>
|
| 259 |
+
</div>
|
| 260 |
+
</div>
|
| 261 |
+
|
| 262 |
+
</body>
|
| 263 |
+
</html>
|
| 264 |
+
''')
|
| 265 |
+
|
| 266 |
+
with gr.Row():
|
| 267 |
+
prompt_input = gr.Textbox(label="プロンプトを入力してください", info="例: すき家は色々な問題がりました。結果、客数も減り、経営上、大きな問題が浮上しました。これらを改善し、経営を正常化するにはどんな打ち手があるか教えてください。 最近、AIは破竹の勢いで成長しています。一方でAIが社会に齎す問題点も多くあると思います。AIが抱える問題点をリストアップして、その解決策を教えてください。もし、答えが見つからない場合は、ウェブから検索して答えてください。 地球温暖化がもたらす未来について説明してください。")
|
| 268 |
+
output_text = gr.Textbox(label="生成されたThought", info="gemini thinkで生成されたthoghtが表示されます。", show_copy_button=True)
|
| 269 |
+
summary_text = gr.Textbox(label="結果", info="gemini thinkで生成された結果が表示されます。", show_copy_button=True)
|
| 270 |
+
|
| 271 |
+
with gr.Row():
|
| 272 |
+
generate_button = gr.Button("生成")
|
| 273 |
+
|
| 274 |
+
# ボタン動作
|
| 275 |
+
generate_button.click(generate_content, inputs=prompt_input, outputs=[output_text,summary_text])
|
| 276 |
+
|
| 277 |
+
gemini.launch(favicon_path="favicon.ico")
|