Skip to content

Commit

Permalink
add mirascope
Browse files Browse the repository at this point in the history
  • Loading branch information
khuyentran1401 committed May 7, 2024
1 parent 9d9eeda commit 85446bd
Show file tree
Hide file tree
Showing 6 changed files with 271 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Chapter2/typing.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.6"
"version": "3.11.6"
},
"toc": {
"base_numbering": 1,
Expand Down
99 changes: 99 additions & 0 deletions Chapter5/llm.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,105 @@
"source": [
"[Link to Outlines](https://github.com/outlines-dev/outlines)."
]
},
{
"cell_type": "markdown",
"id": "ff519c06",
"metadata": {},
"source": [
"### Mirascope: Extract Structured Data Extraction From LLM Outputs"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dc12a009",
"metadata": {
"tags": [
"hide-cell"
]
},
"outputs": [],
"source": [
"!pip install mirascope"
]
},
{
"cell_type": "markdown",
"id": "0ea9cc6f",
"metadata": {},
"source": [
"Large Language Models (LLMs) are powerful at producing human-like text, but their outputs lack structure, which can limit their usefulness in many practical applications that require organized data.\n",
"\n",
"Mirascope offers a solution by enabling the extraction of structured information from LLM outputs reliably.\n",
"\n",
"The following code uses Mirascope to extract meeting details such as topic, date, time, and participants."
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "03c7cfb0",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
"\n",
"os.environ[\"OPENAI_API_KEY\"] = \"YOUR_API_KEY\"\n"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "79f19428",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"topic='Upcoming product launch' date='June 15th' time='3 PM' participants=['John', 'Sarah', 'Mike']\n"
]
}
],
"source": [
"from typing import List, Type\n",
"from pydantic import BaseModel\n",
"from mirascope.openai import OpenAIExtractor\n",
"\n",
"\n",
"class MeetingDetails(BaseModel):\n",
" topic: str\n",
" date: str\n",
" time: str\n",
" participants: List[str]\n",
"\n",
"\n",
"class MeetingExtractor(OpenAIExtractor[MeetingDetails]):\n",
" extract_schema: Type[MeetingDetails] = MeetingDetails\n",
" prompt_template = \"\"\"\n",
" Extract the meeting details from the following description:\n",
" {description}\n",
" \"\"\"\n",
"\n",
" description: str\n",
"\n",
"\n",
"# Example usage:\n",
"description = \"Discuss the upcoming product launch on June 15th at 3 PM with John, Sarah, and Mike.\"\n",
"meeting_details = MeetingExtractor(description=description).extract()\n",
"assert isinstance(meeting_details, MeetingDetails)\n",
"print(meeting_details)"
]
},
{
"cell_type": "markdown",
"id": "641d2dd5",
"metadata": {},
"source": [
"[Link to Mirascope](https://bit.ly/4bkciv3)."
]
}
],
"metadata": {
Expand Down
70 changes: 70 additions & 0 deletions docs/Chapter5/llm.html
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,7 @@ <h2> Contents </h2>
<ul class="visible nav section-nav flex-column">
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#simplify-llm-integration-with-magentic-s-prompt-decorator">6.16.1. Simplify LLM Integration with Magentic’s @prompt Decorator</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#outlines-ensuring-consistent-outputs-from-language-models">6.16.2. Outlines: Ensuring Consistent Outputs from Language Models</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#mirascope-extract-structured-data-extraction-from-llm-outputs">6.16.3. Mirascope: Extract Structured Data Extraction From LLM Outputs</a></li>
</ul>
</nav>
</div>
Expand Down Expand Up @@ -660,6 +661,74 @@ <h2><span class="section-number">6.16.2. </span>Outlines: Ensuring Consistent Ou
</div>
<p><a class="reference external" href="https://github.com/outlines-dev/outlines">Link to Outlines</a>.</p>
</section>
<section id="mirascope-extract-structured-data-extraction-from-llm-outputs">
<h2><span class="section-number">6.16.3. </span>Mirascope: Extract Structured Data Extraction From LLM Outputs<a class="headerlink" href="#mirascope-extract-structured-data-extraction-from-llm-outputs" title="Permalink to this heading">#</a></h2>
<div class="cell tag_hide-cell docutils container">
<details class="hide above-input">
<summary aria-label="Toggle hidden content">
<span class="collapsed">Show code cell content</span>
<span class="expanded">Hide code cell content</span>
</summary>
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="o">!</span>pip<span class="w"> </span>install<span class="w"> </span>mirascope
</pre></div>
</div>
</div>
</details>
</div>
<p>Large Language Models (LLMs) are powerful at producing human-like text, but their outputs lack structure, which can limit their usefulness in many practical applications that require organized data.</p>
<p>Mirascope offers a solution by enabling the extraction of structured information from LLM outputs reliably.</p>
<p>The following code uses Mirascope to extract meeting details such as topic, date, time, and participants.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">os</span>


<span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s2">&quot;OPENAI_API_KEY&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&quot;YOUR_API_KEY&quot;</span>
</pre></div>
</div>
</div>
</div>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">List</span><span class="p">,</span> <span class="n">Type</span>
<span class="kn">from</span> <span class="nn">pydantic</span> <span class="kn">import</span> <span class="n">BaseModel</span>
<span class="kn">from</span> <span class="nn">mirascope.openai</span> <span class="kn">import</span> <span class="n">OpenAIExtractor</span>


<span class="k">class</span> <span class="nc">MeetingDetails</span><span class="p">(</span><span class="n">BaseModel</span><span class="p">):</span>
<span class="n">topic</span><span class="p">:</span> <span class="nb">str</span>
<span class="n">date</span><span class="p">:</span> <span class="nb">str</span>
<span class="n">time</span><span class="p">:</span> <span class="nb">str</span>
<span class="n">participants</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="nb">str</span><span class="p">]</span>


