Spaces:
Running
Running
devjas1
commited on
Commit
·
3f0c944
1
Parent(s):
172e178
Update index.html to enhance accessibility and structure with improved headings and IDs for the Table of Contents and sections.
Browse files- index.html +88 -89
index.html
CHANGED
|
@@ -77,7 +77,7 @@
|
|
| 77 |
queries into SQL, executing them, and intelligently processing the results, including handling complex
|
| 78 |
scenarios
|
| 79 |
like table joins.</p>
|
| 80 |
-
<h2>Table of Contents</h2>
|
| 81 |
<ul>
|
| 82 |
<li><a href="#intelligent-text-to-sql-agent-with-smolagents">Intelligent Text-to-SQL Agent with
|
| 83 |
<code>smolagents</code></a></li>
|
|
@@ -97,7 +97,8 @@
|
|
| 97 |
<li><a href="#contributing">Contributing</a></li>
|
| 98 |
<li><a href="#license">License</a></li>
|
| 99 |
</ul>
|
| 100 |
-
<h2 class="code-line" data-line-start=29 data-line-end=30><a
|
|
|
|
| 101 |
Agent for Text-to-SQL?</h2>
|
| 102 |
<p class="has-line-data" data-line-start="31" data-line-end="32">Traditional Text-to-SQL pipelines often suffer
|
| 103 |
from
|
|
@@ -122,7 +123,7 @@
|
|
| 122 |
<li class="has-line-data" data-line-start="40" data-line-end="42"><strong>Enhanced Robustness:</strong>
|
| 123 |
Providing a more reliable and intelligent way to interact with databases from natural language.</li>
|
| 124 |
</ul>
|
| 125 |
-
<h2 class="code-line" data-line-start=42 data-line-end=43><a id="Features_42"></a>Features</h2>
|
| 126 |
<ul>
|
| 127 |
<li class="has-line-data" data-line-start="44" data-line-end="45"><strong>Natural Language to SQL:</strong>
|
| 128 |
Translates user questions into executable SQL queries.</li>
|
|
@@ -139,7 +140,8 @@
|
|
| 139 |
Integrates
|
| 140 |
with various Large Language Models (LLMs) via <code>smolagents</code>.</li>
|
| 141 |
</ul>
|
| 142 |
-
<h2 class="code-line" data-line-start=51 data-line-end=52><a
|
|
|
|
| 143 |
<p class="has-line-data" data-line-start="53" data-line-end="54">To get started, clone this repository and
|
| 144 |
install
|
| 145 |
the required dependencies:</p>
|
|
@@ -147,33 +149,31 @@
|
|
| 147 |
<span class="hljs-built_in">cd</span> text-to-sql-agent
|
| 148 |
pip install smolagents python-dotenv sqlalchemy --upgrade -q
|
| 149 |
</code></pre>
|
| 150 |
-
<pre><code class="has-line-data" data-line-start="62" data-line-end="148">
|
| 151 |
-
**Note:** To interact with Large Language Models via inference providers (e.g., Hugging Face Inference API), you'll need a valid authentication token set as an environment variable, typically `HF_TOKEN`.
|
| 152 |
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
.
|
|
|
|
|
|
|
| 159 |
├── README.md
|
| 160 |
└── text_to_sql.py
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
# text_to_sql.py
|
| 173 |
-
from dotenv import load_dotenv
|
| 174 |
load_dotenv()
|
| 175 |
|
| 176 |
-
from sqlalchemy import (
|
| 177 |
create_engine,
|
| 178 |
MetaData,
|
| 179 |
Table,
|
|
@@ -185,71 +185,66 @@ from sqlalchemy import (
|
|
| 185 |
inspect,
|
| 186 |
text,
|
| 187 |
)
|
| 188 |
-
from smolagents import tool, CodeAgent, InferenceClientModel
|
| 189 |
-
|
| 190 |
-
# ... (rest of the code)
|
| 191 |
-
```
|
| 192 |
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
|
|
|
|
|
|
| 200 |
metadata_obj = MetaData()
|
| 201 |
|
| 202 |
-
def insert_rows_into_table(rows, table, engine=engine)
|
| 203 |
-
for row in rows:
|
| 204 |
stmt = insert(table).values(**row)
|
| 205 |
-
with engine.begin() as connection:
|
| 206 |
connection.execute(stmt)
|
| 207 |
|
| 208 |
-
|
| 209 |
receipts = Table(
|
| 210 |
-
|
| 211 |
metadata_obj,
|
| 212 |
-
Column(
|
| 213 |
-
Column(
|
| 214 |
-
Column(
|
| 215 |
-
Column(
|
| 216 |
)
|
| 217 |
metadata_obj.create_all(engine)
|
| 218 |
|
| 219 |
-
|
| 220 |
rows = [
|
| 221 |
-
{
|
| 222 |
-
{
|
| 223 |
-
{
|
| 224 |
-
{
|
| 225 |
]
|
| 226 |
insert_rows_into_table(rows, receipts)
|
| 227 |
|
| 228 |
-
|
| 229 |
inspector = inspect(engine)
|
| 230 |
-
columns_info = [(col[
|
| 231 |
-
table_description =
|
| 232 |
print(table_description)```
|
| 233 |
-
|
| 234 |
-
**Output:**
|
| 235 |
</code></pre>
|
| 236 |
-
<
|
| 237 |
-
<
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
def sql_engine(query: str) -> str:
|
| 252 |
-
"""
|
| 253 |
Enables execution of SQL queries against the database.
|
| 254 |
Outputs the query results as a formatted string.
|
| 255 |
|
|
@@ -263,16 +258,16 @@ def sql_engine(query: str) -> str:
|
|
| 263 |
|
| 264 |
Args:
|
| 265 |
query: The precise SQL query string to be executed.
|
| 266 |
-
Example:
|
| 267 |
-
|
| 268 |
-
output =
|
| 269 |
-
with engine.connect() as con:
|
| 270 |
rows = con.execute(text(query))
|
| 271 |
-
for row in rows:
|
| 272 |
-
output +=
|
| 273 |
-
return output
|
| 274 |
-
|
| 275 |
-
<h3 class="code-line" data-line-start=190 data-line-end=191><a
|
| 276 |
id="Instantiating_the_Agent_Single_Table_190"></a>Instantiating the Agent (Single Table)</h3>
|
| 277 |
<p class="has-line-data" data-line-start="192" data-line-end="193">We create a <code>CodeAgent</code> and
|
| 278 |
provide it
|
|
@@ -283,7 +278,7 @@ agent = CodeAgent(
|
|
| 283 |
model=InferenceClientModel(model_id=<span class="hljs-string">"meta-llama/Llama-3.1-8B-Instruct"</span>),
|
| 284 |
)
|
| 285 |
</code></pre>
|
| 286 |
-
<h3 class="code-line" data-line-start=202 data-line-end=203><a
|
| 287 |
id="Querying_the_Agent_Single_Table_202"></a>Querying
|
| 288 |
the Agent: Single Table</h3>
|
| 289 |
<p class="has-line-data" data-line-start="204" data-line-end="205">Now, we can ask the agent a question and
|
|
@@ -301,7 +296,8 @@ agent.run(<span class="hljs-string">"Can you give me the name of the client who
|
|
| 301 |
and execute <code>SELECT MAX(price), customer_name FROM receipts ORDER BY price DESC LIMIT 1</code>, parse
|
| 302 |
the
|
| 303 |
result <code>(53.43, 'Woodrow Wilson')</code>, and identify ‘Woodrow Wilson’.</p>
|
| 304 |
-
<h3 class="code-line" data-line-start=214 data-line-end=215><a
|
|
|
|
| 305 |
for
|
| 306 |
Table Joins</h3>
|
| 307 |
<p class="has-line-data" data-line-start="216" data-line-end="217">To handle more complex queries, we add a
|
|
@@ -356,7 +352,7 @@ Table 'waiters':
|
|
| 356 |
- receipt_id: INTEGER
|
| 357 |
- waiter_name: VARCHAR(16)
|
| 358 |
</code></pre>
|
| 359 |
-
<h3 class="code-line" data-line-start=272 data-line-end=273><a
|
| 360 |
id="Querying_the_Agent_MultiTable_272"></a>Querying
|
| 361 |
the Agent: Multi-Table</h3>
|
| 362 |
<p class="has-line-data" data-line-start="274" data-line-end="275">We switch to a more powerful LLM
|
|
@@ -377,7 +373,8 @@ agent.run(<span class="hljs-string">"Which waiter received the highest total amo
|
|
| 377 |
then process the results in Python to sum tips per waiter and identify “Michael Watts” as having the highest
|
| 378 |
total tips.
|
| 379 |
</p>
|
| 380 |
-
<h2 class="code-line" data-line-start=289 data-line-end=290><a id="How_it_Works_289"></a>How
|
|
|
|
| 381 |
<p class="has-line-data" data-line-start="291" data-line-end="292">The <code>smolagents</code>
|
| 382 |
<code>CodeAgent</code> operates on the <strong>ReAct (Reasoning + Acting)</strong> framework:
|
| 383 |
</p>
|
|
@@ -408,7 +405,8 @@ agent.run(<span class="hljs-string">"Which waiter received the highest total amo
|
|
| 408 |
solve
|
| 409 |
complex problems and recover from errors, making it more robust than traditional direct translation methods.
|
| 410 |
</p>
|
| 411 |
-
<h2 class="code-line" data-line-start=300 data-line-end=301><a
|
|
|
|
| 412 |
Concepts
|
| 413 |
Demonstrated</h2>
|
| 414 |
<ul>
|
|
@@ -430,11 +428,12 @@ agent.run(<span class="hljs-string">"Which waiter received the highest total amo
|
|
| 430 |
<li class="has-line-data" data-line-start="307" data-line-end="309"><strong>ReAct Paradigm:</strong> The
|
| 431 |
iterative cycle of reasoning, acting, and observation that enables self-correction.</li>
|
| 432 |
</ul>
|
| 433 |
-
<h2 class="code-line" data-line-start=309 data-line-end=310><a
|
|
|
|
| 434 |
<p class="has-line-data" data-line-start="311" data-line-end="312">Feel free to open issues or submit pull
|
| 435 |
requests
|
| 436 |
if you have suggestions or improvements!</p>
|
| 437 |
-
<h2 class="code-line" data-line-start=313 data-line-end=314><a id="License_313"></a>License</h2>
|
| 438 |
<p class="has-line-data" data-line-start="315" data-line-end="316">This project is open-sourced under the MIT
|
| 439 |
License. See the <code>LICENSE</code> file for more details.</p>
|
| 440 |
</div>
|
|
|
|
| 77 |
queries into SQL, executing them, and intelligently processing the results, including handling complex
|
| 78 |
scenarios
|
| 79 |
like table joins.</p>
|
| 80 |
+
<h2 id="table-of-contents">Table of Contents</h2>
|
| 81 |
<ul>
|
| 82 |
<li><a href="#intelligent-text-to-sql-agent-with-smolagents">Intelligent Text-to-SQL Agent with
|
| 83 |
<code>smolagents</code></a></li>
|
|
|
|
| 97 |
<li><a href="#contributing">Contributing</a></li>
|
| 98 |
<li><a href="#license">License</a></li>
|
| 99 |
</ul>
|
| 100 |
+
<h2 id="why-an-ai-agent-for-text-to-sql" class="code-line" data-line-start=29 data-line-end=30><a
|
| 101 |
+
id="Why_an_AI_Agent_for_TexttoSQL_29"></a>Why an AI
|
| 102 |
Agent for Text-to-SQL?</h2>
|
| 103 |
<p class="has-line-data" data-line-start="31" data-line-end="32">Traditional Text-to-SQL pipelines often suffer
|
| 104 |
from
|
|
|
|
| 123 |
<li class="has-line-data" data-line-start="40" data-line-end="42"><strong>Enhanced Robustness:</strong>
|
| 124 |
Providing a more reliable and intelligent way to interact with databases from natural language.</li>
|
| 125 |
</ul>
|
| 126 |
+
<h2 id="features" class="code-line" data-line-start=42 data-line-end=43><a id="Features_42"></a>Features</h2>
|
| 127 |
<ul>
|
| 128 |
<li class="has-line-data" data-line-start="44" data-line-end="45"><strong>Natural Language to SQL:</strong>
|
| 129 |
Translates user questions into executable SQL queries.</li>
|
|
|
|
| 140 |
Integrates
|
| 141 |
with various Large Language Models (LLMs) via <code>smolagents</code>.</li>
|
| 142 |
</ul>
|
| 143 |
+
<h2 id="installation" class="code-line" data-line-start=51 data-line-end=52><a
|
| 144 |
+
id="Installation_51"></a>Installation</h2>
|
| 145 |
<p class="has-line-data" data-line-start="53" data-line-end="54">To get started, clone this repository and
|
| 146 |
install
|
| 147 |
the required dependencies:</p>
|
|
|
|
| 149 |
<span class="hljs-built_in">cd</span> text-to-sql-agent
|
| 150 |
pip install smolagents python-dotenv sqlalchemy --upgrade -q
|
| 151 |
</code></pre>
|
|
|
|
|
|
|
| 152 |
|
| 153 |
+
<p class="has-line-data" data-line-start="4" data-line-end="5"><strong>Note:</strong> To interact with Large
|
| 154 |
+
Language Models via inference providers (e.g., Hugging Face Inference API), you’ll need a valid
|
| 155 |
+
authentication token set as an environment variable, typically <code>HF_TOKEN</code>.</p>
|
| 156 |
+
<h2 class="code-line" data-line-start=6 data-line-end=7><a id="Project_Structure_6"></a>Project Structure</h2>
|
| 157 |
+
<p class="has-line-data" data-line-start="8" data-line-end="9">The core logic of this project is encapsulated in
|
| 158 |
+
<code>text_to_sql.py</code>.
|
| 159 |
+
</p>
|
| 160 |
+
<pre><code class="has-line-data" data-line-start="11" data-line-end="15" class="language-text">.
|
| 161 |
├── README.md
|
| 162 |
└── text_to_sql.py
|
| 163 |
+
</code></pre>
|
| 164 |
+
<h2 class="code-line" data-line-start=16 data-line-end=17><a id="Usage_16"></a>Usage</h2>
|
| 165 |
+
<p class="has-line-data" data-line-start="18" data-line-end="19">This section walks through the
|
| 166 |
+
<code>text_to_sql.py</code> script, explaining each part of building and using the agent.
|
| 167 |
+
</p>
|
| 168 |
+
<h3 class="code-line" data-line-start=20 data-line-end=21><a id="Setup_and_Dependencies_20"></a>Setup and
|
| 169 |
+
Dependencies</h3>
|
| 170 |
+
<p class="has-line-data" data-line-start="22" data-line-end="23">First, load your environment variables,
|
| 171 |
+
including your LLM token.</p>
|
| 172 |
+
<pre><code class="has-line-data" data-line-start="25" data-line-end="45" class="language-python"><span class="hljs-comment"># text_to_sql.py</span>
|
| 173 |
+
<span class="hljs-keyword">from</span> dotenv <span class="hljs-keyword">import</span> load_dotenv
|
|
|
|
|
|
|
| 174 |
load_dotenv()
|
| 175 |
|
| 176 |
+
<span class="hljs-keyword">from</span> sqlalchemy <span class="hljs-keyword">import</span> (
|
| 177 |
create_engine,
|
| 178 |
MetaData,
|
| 179 |
Table,
|
|
|
|
| 185 |
inspect,
|
| 186 |
text,
|
| 187 |
)
|
| 188 |
+
<span class="hljs-keyword">from</span> smolagents <span class="hljs-keyword">import</span> tool, CodeAgent, InferenceClientModel
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
+
<span class="hljs-comment"># ... (rest of the code)</span>
|
| 191 |
+
</code></pre>
|
| 192 |
+
<h3 class="code-line" data-line-start=46 data-line-end=47><a id="Database_Initialization_46"></a>Database
|
| 193 |
+
Initialization</h3>
|
| 194 |
+
<p class="has-line-data" data-line-start="48" data-line-end="49">We set up an in-memory SQLite database using
|
| 195 |
+
SQLAlchemy, defining <code>receipts</code> and <code>waiters</code> tables and populating them with sample
|
| 196 |
+
data.</p>
|
| 197 |
+
<pre><code class="has-line-data" data-line-start="51" data-line-end="88" class="language-python"><span class="hljs-comment"># text_to_sql.py</span>
|
| 198 |
+
engine = create_engine(<span class="hljs-string">"sqlite:///:memory:"</span>)
|
| 199 |
metadata_obj = MetaData()
|
| 200 |
|
| 201 |
+
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">insert_rows_into_table</span><span class="hljs-params">(rows, table, engine=engine)</span>:</span>
|
| 202 |
+
<span class="hljs-keyword">for</span> row <span class="hljs-keyword">in</span> rows:
|
| 203 |
stmt = insert(table).values(**row)
|
| 204 |
+
<span class="hljs-keyword">with</span> engine.begin() <span class="hljs-keyword">as</span> connection:
|
| 205 |
connection.execute(stmt)
|
| 206 |
|
| 207 |
+
<span class="hljs-comment"># Define the 'receipts' table</span>
|
| 208 |
receipts = Table(
|
| 209 |
+
<span class="hljs-string">"receipts"</span>,
|
| 210 |
metadata_obj,
|
| 211 |
+
Column(<span class="hljs-string">"receipt_id"</span>, Integer, primary_key=<span class="hljs-keyword">True</span>),
|
| 212 |
+
Column(<span class="hljs-string">"customer_name"</span>, String(<span class="hljs-number">255</span>)), <span class="hljs-comment"># Adjusted from String(16) for longer names</span>
|
| 213 |
+
Column(<span class="hljs-string">"price"</span>, Float),
|
| 214 |
+
Column(<span class="hljs-string">"tip"</span>, Float),
|
| 215 |
)
|
| 216 |
metadata_obj.create_all(engine)
|
| 217 |
|
| 218 |
+
<span class="hljs-comment"># Sample data for 'receipts'</span>
|
| 219 |
rows = [
|
| 220 |
+
{<span class="hljs-string">"receipt_id"</span>: <span class="hljs-number">1</span>, <span class="hljs-string">"customer_name"</span>: <span class="hljs-string">"Alan Payne"</span>, <span class="hljs-string">"price"</span>: <span class="hljs-number">12.06</span>, <span class="hljs-string">"tip"</span>: <span class="hljs-number">1.20</span>},
|
| 221 |
+
{<span class="hljs-string">"receipt_id"</span>: <span class="hljs-number">2</span>, <span class="hljs-string">"customer_name"</span>: <span class="hljs-string">"Alex Mason"</span>, <span class="hljs-string">"price"</span>: <span class="hljs-number">23.86</span>, <span class="hljs-string">"tip"</span>: <span class="hljs-number">0.24</span>},
|
| 222 |
+
{<span class="hljs-string">"receipt_id"</span>: <span class="hljs-number">3</span>, <span class="hljs-string">"customer_name"</span>: <span class="hljs-string">"Woodrow Wilson"</span>, <span class="hljs-string">"price"</span>: <span class="hljs-number">53.43</span>, <span class="hljs-string">"tip"</span>: <span class="hljs-number">5.43</span>},
|
| 223 |
+
{<span class="hljs-string">"receipt_id"</span>: <span class="hljs-number">4</span>, <span class="hljs-string">"customer_name"</span>: <span class="hljs-string">"Margaret James"</span>, <span class="hljs-string">"price"</span>: <span class="hljs-number">21.11</span>, <span class="hljs-string">"tip"</span>: <span class="hljs-number">1.00</span>},
|
| 224 |
]
|
| 225 |
insert_rows_into_table(rows, receipts)
|
| 226 |
|
| 227 |
+
<span class="hljs-comment"># Print table schema (for LLM context)</span>
|
| 228 |
inspector = inspect(engine)
|
| 229 |
+
columns_info = [(col[<span class="hljs-string">"name"</span>], col[<span class="hljs-string">"type"</span>]) <span class="hljs-keyword">for</span> col <span class="hljs-keyword">in</span> inspector.get_columns(<span class="hljs-string">"receipts"</span>)]
|
| 230 |
+
table_description = <span class="hljs-string">"Columns:\n"</span> + <span class="hljs-string">"\n"</span>.join([f<span class="hljs-string">" - {name}: {col_type}"</span> <span class="hljs-keyword">for</span> name, col_type <span class="hljs-keyword">in</span> columns_info])
|
| 231 |
print(table_description)```
|
|
|
|
|
|
|
| 232 |
</code></pre>
|
| 233 |
+
<b>Output:</b>
|
| 234 |
+
<p class="has-line-data" data-line-start="0" data-line-end="1">Columns:</p>
|
| 235 |
+
<p class="has-line-data" data-line-start="2" data-line-end="6">receipt_id: INTEGER<br>
|
| 236 |
+
customer_name: VARCHAR(255)<br>
|
| 237 |
+
price: FLOAT<br>
|
| 238 |
+
tip: FLOAT</p>
|
| 239 |
+
<h3 class="code-line" data-line-start=7 data-line-end=8><a id="Creating_the_SQL_Tool_7"></a>Creating the SQL
|
| 240 |
+
Tool</h3>
|
| 241 |
+
<p class="has-line-data" data-line-start="9" data-line-end="10">The <code>sql_engine</code> function acts as the
|
| 242 |
+
agent’s interface to the database. Its detailed docstring provides the LLM with crucial information about
|
| 243 |
+
its functionality and the database schema.</p>
|
| 244 |
+
<pre><code class="has-line-data" data-line-start="12" data-line-end="37" class="language-python"><span class="hljs-comment"># text_to_sql.py</span>
|
| 245 |
+
<span class="hljs-decorator">@tool</span>
|
| 246 |
+
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">sql_engine</span><span class="hljs-params">(query: str)</span> -> <span class="hljs-title">str</span>:</span>
|
| 247 |
+
<span class="hljs-string">"""
|
|
|
|
|
|
|
| 248 |
Enables execution of SQL queries against the database.
|
| 249 |
Outputs the query results as a formatted string.
|
| 250 |
|
|
|
|
| 258 |
|
| 259 |
Args:
|
| 260 |
query: The precise SQL query string to be executed.
|
| 261 |
+
Example: "SELECT customer_name FROM receipts WHERE price > 10.0;"
|
| 262 |
+
"""</span>
|
| 263 |
+
output = <span class="hljs-string">""</span>
|
| 264 |
+
<span class="hljs-keyword">with</span> engine.connect() <span class="hljs-keyword">as</span> con:
|
| 265 |
rows = con.execute(text(query))
|
| 266 |
+
<span class="hljs-keyword">for</span> row <span class="hljs-keyword">in</span> rows:
|
| 267 |
+
output += <span class="hljs-string">"\n"</span> + str(row)
|
| 268 |
+
<span class="hljs-keyword">return</span> output</code></pre>
|
| 269 |
+
|
| 270 |
+
<h3 id="instantiating-the-agent-single-table" class="code-line" data-line-start=190 data-line-end=191><a
|
| 271 |
id="Instantiating_the_Agent_Single_Table_190"></a>Instantiating the Agent (Single Table)</h3>
|
| 272 |
<p class="has-line-data" data-line-start="192" data-line-end="193">We create a <code>CodeAgent</code> and
|
| 273 |
provide it
|
|
|
|
| 278 |
model=InferenceClientModel(model_id=<span class="hljs-string">"meta-llama/Llama-3.1-8B-Instruct"</span>),
|
| 279 |
)
|
| 280 |
</code></pre>
|
| 281 |
+
<h3 id="querying-the-agent-single-table" class="code-line" data-line-start=202 data-line-end=203><a
|
| 282 |
id="Querying_the_Agent_Single_Table_202"></a>Querying
|
| 283 |
the Agent: Single Table</h3>
|
| 284 |
<p class="has-line-data" data-line-start="204" data-line-end="205">Now, we can ask the agent a question and
|
|
|
|
| 296 |
and execute <code>SELECT MAX(price), customer_name FROM receipts ORDER BY price DESC LIMIT 1</code>, parse
|
| 297 |
the
|
| 298 |
result <code>(53.43, 'Woodrow Wilson')</code>, and identify ‘Woodrow Wilson’.</p>
|
| 299 |
+
<h3 id="extending-for-table-joins" class="code-line" data-line-start=214 data-line-end=215><a
|
| 300 |
+
id="Extending_for_Table_Joins_214"></a>Extending
|
| 301 |
for
|
| 302 |
Table Joins</h3>
|
| 303 |
<p class="has-line-data" data-line-start="216" data-line-end="217">To handle more complex queries, we add a
|
|
|
|
| 352 |
- receipt_id: INTEGER
|
| 353 |
- waiter_name: VARCHAR(16)
|
| 354 |
</code></pre>
|
| 355 |
+
<h3 id="querying-the-agent-multi-table" class="code-line" data-line-start=272 data-line-end=273><a
|
| 356 |
id="Querying_the_Agent_MultiTable_272"></a>Querying
|
| 357 |
the Agent: Multi-Table</h3>
|
| 358 |
<p class="has-line-data" data-line-start="274" data-line-end="275">We switch to a more powerful LLM
|
|
|
|
| 373 |
then process the results in Python to sum tips per waiter and identify “Michael Watts” as having the highest
|
| 374 |
total tips.
|
| 375 |
</p>
|
| 376 |
+
<h2 id="how-it-works" class="code-line" data-line-start=289 data-line-end=290><a id="How_it_Works_289"></a>How
|
| 377 |
+
it Works</h2>
|
| 378 |
<p class="has-line-data" data-line-start="291" data-line-end="292">The <code>smolagents</code>
|
| 379 |
<code>CodeAgent</code> operates on the <strong>ReAct (Reasoning + Acting)</strong> framework:
|
| 380 |
</p>
|
|
|
|
| 405 |
solve
|
| 406 |
complex problems and recover from errors, making it more robust than traditional direct translation methods.
|
| 407 |
</p>
|
| 408 |
+
<h2 id="key-concepts-demonstrated" class="code-line" data-line-start=300 data-line-end=301><a
|
| 409 |
+
id="Key_Concepts_Demonstrated_300"></a>Key
|
| 410 |
Concepts
|
| 411 |
Demonstrated</h2>
|
| 412 |
<ul>
|
|
|
|
| 428 |
<li class="has-line-data" data-line-start="307" data-line-end="309"><strong>ReAct Paradigm:</strong> The
|
| 429 |
iterative cycle of reasoning, acting, and observation that enables self-correction.</li>
|
| 430 |
</ul>
|
| 431 |
+
<h2 id="contibuting" class="code-line" data-line-start=309 data-line-end=310><a
|
| 432 |
+
id="Contributing_309"></a>Contributing</h2>
|
| 433 |
<p class="has-line-data" data-line-start="311" data-line-end="312">Feel free to open issues or submit pull
|
| 434 |
requests
|
| 435 |
if you have suggestions or improvements!</p>
|
| 436 |
+
<h2 id="license" class="code-line" data-line-start=313 data-line-end=314><a id="License_313"></a>License</h2>
|
| 437 |
<p class="has-line-data" data-line-start="315" data-line-end="316">This project is open-sourced under the MIT
|
| 438 |
License. See the <code>LICENSE</code> file for more details.</p>
|
| 439 |
</div>
|