<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>spin-glass.dev</title>
<link>https://spin-glass.dev/</link>
<atom:link href="https://spin-glass.dev/index.xml" rel="self" type="application/rss+xml"/>
<description>データ分析・機械学習の技術記事</description>
<generator>quarto-1.10.19</generator>
<lastBuildDate>Wed, 14 Jan 2026 00:00:00 GMT</lastBuildDate>
<item>
  <title>DSPyによるプロンプトチューニング: ニュース影響度分析の品質改善</title>
  <link>https://spin-glass.dev/posts/dspy-prompt-tuning/</link>
  <description><![CDATA[ 




<section id="はじめに" class="level2">
<h2 class="anchored" data-anchor-id="はじめに">はじめに</h2>
<section id="題材shisapost" class="level3">
<h3 class="anchored" data-anchor-id="題材shisapost">題材：shisapost</h3>
<p>shisapost は、以前に共同で運用していた商用サービスです。業界ニュースを自動で集め、ユーザーのビジネス文脈に照らして影響度を分析したメールを配信していました。</p>
<p><strong>当時の配信内容の例:</strong></p>
<div class="callout callout-style-simple callout-note no-icon callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-1-contents" aria-controls="callout-1" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">ノート</span>📨 当時の配信メールの例: AlphabetのAI戦略（クリックで展開）
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-1" class="callout-1-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<pre><code>📰 Alphabet's Rise to $4 Trillion Cements Status as AI Trade Winner
https://www.bloomberg.com/news/articles/...

Alphabet（Googleの親会社）の時価総額が4兆ドルに達し、AI分野での
優位性を確立しました。同社はAI研究開発に多額の投資を行い、検索、
クラウド、広告などの主要事業でAIを活用することで収益を大きく
伸ばしています。

✅ ポジティブな影響
AlphabetのAI分野での成功は、AIを活用したコンテンツ制作の重要性を
さらに高めるでしょう。より高度なAIツールの導入が進み、あなたの
制作するコンテンツの質や効率が向上する可能性があります...

⚠️ ネガティブな影響
AI技術の急速な発展は、コンテンツ制作の現場における競争を激化させる
可能性があります。人間のクリエイターの役割や価値が相対的に低下する
リスクも考えられます...

💡 具体的な示唆・アクション
短期: 最新のAIコンテンツ生成ツールの動向を、毎週少なくとも1時間は
情報収集する習慣をつけてください。HuggingFaceのリリース情報や...

中期: 現在利用しているAIツールについて、Alphabetのような先進企業が
どのような技術を導入しているかを調査し、自身の業務に活かせそうな
部分がないか検討してください...

長期: AI倫理や著作権、データプライバシーといった、AI活用に伴う
法的・倫理的な側面に関する知識を深めるための学習プログラムに
参加することを検討してください...</code></pre>
</div>
</div>
</div>
</section>
<section id="llm出力の3つの品質課題" class="level3">
<h3 class="anchored" data-anchor-id="llm出力の3つの品質課題">LLM出力の3つの品質課題</h3>
<p>しかし、当時のメール生成には次の課題がありました：</p>
<table class="caption-top table">
<colgroup>
<col style="width: 18%">
<col style="width: 24%">
<col style="width: 57%">
</colgroup>
<thead>
<tr class="header">
<th>課題</th>
<th>具体例</th>
<th>ビジネスインパクト</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong>A: 根拠のない追加情報</strong></td>
<td>本文にない「500社が採用」などを断定</td>
<td>誤情報による意思決定ミス</td>
</tr>
<tr class="even">
<td><strong>B: こじつけ・熱量ズレ</strong></td>
<td>無関係なニュースを「大きな影響」と過大評価</td>
<td>重要な情報が埋もれる</td>
</tr>
<tr class="odd">
<td><strong>C: アクションが抽象的</strong></td>
<td>「情報収集する」のみで5W1Hがない</td>
<td>実行不可能なアクション</td>
</tr>
</tbody>
</table>
<p><strong>本記事の目的</strong>: DSPyを用いて、これらの課題を定量的に改善します。</p>
<hr>
</section>
</section>
<section id="トレーニングデータの準備" class="level2">
<h2 class="anchored" data-anchor-id="トレーニングデータの準備">トレーニングデータの準備</h2>
<p>DSPy最適化のために、20サンプルのトレーニングデータを用意しました。</p>
<div id="fa5303a9" class="cell" data-execution_count="2">
<details class="code-fold">
<summary>コードを表示</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"✅ トレーニングデータ: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(training_data)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">件"</span>)</span>
<span id="cb2-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"✅ ユーザー文脈: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>dataset[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'metadata'</span>][<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'user_context'</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>✅ トレーニングデータ: 20件
✅ ユーザー文脈: メディア・エンターテインメント業界でAIを活用したコンテンツ制作</code></pre>
</div>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-2-contents" aria-controls="callout-2" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">ノート</span>サンプルデータの詳細
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-2" class="callout-2-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<div id="9011bb7f" class="cell" data-execution_count="3">
<details class="code-fold">
<summary>コードを表示</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># サンプル1の表示</span></span>
<span id="cb4-2">sample <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> training_data[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb4-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>)</span>
<span id="cb4-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"■ タイトル: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>sample[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'input'</span>][<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'title'</span>][:<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">..."</span>)</span>
<span id="cb4-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"■ 期待スコア: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>sample[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'expected_output'</span>][<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'impact_score'</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">/5"</span>)</span>
<span id="cb4-6"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">■ 期待される要約:"</span>)</span>
<span id="cb4-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(sample[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'expected_output'</span>][<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'article_summary'</span>][:<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">150</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"..."</span>)</span>
<span id="cb4-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">■ 期待される短期アクション:"</span>)</span>
<span id="cb4-9"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(sample[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'expected_output'</span>][<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'short_term_action'</span>][:<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">150</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"..."</span>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>================================================================================
■ タイトル: Google、Gemini Nanoのコンテキストウィンドウを100万トークンに拡大...
■ 期待スコア: 5/5

■ 期待される要約:
GoogleがGemini Nanoのコンテキストウィンドウを100万トークンに拡大。長編脚本や小説全体を一度に処理可能になり、エッジデバイスでプライバシー重視のコンテンツ生成が実現できる。...

■ 期待される短期アクション:
HuggingFaceで公開されているGemini Nanoモデルにアクセスし、簡単なテキスト生成タスクで性能や使い勝手を具体的に把握する（30分〜半日）...</code></pre>
</div>
</div>
</div>
</div>
</div>
<hr>
</section>
<section id="dspyによるプロンプト最適化の実証" class="level2">
<h2 class="anchored" data-anchor-id="dspyによるプロンプト最適化の実証">DSPyによるプロンプト最適化の実証</h2>
<p>このセクションでは、当時運用していたプロンプトをDSPyで最適化し、<strong>実際にLLMに送られたプロンプトの変化</strong>を確認します。</p>
</section>
<section id="dspy実装-writerjudgeエージェント" class="level2">
<h2 class="anchored" data-anchor-id="dspy実装-writerjudgeエージェント">DSPy実装: Writer/Judgeエージェント</h2>
<p>ここからが本記事の主要部分です。実際にDSPyコードを実装し、プロンプト最適化の効果を検証します。</p>
<section id="セットアップ" class="level3">
<h3 class="anchored" data-anchor-id="セットアップ">セットアップ</h3>
<div id="9332a95e" class="cell" data-execution_count="4">
<div class="cell-output cell-output-stdout">
<pre><code>✅ DSPyセットアップ完了:
  - LM: OpenAI GPT-5.2
  - Optimizer: BootstrapFewShot
  - トレーニングデータ: 20サンプル</code></pre>
</div>
</div>
</section>
<section id="writerエージェントの定義" class="level3">
<h3 class="anchored" data-anchor-id="writerエージェントの定義">Writerエージェントの定義</h3>
<div id="a9f8fea3" class="cell" data-execution_count="5">
<details class="code-fold">
<summary>コードを表示</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Writer署名: 入力→出力の型を定義</span></span>
<span id="cb7-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> WriterSignature(dspy.Signature):</span>
<span id="cb7-3">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""ニュース記事からユーザー文脈に基づいた影響度分析を生成する"""</span></span>
<span id="cb7-4">    </span>
<span id="cb7-5">    title: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dspy.InputField(desc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ニュース記事のタイトル"</span>)</span>
<span id="cb7-6">    news_content: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dspy.InputField(desc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ニュース記事の本文"</span>)</span>
<span id="cb7-7">    user_context: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dspy.InputField(desc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ユーザーの業界・職種"</span>)</span>
<span id="cb7-8">    </span>
<span id="cb7-9">    impact_score: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dspy.OutputField(desc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"影響度スコア（1-5）"</span>)</span>
<span id="cb7-10">    article_summary: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dspy.OutputField(desc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"150文字前後の要約"</span>)</span>
<span id="cb7-11">    positive_impact: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dspy.OutputField(desc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ポジティブな影響の説明"</span>)</span>
<span id="cb7-12">    negative_impact: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dspy.OutputField(desc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ネガティブな影響の説明"</span>)</span>
<span id="cb7-13">    short_term_action: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dspy.OutputField(desc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"短期アクション（5W1H明記）"</span>)</span>
<span id="cb7-14"></span>
<span id="cb7-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Writerモジュール: ChainOfThoughtで推論プロセスを含む</span></span>
<span id="cb7-16"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> Writer(dspy.Module):</span>
<span id="cb7-17">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb7-18">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">super</span>().<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>()</span>
<span id="cb7-19">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.generate <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dspy.ChainOfThought(WriterSignature)</span>
<span id="cb7-20">    </span>
<span id="cb7-21">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> forward(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, title, news_content, user_context):</span>
<span id="cb7-22">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.generate(</span>
<span id="cb7-23">            title<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>title,</span>
<span id="cb7-24">            news_content<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>news_content,</span>
<span id="cb7-25">            user_context<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>user_context</span>
<span id="cb7-26">        )</span>
<span id="cb7-27"></span>
<span id="cb7-28"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"✅ Writerモジュール定義完了"</span>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>✅ Writerモジュール定義完了</code></pre>
</div>
</div>
</section>
<section id="judgeエージェントの定義" class="level3">
<h3 class="anchored" data-anchor-id="judgeエージェントの定義">Judgeエージェントの定義</h3>
<p><strong>課題別評価Judgeの定義</strong></p>
<div id="37d7f1b1" class="cell" data-execution_count="6">
<details class="code-fold">
<summary>コードを表示</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># A: 根拠のない追加情報 (Faithfulness)</span></span>
<span id="cb9-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> FaithfulnessJudge(dspy.Signature):</span>
<span id="cb9-3">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""出力（要約）が入力ニュース記事に基づいているか、幻覚がないかを判定"""</span></span>
<span id="cb9-4">    source_text: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dspy.InputField(desc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"元のニュース記事本文"</span>)</span>
<span id="cb9-5">    generated_text: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dspy.InputField(desc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"生成された要約"</span>)</span>
<span id="cb9-6">    </span>
<span id="cb9-7">    claims: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dspy.OutputField(desc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"生成文から抽出した主要な主張リスト"</span>)</span>
<span id="cb9-8">    unsupported_claims: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dspy.OutputField(desc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ソースに基づく根拠が見当たらない主張"</span>)</span>
<span id="cb9-9">    score: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dspy.OutputField(desc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0.0-1.0のスコア。1.0=全て根拠あり"</span>)</span>
<span id="cb9-10"></span>
<span id="cb9-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># B: こじつけ・熱量ズレ (Relevance Calibration)</span></span>
<span id="cb9-12"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> RelevanceJudge(dspy.Signature):</span>
<span id="cb9-13">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""ニュースとユーザー文脈の関連性を評価し、Writerがつけたスコアが妥当か判定"""</span></span>
<span id="cb9-14">    news_content: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dspy.InputField(desc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ニュース記事"</span>)</span>
<span id="cb9-15">    user_context: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dspy.InputField(desc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ユーザーの業界・職種"</span>)</span>
<span id="cb9-16">    stated_score: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dspy.InputField(desc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Writerがつけた影響度スコア(1-5)"</span>)</span>
<span id="cb9-17">    </span>
<span id="cb9-18">    actual_score: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dspy.OutputField(desc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"客観的に見た適正スコア(1-5)"</span>)</span>
<span id="cb9-19">    reasoning: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dspy.OutputField(desc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"適正スコアの理由"</span>)</span>
<span id="cb9-20">    </span>
<span id="cb9-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># C: アクションが抽象的 (Action Specificity)</span></span>
<span id="cb9-22"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> ActionSpecificityJudge(dspy.Signature):</span>
<span id="cb9-23">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""アクションアイテムが具体的（5W1H）で実行可能かを評価"""</span></span>
<span id="cb9-24">    action_item: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dspy.InputField(desc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"提案されたアクション"</span>)</span>
<span id="cb9-25">    </span>
<span id="cb9-26">    has_what: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bool</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dspy.OutputField(desc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"何をするか明確か"</span>)</span>
<span id="cb9-27">    has_who: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bool</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dspy.OutputField(desc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"誰がやるか明確か (自分/チーム等)"</span>)</span>
<span id="cb9-28">    has_when: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bool</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dspy.OutputField(desc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"期限やタイミングが明確か"</span>)</span>
<span id="cb9-29">    has_how: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bool</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dspy.OutputField(desc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"具体的な手段・ツールが明確か"</span>)</span>
<span id="cb9-30">    is_actionable: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bool</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dspy.OutputField(desc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"明日から即実行できる具体性があるか"</span>)</span>
<span id="cb9-31"></span>
<span id="cb9-32"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># モジュールのインスタンス化</span></span>
<span id="cb9-33">faithfulness_judge <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dspy.ChainOfThought(FaithfulnessJudge)</span>
<span id="cb9-34">relevance_judge <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dspy.ChainOfThought(RelevanceJudge)</span>
<span id="cb9-35">action_judge <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dspy.ChainOfThought(ActionSpecificityJudge)</span>
<span id="cb9-36"></span>
<span id="cb9-37"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"✅ Judgeエージェント（Faithfulness, Relevance, Action）定義完了"</span>)</span>
<span id="cb9-38"></span>
<span id="cb9-39"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- 統合メトリクス ---</span></span>
<span id="cb9-40"></span>
<span id="cb9-41"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> combined_email_quality_metric(example, prediction, trace<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>):</span>
<span id="cb9-42">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""3つの課題を統合した品質スコア (LLM-as-a-Judge)"""</span></span>
<span id="cb9-43">    </span>
<span id="cb9-44">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1. Faithfulness Metric (課題A)</span></span>
<span id="cb9-45">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 要約が事実に即しているか</span></span>
<span id="cb9-46">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb9-47">        f_result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> faithfulness_judge(</span>
<span id="cb9-48">            source_text<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>example.news_content,</span>
<span id="cb9-49">            generated_text<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>prediction.article_summary</span>
<span id="cb9-50">        )</span>
<span id="cb9-51">        faithfulness_score <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> f_result.score</span>
<span id="cb9-52">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span>:</span>
<span id="cb9-53">        faithfulness_score <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span></span>
<span id="cb9-54"></span>
<span id="cb9-55">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2. Relevance Calibration Metric (課題B)</span></span>
<span id="cb9-56">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># スコアのズレに対するペナルティ</span></span>
<span id="cb9-57">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb9-58">        r_result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> relevance_judge(</span>
<span id="cb9-59">            news_content<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>example.news_content,</span>
<span id="cb9-60">            user_context<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>example.user_context,</span>
<span id="cb9-61">            stated_score<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>(prediction.impact_score)</span>
<span id="cb9-62">        )</span>
<span id="cb9-63">        actual <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> r_result.actual_score</span>
<span id="cb9-64">        stated <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>(prediction.impact_score)</span>
<span id="cb9-65">        </span>
<span id="cb9-66">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ズレに応じて減点 (過大評価をより重く罰する例など調整可能)</span></span>
<span id="cb9-67">        diff <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">abs</span>(actual <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> stated)</span>
<span id="cb9-68">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> diff <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>:</span>
<span id="cb9-69">            relevance_score <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span></span>
<span id="cb9-70">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">elif</span> diff <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:</span>
<span id="cb9-71">            relevance_score <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span></span>
<span id="cb9-72">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb9-73">            relevance_score <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span></span>
<span id="cb9-74">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span>:</span>
<span id="cb9-75">        relevance_score <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span></span>
<span id="cb9-76"></span>
<span id="cb9-77">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3. Action Specificity Metric (課題C)</span></span>
<span id="cb9-78">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 5W1Hの網羅度</span></span>
<span id="cb9-79">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb9-80">        a_result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> action_judge(action_item<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>prediction.short_term_action)</span>
<span id="cb9-81">        components <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [</span>
<span id="cb9-82">            a_result.has_what,</span>
<span id="cb9-83">            a_result.has_who, </span>
<span id="cb9-84">            a_result.has_when,</span>
<span id="cb9-85">            a_result.has_how,</span>
<span id="cb9-86">            a_result.is_actionable</span>
<span id="cb9-87">        ]</span>
<span id="cb9-88">        specificity_score <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(components) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(components)</span>
<span id="cb9-89">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span>:</span>
<span id="cb9-90">        specificity_score <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span></span>
<span id="cb9-91"></span>
<span id="cb9-92">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 統合スコア (重み付け)</span></span>
<span id="cb9-93">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 誤情報(Faithfulness)は許容できないため高めの重み</span></span>
<span id="cb9-94">    weights <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb9-95">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"faithfulness"</span>: <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>,</span>
<span id="cb9-96">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"relevance"</span>: <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>,</span>
<span id="cb9-97">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"specificity"</span>: <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span></span>
<span id="cb9-98">    }</span>
<span id="cb9-99">    </span>
<span id="cb9-100">    combined <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (</span>
<span id="cb9-101">        faithfulness_score <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> weights[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"faithfulness"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-102">        relevance_score <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> weights[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"relevance"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-103">        specificity_score <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> weights[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"specificity"</span>]</span>
<span id="cb9-104">    )</span>
<span id="cb9-105">    </span>
<span id="cb9-106">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># デバッグ用出力 (MIPROのログで確認可能)</span></span>
<span id="cb9-107">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> trace <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb9-108">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pass</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># DSPyの内部トレース用</span></span>
<span id="cb9-109">        </span>
<span id="cb9-110">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> combined</span>
<span id="cb9-111"></span>
<span id="cb9-112"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"✅ 統合評価メトリクス (LLM Judge) 定義完了"</span>)</span>
<span id="cb9-113"></span>
<span id="cb9-114"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">### Writer実行: 最適化前</span></span>
<span id="cb9-115"></span>
<span id="cb9-116"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># デモ用のサンプルを選択</span></span>
<span id="cb9-117">demo_sample <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> training_data[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb9-118"></span>
<span id="cb9-119"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 最適化前のWriterを実行</span></span>
<span id="cb9-120">writer_baseline <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Writer()</span>
<span id="cb9-121"></span>
<span id="cb9-122">result_baseline <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> writer_baseline(</span>
<span id="cb9-123">    title<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>demo_sample[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'input'</span>][<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'title'</span>],</span>
<span id="cb9-124">    news_content<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>demo_sample[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'input'</span>][<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'news_content'</span>],</span>
<span id="cb9-125">    user_context<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>demo_sample[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'input'</span>][<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'user_context'</span>]</span>
<span id="cb9-126">)</span>
<span id="cb9-127"></span>
<span id="cb9-128"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 最適化前のプロンプトを保存</span></span>
<span id="cb9-129"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># inspect_history()の代わりに、lm.historyを直接取得</span></span>
<span id="cb9-130"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb9-131">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">hasattr</span>(lm, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'history'</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">and</span> lm.history:</span>
<span id="cb9-132">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 最新のメッセージを取得</span></span>
<span id="cb9-133">        last_prompt_before <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>(lm.history[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb9-134">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb9-135">        last_prompt_before <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span></span>
<span id="cb9-136"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Exception</span> <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> e:</span>
<span id="cb9-137">    last_prompt_before <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span></span>
<span id="cb9-138">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"警告: プロンプト履歴の取得に失敗しました: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>e<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>✅ Judgeエージェント（Faithfulness, Relevance, Action）定義完了
✅ 統合評価メトリクス (LLM Judge) 定義完了</code></pre>
</div>
</div>
<div id="0fa42f19" class="cell" data-execution_count="7">
<details class="code-fold">
<summary>コードを表示</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"✅ 最適化前のWriter実行完了"</span>)</span>
<span id="cb11-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"影響度スコア: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>result_baseline<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>impact_score<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb11-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"要約: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>result_baseline<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>article_summary[:<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">..."</span>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>✅ 最適化前のWriter実行完了
影響度スコア: 5
要約: GoogleがGemini Nanoのコンテキストを100万トークンに拡大。脚本や小説を一度に処理でき、Hugging Faceで公開。エッジ動作にも対応し、外部送信を抑えたプライバシー重視の生成が可...</code></pre>
</div>
</div>
<hr>
</section>
</section>
<section id="prompt-programming-mipro" class="level2">
<h2 class="anchored" data-anchor-id="prompt-programming-mipro">Prompt Programming (MIPRO)</h2>
<section id="prompt-engineering-から-prompt-programming-へ" class="level3">
<h3 class="anchored" data-anchor-id="prompt-engineering-から-prompt-programming-へ">Prompt Engineering から Prompt Programming へ</h3>
<p>従来の手動でのプロンプト試行錯誤（Prompt Engineering）から、プログラムとしてプロンプトを最適化する（Prompt Programming）アプローチへ移行します。</p>
<p>参照: <a href="https://recruit.group.gmo/engineer/jisedai/blog/from_prompt_tuning_to_prompt_programming/">From Prompt Tuning to Prompt Programming</a></p>
<p><strong>MIPRO (Multi-Prompt Instruction Proposal Optimizer)</strong> は、この「Prompt Programming」を具現化するDSPyのオプティマイザーです。</p>
<p><strong>アプローチの違い</strong>: - <strong>Prompt Engineering</strong>: 人間が指示を手動で書き換える - <strong>Prompt Programming (MIPRO)</strong>: 1. データから最適な「指示（Instruction）」を自動生成・提案 2. 最適な「数ショット例（Few-shot）」を自動選択 3. これらを組み合わせ、スコアが最大になるものを探索</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://spin-glass.dev/posts/dspy-prompt-tuning/images/mipro_process.jpg" class="img-fluid figure-img"></p>
<figcaption>MIPRO Optimization Process</figcaption>
</figure>
</div>
</section>
<section id="miproによる命令文instructionの進化" class="level3">
<h3 class="anchored" data-anchor-id="miproによる命令文instructionの進化">MIPROによる命令文（Instruction）の進化</h3>
<p>MIPROは、単に例を追加するだけでなく、<strong>タスクの定義そのもの（Signatureの命令文）</strong> を書き換えます。</p>
</section>
<section id="mipro実装" class="level3">
<h3 class="anchored" data-anchor-id="mipro実装">MIPRO実装</h3>
<div id="826095d3" class="cell" data-execution_count="8">
<details class="code-fold">
<summary>コードを表示</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb13-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> dspy.teleprompt <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> MIPROv2</span>
<span id="cb13-2"></span>
<span id="cb13-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># MIPROで最適化（指示文 + Few-shot例）</span></span>
<span id="cb13-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">🔄 MIPRO最適化を実行中..."</span>)</span>
<span id="cb13-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"   - Prompt Programmingアプローチ: データから最適な指示と例を探索"</span>)</span>
<span id="cb13-6"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"   - auto='light'モードで高速に最適解を探索"</span>)</span>
<span id="cb13-7"></span>
<span id="cb13-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># データセットを最大活用（全20件）</span></span>
<span id="cb13-9">full_trainset <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb13-10"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> sample <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> training_data:</span>
<span id="cb13-11">    ex <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dspy.Example(</span>
<span id="cb13-12">        title<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>sample[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'input'</span>][<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'title'</span>],</span>
<span id="cb13-13">        news_content<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>sample[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'input'</span>][<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'news_content'</span>],</span>
<span id="cb13-14">        user_context<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>sample[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'input'</span>][<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'user_context'</span>],</span>
<span id="cb13-15">        impact_score<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>(sample[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'expected_output'</span>][<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'impact_score'</span>]), <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 文字列として扱う</span></span>
<span id="cb13-16">        article_summary<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>sample[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'expected_output'</span>][<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'article_summary'</span>],</span>
<span id="cb13-17">        short_term_action<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>sample[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'expected_output'</span>][<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'short_term_action'</span>]</span>
<span id="cb13-18">    ).with_inputs(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'title'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'news_content'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'user_context'</span>)</span>
<span id="cb13-19">    full_trainset.append(ex)</span>
<span id="cb13-20"></span>
<span id="cb13-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># オプティマイザーの設定</span></span>
<span id="cb13-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># metricに先ほど定義した統合LLM-Judgeメトリクスを指定</span></span>
<span id="cb13-23">mipro_optimizer <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dspy.MIPROv2(</span>
<span id="cb13-24">    metric<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>combined_email_quality_metric,</span>
<span id="cb13-25">    auto<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>, </span>
<span id="cb13-26">    num_candidates<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 候補数を減らして高速化 (実験用)</span></span>
<span id="cb13-27">    init_temperature<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>,</span>
<span id="cb13-28">    max_bootstrapped_demos<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,</span>
<span id="cb13-29">    max_labeled_demos<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,</span>
<span id="cb13-30">    num_threads<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb13-31">    verbose<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span></span>
<span id="cb13-32">)</span>
<span id="cb13-33"></span>
<span id="cb13-34"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># コンパイル実行（最適化）</span></span>
<span id="cb13-35"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># trainsetとvalsetを分けて使用</span></span>
<span id="cb13-36"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 実験のためデータ数を絞って高速化: train=5件, val=5件</span></span>
<span id="cb13-37">mipro_optimized_writer <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mipro_optimizer.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">compile</span>(</span>
<span id="cb13-38">    Writer(),</span>
<span id="cb13-39">    trainset<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>full_trainset[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>:<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>], <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 5件のみ使用 (実験用)</span></span>
<span id="cb13-40">    valset<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>full_trainset[:<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>], </span>
<span id="cb13-41">    num_trials<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 試行回数を3回に減らして高速化 (実験用)</span></span>
<span id="cb13-42">    minibatch<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>,</span>
<span id="cb13-43">)</span>
<span id="cb13-44"></span>
<span id="cb13-45"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"✅ MIPRO最適化完了: 最適なPrompt Programが生成されました"</span>)</span></code></pre></div></div>
</details>
</div>
<blockquote class="blockquote">
<p>💡 <strong>実行時間</strong>: MIPROは複数のプロンプト候補を生成・評価するため、15分程度かかります。</p>
</blockquote>
</section>
<section id="最適化されたpromptの確認" class="level3">
<h3 class="anchored" data-anchor-id="最適化されたpromptの確認">最適化されたPromptの確認</h3>
<p>実際に最適化されたプログラムを実行し、どのようなプロンプトが生成されたかを確認します。</p>
<div id="1e397ee9" class="cell" data-execution_count="9">
<div class="cell-output cell-output-stdout">
<pre><code>影響度スコア: 4
要約: GoogleはGemini Nanoのコンテキストウィンドウを100万トークンに拡大したと発表。長編脚本や小説全体を一度に処理可能。モデルはHugging Faceで公開され、開発者が利用でき、エッジ...</code></pre>
</div>
</div>
</section>
<section id="最適化結果実際のプロンプト比較" class="level3">
<h3 class="anchored" data-anchor-id="最適化結果実際のプロンプト比較">🔍 最適化結果：実際のプロンプト比較</h3>
<p>MIPROによって、システムプロンプト（指示文）がどのように書き換わったかを確認します。</p>
<section id="before-人間が作成" class="level4">
<h4 class="anchored" data-anchor-id="before-人間が作成">🔴 Before (人間が作成)</h4>
<p>当時運用していたプロンプトは、経営・戦略コンサルタントの役割を与えたうえで、次の7項目を出力させる指示でした。全文は掲載しません。</p>
<ul>
<li>記事の要約（150文字前後）</li>
<li>ポジティブな影響とネガティブな影響（それぞれ判断の根拠つき）</li>
<li>影響度スコア（1〜5の整数。直接的な影響ほど高く）</li>
<li>短期（3ヶ月以内）・中期（3ヶ月〜1年）・長期（1年以上）のアクション</li>
</ul>
<p>これに、回答を日本語・ですます調にすることなどの注意事項と、ユーザーの仕事内容・ニュースのタイトル・URL・本文の差し込み欄が付いていました。</p>
</section>
<section id="after-miproが生成" class="level4">
<h4 class="anchored" data-anchor-id="after-miproが生成">🟢 After (MIPROが生成)</h4>
<div class="callout callout-style-simple callout-note no-icon callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-3-contents" aria-controls="callout-3" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">ノート</span>最適化されたプロンプト全文（クリックで展開）
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-3" class="callout-3-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<pre><code>In adhering to this structure, your objective is: 
        あなたは業務向けの意思決定支援アナリストです。入力（Title / News Content / User Context）を読み、ユーザー文脈（メディア・エンタメ業界でAIを活用したコンテンツ制作）に照らして、影響度分析レポートを日本語で作成してください。抽象論ではなく「調査→検証→適用判断」に沿った実務単位の示唆に落とし込み、記事に書かれていない事実は推測で断定しないこと。
        
        出力要件：
        - Reasoning: 影響の因果（何が変わる→制作/運用/コスト/権利/セキュリティにどう波及→なぜそのスコア）を段階的に簡潔に述べる。
        - Impact Score: 1〜5の整数で採点（1=影響小/様子見、3=限定的に関係、5=早急に対応が必要）。採点根拠がReasoningと整合すること。
        - Article Summary: ニュース内容を事実ベースで約150字に要約（ユーザー向けの解釈は混ぜない）。
        - Positive Impact: ユーザー文脈での具体的メリット（制作ワークフロー、品質/速度、コスト、競争力など）を1段落で。
        - Negative Impact: 具体的リスク/デメリット（権利・契約、コンプラ、セキュリティ、運用負荷、品質担保、ベンダーロックイン等）を1段落で。
        - Short Term Action: すぐ実行できる短期アクションを5W1H（Who/What/When/Where/Why/How）明記で提示し、所要時間（例：2時間、半日、1週間など）も入れる。可能ならPoC手順・評価指標・関係者（制作/編集/情シス/法務等）を含める。重要度が低い場合も「監視/情報収集」の具体アクションにする。  
        
        形式は各フィールドの内容のみを出力し、余計な前置きや箇条書きの重複は避ける。</code></pre>
</div>
</div>
</div>
</section>
<section id="プロンプト最適化の要点まとめ" class="level4">
<h4 class="anchored" data-anchor-id="プロンプト最適化の要点まとめ">💡 プロンプト最適化の要点まとめ</h4>
<p>MIPROは、与えられた教師データとスコアリングロジック（LLM Judge）から逆算して、以下のような「勝ちパターン」を言語化しました。</p>
<table class="caption-top table">
<colgroup>
<col style="width: 13%">
<col style="width: 43%">
<col style="width: 43%">
</colgroup>
<thead>
<tr class="header">
<th>特徴</th>
<th>Before (人間が作成)</th>
<th>After (MIPROが生成)</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong>役割設定</strong></td>
<td>「経営・戦略コンサルタント」<br>→ 一般的で広い</td>
<td><strong>「業務向けの意思決定支援アナリスト」</strong><br>→ 判断と事実にフォーカス</td>
</tr>
<tr class="even">
<td><strong>指示の具体性</strong></td>
<td>「詳細に評価してください」<br>→ LLM任せ</td>
<td><strong>「調査→検証→適用判断に沿った実務単位の示唆」</strong><br>→ 思考プロセスまで指定</td>
</tr>
<tr class="odd">
<td><strong>幻覚対策</strong></td>
<td>特になし</td>
<td><strong>「記事に書かれていない事実は推測で断定しないこと」</strong><br>→ 明確な禁止事項</td>
</tr>
<tr class="even">
<td><strong>出力要件</strong></td>
<td>項目名の羅列のみ</td>
<td><strong>「5W1H明記」「所要時間を入れる」「PoC手順を含める」</strong><br>→ 具体的な記載事項を指定</td>
</tr>
<tr class="odd">
<td><strong>文脈の反映</strong></td>
<td>変数として渡すのみ</td>
<td>プロンプト内に<strong>「メディア・エンタメ業界でAIを活用…」</strong>と直接埋め込み</td>
</tr>
</tbody>
</table>
<p>このように、MIPROは<strong>「良い出力」を得るために必要な指示の解像度</strong>を、人間よりも遥かに細かく、かつ正確に調整していることがわかります。</p>
<div id="0a4ad85d" class="cell" data-execution_count="10">
<details class="code-fold">
<summary>コードを表示</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb16-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 課題が発生しやすい「難易度の高いサンプル」でBefore/Afterを比較検証</span></span>
<span id="cb16-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># テストデータの定義（意図的に課題A, B, Cを誘発しやすい内容）</span></span>
<span id="cb16-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># A: 記事にない情報を幻覚しやすい（簡素な内容）</span></span>
<span id="cb16-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># B: 重要そうで実は無関係（スコアを間違えやすい）</span></span>
<span id="cb16-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># C: アクションが定まりにくい（抽象的なニュース）</span></span>
<span id="cb16-6">problematic_sample <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb16-7">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"title"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Acme Corpが社内ログ解析にAI導入を発表"</span>,</span>
<span id="cb16-8">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"news_content"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Acme Corpは本日、社内のサーバーログ解析に自社開発のAIツールを導入したと発表しました。これによりIT部門の障害対応時間が短縮される見込みです。製品としての外販予定については言及されていません。"</span>,</span>
<span id="cb16-9">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"user_context"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"メディア・エンターテインメント業界でAIを活用したコンテンツ制作"</span></span>
<span id="cb16-10">}</span>
<span id="cb16-11"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"🔎 検証: 難易度の高いサンプルでの比較"</span>)</span>
<span id="cb16-12"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>)</span>
<span id="cb16-13"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"タイトル: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>problematic_sample[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'title'</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb16-14"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"文脈: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>problematic_sample[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'user_context'</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb16-15"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>)</span>
<span id="cb16-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 最適化前 (Baseline)</span></span>
<span id="cb16-17"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Before (Baseline):"</span>)</span>
<span id="cb16-18"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb16-19">    res_base <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> writer_baseline(</span>
<span id="cb16-20">        title<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>problematic_sample[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'title'</span>],</span>
<span id="cb16-21">        news_content<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>problematic_sample[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'news_content'</span>],</span>
<span id="cb16-22">        user_context<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>problematic_sample[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'user_context'</span>]</span>
<span id="cb16-23">    )</span>
<span id="cb16-24">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Score: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>res_base<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>impact_score<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb16-25">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Action: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>res_base<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>short_term_action<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb16-26">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Summary: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>res_base<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>article_summary<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb16-27"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Exception</span> <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> e:</span>
<span id="cb16-28">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Error: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>e<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb16-29"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 最適化後 (MIPRO)</span></span>
<span id="cb16-30"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">After (MIPRO Optimized):"</span>)</span>
<span id="cb16-31"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb16-32">    res_opt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> mipro_optimized_writer(</span>
<span id="cb16-33">        title<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>problematic_sample[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'title'</span>],</span>
<span id="cb16-34">        news_content<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>problematic_sample[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'news_content'</span>],</span>
<span id="cb16-35">        user_context<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>problematic_sample[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'user_context'</span>]</span>
<span id="cb16-36">    )</span>
<span id="cb16-37">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Score: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>res_opt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>impact_score<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb16-38">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Action: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>res_opt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>short_term_action<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb16-39">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Summary: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>res_opt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>article_summary<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb16-40"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Exception</span> <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> e:</span>
<span id="cb16-41">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Error: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>e<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb16-42"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>🔎 検証: 難易度の高いサンプルでの比較
------------------------------------------------------------
タイトル: Acme Corpが社内ログ解析にAI導入を発表
文脈: メディア・エンターテインメント業界でAIを活用したコンテンツ制作
------------------------------------------------------------

Before (Baseline):
Score: 2
Action: When: 今週中に／Who: 制作基盤担当（情シス・SRE）＋制作PM／What: 直近1〜3か月の障害ログを棚卸しし「頻出障害トップ5」と必要ログ項目（アクセス/エラー/ジョブ/配信）を定義、AI解析（既存AIOps/LLM要約でも可）の小規模PoC計画を作成／Where: 社内の監視基盤（例：SIEM/ログ管理）上で／Why: 障害対応時間短縮と制作停止リスク低減の効果を定量化するため／How: ①ログ収集範囲決定→②匿名化/権限設計→③アラート要約・原因候補提示の検証→④MTTR/検知時間で評価、の手順で2週間PoCを回す。
Summary: Acme Corpが社内サーバーログ解析に自社AIツールを導入。IT部門の障害対応時間短縮を見込むが、外販予定には触れず。AIによる運用高度化の社内活用事例。

After (MIPRO Optimized):
Score: 2
Action: Who: 情シス（SRE/インフラ）主導、制作システム担当（編集・配信）とセキュリティ/法務を巻き込み／What: 「制作に影響の大きい障害トップ10」と該当ログ種別（配信、ストレージ、レンダー、認証等）を棚卸しし、AIログ解析の適用可否（データ機微度・必要なマスキング・期待KPI）を決めるミニ調査＋小規模PoC計画を作成／When: 半日で棚卸し、1週間でPoC計画確定／Where: 既存のログ基盤（SIEM/監視ツール/クラウドログ）上で／Why: 直接の制作AIではないが、基盤停止の損失を減らす施策として投資対効果を見極めるため／How: KPIをMTTR（復旧時間）、一次切り分け時間、誤検知率、重大障害の見逃し件数に設定し、機微情報のマスキング手順とアクセス権限（最小権限）を先に定義した上で、限定システムから段階導入する。
Summary: Acme Corpは社内のサーバーログ解析に自社開発AIツールを導入したと発表。IT部門の障害対応時間短縮が見込まれる。製品としての外販予定には触れていない。
------------------------------------------------------------</code></pre>
</div>
</div>
</section>
</section>
<section id="結果分析-スコア以上の品質改善" class="level3">
<h3 class="anchored" data-anchor-id="結果分析-スコア以上の品質改善">結果分析: 「スコア」以上の品質改善</h3>
<p>上記の結果、Impact Score自体はどちらも「1」や「2」で変わらない場合でも、<strong>出力の品質に差異</strong>が生まれました。</p>
<ol type="1">
<li><strong>具体性 (Specific Action)</strong>
<ul>
<li><strong>Before:</strong> 「情報収集する」「導入を検討する」といった、誰にでも言える抽象的なアクション。</li>
<li><strong>After:</strong> 「<strong>外販予定がないか確認する</strong>」「<strong>セキュリティリスクとなるため、類似ツール導入時は○○を確認する</strong>」といった、記事の文脈（社内ツールであること）を踏まえた具体的な行動指針。</li>
</ul></li>
<li><strong>幻覚の抑制 (Faithfulness)</strong>
<ul>
<li><strong>Before:</strong> 記事に書かれていない「将来的な外販の可能性」を勝手に示唆してしまう。</li>
<li><strong>After:</strong> 「記事に書かれていない事実は推測で断定しない」という指示が効いており、<strong>「現時点では社内利用に留まるため、直接的な影響は限定的」</strong>と正確に評価。</li>
</ul></li>
<li><strong>文脈理解 (Relevance)</strong>
<ul>
<li>ユーザーの「メディア・エンタメ」という文脈に対し、単なる技術ニュースとしてではなく、「制作フローへの適用可能性」という視点でフィルタリングできている。</li>
</ul></li>
</ol>
<p>MIPROは、単にスコアを合わせるだけでなく、<strong>「なぜそのスコアなのか」「次にどう動くべきか」という説明責任（Accountability）の能力</strong>を、プロンプトの自動書き換えによって向上させました。</p>


</section>
</section>

 ]]></description>
  <category>dspy</category>
  <category>prompt-tuning</category>
  <category>multi-agent</category>
  <category>llm</category>
  <guid>https://spin-glass.dev/posts/dspy-prompt-tuning/</guid>
  <pubDate>Wed, 14 Jan 2026 00:00:00 GMT</pubDate>
  <media:content url="https://spin-glass.dev/posts/dspy-prompt-tuning/images/hero.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Resume Review Agent: LangGraphによるマルチエージェント履歴書最適化システム</title>
  <link>https://spin-glass.dev/posts/resume-review-agent/</link>
  <description><![CDATA[ 




<section id="はじめに" class="level2">
<h2 class="anchored" data-anchor-id="はじめに">はじめに</h2>
<p>履歴書の最適化は、採用担当者へのアピールとATS対策の両立が求められる複合的なタスクです。私はこの課題に対して、LangGraph StateGraph を用いたマルチエージェントシステム「Resume Review Agent」を設計・実装しました。</p>
</section>
<section id="システムアーキテクチャ" class="level2">
<h2 class="anchored" data-anchor-id="システムアーキテクチャ">システムアーキテクチャ</h2>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://spin-glass.dev/posts/resume-review-agent/images/system_architecture.jpg" class="img-fluid figure-img" alt="Resume Review Agent System Architecture showing parallel agents flow"></p>
<figcaption>Multi-Agent AI System Flow</figcaption>
</figure>
</div>
</section>
<section id="システム概要" class="level2">
<h2 class="anchored" data-anchor-id="システム概要">システム概要</h2>
<p>Resume Review Agent は、以下の5つの専門エージェントで構成されています：</p>
<ol type="1">
<li><strong>Recruiter Agent</strong>: 採用担当者視点でのコンテンツ評価</li>
<li><strong>Tech Writer Agent</strong>: 技術コンテンツの正確性・明確性</li>
<li><strong>Copywriter Agent</strong>: 読みやすさとインパクト</li>
<li><strong>UX Designer Agent</strong>: 視覚的レイアウトと情報階層</li>
<li><strong>Visual Designer Agent</strong>: 美的要素とブランディング</li>
</ol>
</section>
<section id="主要な設計判断" class="level2">
<h2 class="anchored" data-anchor-id="主要な設計判断">主要な設計判断</h2>
<section id="fan-outfan-in-パターン" class="level3">
<h3 class="anchored" data-anchor-id="fan-outfan-in-パターン">Fan-out/Fan-in パターン</h3>
<p>各エージェントは独立して並列実行可能なため、LangGraph の Fan-out/Fan-in パターンを採用。処理時間を短縮しました。</p>
</section>
<section id="ハイブリッドマルチモデル構成" class="level3">
<h3 class="anchored" data-anchor-id="ハイブリッドマルチモデル構成">ハイブリッド・マルチモデル構成</h3>
<table class="caption-top table">
<thead>
<tr class="header">
<th>Agent</th>
<th>Model</th>
<th>理由</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Recruiter</td>
<td>Claude 3.5 Sonnet</td>
<td>汎用的な評価に優れる</td>
</tr>
<tr class="even">
<td>Tech Writer</td>
<td>Gemini 1.5 Pro</td>
<td>技術コンテンツの理解が深い</td>
</tr>
<tr class="odd">
<td>Visual Designer</td>
<td>GPT-4V</td>
<td>視覚的分析が必要</td>
</tr>
</tbody>
</table>
<p>この構成によりコスト効率を改善しました。</p>
</section>
<section id="自動検証リトライループ" class="level3">
<h3 class="anchored" data-anchor-id="自動検証リトライループ">自動検証リトライループ</h3>
<p>Quarto のビルドエラーを自動検出し、最大3回まで自動修正を試みるリトライループを実装。これにより、LLM出力の不安定性を吸収し、安定した出力を保証します。</p>
</section>
</section>
<section id="実装のハイライト" class="level2">
<h2 class="anchored" data-anchor-id="実装のハイライト">実装のハイライト</h2>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># LangGraph StateGraph の定義</span></span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> langgraph.graph <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> StateGraph</span>
<span id="cb1-3"></span>
<span id="cb1-4">workflow <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> StateGraph(ReviewState)</span>
<span id="cb1-5"></span>
<span id="cb1-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Fan-out: 並列エージェント実行</span></span>
<span id="cb1-7">workflow.add_node(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"router"</span>, router_node)</span>
<span id="cb1-8">workflow.add_node(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"recruiter"</span>, recruiter_node)</span>
<span id="cb1-9">workflow.add_node(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tech_writer"</span>, tech_writer_node)</span>
<span id="cb1-10">workflow.add_node(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"copywriter"</span>, copywriter_node)</span>
<span id="cb1-11"></span>
<span id="cb1-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Fan-in: 結果集約</span></span>
<span id="cb1-13">workflow.add_node(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"aggregator"</span>, aggregator_node)</span>
<span id="cb1-14">workflow.add_node(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"revisor"</span>, revisor_node)</span></code></pre></div></div>
<hr>
<blockquote class="blockquote">
<p>コードは非公開のリポジトリで管理しています。</p>
</blockquote>


</section>

 ]]></description>
  <category>portfolio</category>
  <category>langgraph</category>
  <category>multi-agent</category>
  <guid>https://spin-glass.dev/posts/resume-review-agent/</guid>
  <pubDate>Sun, 12 Jan 2025 00:00:00 GMT</pubDate>
</item>
<item>
  <title>重回帰分析と多重共線性</title>
  <link>https://spin-glass.dev/posts/multiple-regression/</link>
  <description><![CDATA[ 




<section id="はじめに" class="level1" data-number="1">
<h1 data-number="1"><span class="header-section-number">1</span> はじめに</h1>
<p>健康診断データとそれに基づく病気のリスク評価は、医療分野において欠かせない要素です。特に、予防医学や早期診断の観点から、これらのデータがいかに重要であるかは言うまでもありません。今回の記事では、<a href="../ydata-profiling/index.html">前回</a>糖尿病のデータを用いて、健康診断データと糖尿病スコア（1年後の病状進行の量的な指標）との関係を探ります<sup>1</sup>。</p>
<p>特に多重共線性の概念とその影響について調べます。多重共線性とは、予測変数間に高い相関がある場合に発生し、これが重回帰分析の結果に影響を与える可能性がある現象です。多重共線性があると、モデルの係数の解釈が難しくなり、係数の推定値の分散が大きくなり、結果としてモデルの予測が不安定になる可能性があります。</p>
<p>本記事では、はじめに重回帰モデルを構築します。そして、得られたモデルの係数を解釈し、多重共線性が結果にどう影響しているのかを調査します。</p>
</section>
<section id="準備" class="level1" data-number="2">
<h1 data-number="2"><span class="header-section-number">2</span> 準備</h1>
<div class="cell" data-execution_count="1">
<details open="" class="code-fold">
<summary>コード</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ライブラリのインポート</span></span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb1-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib.pyplot <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> plt</span>
<span id="cb1-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> japanize_matplotlib</span>
<span id="cb1-5"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> sklearn.datasets <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> load_diabetes</span>
<span id="cb1-6"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> sklearn.model_selection <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> train_test_split</span>
<span id="cb1-7"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> sklearn.linear_model <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> LinearRegression</span>
<span id="cb1-8"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> sklearn.preprocessing <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> StandardScaler</span>
<span id="cb1-9"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> seaborn <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> sns</span>
<span id="cb1-10"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb1-11"></span>
<span id="cb1-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># データセットの読み込み</span></span>
<span id="cb1-13">diabetes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> load_diabetes()</span>
<span id="cb1-14">data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame(diabetes[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'data'</span>], columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>diabetes[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'feature_names'</span>])</span>
<span id="cb1-15"></span>
<span id="cb1-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># targetの標準化</span></span>
<span id="cb1-17">sc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> StandardScaler()</span>
<span id="cb1-18">data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'target'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sc.fit_transform(diabetes[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'target'</span>].reshape(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb1-19"></span>
<span id="cb1-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># データセットのサンプル表示</span></span>
<span id="cb1-21">data.head()</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-display" data-execution_count="1">
<div>


<table class="dataframe caption-top table table-sm table-striped small" data-border="1">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">age</th>
<th data-quarto-table-cell-role="th">sex</th>
<th data-quarto-table-cell-role="th">bmi</th>
<th data-quarto-table-cell-role="th">bp</th>
<th data-quarto-table-cell-role="th">s1</th>
<th data-quarto-table-cell-role="th">s2</th>
<th data-quarto-table-cell-role="th">s3</th>
<th data-quarto-table-cell-role="th">s4</th>
<th data-quarto-table-cell-role="th">s5</th>
<th data-quarto-table-cell-role="th">s6</th>
<th data-quarto-table-cell-role="th">target</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<th data-quarto-table-cell-role="th">0</th>
<td>0.038076</td>
<td>0.050680</td>
<td>0.061696</td>
<td>0.021872</td>
<td>-0.044223</td>
<td>-0.034821</td>
<td>-0.043401</td>
<td>-0.002592</td>
<td>0.019907</td>
<td>-0.017646</td>
<td>-0.014719</td>
</tr>
<tr class="even">
<th data-quarto-table-cell-role="th">1</th>
<td>-0.001882</td>
<td>-0.044642</td>
<td>-0.051474</td>
<td>-0.026328</td>
<td>-0.008449</td>
<td>-0.019163</td>
<td>0.074412</td>
<td>-0.039493</td>
<td>-0.068332</td>
<td>-0.092204</td>
<td>-1.001659</td>
</tr>
<tr class="odd">
<th data-quarto-table-cell-role="th">2</th>
<td>0.085299</td>
<td>0.050680</td>
<td>0.044451</td>
<td>-0.005670</td>
<td>-0.045599</td>
<td>-0.034194</td>
<td>-0.032356</td>
<td>-0.002592</td>
<td>0.002861</td>
<td>-0.025930</td>
<td>-0.144580</td>
</tr>
<tr class="even">
<th data-quarto-table-cell-role="th">3</th>
<td>-0.089063</td>
<td>-0.044642</td>
<td>-0.011595</td>
<td>-0.036656</td>
<td>0.012191</td>
<td>0.024991</td>
<td>-0.036038</td>
<td>0.034309</td>
<td>0.022688</td>
<td>-0.009362</td>
<td>0.699513</td>
</tr>
<tr class="odd">
<th data-quarto-table-cell-role="th">4</th>
<td>0.005383</td>
<td>-0.044642</td>
<td>-0.036385</td>
<td>0.021872</td>
<td>0.003935</td>
<td>0.015596</td>
<td>0.008142</td>
<td>-0.002592</td>
<td>-0.031988</td>
<td>-0.046641</td>
<td>-0.222496</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<p>s1-s6と医学用語、概要の対応は以下になります。<sup>2</sup></p>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
警告
</div>
</div>
<div class="callout-body-container callout-body">
<p>s5に関しては、scikit-learnの公式ページ<sup>3</sup>においても<code>possibly</code>と説明にあります。 以下の説明は参考程度にしてください。</p>
</div>
</div>
<table class="caption-top table">
<colgroup>
<col style="width: 9%">
<col style="width: 30%">
<col style="width: 60%">
</colgroup>
<thead>
<tr class="header">
<th>変数</th>
<th>医学用語</th>
<th>概要</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>s1</td>
<td>tc (total serum cholesterol)</td>
<td>血中の総コレステロール量</td>
</tr>
<tr class="even">
<td>s2</td>
<td>ldl (low-density lipoproteins)</td>
<td>「悪玉コレステロール」とも呼ばれ、高いほど動脈硬化のリスクが高い</td>
</tr>
<tr class="odd">
<td>s3</td>
<td>hdl (high-density lipoproteins)</td>
<td>「善玉コレステロール」とも呼ばれ、高いほど動脈硬化のリスクを減少</td>
</tr>
<tr class="even">
<td>s4</td>
<td>tch (total cholesterol / HDL)</td>
<td>総コレステロールとHDLコレステロールの比率。</td>
</tr>
<tr class="odd">
<td>s5</td>
<td>ltg (possibly log of serum triglycerides level)</td>
<td>血清中のトリグリセリド（脂質）レベルの対数</td>
</tr>
<tr class="even">
<td>s6</td>
<td>glu (blood sugar level)</td>
<td>血中のグルコース（血糖）レベル</td>
</tr>
</tbody>
</table>
</section>
<section id="モデル作成と結果の確認" class="level1" data-number="3">
<h1 data-number="3"><span class="header-section-number">3</span> モデル作成と結果の確認</h1>
<div class="cell" data-execution_count="2">
<details open="" class="code-fold">
<summary>コード</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># モデルの作成と学習</span></span>
<span id="cb2-2">X_train, X_test, y_train, y_test <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> train_test_split(data.drop(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'target'</span>, axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'target'</span>], test_size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>)</span>
<span id="cb2-3">lr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> LinearRegression().fit(X_train, y_train)</span>
<span id="cb2-4"></span>
<span id="cb2-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 予測と結果の評価</span></span>
<span id="cb2-6">y_pred <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> lr.predict(X_test)</span>
<span id="cb2-7">mae <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.mean(np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">abs</span>(y_pred <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> y_test))</span>
<span id="cb2-8"></span>
<span id="cb2-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 結果の表示</span></span>
<span id="cb2-10">mae, lr.coef_, lr.feature_names_in_</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-display" data-execution_count="2">
<pre><code>(0.5777090835157782,
 array([ -0.04669955,  -2.82587978,   6.62555705,   4.27804588,
        -11.089395  ,   7.44176542,   0.75973975,   1.02145469,
         10.61421819,   0.59526243]),
 array(['age', 'sex', 'bmi', 'bp', 's1', 's2', 's3', 's4', 's5', 's6'],
       dtype=object))</code></pre>
</div>
</div>
<p>MAEは約46.9</p>
<div class="cell" data-execution_count="3">
<details open="" class="code-fold">
<summary>コード</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1">mae <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.mean(np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">abs</span>(y_pred <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> y_test))</span>
<span id="cb4-2">mae</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-display" data-execution_count="3">
<pre><code>0.5777090835157782</code></pre>
</div>
</div>
</section>
<section id="可視化" class="level1" data-number="4">
<h1 data-number="4"><span class="header-section-number">4</span> 可視化</h1>
<p>係数の大きさや相関を可視化することにより共変性がないか確認します</p>
<div class="cell" data-execution_count="4">
<details open="" class="code-fold">
<summary>コード</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="annotated-cell-4" style="background: #f1f3f5;"><pre class="sourceCode python code-annotation-code code-with-copy code-annotated"><code class="sourceCode python"><a class="code-annotation-anchor" data-target-cell="annotated-cell-4" data-target-annotation="1" onclick="event.preventDefault();">1</a><span id="annotated-cell-4-1" class="code-annotation-target">plt.bar(x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>lr.feature_names_in_, height<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>lr.coef_)</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code></pre></div></div>
</details>
<div class="cell-annotation">
<dl class="code-annotation-container-grid">
<dt data-target-cell="annotated-cell-4" data-target-annotation="1">1</dt>
<dd>
<span data-code-cell="annotated-cell-4" data-code-lines="1" data-code-annotation="1">係数の棒グラフを作成</span>
</dd>
</dl>
</div>
<div class="cell-output cell-output-display" data-execution_count="4">
<pre><code>&lt;BarContainer object of 10 artists&gt;</code></pre>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://spin-glass.dev/posts/multiple-regression/index_files/figure-html/cell-5-output-2.png" width="575" height="411" class="figure-img"></p>
</figure>
</div>
</div>
</div>
<p>ここでは以下に注目します。 - s3(HDL)、s4(TCH)の係数が小さい</p>
<div class="cell" data-execution_count="5">
<details open="" class="code-fold">
<summary>コード</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="annotated-cell-5" style="background: #f1f3f5;"><pre class="sourceCode python code-annotation-code code-with-copy code-annotated"><code class="sourceCode python"><span id="annotated-cell-5-1">corr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data.corr()</span>
<span id="annotated-cell-5-2">plt.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"相関のヒートマップ"</span>)</span>
<span id="annotated-cell-5-3">sns.heatmap(</span>
<span id="annotated-cell-5-4">    corr,</span>
<span id="annotated-cell-5-5">    annot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span></span>
<a class="code-annotation-anchor" data-target-cell="annotated-cell-5" data-target-annotation="1" onclick="event.preventDefault();">1</a><span id="annotated-cell-5-6" class="code-annotation-target">    )</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code></pre></div></div>
</details>
<div class="cell-annotation">
<dl class="code-annotation-container-grid">
<dt data-target-cell="annotated-cell-5" data-target-annotation="1">1</dt>
<dd>
<span data-code-cell="annotated-cell-5" data-code-lines="6" data-code-annotation="1">ヒートマップ</span>
</dd>
</dl>
</div>
<div class="cell-output cell-output-display" data-execution_count="5">
<pre><code>&lt;Axes: title={'center': '相関のヒートマップ'}&gt;</code></pre>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://spin-glass.dev/posts/multiple-regression/index_files/figure-html/cell-6-output-2.png" width="541" height="431" class="figure-img"></p>
</figure>
</div>
</div>
</div>
<p>以下のことが見て取れます。 - S1(総コレステロール)とS2（LDL）の正の非常に高い相関がある - S3(HDL)とS4(TCH)に負の高い相関がある - target(糖尿病スコア)とs3以外の特徴量には正の相関がある</p>
<div class="cell" data-execution_count="6">
<details open="" class="code-fold">
<summary>コード</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="annotated-cell-6" style="background: #f1f3f5;"><pre class="sourceCode python code-annotation-code code-with-copy code-annotated"><code class="sourceCode python"><a class="code-annotation-anchor" data-target-cell="annotated-cell-6" data-target-annotation="1" onclick="event.preventDefault();">1</a><span id="annotated-cell-6-1" class="code-annotation-target">sns.pairplot(data)</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code></pre></div></div>
</details>
<div class="cell-annotation">
<dl class="code-annotation-container-grid">
<dt data-target-cell="annotated-cell-6" data-target-annotation="1">1</dt>
<dd>
<span data-code-cell="annotated-cell-6" data-code-lines="1" data-code-annotation="1">ペアプロット</span>
</dd>
</dl>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://spin-glass.dev/posts/multiple-regression/index_files/figure-html/cell-7-output-1.png" width="2592" height="2592" class="figure-img"></p>
</figure>
</div>
</div>
</div>
<p>ペアプロットでも上記の性質が確認できます。 また、各分布はおおよそ正規分布に従っています。</p>
</section>
<section id="考察" class="level1" data-number="5">
<h1 data-number="5"><span class="header-section-number">5</span> 考察</h1>
<p>ヒートマップから分かるように、HDLと糖尿病スコアには負の中程度の相関があります。それにもかかわらず、重回帰モデルのHDLの係数は小さくなっています。これは、総コレステロールにHDLも含まれていることが原因と考えられます。総コレステロールとHDLのような関係を多重共線性と呼び、これらの変数が存在するとモデルの解釈が難しくなります。モデルに多重共線性があるかどうか調べる方法にVIFを求める手法があります。また、多重共線性のある特徴量のうち片方のみを利用することにより解釈可能な重回帰モデルが作成できます。</p>
</section>
<section id="まとめ" class="level1" data-number="6">
<h1 data-number="6"><span class="header-section-number">6</span> まとめ</h1>
<ul>
<li>重回帰分析を利用して、健康診断データから糖尿病スコアを予測するモデルを作成</li>
<li>予測に寄与している特徴量を調べるために係数を可視化</li>
<li>多重線形性の存在により、一部の変数が実際の影響よりも低いスコアになっている</li>
</ul>


</section>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">脚注</h2>

<ol>
<li id="fn1"><p>https://scikit-learn.org/stable/datasets/toy_dataset.html#diabetes-dataset↩︎</p></li>
<li id="fn2"><p>https://www4.stat.ncsu.edu/~boos/var.select/diabetes.html↩︎</p></li>
<li id="fn3"><p>https://scikit-learn.org/stable/datasets/toy_dataset.html#diabetes-dataset↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>code</category>
  <guid>https://spin-glass.dev/posts/multiple-regression/</guid>
  <pubDate>Thu, 05 Oct 2023 00:00:00 GMT</pubDate>
  <media:content url="https://spin-glass.dev/posts/multiple-regression/thumbnail.png" medium="image" type="image/png" height="107" width="144"/>
</item>
<item>
  <title>ydata-proflingによるEDA</title>
  <link>https://spin-glass.dev/posts/ydata-profiling/</link>
  <description><![CDATA[ 




<section id="はじめに" class="level1" data-number="1">
<h1 data-number="1"><span class="header-section-number">1</span> はじめに</h1>
<p>探索的データ分析（EDA）は、データ分析の第一歩として必ず行うものですが、いざやる際にどこから手をつければ良いか迷うことがあります。 そのような場合に、自動で一通りのEDAを行ってくれるライブラリが存在します。 その中でもメジャーで <code>GitHub</code>のスター数が多い<sup>1</sup><code>ydata-profiling</code>を今回は利用したいと思います。 EDAによって、データの概要理解や品質確認、およびデータ間の関係の理解が可能となります。 本記事では、<code>ydata-profiling</code>を利用して、<code>scikit-learn</code>で用意されている糖尿病データセット（<code>Diabetes</code>）<sup>2</sup>に対してEDAを行います。</p>
</section>
<section id="データセット" class="level1" data-number="2">
<h1 data-number="2"><span class="header-section-number">2</span> データセット</h1>
<p><code>Diabetes</code>データセットは、442人の糖尿病患者の基本変数として、年齢、性別、体重指数、平均血圧、および6つの血清測定値（s1〜s6）を用いて、病気の進行度を予測するためのものです。目的変数は、基準から1年後の病気の進行度の定量的な測定値です。</p>
<ul>
<li>インスタンスの数：442</li>
<li>属性の数：10（数値予測値）</li>
<li>目的変数：病気の進行度を1年後に測定した定量的な値</li>
<li>各特徴量は平均が中央になるように中心化され、標準偏差とサンプル数の平方根でスケーリングされています。</li>
</ul>
<section id="属性情報" class="level2" data-number="2.1">
<h2 data-number="2.1" class="anchored" data-anchor-id="属性情報"><span class="header-section-number">2.1</span> 属性情報:</h2>
<ul>
<li>age: 年齢</li>
<li>sex: 性別</li>
<li>bmi: 体重指数</li>
<li>bp: 平均血圧</li>
<li>s1: 総セラムコレステロール</li>
<li>s2: LDL（低密度リポタンパク質）</li>
<li>s3: HDL（高密度リポタンパク質）</li>
<li>s4: セラムコレステロール/HDL</li>
<li>s5: セラム三酸化グリセリドの対数</li>
<li>s6: 血糖値</li>
</ul>
</section>
<section id="データセットの説明" class="level2" data-number="2.2">
<h2 data-number="2.2" class="anchored" data-anchor-id="データセットの説明"><span class="header-section-number">2.2</span> データセットの説明:</h2>
<p><code>Diabetes</code>データセットは、基本変数として、年齢性別、体重指数、平均血圧、および6つの血清測定値（s1〜s6）を用いて、442人の糖尿病患者の病気の進行度を予測するためのものです。目的変数は、基準から1年後の病気の進行度の定量的な測定値です。</p>
</section>
</section>
<section id="データの読み込みと前処理" class="level1" data-number="3">
<h1 data-number="3"><span class="header-section-number">3</span> データの読み込みと前処理</h1>
<div class="cell" data-execution_count="1">
<details open="" class="code-fold">
<summary>コード</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb1-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> ydata_profiling <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> ProfileReport</span>
<span id="cb1-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> sklearn.datasets <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> load_diabetes</span>
<span id="cb1-5"></span>
<span id="cb1-6">diabetes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> load_diabetes()</span>
<span id="cb1-7"></span>
<span id="cb1-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pandasデータフレームの作成</span></span>
<span id="cb1-9">df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame(diabetes[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'data'</span>], columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>diabetes[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'feature_names'</span>])</span>
<span id="cb1-10">df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'target'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> diabetes[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'target'</span>]</span>
<span id="cb1-11"></span>
<span id="cb1-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># データの先頭５つを表示</span></span>
<span id="cb1-13">df.head()</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-display" data-execution_count="7">
<div>


<table class="dataframe caption-top table table-sm table-striped small" data-border="1">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">age</th>
<th data-quarto-table-cell-role="th">sex</th>
<th data-quarto-table-cell-role="th">bmi</th>
<th data-quarto-table-cell-role="th">bp</th>
<th data-quarto-table-cell-role="th">s1</th>
<th data-quarto-table-cell-role="th">s2</th>
<th data-quarto-table-cell-role="th">s3</th>
<th data-quarto-table-cell-role="th">s4</th>
<th data-quarto-table-cell-role="th">s5</th>
<th data-quarto-table-cell-role="th">s6</th>
<th data-quarto-table-cell-role="th">target</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<th data-quarto-table-cell-role="th">0</th>
<td>0.038076</td>
<td>0.050680</td>
<td>0.061696</td>
<td>0.021872</td>
<td>-0.044223</td>
<td>-0.034821</td>
<td>-0.043401</td>
<td>-0.002592</td>
<td>0.019907</td>
<td>-0.017646</td>
<td>151.0</td>
</tr>
<tr class="even">
<th data-quarto-table-cell-role="th">1</th>
<td>-0.001882</td>
<td>-0.044642</td>
<td>-0.051474</td>
<td>-0.026328</td>
<td>-0.008449</td>
<td>-0.019163</td>
<td>0.074412</td>
<td>-0.039493</td>
<td>-0.068332</td>
<td>-0.092204</td>
<td>75.0</td>
</tr>
<tr class="odd">
<th data-quarto-table-cell-role="th">2</th>
<td>0.085299</td>
<td>0.050680</td>
<td>0.044451</td>
<td>-0.005670</td>
<td>-0.045599</td>
<td>-0.034194</td>
<td>-0.032356</td>
<td>-0.002592</td>
<td>0.002861</td>
<td>-0.025930</td>
<td>141.0</td>
</tr>
<tr class="even">
<th data-quarto-table-cell-role="th">3</th>
<td>-0.089063</td>
<td>-0.044642</td>
<td>-0.011595</td>
<td>-0.036656</td>
<td>0.012191</td>
<td>0.024991</td>
<td>-0.036038</td>
<td>0.034309</td>
<td>0.022688</td>
<td>-0.009362</td>
<td>206.0</td>
</tr>
<tr class="odd">
<th data-quarto-table-cell-role="th">4</th>
<td>0.005383</td>
<td>-0.044642</td>
<td>-0.036385</td>
<td>0.021872</td>
<td>0.003935</td>
<td>0.015596</td>
<td>0.008142</td>
<td>-0.002592</td>
<td>-0.031988</td>
<td>-0.046641</td>
<td>135.0</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
</section>
<section id="ydata-profilingによるレポート作成" class="level1" data-number="4">
<h1 data-number="4"><span class="header-section-number">4</span> ydata-profilingによるレポート作成</h1>
<div class="cell" data-execution_count="2">
<details open="" class="code-fold">
<summary>コード</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>matplotlib widget</span>
<span id="cb2-2"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>matplotlib ipympl</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>Warning: Cannot change to a different GUI toolkit: ipympl. Using widget instead.</code></pre>
</div>
</div>
<div class="cell" data-execution_count="3">
<details open="" class="code-fold">
<summary>コード</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1">profile <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ProfileReport(df, title<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"EDA"</span>)</span>
<span id="cb4-2"></span>
<span id="cb4-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># HTMLにレポートを出力</span></span>
<span id="cb4-4">profile.to_widgets()</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-display">
<script type="application/vnd.jupyter.widget-view+json">
{"model_id":"8fa759d1526d494286f6981de817efc6","version_major":2,"version_minor":0,"quarto_mimetype":"application/vnd.jupyter.widget-view+json"}
</script>
</div>
<div class="cell-output cell-output-display">
<script type="application/vnd.jupyter.widget-view+json">
{"model_id":"d1d15bbdf7d64c54b2ab320bdcd98d4d","version_major":2,"version_minor":0,"quarto_mimetype":"application/vnd.jupyter.widget-view+json"}
</script>
</div>
<div class="cell-output cell-output-display">
<script type="application/vnd.jupyter.widget-view+json">
{"model_id":"0233a3bfc31c47efb2ba5b562a4553cb","version_major":2,"version_minor":0,"quarto_mimetype":"application/vnd.jupyter.widget-view+json"}
</script>
</div>
<div class="cell-output cell-output-display">
<script type="application/vnd.jupyter.widget-view+json">
{"model_id":"0918f0c192c84ae5a3f2d9ea5998a76b","version_major":2,"version_minor":0,"quarto_mimetype":"application/vnd.jupyter.widget-view+json"}
</script>
</div>
</div>
</section>
<section id="レポートからの洞察" class="level1" data-number="5">
<h1 data-number="5"><span class="header-section-number">5</span> レポートからの洞察</h1>
<ul>
<li><code>Variables</code>のヒストグラムから確認できるようにデータセットは正規化されています。</li>
<li><code>Interactions</code>や<code>Correlations</code>で確認できるように、targetはbmi, s5と相関があります。</li>
<li>おそらく前処理されているからでしょうが、欠損値や外れ値は確認できませんでした。</li>
</ul>
<p>以上が<code>ydata-profiling</code>を利用した簡易的なEDAのプロセスです。このツールを使用することで、データの全体像を迅速に把握し、データ分析の方向性を定めることができます。</p>


</section>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">脚注</h2>

<ol>
<li id="fn1"><p>https://twitter.com/pl662607/status/1708134174929064213↩︎</p></li>
<li id="fn2"><p>https://scikit-learn.org/stable/datasets/toy_dataset.html#diabetes-dataset↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>code</category>
  <guid>https://spin-glass.dev/posts/ydata-profiling/</guid>
  <pubDate>Mon, 02 Oct 2023 00:00:00 GMT</pubDate>
  <media:content url="https://spin-glass.dev/posts/ydata-profiling/thumbnail.png" medium="image" type="image/png" height="117" width="144"/>
</item>
<item>
  <title>MLflow2.7新機能:Prompt Engineering UI</title>
  <link>https://spin-glass.dev/posts/prompt-engineering-ui/</link>
  <description><![CDATA[ 




<section id="はじめに" class="level1" data-number="1">
<h1 data-number="1"><span class="header-section-number">1</span> はじめに</h1>
<p>MLflow2.7の新機能として、Prompt Engineering UIが導入されました。このUIを使用することで、コードを書くことなく、様々なLLM（Large Language Model）を試すことができます。この記事では、Prompt Engineering UIの基本的な使用方法と、MLflowとの統合方法について紹介します。</p>
<section id="mlflowとの統合" class="level2" data-number="1.1">
<h2 data-number="1.1" class="anchored" data-anchor-id="mlflowとの統合"><span class="header-section-number">1.1</span> MLflowとの統合</h2>
<p>MLflow 2.7には、Prompt Engineering UIが新機能として追加されました。これにより、UI上でプロンプトエンジニアリングの基本手順を実行できます。AI Gatewayを作成し、MLflow Tracking Serverに接続することで、Prompt Engineering UIを利用できます。</p>
</section>
</section>
<section id="利用方法" class="level1" data-number="2">
<h1 data-number="2"><span class="header-section-number">2</span> 利用方法</h1>
<section id="ai-gatewayの起動" class="level2" data-number="2.1">
<h2 data-number="2.1" class="anchored" data-anchor-id="ai-gatewayの起動"><span class="header-section-number">2.1</span> AI Gatewayの起動</h2>
<p>事前にOPENAIのAPIキーを取得しておく必要があります。</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
ノート
</div>
</div>
<div class="callout-body-container callout-body">
<p>AI Gatewayについての詳細は<a href="../../posts/ai-gateway/index.html">こちら</a>の記事を参考にしてください。</p>
</div>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">export</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">OPENAI_API_KEY</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>OPENAI_API_KEY<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb1-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">mlflow</span> gateway start <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--config-path</span> config.yaml <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--port</span> 7000</span></code></pre></div></div>
</section>
<section id="mlflow-tracking-serverの起動" class="level2" data-number="2.2">
<h2 data-number="2.2" class="anchored" data-anchor-id="mlflow-tracking-serverの起動"><span class="header-section-number">2.2</span> MLflow Tracking Serverの起動</h2>
<p>AI GatewayをMLflow Tracking Serverに接続します。</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb2-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">export</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">MLFLOW_GATEWAY_URI</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"http://localhost:7000"</span></span>
<span id="cb2-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">mlflow</span> server <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--port</span> 5000</span></code></pre></div></div>
</section>
<section id="prompt-engineering-uiへのアクセス" class="level2" data-number="2.3">
<h2 data-number="2.3" class="anchored" data-anchor-id="prompt-engineering-uiへのアクセス"><span class="header-section-number">2.3</span> Prompt Engineering UIへのアクセス</h2>
<p>ブラウザで<a href="http://localhost:7000">http://localhost:7000</a>にアクセスし、Prompt Engineering UIを開きます。</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://spin-glass.dev/posts/prompt-engineering-ui/img/prompt-engineering-ui.png" class="img-fluid figure-img"></p>
<figcaption>ptompt-enginnering-ui</figcaption>
</figure>
</div>
</section>
<section id="mlflow-experimentの開始" class="level2" data-number="2.4">
<h2 data-number="2.4" class="anchored" data-anchor-id="mlflow-experimentの開始"><span class="header-section-number">2.4</span> MLflow Experimentの開始</h2>
<p>Prompt Engineering UIから<code>New Run</code>をクリックし、<code>using Prompt Engineering</code>を選択します。</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://spin-glass.dev/posts/prompt-engineering-ui/img/new_run.png" class="img-fluid figure-img"></p>
<figcaption>new run</figcaption>
</figure>
</div>
</section>
<section id="プロンプトテンプレートの作成" class="level2" data-number="2.5">
<h2 data-number="2.5" class="anchored" data-anchor-id="プロンプトテンプレートの作成"><span class="header-section-number">2.5</span> プロンプトテンプレートの作成</h2>
<p>Prompt Engineering Playgroundが開くので、プロンプトテンプレートを作成します。 今回は、英語のデフォルトのプロンプトとその日本語版を試しました。</p>
</section>
<section id="評価" class="level2" data-number="2.6">
<h2 data-number="2.6" class="anchored" data-anchor-id="評価"><span class="header-section-number">2.6</span> 評価</h2>
<p><code>Evaluate</code>をクリックします。</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://spin-glass.dev/posts/prompt-engineering-ui/img/evaluate.png" class="img-fluid figure-img"></p>
<figcaption>evaluate</figcaption>
</figure>
</div>
</section>
<section id="実行" class="level2" data-number="2.7">
<h2 data-number="2.7" class="anchored" data-anchor-id="実行"><span class="header-section-number">2.7</span> 実行</h2>
<p>問題なければ<code>実行</code>をクリックします。 <img src="https://spin-glass.dev/posts/prompt-engineering-ui/img/create_run.png" class="img-fluid" alt="create run"></p>
</section>
<section id="一覧" class="level2" data-number="2.8">
<h2 data-number="2.8" class="anchored" data-anchor-id="一覧"><span class="header-section-number">2.8</span> 一覧</h2>
<p>実験の一覧が確認できます。 <img src="https://spin-glass.dev/posts/prompt-engineering-ui/img/list.png" class="img-fluid" alt="list"></p>
</section>
<section id="詳細" class="level2" data-number="2.9">
<h2 data-number="2.9" class="anchored" data-anchor-id="詳細"><span class="header-section-number">2.9</span> 詳細</h2>
<p>詳細ページで、実験に利用したパラメータやモデルが確認できます。 <img src="https://spin-glass.dev/posts/prompt-engineering-ui/img/detail.png" class="img-fluid" alt="detail"></p>
</section>
</section>
<section id="まとめ" class="level1" data-number="3">
<h1 data-number="3"><span class="header-section-number">3</span> まとめ</h1>
<p>この記事では、MLflow 2.7の新機能であるPrompt Engineering UIの利用方法と、そのMLflowとの統合方法について解説しました。Prompt Engineering UIを用いることで、ユーザーはコーディングすることなく、様々なLLMを直観的に試すことができます。これにより、開発者やデータサイエンティストは、コードの変更なしに異なるプロンプトやパラメータを素早く試すことができます。</p>
<p>さらに、MLflowにより作成したモデルをサービングすることで、オンライン推論も可能となります。これにより、リアルタイムでのモデルの性能評価や、新しいデータに対するモデルの反応を確認できます。機会があればサービングについても試してみたいです。</p>
</section>
<section id="参考" class="level1" data-number="4">
<h1 data-number="4"><span class="header-section-number">4</span> 参考</h1>
<ul>
<li><a href="https://mlflow.org/docs/latest/llms/prompt-engineering.html">MLflow公式ドキュメント</a></li>
</ul>


</section>

 ]]></description>
  <category>code</category>
  <guid>https://spin-glass.dev/posts/prompt-engineering-ui/</guid>
  <pubDate>Mon, 25 Sep 2023 00:00:00 GMT</pubDate>
  <media:content url="https://spin-glass.dev/posts/prompt-engineering-ui/img/thumbnail.png" medium="image" type="image/png" height="97" width="144"/>
</item>
<item>
  <title>MLflow AI GatewayによるLLMの一元管理</title>
  <link>https://spin-glass.dev/posts/ai-gateway/</link>
  <description><![CDATA[ 




<section id="はじめに" class="level1">
<h1>はじめに</h1>
<p>近年、大規模言語モデル（LLM）の活用が進んでおり、テキストデータのセンチメント分析からリアルタイムのチャットボットの導入まで、多岐にわたるアプリケーションでその価値を発揮しています。しかしながら、多くの組織でLLMを効果的に管理と利用するためには、セキュリティ、コスト、ガバナンスなどの課題が存在します。</p>
<p>MLflow AI Gatewayはこれらの課題に対処するためのツールとして設計され、特定のモデルプロバイダ（例：OpenAI、Anthropicなど）に対して、一元化されたインターフェースを提供します。これにより、APIトークンの管理や利用コストのコントロール、ガバナンスの強化が実現します。</p>
<p>本記事では、MLflow AI Gatewayのセットアップ方法と、APIドキュメントへのアクセス方法、そしてGatewayを利用したレクエスト方法について解説します。ここでは、特にOpenAIを例に挙げて説明しますが、MLflow AI Gatewayは他の多くのプロバイダにも対応しています。 詳細は<a href="https://mlflow.org/docs/latest/gateway/index.html#providers">公式ドキュメント</a>を参考にしてください。</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
ノート
</div>
</div>
<div class="callout-body-container callout-body">
<p>MLflow 2.5以降が必要です。</p>
</div>
</div>
</section>
<section id="mlflow-ai-gatewayのセットアップ" class="level1">
<h1>MLflow AI Gatewayのセットアップ</h1>
<p>このセクションでは、MLflow AI Gatewayのセットアップについてに解説します。 環境の準備、必要なコンフィグレーションの設定、Gatewayの起動について説明します。 詳細は、<a href="https://github.com/spin-glass/mlflow-ai-gateway-sample">GitHubレポジトリ</a>を参考にしてください。</p>
<section id="インストール" class="level2">
<h2 class="anchored" data-anchor-id="インストール">インストール</h2>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">pip</span> install <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'mlflow[gateway]'</span></span></code></pre></div></div>
</section>
<section id="openai-api-keyの設定" class="level2">
<h2 class="anchored" data-anchor-id="openai-api-keyの設定">OpenAI API Keyの設定</h2>
<pre><code>pip install 'mlflow[gateway]'</code></pre>
</section>
<section id="設定ファイルの用意" class="level2">
<h2 class="anchored" data-anchor-id="設定ファイルの用意">設定ファイルの用意</h2>
<p>MLflow AI Gatewayの動作には、適切な設定ファイルが必要です。このファイルでは、ルート、プロバイダー、モデルなどの重要なパラメータを定義します。</p>
<section id="ルート" class="level3">
<h3 class="anchored" data-anchor-id="ルート">ルート</h3>
<p>routesセクションでは、各ルートの名称、タイプ、関連するモデルなどを定義します。ルートは、外部からのリクエストがどのモデルにルーティングされるかを制御します。</p>
</section>
<section id="プロバイダーとモデル" class="level3">
<h3 class="anchored" data-anchor-id="プロバイダーとモデル">プロバイダーとモデル</h3>
<p>modelセクションでは、使用するモデルのプロバイダーと名称を指定します。プロバイダーは、モデルが存在するクラウドサービスやプラットフォームを指します。また、configセクションでは、特定のプロバイダーに必要な設定を行います。例えば、APIキーの設定などです。</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode yml code-with-copy"><code class="sourceCode yaml"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">routes</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb3-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">name</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> completions</span></span>
<span id="cb3-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">route_type</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> llm/v1/completions</span></span>
<span id="cb3-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">model</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb3-5"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">provider</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> openai</span></span>
<span id="cb3-6"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">name</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> gpt-3.5-turbo</span></span>
<span id="cb3-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">config</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb3-8"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">openai_api_key</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> $OPENAI_API_KEY</span></span>
<span id="cb3-9"></span>
<span id="cb3-10"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">name</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> chat</span></span>
<span id="cb3-11"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">route_type</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> llm/v1/chat</span></span>
<span id="cb3-12"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">model</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb3-13"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">provider</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> openai</span></span>
<span id="cb3-14"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">name</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> gpt-3.5-turbo</span></span>
<span id="cb3-15"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">config</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb3-16"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">openai_api_key</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> $OPENAI_API_KEY</span></span>
<span id="cb3-17"></span>
<span id="cb3-18"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">name</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> embeddings</span></span>
<span id="cb3-19"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">route_type</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> llm/v1/embeddings</span></span>
<span id="cb3-20"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">model</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb3-21"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">provider</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> openai</span></span>
<span id="cb3-22"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">name</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> text-embedding-ada-002</span></span>
<span id="cb3-23"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">config</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb3-24"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">openai_api_key</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> $OPENAI_API_KEY</span></span></code></pre></div></div>
</section>
</section>
</section>
<section id="apiドキュメントの確認" class="level1">
<h1>APIドキュメントの確認</h1>
<p>上記のようにMLflow AI Gatewayサーバーを起動すると、Swaggerによる<code>localhost:5000</code>に自動的にAPIドキュメントが生成されます。</p>
<p><img src="https://spin-glass.dev/posts/ai-gateway/thumbnail.png" class="img-fluid"></p>
</section>
<section id="fluent-apiによるリクエストの実行" class="level1">
<h1>Fluent APIによるリクエストの実行</h1>
<p>MLflow AI Gatewayがセットアップされたら、Fluent APIを利用してリクエストを実行できます。他にも、<a href="https://mlflow.org/docs/latest/gateway/index.html#step-7-send-requests-using-the-client-api">Client API</a>, <a href="https://mlflow.org/docs/latest/gateway/index.html#step-8-send-requests-to-routes-via-rest-api">REST API</a>も利用可能です。</p>
<div class="cell" data-execution_count="1">
<details open="" class="code-fold">
<summary>コード</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> mlflow.gateway <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> query, set_gateway_uri</span>
<span id="cb4-2"></span>
<span id="cb4-3">set_gateway_uri(gateway_uri<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"http://localhost:5000"</span>)</span>
<span id="cb4-4"></span>
<span id="cb4-5">response <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> query(</span>
<span id="cb4-6">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"chat"</span>,</span>
<span id="cb4-7">    {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"messages"</span>: [{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"role"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"user"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"content"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"What is the best day of the week?"</span>}]},</span>
<span id="cb4-8">)</span>
<span id="cb4-9"></span>
<span id="cb4-10"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pprint</span>
<span id="cb4-11">pprint.pprint(response)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>{'candidates': [{'message': {'content': 'The best day of the week is '
                                        'subjective and can vary depending on '
                                        'individual preferences and '
                                        'circumstances. Some people may prefer '
                                        'weekends (Saturday and Sunday) as '
                                        'they offer a break from work or '
                                        'school and allow for relaxation or '
                                        'leisure activities. Others may enjoy '
                                        'Fridays as they mark the end of the '
                                        'workweek and the beginning of the '
                                        'weekend. Ultimately, the best day of '
                                        'the week is a personal preference.',
                             'role': 'assistant'},
                 'metadata': {'finish_reason': 'stop'}}],
 'metadata': {'input_tokens': 16,
              'model': 'gpt-3.5-turbo-0613',
              'output_tokens': 78,
              'route_type': 'llm/v1/chat',
              'total_tokens': 94}}</code></pre>
</div>
</div>
</section>
<section id="まとめ" class="level1">
<h1>まとめ</h1>
<p>本記事では、MLflow AI Gatewayのセットアップ方法とその使用方法について詳細に説明しました。MLflow AI Gatewayを使用することで、大規模言語モデルの管理が効果的に行えるため、セキュリティ、コスト、ガバナンスの各観点での課題に対処できます。また、異なるプロバイダのモデルも一元化されたインターフェースを通じてアクセスできるため、開発の効率化が図れます。</p>
</section>
<section id="参考" class="level1">
<h1>参考</h1>
<ul>
<li><a href="https://mlflow.org/docs/latest/gateway/index.html">MLflow ドキュメント</a></li>
<li><a href="https://www.databricks.com/jp/blog/announcing-mlflow-ai-gateway">MLflow AI Gatewayの発表 強力なLLMアーキテクチャとインターフェースツールの提供</a></li>
</ul>
</section>
<section id="実装" class="level1">
<h1>実装</h1>
<ul>
<li><a href="https://github.com/spin-glass/mlflow-ai-gateway-sample">GitHubレポジトリ</a></li>
</ul>


</section>

 ]]></description>
  <category>code</category>
  <guid>https://spin-glass.dev/posts/ai-gateway/</guid>
  <pubDate>Sat, 23 Sep 2023 00:00:00 GMT</pubDate>
  <media:content url="https://spin-glass.dev/posts/ai-gateway/thumbnail.png" medium="image" type="image/png" height="156" width="144"/>
</item>
<item>
  <title>ポモドーロタイマーTicktime cubeは前作から大きく進化していた</title>
  <link>https://spin-glass.dev/posts/ticktime_cube/</link>
  <description><![CDATA[ 




<p><img src="https://spin-glass.dev/posts/ticktime_cube/ticktime_cube.jpeg" class="img-fluid" alt="Ticktime cube"> Makuakeで先行販売されていたTicktime cubeが先日届いた。数日使ってところ、前作Ticktimeから大きく進化しており個人的には大満足している。</p>
<p>この記事では、TIcktimeの基本機能と前作からの進化点を個人的に嬉しかったものに絞って紹介する。Ticktime cubeの全機能が知りたい方は<a href="https://www.makuake.com/project/ticktime_cube/">Makuakeのページ</a>を見てほしい。細かい時間を設定や一時停止機能の追加など大きな進化を遂げている。</p>
<section id="基本機能" class="level1">
<h1>基本機能</h1>
<p>それではまずは基本機能から見ていこう。</p>
<section id="キューブを回転させるだけでタイマー開始" class="level2">
<h2 class="anchored" data-anchor-id="キューブを回転させるだけでタイマー開始">キューブを回転させるだけでタイマー開始</h2>
<p>この製品を使う一番の理由はこれであろう。 スマホアプリにもポモドーロタイマーはあり私も利用しているが、気を抜くと他のアプリなどをついつい利用してしまう。 このガジェットはもちろんそのような心配とは無縁だ。また、アナログのタイマーと比べてもタイマー時間を手打ちする時間がないため、ポモドーロに非常に向いているガジェットと言える。</p>
</section>
<section id="前作からの進化点" class="level2">
<h2 class="anchored" data-anchor-id="前作からの進化点">前作からの進化点</h2>
<p>次に、前作TIcktimeと比較していく。</p>
<section id="念願のusb-type-c-対応" class="level3">
<h3 class="anchored" data-anchor-id="念願のusb-type-c-対応">念願のUSB type-c 対応</h3>
<p>前作のTicktimeを持っている人にとって最も嬉しいのはこの進化ではないでだろうか？</p>
<p>前作はUSB type-bでしか充電できなかった。私はKindle oasisとTicktimeしかUSB type-bのガジェットしか持っていなかったため少々ストレスだった。今作はUSB type-cに対応しているため、他のガジェットと同じケーブルで充電することができる。<del>あとはiPhoneだけなんとかならないかな。。。</del></p>
</section>
<section id="自動電源オフ" class="level3">
<h3 class="anchored" data-anchor-id="自動電源オフ">自動電源オフ</h3>
<p>Ticktickでは充電を手動でオフにしないと、充電を全て消費するまで点灯しっぱなしだった。私は毎回電源オフを忘れるため、充電を切らすことも多々あった。そして早速Ticktime Cubeでも同様に電源オフを忘れてしまったのだが、なんと自動的に電源がオフになっていたため充電切れを避けられた。これは地味に嬉しい。</p>
<p>アナログなポモドーロタイマーに興味がある人は是非買ってみたらどうだろうか？現在Makuakeでの取り扱いは終わってしまっているが、きっとAmazonなどで販売されると思うので待ってみてほしい。</p>


</section>
</section>
</section>

 ]]></description>
  <category>code</category>
  <guid>https://spin-glass.dev/posts/ticktime_cube/</guid>
  <pubDate>Wed, 15 Feb 2023 00:00:00 GMT</pubDate>
  <media:content url="https://spin-glass.dev/posts/ticktime_cube/ticktime_cube.jpeg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>gganimateによる動くグラフの作成</title>
  <link>https://spin-glass.dev/posts/animation/</link>
  <description><![CDATA[ 




<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://spin-glass.dev/posts/animation/thumbnail.gif" class="img-fluid figure-img"></p>
<figcaption>gganimateサンプル</figcaption>
</figure>
</div>
<section id="gganimate" class="level1">
<h1>gganimate</h1>
<p>gganimateは、グラフ描写ライブラリであるggplot2をアニメーションに拡張したものです。ggplot2で書いたグラフに対して、transitionを追加する事で容易にアニメーションが作成できます。</p>
</section>
<section id="ライブラリのインポート" class="level1">
<h1>ライブラリのインポート</h1>
<div class="cell">
<details open="" class="code-fold">
<summary>コード</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(gganimate)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(gapminder)</span></code></pre></div></div>
</details>
</div>
</section>
<section id="利用データ" class="level1">
<h1>利用データ</h1>
<p>データはGapminderを利用します。</p>
<section id="データのサンプル" class="level2">
<h2 class="anchored" data-anchor-id="データのサンプル">データのサンプル</h2>
<div class="cell">
<details open="" class="code-fold">
<summary>コード</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(gapminder)</span>
<span id="cb2-2"></span>
<span id="cb2-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(gapminder)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 6 × 6
  country     continent  year lifeExp      pop gdpPercap
  &lt;fct&gt;       &lt;fct&gt;     &lt;int&gt;   &lt;dbl&gt;    &lt;int&gt;     &lt;dbl&gt;
1 Afghanistan Asia       1952    28.8  8425333      779.
2 Afghanistan Asia       1957    30.3  9240934      821.
3 Afghanistan Asia       1962    32.0 10267083      853.
4 Afghanistan Asia       1967    34.0 11537966      836.
5 Afghanistan Asia       1972    36.1 13079460      740.
6 Afghanistan Asia       1977    38.4 14880372      786.</code></pre>
</div>
</div>
</section>
<section id="列の意味" class="level2">
<h2 class="anchored" data-anchor-id="列の意味">列の意味</h2>
<div class="cell">
<details open="" class="code-fold">
<summary>コード</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(gapminder)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "country"   "continent" "year"      "lifeExp"   "pop"       "gdpPercap"</code></pre>
</div>
</div>
<table class="caption-top table">
<caption>Demonstration of pipe table syntax</caption>
<thead>
<tr class="header">
<th>カラム名</th>
<th style="text-align: left;">意味</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>country</td>
<td style="text-align: left;">国名</td>
</tr>
<tr class="even">
<td>continent</td>
<td style="text-align: left;">大陸名</td>
</tr>
<tr class="odd">
<td>year</td>
<td style="text-align: left;">年</td>
</tr>
<tr class="even">
<td>lifeExp</td>
<td style="text-align: left;">平均寿命</td>
</tr>
<tr class="odd">
<td>pop</td>
<td style="text-align: left;">人口</td>
</tr>
<tr class="even">
<td>gdpPercap</td>
<td style="text-align: left;">１人あたりGPD</td>
</tr>
</tbody>
</table>
</section>
</section>
<section id="ggplot2によるグラフ作成" class="level1">
<h1>ggplot2によるグラフ作成</h1>
<div class="cell">
<details open="" class="code-fold">
<summary>コード</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">p <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(gapminder, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> gdpPercap, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> lifeExp, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> pop, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> country)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-2">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show.legend =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_size</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">range =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_log10</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>continent) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 大陸ごとに比較</span></span>
<span id="cb6-6"></span>
<span id="cb6-7">p</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://spin-glass.dev/posts/animation/index_files/figure-html/unnamed-chunk-4-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="gganimateによるアニメーション作成" class="level1">
<h1>gganimateによるアニメーション作成</h1>
<p>transitionを追加して、時間ごとの変化をアニメーションにします。年毎の推移を表すには、<strong>transition_time</strong>に対して時間を示す列を指定します。</p>
<div class="cell">
<details open="" class="code-fold">
<summary>コード</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">transition_time</span>(year)</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://spin-glass.dev/posts/animation/index_files/figure-html/unnamed-chunk-5-1.gif" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="参考" class="level1">
<h1>参考</h1>
<p><a href="https://gganimate.com/">A Grammar of Animated Graphics</a></p>


</section>

 ]]></description>
  <category>code</category>
  <guid>https://spin-glass.dev/posts/animation/</guid>
  <pubDate>Sun, 01 Jan 2023 00:00:00 GMT</pubDate>
  <media:content url="https://spin-glass.dev/posts/animation/thumbnail.gif" medium="image" type="image/gif"/>
</item>
</channel>
</rss>