<span class="k">class</span> <span class="nc">MeetingExtractor</span><span class="p">(</span><span class="n">OpenAIExtractor</span><span class="p">[</span><span class="n">MeetingDetails</span><span class="p">]):</span>
<span class="n">extract_schema</span><span class="p">:</span> <span class="n">Type</span><span class="p">[</span><span class="n">MeetingDetails</span><span class="p">]</span> <span class="o">=</span> <span class="n">MeetingDetails</span>
<span class="n">prompt_template</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;</span>
<span class="s2"> Extract the meeting details from the following description:</span>
<span class="s2"> </span><span class="si">{description}</span>
<span class="s2"> &quot;&quot;&quot;</span>

<span class="n">description</span><span class="p">:</span> <span class="nb">str</span>


<span class="c1"># Example usage:</span>
<span class="n">description</span> <span class="o">=</span> <span class="s2">&quot;Discuss the upcoming product launch on June 15th at 3 PM with John, Sarah, and Mike.&quot;</span>
<span class="n">meeting_details</span> <span class="o">=</span> <span class="n">MeetingExtractor</span><span class="p">(</span><span class="n">description</span><span class="o">=</span><span class="n">description</span><span class="p">)</span><span class="o">.</span><span class="n">extract</span><span class="p">()</span>
<span class="k">assert</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">meeting_details</span><span class="p">,</span> <span class="n">MeetingDetails</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">meeting_details</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>topic=&#39;Upcoming product launch&#39; date=&#39;June 15th&#39; time=&#39;3 PM&#39; participants=[&#39;John&#39;, &#39;Sarah&#39;, &#39;Mike&#39;]
</pre></div>
</div>
</div>
</div>
<p><a class="reference external" href="https://bit.ly/4bkciv3">Link to Mirascope</a>.</p>
</section>
</section>

<script type="text/x-thebe-config">
Expand Down Expand Up @@ -727,6 +796,7 @@ <h2><span class="section-number">6.16.2. </span>Outlines: Ensuring Consistent Ou
<ul class="visible nav section-nav flex-column">
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#simplify-llm-integration-with-magentic-s-prompt-decorator">6.16.1. Simplify LLM Integration with Magentic’s @prompt Decorator</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#outlines-ensuring-consistent-outputs-from-language-models">6.16.2. Outlines: Ensuring Consistent Outputs from Language Models</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#mirascope-extract-structured-data-extraction-from-llm-outputs">6.16.3. Mirascope: Extract Structured Data Extraction From LLM Outputs</a></li>
</ul>
</nav></div>

Expand Down
2 changes: 1 addition & 1 deletion docs/_sources/Chapter2/typing.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.6"
"version": "3.11.6"
},
"toc": {
"base_numbering": 1,
Expand Down
99 changes: 99 additions & 0 deletions docs/_sources/Chapter5/llm.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,105 @@
"source": [
"[Link to Outlines](https://github.com/outlines-dev/outlines)."
]
},
{
"cell_type": "markdown",
"id": "ff519c06",
"metadata": {},
"source": [
"### Mirascope: Extract Structured Data Extraction From LLM Outputs"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dc12a009",
"metadata": {
"tags": [
"hide-cell"
]
},
"outputs": [],
"source": [
"!pip install mirascope"
]
},
{
"cell_type": "markdown",
"id": "0ea9cc6f",
"metadata": {},
"source": [
"Large Language Models (LLMs) are powerful at producing human-like text, but their outputs lack structure, which can limit their usefulness in many practical applications that require organized data.\n",
"\n",
"Mirascope offers a solution by enabling the extraction of structured information from LLM outputs reliably.\n",
"\n",
"The following code uses Mirascope to extract meeting details such as topic, date, time, and participants."
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "03c7cfb0",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
"\n",
"os.environ[\"OPENAI_API_KEY\"] = \"YOUR_API_KEY\"\n"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "79f19428",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"topic='Upcoming product launch' date='June 15th' time='3 PM' participants=['John', 'Sarah', 'Mike']\n"
]
}
],
"source": [
"from typing import List, Type\n",
"from pydantic import BaseModel\n",
"from mirascope.openai import OpenAIExtractor\n",
"\n",
"\n",
"class MeetingDetails(BaseModel):\n",
" topic: str\n",
" date: str\n",
" time: str\n",
" participants: List[str]\n",
"\n",
"\n",
"class MeetingExtractor(OpenAIExtractor[MeetingDetails]):\n",
" extract_schema: Type[MeetingDetails] = MeetingDetails\n",
" prompt_template = \"\"\"\n",
" Extract the meeting details from the following description:\n",
" {description}\n",
" \"\"\"\n",
"\n",
" description: str\n",
"\n",
"\n",
"# Example usage:\n",
"description = \"Discuss the upcoming product launch on June 15th at 3 PM with John, Sarah, and Mike.\"\n",
"meeting_details = MeetingExtractor(description=description).extract()\n",
"assert isinstance(meeting_details, MeetingDetails)\n",
"print(meeting_details)"
]
},
{
"cell_type": "markdown",
"id": "641d2dd5",
"metadata": {},
"source": [
"[Link to Mirascope](https://bit.ly/4bkciv3)."
]
}
],
"metadata": {
Expand Down
2 changes: 1 addition & 1 deletion docs/searchindex.js

Large diffs are not rendered by default.

0 comments on commit 85446bd

Please sign in to comment.