Buckets:

download
raw
59.4 kB
<meta charset="utf-8" /><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Hands-On: Extending Nano Harness&quot;,&quot;local&quot;:&quot;hands-on-extending-nano-harness&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Extension 1: Add web_fetch Tool&quot;,&quot;local&quot;:&quot;extension-1-add-webfetch-tool&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Extension 2: Add hf_search Tool&quot;,&quot;local&quot;:&quot;extension-2-add-hfsearch-tool&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Full Extended Example&quot;,&quot;local&quot;:&quot;full-extended-example&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Running the Harness&quot;,&quot;local&quot;:&quot;running-the-harness&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Exercise: Extend Further&quot;,&quot;local&quot;:&quot;exercise-extend-further&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;1. git_log Tool&quot;,&quot;local&quot;:&quot;1-gitlog-tool&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;2. json_parse Tool&quot;,&quot;local&quot;:&quot;2-jsonparse-tool&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;3. compute_stats Tool&quot;,&quot;local&quot;:&quot;3-computestats-tool&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Key Takeaways&quot;,&quot;local&quot;:&quot;key-takeaways&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}">
<link href="/docs/context-course/pr_7/en/_app/immutable/assets/0.e3b0c442.css" rel="modulepreload">
<link rel="modulepreload" href="/docs/context-course/pr_7/en/_app/immutable/entry/start.55c3acaa.js">
<link rel="modulepreload" href="/docs/context-course/pr_7/en/_app/immutable/chunks/scheduler.4048030c.js">
<link rel="modulepreload" href="/docs/context-course/pr_7/en/_app/immutable/chunks/singletons.42509544.js">
<link rel="modulepreload" href="/docs/context-course/pr_7/en/_app/immutable/chunks/index.dd3f5310.js">
<link rel="modulepreload" href="/docs/context-course/pr_7/en/_app/immutable/chunks/paths.f6762227.js">
<link rel="modulepreload" href="/docs/context-course/pr_7/en/_app/immutable/entry/app.10ec5479.js">
<link rel="modulepreload" href="/docs/context-course/pr_7/en/_app/immutable/chunks/preload-helper.665302cf.js">
<link rel="modulepreload" href="/docs/context-course/pr_7/en/_app/immutable/chunks/index.5d0b9360.js">
<link rel="modulepreload" href="/docs/context-course/pr_7/en/_app/immutable/nodes/0.b63aa764.js">
<link rel="modulepreload" href="/docs/context-course/pr_7/en/_app/immutable/chunks/each.e59479a4.js">
<link rel="modulepreload" href="/docs/context-course/pr_7/en/_app/immutable/nodes/36.1ae5b65f.js">
<link rel="modulepreload" href="/docs/context-course/pr_7/en/_app/immutable/chunks/MermaidChart.svelte_svelte_type_style_lang.99c2e6c8.js">
<link rel="modulepreload" href="/docs/context-course/pr_7/en/_app/immutable/chunks/CodeBlock.ff2a3276.js"><!-- HEAD_svelte-u9bgzb_START --><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Hands-On: Extending Nano Harness&quot;,&quot;local&quot;:&quot;hands-on-extending-nano-harness&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Extension 1: Add web_fetch Tool&quot;,&quot;local&quot;:&quot;extension-1-add-webfetch-tool&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Extension 2: Add hf_search Tool&quot;,&quot;local&quot;:&quot;extension-2-add-hfsearch-tool&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Full Extended Example&quot;,&quot;local&quot;:&quot;full-extended-example&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Running the Harness&quot;,&quot;local&quot;:&quot;running-the-harness&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Exercise: Extend Further&quot;,&quot;local&quot;:&quot;exercise-extend-further&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;1. git_log Tool&quot;,&quot;local&quot;:&quot;1-gitlog-tool&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;2. json_parse Tool&quot;,&quot;local&quot;:&quot;2-jsonparse-tool&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;3. compute_stats Tool&quot;,&quot;local&quot;:&quot;3-computestats-tool&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Key Takeaways&quot;,&quot;local&quot;:&quot;key-takeaways&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}"><!-- HEAD_svelte-u9bgzb_END --> <p></p> <div class="items-center shrink-0 min-w-[100px] max-sm:min-w-[50px] justify-end ml-auto flex" style="float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"><div class="inline-flex rounded-md max-sm:rounded-sm"><button class="inline-flex items-center gap-1 h-7 max-sm:h-7 px-2 max-sm:px-1.5 text-sm font-medium text-gray-800 border border-r-0 rounded-l-md max-sm:rounded-l-sm border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-live="polite"><span class="inline-flex items-center justify-center rounded-md p-0.5 max-sm:p-0 hover:text-gray-800 dark:hover:text-gray-200"><svg class="sm:size-3.5 size-3" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg></span> <span>Copy page</span></button> <button class="inline-flex items-center justify-center w-6 max-sm:w-5 h-7 max-sm:h-7 disabled:pointer-events-none text-sm text-gray-500 hover:text-gray-700 dark:hover:text-white rounded-r-md max-sm:rounded-r-sm border border-l transition border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-haspopup="menu" aria-expanded="false" aria-label="Open copy menu"><svg class="transition-transform text-gray-400 overflow-visible sm:size-3.5 size-3 rotate-0" width="1em" height="1em" viewBox="0 0 12 7" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M1 1L6 6L11 1" stroke="currentColor"></path></svg></button></div> </div> <h1 class="relative group"><a id="hands-on-extending-nano-harness" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#hands-on-extending-nano-harness"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Hands-On: Extending Nano Harness</span></h1> <p data-svelte-h="svelte-167swwi">This section adds two tools — <code>web_fetch</code> and <code>hf_search</code> — and runs the harness against several models.</p> <h2 class="relative group"><a id="extension-1-add-webfetch-tool" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#extension-1-add-webfetch-tool"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Extension 1: Add web_fetch Tool</span></h2> <p data-svelte-h="svelte-tclhzn">Add to your nano_harness.py:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-keyword">import</span> urllib.request
<span class="hljs-keyword">import</span> urllib.error
<span class="hljs-keyword">def</span> <span class="hljs-title function_">web_fetch</span>(<span class="hljs-params">url, max_bytes=<span class="hljs-number">10000</span></span>):
<span class="hljs-string">&quot;&quot;&quot;Fetch web page content with size limit.&quot;&quot;&quot;</span>
<span class="hljs-keyword">try</span>:
<span class="hljs-keyword">with</span> urllib.request.urlopen(url, timeout=TIMEOUT_S) <span class="hljs-keyword">as</span> response:
content = response.read(max_bytes + <span class="hljs-number">1</span>)
<span class="hljs-keyword">if</span> <span class="hljs-built_in">len</span>(content) &gt; max_bytes:
content = content[:max_bytes] + <span class="hljs-string">b&quot;\n...[truncated]&quot;</span>
<span class="hljs-keyword">return</span> content.decode(<span class="hljs-string">&quot;utf-8&quot;</span>, errors=<span class="hljs-string">&quot;replace&quot;</span>)
<span class="hljs-keyword">except</span> urllib.error.URLError <span class="hljs-keyword">as</span> e:
<span class="hljs-keyword">return</span> <span class="hljs-string">f&quot;Error: Failed to fetch <span class="hljs-subst">{url}</span>: <span class="hljs-subst">{e}</span>&quot;</span>
<span class="hljs-keyword">except</span> Exception <span class="hljs-keyword">as</span> e:
<span class="hljs-keyword">return</span> <span class="hljs-string">f&quot;Error: <span class="hljs-subst">{<span class="hljs-built_in">type</span>(e).__name__}</span>: <span class="hljs-subst">{<span class="hljs-built_in">str</span>(e)}</span>&quot;</span><!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-jsbop7">A byte cap prevents loading huge responses, the <code>timeout</code> stops the request from hanging, and errors are returned as strings so the agent can react.</p> <p data-svelte-h="svelte-cj5i9t"><strong>In system prompt:</strong></p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START -->SYSTEM_PROMPT = <span class="hljs-string">&quot;&quot;&quot;
...
Tools:
- web_fetch(url, max_bytes=10000) → fetch webpage
...
&quot;&quot;&quot;</span><!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-1n8vwmi"><strong>Example usage:</strong></p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-comment"># Agent writes:</span>
content = web_fetch(<span class="hljs-string">&quot;https://huggingface.co/&quot;</span>)
<span class="hljs-comment"># Or with size limit:</span>
content = web_fetch(<span class="hljs-string">&quot;https://huggingface.co/&quot;</span>, max_bytes=<span class="hljs-number">5000</span>)<!-- HTML_TAG_END --></pre></div> <h2 class="relative group"><a id="extension-2-add-hfsearch-tool" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#extension-2-add-hfsearch-tool"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Extension 2: Add hf_search Tool</span></h2> <p data-svelte-h="svelte-3g8tvo">Add to nano_harness.py:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-keyword">def</span> <span class="hljs-title function_">hf_search</span>(<span class="hljs-params">query, resource_type=<span class="hljs-string">&quot;models&quot;</span>, limit=<span class="hljs-number">5</span></span>):
<span class="hljs-string">&quot;&quot;&quot;Search Hugging Face Hub (requires HF_TOKEN).&quot;&quot;&quot;</span>
<span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> API_KEY:
<span class="hljs-keyword">return</span> <span class="hljs-string">&quot;Error: HF_TOKEN not set. Can&#x27;t access Hugging Face API.&quot;</span>
<span class="hljs-keyword">try</span>:
url = <span class="hljs-string">f&quot;https://huggingface.co/api/<span class="hljs-subst">{resource_type}</span>&quot;</span>
params = <span class="hljs-string">f&quot;?search=<span class="hljs-subst">{query}</span>&amp;limit=<span class="hljs-subst">{limit}</span>&quot;</span>
req = urllib.request.Request(
url + params,
headers={<span class="hljs-string">&quot;Authorization&quot;</span>: <span class="hljs-string">f&quot;Bearer <span class="hljs-subst">{API_KEY}</span>&quot;</span>}
)
<span class="hljs-keyword">with</span> urllib.request.urlopen(req, timeout=TIMEOUT_S) <span class="hljs-keyword">as</span> response:
data = json.loads(response.read())
<span class="hljs-comment"># Format results</span>
results = []
<span class="hljs-keyword">for</span> item <span class="hljs-keyword">in</span> data[:limit]:
results.append({
<span class="hljs-string">&quot;id&quot;</span>: item.get(<span class="hljs-string">&quot;id&quot;</span>),
<span class="hljs-string">&quot;downloads&quot;</span>: item.get(<span class="hljs-string">&quot;downloads&quot;</span>, <span class="hljs-number">0</span>),
<span class="hljs-string">&quot;description&quot;</span>: item.get(<span class="hljs-string">&quot;description&quot;</span>, <span class="hljs-string">&quot;&quot;</span>)[:<span class="hljs-number">200</span>]
})
<span class="hljs-keyword">return</span> results
<span class="hljs-keyword">except</span> Exception <span class="hljs-keyword">as</span> e:
<span class="hljs-keyword">return</span> <span class="hljs-string">f&quot;Error: <span class="hljs-subst">{<span class="hljs-built_in">type</span>(e).__name__}</span>: <span class="hljs-subst">{<span class="hljs-built_in">str</span>(e)}</span>&quot;</span><!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-1tfydg9">The API key stays in the environment, results are capped by <code>limit</code>, and errors are caught and returned.</p> <p data-svelte-h="svelte-cj5i9t"><strong>In system prompt:</strong></p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START -->SYSTEM_PROMPT = <span class="hljs-string">&quot;&quot;&quot;
...
Tools:
- hf_search(query, resource_type=&#x27;models&#x27;, limit=10) → search HF
...
&quot;&quot;&quot;</span><!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-1n8vwmi"><strong>Example usage:</strong></p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-comment"># Agent writes:</span>
results = hf_search(<span class="hljs-string">&quot;bert&quot;</span>, resource_type=<span class="hljs-string">&quot;models&quot;</span>, limit=<span class="hljs-number">5</span>)
final_answer(results)<!-- HTML_TAG_END --></pre></div> <h2 class="relative group"><a id="full-extended-example" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#full-extended-example"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Full Extended Example</span></h2> <p data-svelte-h="svelte-dt21o9">Create <code>nano_harness_extended.py</code> with all tools. This version uses the Responses API so the same loop works against the Hugging Face router and a direct OpenAI endpoint without changing the control flow.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-comment">#!/usr/bin/env python3</span>
<span class="hljs-keyword">import</span> io
<span class="hljs-keyword">import</span> json
<span class="hljs-keyword">import</span> os
<span class="hljs-keyword">import</span> re
<span class="hljs-keyword">import</span> subprocess
<span class="hljs-keyword">import</span> urllib.error
<span class="hljs-keyword">import</span> urllib.request
<span class="hljs-keyword">from</span> contextlib <span class="hljs-keyword">import</span> redirect_stderr, redirect_stdout
<span class="hljs-keyword">from</span> pathlib <span class="hljs-keyword">import</span> Path
<span class="hljs-keyword">from</span> openai <span class="hljs-keyword">import</span> OpenAI
<span class="hljs-comment"># Configuration</span>
TASK = <span class="hljs-string">&quot;Search for bert models on Hugging Face and summarize top 3.&quot;</span>
MODEL = os.getenv(<span class="hljs-string">&quot;NANO_MODEL&quot;</span>, <span class="hljs-string">&quot;zai-org/GLM-5.1&quot;</span>)
BASE_URL = os.getenv(<span class="hljs-string">&quot;OPENAI_BASE_URL&quot;</span>, <span class="hljs-string">&quot;https://router.huggingface.co/v1&quot;</span>)
API_KEY = os.getenv(<span class="hljs-string">&quot;HF_TOKEN&quot;</span>) <span class="hljs-keyword">or</span> os.getenv(<span class="hljs-string">&quot;OPENAI_API_KEY&quot;</span>, <span class="hljs-string">&quot;&quot;</span>)
WORKSPACE = <span class="hljs-built_in">str</span>(Path.cwd())
MAX_STEPS = <span class="hljs-number">50</span>
TIMEOUT_S = <span class="hljs-number">30</span>
MAX_CHARS = <span class="hljs-number">8000</span>
ALLOW_WRITE = <span class="hljs-literal">False</span>
ALLOW_COMMANDS = [<span class="hljs-string">&quot;ls&quot;</span>, <span class="hljs-string">&quot;cat&quot;</span>, <span class="hljs-string">&quot;pwd&quot;</span>, <span class="hljs-string">&quot;echo&quot;</span>, <span class="hljs-string">&quot;head&quot;</span>, <span class="hljs-string">&quot;tail&quot;</span>, <span class="hljs-string">&quot;wc&quot;</span>, <span class="hljs-string">&quot;rg&quot;</span>]
TEMPERATURE = <span class="hljs-number">0.2</span>
SYSTEM_PROMPT = <span class="hljs-string">f&quot;&quot;&quot;You are a code-first agent.
Reply with executable Python only.
Tools:
- list_dir(path=&#x27;.&#x27;) → list files
- read_file(path, max_chars=4000) → read file
- write_file(path, content) → write file (only if ALLOW_WRITE=True)
- exec_cmd(args) → run allowed command
- web_fetch(url, max_bytes=10000) → fetch webpage
- hf_search(query, limit=5) → search HF Hub
Allowed commands: <span class="hljs-subst">{ALLOW_COMMANDS}</span>
Writes enabled: <span class="hljs-subst">{ALLOW_WRITE}</span>
When done, call final_answer(result).
Output only Python code, no prose.&quot;&quot;&quot;</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">clip</span>(<span class="hljs-params">x, n=MAX_CHARS</span>):
s = <span class="hljs-built_in">str</span>(x)
<span class="hljs-keyword">return</span> s[:n] + <span class="hljs-string">f&quot;\n...[truncated]&quot;</span> <span class="hljs-keyword">if</span> <span class="hljs-built_in">len</span>(s) &gt; n <span class="hljs-keyword">else</span> s
<span class="hljs-keyword">def</span> <span class="hljs-title function_">main</span>():
ws = Path(WORKSPACE).resolve()
done = <span class="hljs-literal">False</span>
final_result = <span class="hljs-literal">None</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">safe_path</span>(<span class="hljs-params">path</span>):
p = (ws / path).resolve()
<span class="hljs-keyword">try</span>:
p.relative_to(ws)
<span class="hljs-keyword">except</span> ValueError:
<span class="hljs-keyword">raise</span> ValueError(<span class="hljs-string">f&quot;Path escapes workspace: <span class="hljs-subst">{path}</span>&quot;</span>)
<span class="hljs-keyword">return</span> p
<span class="hljs-keyword">def</span> <span class="hljs-title function_">list_dir</span>(<span class="hljs-params">path=<span class="hljs-string">&quot;.&quot;</span></span>):
p = safe_path(path)
<span class="hljs-keyword">return</span> <span class="hljs-built_in">sorted</span>(x.name + (<span class="hljs-string">&quot;/&quot;</span> <span class="hljs-keyword">if</span> x.is_dir() <span class="hljs-keyword">else</span> <span class="hljs-string">&quot;&quot;</span>) <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> p.iterdir())
<span class="hljs-keyword">def</span> <span class="hljs-title function_">read_file</span>(<span class="hljs-params">path, max_chars=<span class="hljs-number">4000</span></span>):
p = safe_path(path)
<span class="hljs-keyword">return</span> clip(p.read_text(errors=<span class="hljs-string">&quot;replace&quot;</span>), <span class="hljs-built_in">min</span>(max_chars, MAX_CHARS))
<span class="hljs-keyword">def</span> <span class="hljs-title function_">write_file</span>(<span class="hljs-params">path, content</span>):
<span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> ALLOW_WRITE:
<span class="hljs-keyword">raise</span> PermissionError(<span class="hljs-string">&quot;write_file disabled&quot;</span>)
p = safe_path(path)
p.parent.mkdir(parents=<span class="hljs-literal">True</span>, exist_ok=<span class="hljs-literal">True</span>)
p.write_text(<span class="hljs-built_in">str</span>(content), encoding=<span class="hljs-string">&quot;utf-8&quot;</span>)
<span class="hljs-keyword">return</span> <span class="hljs-string">f&quot;Wrote <span class="hljs-subst">{<span class="hljs-built_in">len</span>(<span class="hljs-built_in">str</span>(content))}</span> bytes&quot;</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">exec_cmd</span>(<span class="hljs-params">args</span>):
<span class="hljs-keyword">if</span> args[<span class="hljs-number">0</span>] <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> ALLOW_COMMANDS:
<span class="hljs-keyword">raise</span> PermissionError(<span class="hljs-string">f&quot;Command <span class="hljs-subst">{args[<span class="hljs-number">0</span>]}</span> not allowed&quot;</span>)
result = subprocess.run(
args, capture_output=<span class="hljs-literal">True</span>, timeout=TIMEOUT_S, text=<span class="hljs-literal">True</span>
)
output_parts = []
<span class="hljs-keyword">if</span> result.stdout:
output_parts.append(<span class="hljs-string">f&quot;stdout:\n<span class="hljs-subst">{result.stdout}</span>&quot;</span>)
<span class="hljs-keyword">if</span> result.stderr:
output_parts.append(<span class="hljs-string">f&quot;stderr:\n<span class="hljs-subst">{result.stderr}</span>&quot;</span>)
output = <span class="hljs-string">&quot;\n\n&quot;</span>.join(output_parts) <span class="hljs-keyword">or</span> <span class="hljs-string">f&quot;(exit code <span class="hljs-subst">{result.returncode}</span> with no output)&quot;</span>
<span class="hljs-keyword">return</span> clip(output, MAX_CHARS)
<span class="hljs-keyword">def</span> <span class="hljs-title function_">web_fetch</span>(<span class="hljs-params">url, max_bytes=<span class="hljs-number">10000</span></span>):
<span class="hljs-keyword">try</span>:
<span class="hljs-keyword">with</span> urllib.request.urlopen(url, timeout=TIMEOUT_S) <span class="hljs-keyword">as</span> r:
content = r.read(max_bytes)
<span class="hljs-keyword">return</span> content.decode(<span class="hljs-string">&quot;utf-8&quot;</span>, errors=<span class="hljs-string">&quot;replace&quot;</span>)
<span class="hljs-keyword">except</span> Exception <span class="hljs-keyword">as</span> e:
<span class="hljs-keyword">return</span> <span class="hljs-string">f&quot;Error: <span class="hljs-subst">{<span class="hljs-built_in">type</span>(e).__name__}</span>: <span class="hljs-subst">{<span class="hljs-built_in">str</span>(e)}</span>&quot;</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">hf_search</span>(<span class="hljs-params">query, resource_type=<span class="hljs-string">&quot;models&quot;</span>, limit=<span class="hljs-number">5</span></span>):
<span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> API_KEY:
<span class="hljs-keyword">return</span> <span class="hljs-string">&quot;Error: HF_TOKEN not set&quot;</span>
<span class="hljs-keyword">try</span>:
url = <span class="hljs-string">f&quot;https://huggingface.co/api/<span class="hljs-subst">{resource_type}</span>&quot;</span>
req = urllib.request.Request(
<span class="hljs-string">f&quot;<span class="hljs-subst">{url}</span>?search=<span class="hljs-subst">{query}</span>&amp;limit=<span class="hljs-subst">{limit}</span>&quot;</span>,
headers={<span class="hljs-string">&quot;Authorization&quot;</span>: <span class="hljs-string">f&quot;Bearer <span class="hljs-subst">{API_KEY}</span>&quot;</span>}
)
<span class="hljs-keyword">with</span> urllib.request.urlopen(req, timeout=TIMEOUT_S) <span class="hljs-keyword">as</span> r:
data = json.loads(r.read())
<span class="hljs-keyword">return</span> [
{
<span class="hljs-string">&quot;id&quot;</span>: item.get(<span class="hljs-string">&quot;id&quot;</span>),
<span class="hljs-string">&quot;downloads&quot;</span>: item.get(<span class="hljs-string">&quot;downloads&quot;</span>, <span class="hljs-number">0</span>),
<span class="hljs-string">&quot;description&quot;</span>: item.get(<span class="hljs-string">&quot;description&quot;</span>, <span class="hljs-string">&quot;&quot;</span>)[:<span class="hljs-number">100</span>]
}
<span class="hljs-keyword">for</span> item <span class="hljs-keyword">in</span> data[:limit]
]
<span class="hljs-keyword">except</span> Exception <span class="hljs-keyword">as</span> e:
<span class="hljs-keyword">return</span> <span class="hljs-string">f&quot;Error: <span class="hljs-subst">{<span class="hljs-built_in">str</span>(e)}</span>&quot;</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">final_answer</span>(<span class="hljs-params">value</span>):
<span class="hljs-keyword">nonlocal</span> done, final_result
done = <span class="hljs-literal">True</span>
final_result = value
<span class="hljs-keyword">return</span> value
<span class="hljs-comment"># Initialize</span>
client = OpenAI(
api_key=API_KEY,
base_url=BASE_URL
)
messages = [
{<span class="hljs-string">&quot;role&quot;</span>: <span class="hljs-string">&quot;system&quot;</span>, <span class="hljs-string">&quot;content&quot;</span>: SYSTEM_PROMPT},
{<span class="hljs-string">&quot;role&quot;</span>: <span class="hljs-string">&quot;user&quot;</span>, <span class="hljs-string">&quot;content&quot;</span>: TASK}
]
<span class="hljs-comment"># Main loop</span>
<span class="hljs-keyword">for</span> step <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(MAX_STEPS):
<span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;\n[Step <span class="hljs-subst">{step + <span class="hljs-number">1</span>}</span>]&quot;</span>)
<span class="hljs-comment"># Call LLM</span>
response = client.responses.create(
model=MODEL,
temperature=TEMPERATURE,
<span class="hljs-built_in">input</span>=messages
)
content = response.output_text
<span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Model:\n<span class="hljs-subst">{content[:<span class="hljs-number">300</span>]}</span>...&quot;</span>)
messages.append({<span class="hljs-string">&quot;role&quot;</span>: <span class="hljs-string">&quot;assistant&quot;</span>, <span class="hljs-string">&quot;content&quot;</span>: content})
<span class="hljs-comment"># Execute code</span>
<span class="hljs-keyword">try</span>:
code_match = re.search(<span class="hljs-string">r&quot;```python\n(.*?)\n```&quot;</span>, content, re.DOTALL)
<span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> code_match:
<span class="hljs-keyword">raise</span> ValueError(<span class="hljs-string">&quot;No Python code block found&quot;</span>)
stdout_buffer = io.StringIO()
stderr_buffer = io.StringIO()
exec_globals = {
<span class="hljs-string">&quot;__builtins__&quot;</span>: {},
<span class="hljs-string">&quot;list_dir&quot;</span>: list_dir,
<span class="hljs-string">&quot;read_file&quot;</span>: read_file,
<span class="hljs-string">&quot;write_file&quot;</span>: write_file,
<span class="hljs-string">&quot;exec_cmd&quot;</span>: exec_cmd,
<span class="hljs-string">&quot;web_fetch&quot;</span>: web_fetch,
<span class="hljs-string">&quot;hf_search&quot;</span>: hf_search,
<span class="hljs-string">&quot;final_answer&quot;</span>: final_answer,
<span class="hljs-string">&quot;json&quot;</span>: json
}
<span class="hljs-keyword">with</span> redirect_stdout(stdout_buffer), redirect_stderr(stderr_buffer):
<span class="hljs-built_in">exec</span>(code_match.group(<span class="hljs-number">1</span>), exec_globals)
stdout_text = stdout_buffer.getvalue().strip()
stderr_text = stderr_buffer.getvalue().strip()
<span class="hljs-keyword">if</span> done:
result = <span class="hljs-string">f&quot;Final answer: <span class="hljs-subst">{clip(final_result)}</span>&quot;</span>
<span class="hljs-keyword">else</span>:
observations = []
<span class="hljs-keyword">if</span> stdout_text:
observations.append(<span class="hljs-string">f&quot;stdout:\n<span class="hljs-subst">{clip(stdout_text)}</span>&quot;</span>)
<span class="hljs-keyword">if</span> stderr_text:
observations.append(<span class="hljs-string">f&quot;stderr:\n<span class="hljs-subst">{clip(stderr_text)}</span>&quot;</span>)
result = <span class="hljs-string">&quot;\n\n&quot;</span>.join(observations) <span class="hljs-keyword">or</span> <span class="hljs-string">&quot;Executed successfully (no output)&quot;</span>
<span class="hljs-keyword">except</span> FileNotFoundError:
result = <span class="hljs-string">&quot;Error: FileNotFoundError: File not found&quot;</span>
<span class="hljs-keyword">except</span> PermissionError <span class="hljs-keyword">as</span> e:
result = <span class="hljs-string">f&quot;Error: PermissionError: <span class="hljs-subst">{<span class="hljs-built_in">str</span>(e)}</span>&quot;</span>
<span class="hljs-keyword">except</span> subprocess.TimeoutExpired:
result = <span class="hljs-string">&quot;Error: TimeoutError: Command took too long&quot;</span>
<span class="hljs-keyword">except</span> Exception <span class="hljs-keyword">as</span> e:
result = <span class="hljs-string">f&quot;Error: <span class="hljs-subst">{<span class="hljs-built_in">type</span>(e).__name__}</span>: <span class="hljs-subst">{<span class="hljs-built_in">str</span>(e)}</span>&quot;</span>
<span class="hljs-keyword">if</span> done:
<span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;✓ Task complete: <span class="hljs-subst">{final_result}</span>&quot;</span>)
<span class="hljs-keyword">break</span>
messages.append({<span class="hljs-string">&quot;role&quot;</span>: <span class="hljs-string">&quot;user&quot;</span>, <span class="hljs-string">&quot;content&quot;</span>: result})
<span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> done:
<span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;✗ Max steps reached&quot;</span>)
<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&quot;__main__&quot;</span>:
main()<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-oxwqla">This extended version keeps the same safety model as the base harness: <code>write_file</code> is still disabled until you flip <code>ALLOW_WRITE</code>, <code>exec()</code> runs without Python’s default builtins, and each loop feeds back stdout, stderr, a final answer, or a structured error string.</p> <h2 class="relative group"><a id="running-the-harness" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#running-the-harness"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Running the Harness</span></h2> <p data-svelte-h="svelte-16qzabf">Run it against <code>zai-org/GLM-5.1</code> on Hugging Face Inference Providers:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-built_in">export</span> HF_TOKEN=<span class="hljs-string">&quot;hf_...&quot;</span>
<span class="hljs-built_in">export</span> NANO_MODEL=<span class="hljs-string">&quot;zai-org/GLM-5.1&quot;</span>
python nano_harness_extended.py<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-1xmdlfy">Inference Providers route the request to a backing provider automatically. If you want to try a different HF model, set <code>NANO_MODEL</code> to any text-generation repo that’s enabled on Inference Providers — the loop and tool set stay the same.</p> <h2 class="relative group"><a id="exercise-extend-further" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#exercise-extend-further"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Exercise: Extend Further</span></h2> <p data-svelte-h="svelte-1kp53wi">Add these tools yourself:</p> <h3 class="relative group"><a id="1-gitlog-tool" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#1-gitlog-tool"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>1. git_log Tool</span></h3> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-comment"># Add &quot;git&quot; to ALLOW_COMMANDS first, then:</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">git_log</span>(<span class="hljs-params">limit=<span class="hljs-number">10</span></span>):
<span class="hljs-string">&quot;&quot;&quot;Get recent git commits.&quot;&quot;&quot;</span>
<span class="hljs-keyword">return</span> exec_cmd([<span class="hljs-string">&quot;git&quot;</span>, <span class="hljs-string">&quot;log&quot;</span>, <span class="hljs-string">&quot;--oneline&quot;</span>, <span class="hljs-string">f&quot;-<span class="hljs-subst">{limit}</span>&quot;</span>])<!-- HTML_TAG_END --></pre></div> <h3 class="relative group"><a id="2-jsonparse-tool" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#2-jsonparse-tool"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>2. json_parse Tool</span></h3> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-keyword">def</span> <span class="hljs-title function_">json_parse</span>(<span class="hljs-params">json_string</span>):
<span class="hljs-string">&quot;&quot;&quot;Parse JSON safely.&quot;&quot;&quot;</span>
<span class="hljs-keyword">try</span>:
<span class="hljs-keyword">return</span> json.loads(json_string)
<span class="hljs-keyword">except</span> json.JSONDecodeError <span class="hljs-keyword">as</span> e:
<span class="hljs-keyword">return</span> <span class="hljs-string">f&quot;Error: <span class="hljs-subst">{<span class="hljs-built_in">str</span>(e)}</span>&quot;</span><!-- HTML_TAG_END --></pre></div> <h3 class="relative group"><a id="3-computestats-tool" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#3-computestats-tool"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>3. compute_stats Tool</span></h3> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-keyword">def</span> <span class="hljs-title function_">compute_stats</span>(<span class="hljs-params">numbers</span>):
<span class="hljs-string">&quot;&quot;&quot;Compute min, max, mean.&quot;&quot;&quot;</span>
nums = <span class="hljs-built_in">list</span>(<span class="hljs-built_in">map</span>(<span class="hljs-built_in">float</span>, numbers))
<span class="hljs-keyword">return</span> {
<span class="hljs-string">&quot;min&quot;</span>: <span class="hljs-built_in">min</span>(nums),
<span class="hljs-string">&quot;max&quot;</span>: <span class="hljs-built_in">max</span>(nums),
<span class="hljs-string">&quot;mean&quot;</span>: <span class="hljs-built_in">sum</span>(nums) / <span class="hljs-built_in">len</span>(nums),
<span class="hljs-string">&quot;count&quot;</span>: <span class="hljs-built_in">len</span>(nums)
}<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-10miomk">Add these to the harness and watch how the agent’s traces change.</p> <h2 class="relative group"><a id="key-takeaways" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#key-takeaways"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Key Takeaways</span></h2> <p data-svelte-h="svelte-196793x">Tools connect the agent to the world. Every tool needs size limits, timeouts, and informative error strings so the agent can adapt. The same loop runs unchanged against any model on Hugging Face Inference Providers.</p> <p data-svelte-h="svelte-1qfy60r">Next, the unit 6 quiz.</p> <a class="!text-gray-400 !no-underline text-sm flex items-center not-prose mt-4" href="https://github.com/huggingface/context-course/blob/main/units/en/unit6/hands-on.mdx" target="_blank"><svg class="mr-1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M31,16l-7,7l-1.41-1.41L28.17,16l-5.58-5.59L24,9l7,7z"></path><path d="M1,16l7-7l1.41,1.41L3.83,16l5.58,5.59L8,23l-7-7z"></path><path d="M12.419,25.484L17.639,6.552l1.932,0.518L14.351,26.002z"></path></svg> <span data-svelte-h="svelte-zjs2n5"><span class="underline">Update</span> on GitHub</span></a> <p></p>
<script>
{
__sveltekit_ck2jc = {
assets: "/docs/context-course/pr_7/en",
base: "/docs/context-course/pr_7/en",
env: {}
};
const element = document.currentScript.parentElement;
const data = [null,null];
Promise.all([
import("/docs/context-course/pr_7/en/_app/immutable/entry/start.55c3acaa.js"),
import("/docs/context-course/pr_7/en/_app/immutable/entry/app.10ec5479.js")
]).then(([kit, app]) => {
kit.start(app, element, {
node_ids: [0, 36],
data,
form: null,
error: null
});
});
}
</script>

Xet Storage Details

Size:
59.4 kB
·
Xet hash:
41842b56c359237d3284ac969f458b79a04aa88d7a0fe8ff5a6b6892e722bcc5

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.