rerank_chat / app.py
myopera9's picture
Update app.py
ed6a54d verified
import os
import cohere
import gradio as gr
from dotenv import load_dotenv
# 環境変数の読み込み
load_dotenv(verbose=True)
# APIキーを環境変数から取得
cohere_api_key = os.getenv("COHERE_API_KEY")
# Cohereクライアントの初期化
try:
cohere_client = cohere.Client(cohere_api_key)
except Exception as e:
print(f"Cohereクライアントの初期化に失敗しました: {e}")
cohere_client = None
def process_query(query, arrayData):
"""
ユーザーのクエリを処理し、再ランキングと回答生成を行う関数。
Gradioのイベントハンドラーとして使用される。
"""
if not cohere_client:
return "エラー: Cohere APIキーが正しく設定されていません。", "", ""
# 1. 再ランキング(Reranking)
try:
cleaned_text = arrayData.replace('\n', '').replace(' ', '')
documents_to_rerank = eval(cleaned_text)
rerank_response = cohere_client.rerank(
query=query,
documents=documents_to_rerank,
top_n=3,
model="rerank-multilingual-v3.0"
)
except Exception as e:
return f"Rerank API call failed: {e}", "", ""
rerank_output = "--- 再ランキングの結果(上位3件) ---\n"
context_for_chat = []
for i, result in enumerate(rerank_response.results):
document_index = result.index
relevance_score = result.relevance_score
reranked_document = documents_to_rerank[document_index]
rerank_output += f"順位: {i+1}, スコア: {relevance_score:.4f}, ドキュメント: {reranked_document}\n"
context_for_chat.append(reranked_document)
# 2. 回答生成(Generation)
tmp = '\n'.join([f"- {doc}" for doc in context_for_chat])
prompt = f"""
以下の情報に基づいて、{query }について具体的に説明してください。
情報:
{tmp}
回答:
"""
try:
response = cohere_client.chat(
model="command-r-plus",
message=prompt
)
chat_output = response.text
except Exception as e:
chat_output = f"Chat API call failed: {e}"
return rerank_output, chat_output
# Gradio BlocksのUIを定義
with gr.Blocks() as demo:
gr.Markdown("# Cohere Rerank と Chat を使った検索強化デモ")
gr.Markdown("Rerankで関連ドキュメントを絞り込み、Chatで回答を生成します。")
with gr.Sidebar(open=False, width=400):
gr.HTML('''
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cohere Rerank: 検索精度を高める強力なツール</title>
<style>
h3, h4 {
color: #2c3e50;
border-bottom: 2px solid #3498db;
padding-bottom: 5px;
}
h3 {
font-size: 2.5em;
text-align: center;
margin-bottom: 20px;
}
h4 {
font-size: 1.8em;
margin-top: 30px;
}
p {
margin-bottom: 15px;
}
ul {
list-style-type: none;
padding: 0;
}
li {
background: #ecf0f1;
padding: 10px 15px;
margin-bottom: 10px;
border-radius: 5px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
li strong {
color: #2980b9;
}
.icon {
font-size: 1.2em;
margin-right: 10px;
}
.highlight {
font-weight: bold;
color: #e74c3c;
}
</style>
</head>
<body>
<h3>Cohere Rerank: 検索結果の精度と関連性を高めるモデル</h3>
<p>Cohere Rerankは、検索結果の精度と関連性を大幅に向上させるためのモデルです。特に、<span class="highlight">Retrieval Augmented Generation (RAG)</span> システムにおいて、その真価を発揮します。</p>
<p>RAGシステムとは、大規模言語モデル(LLM)が外部の知識ベースやドキュメントを参照して回答を生成する仕組みです。しかし、初期の検索段階で取得された情報が必ずしもユーザーの意図に合致するとは限りません。ここでCohere Rerankが活躍します。</p>
<h4><span class="icon">✨</span> Cohere Rerankの主な役割とメリット</h4>
<ul>
<li>
<strong>1. 検索結果の再ランク付け <span class="icon">🎯</span></strong>
<p><strong>初期検索の補完:</strong> キーワード検索やベクトル検索で得られた候補の中から、ユーザーのクエリに対する意味的な関連性を深く理解し、最も適切な情報を優先的に並べ替えます。</p>
<p><strong>ユーザー意図への適合:</strong> 単にキーワードが含まれているだけでなく、ユーザーの本当の目的や背景に合った情報を提供することで、検索体験を向上させます。</p>
</li>
<li>
<strong>2. RAGシステムの性能向上 <span class="icon">🚀</span></strong>
<p><strong>回答の質向上:</strong> LLMに提供する情報源の質が高まるため、生成される回答の精度と信頼性が向上します。</p>
<p><strong>ハルシネーション(幻覚)の抑制:</strong> 不正確な情報や関連性の低い情報がLLMに渡されるのを防ぎ、事実に基づいた回答を生成しやすくします。</p>
<p><strong>応答性の向上とコスト削減:</strong> LLMが処理する情報量が絞り込まれるため、応答速度が向上し、利用コストの削減にも繋がります。</p>
</li>
<li>
<strong>3. 多様なデータ形式と多言語対応 <span class="icon">🌍</span></strong>
<p><strong>幅広いデータ:</strong> テキストだけでなく、メール、レポート、JSON、プログラムコード、表形式のデータなど、多様なデータ形式に対応しています。</p>
<p><strong>多言語サポート:</strong> 100以上の言語をサポートしており、グローバルな検索システムにも適しています。</p>
</li>
</ul>
<h4><span class="icon">🛠️</span> 活用例</h4>
<ul>
<li><strong>チャットボット:</strong> 曖昧な質問に対しても、ユーザーが求める適切な回答を提供し、顧客満足度を向上させます。</li>
<li><strong>社内ナレッジ検索:</strong> 従業員が探している情報を迅速かつ正確に見つけられるようにし、業務効率を向上させます。</li>
<li><strong>ECサイト:</strong> ユーザーの閲覧履歴や購入履歴に基づき、関連性の高い商品を優先表示します。</li>
<li><strong>広告マーケティング:</strong> 検索ワードに最適な広告を、最適な順序で表示します。</li>
</ul>
<p>Cohere Rerankは、既存の検索システムに比較的容易に統合でき、検索品質を大幅に改善できる強力なツールです。</p>
</body>
</html>
''')
#aryData = gr.Textbox(label="データ", value='["AIは、顧客行動の予測やパーソナライズされた広告配信に利用できます。","マーケティングオートメーションツールは、AIの力を借りて顧客エンゲージメントを向上させます。","AIを導入することで、マーケティングキャンペーンの効果をリアルタイムで分析できます。","最新のAI技術により、よりパーソナライズされた顧客体験を提供できます。","AIがもたらすマーケティングの進化は、企業の競争力を高める鍵となります。","ウェブサイトのデザインには、ユーザーインターフェース(UI)とユーザーエクスペリエンス(UX)の最適化が重要です。","SEO対策は、検索エンジンからのオーガニックトラフィックを増やすために不可欠です。"]')
aryData = gr.Textbox(label="データ", value='''
[
"AIは、顧客行動の予測やパーソナライズされた広告配信に利用できます。",
"マーケティングオートメーションツールは、AIの力を借りて顧客エンゲージメントを向上させます。",
"AIを導入することで、マーケティングキャンペーンの効果をリアルタイムで分析できます。",
"最新のAI技術により、よりパーソナライズされた顧客体験を提供できます。",
"AIがもたらすマーケティングの進化は、企業の競争力を高める鍵となります。",
"ウェブサイトのデザインには、ユーザーインターフェース(UI)とユーザーエクスペリエンス(UX)の最適化が重要です。",
"SEO対策は、検索エンジンからのオーガニックトラフィックを増やすために不可欠です。",
]
''')
with gr.Row():
query_input = gr.Textbox(label="クエリを入力", lines=2, info="例: AIを活用したマーケティング戦略について教えてください")
run_button = gr.Button("実行")
with gr.Row():
rerank_output = gr.Textbox(label="再ランキング結果", lines=5, interactive=False)
with gr.Row():
chat_output = gr.Textbox(label="AIによる回答", lines=10, interactive=False, show_copy_button=True)
# ボタンクリック時のアクションを定義
run_button.click(
fn=process_query,
inputs=[query_input, aryData],
outputs=[rerank_output, chat_output]
)
# アプリケーションを起動
if __name__ == "__main__":
demo.launch()