{"id":3638,"date":"2025-03-12T11:13:47","date_gmt":"2025-03-12T05:43:47","guid":{"rendered":"https:\/\/www.rangakrish.com\/?p=3638"},"modified":"2025-03-12T11:13:47","modified_gmt":"2025-03-12T05:43:47","slug":"getting-started-with-huggingface-smolagents","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2025\/03\/12\/getting-started-with-huggingface-smolagents\/","title":{"rendered":"Getting Started with HuggingFace Smolagents"},"content":{"rendered":"<p><em><strong>Agents<\/strong><\/em> and <em><strong>Agent frameworks<\/strong><\/em> are hot topics these days. <a href=\"https:\/\/www.langchain.com\/langchain\" target=\"_blank\" rel=\"noopener\"><em><strong>LangChain<\/strong><\/em><\/a>, <a href=\"https:\/\/www.crewai.com\" target=\"_blank\" rel=\"noopener\"><em><strong>crewAI<\/strong><\/em><\/a>, <a href=\"https:\/\/www.langchain.com\/langgraph\" target=\"_blank\" rel=\"noopener\"><em><strong>LangGraph<\/strong><\/em><\/a>, <a href=\"https:\/\/github.com\/microsoft\/semantic-kernel\" target=\"_blank\" rel=\"noopener\"><em><strong>Microsoft Semantic Kernel<\/strong><\/em><\/a>, and <a href=\"https:\/\/microsoft.github.io\/autogen\/stable\/\" target=\"_blank\" rel=\"noopener\"><em><strong>Microsoft Autogen<\/strong><\/em><\/a> are some of the popular agent frameworks. <a href=\"https:\/\/smolagents.org\" target=\"_blank\" rel=\"noopener\"><em><strong>Smolagents<\/strong><\/em><\/a> is a relatively new entry in this arena. It is a lightweight agent framework from the well-known <em><strong>HuggingFace<\/strong><\/em> platform.<\/p>\n<p>In today\u2019s article, I want to show how easy it is to get started with <em><strong>Smolagents<\/strong><\/em>. The example is about interacting with an LLM regarding imaginary universes!<\/p>\n<p>Agents are built on top of <em><strong>\u201ctools\u201d<\/strong><\/em> and use the underlying <em><strong>\u201cfunction calling\u201d<\/strong><\/em> feature of the LLM. One of the nice features of <em><strong>Smolagents<\/strong><\/em> is <em><strong>\u201cCodeAgent\u201d<\/strong><\/em>, which supports the tool calling LLMs in code.<\/p>\n<p><em><strong>Python<\/strong><\/em> functions are converted to tools using the <em><strong>\u201c@tool\u201d<\/strong><\/em> decorator. See the figure below:<\/p>\n<figure id=\"attachment_3639\" aria-describedby=\"caption-attachment-3639\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/03\/code1.png?ssl=1\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"3639\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2025\/03\/12\/getting-started-with-huggingface-smolagents\/code1-12\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2025\/03\/code1.png\" data-orig-size=\"1240,832\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Defining Tools\" data-image-description=\"&lt;p&gt;Defining Tools&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Defining Tools&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2025\/03\/code1-1024x687.png\" class=\"wp-image-3639\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/03\/code1.png?resize=550%2C369&#038;ssl=1\" alt=\"Defining Tools\" width=\"550\" height=\"369\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/03\/code1.png?resize=300%2C201&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/03\/code1.png?resize=1024%2C687&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/03\/code1.png?resize=768%2C515&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/03\/code1.png?w=1240&amp;ssl=1 1240w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-3639\" class=\"wp-caption-text\"><strong>Defining Tools<\/strong><\/figcaption><\/figure>\n<p>As is customary, we provide appropriate docstrings that describe the inputs and output.<\/p>\n<p>The rest of the code how to create the <em><strong>CodeAgent<\/strong><\/em> and execute it. See below:<\/p>\n<figure id=\"attachment_3640\" aria-describedby=\"caption-attachment-3640\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/03\/code2.png?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"3640\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2025\/03\/12\/getting-started-with-huggingface-smolagents\/code2-15\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2025\/03\/code2.png\" data-orig-size=\"1694,900\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Creating and Using the CodeAgent\" data-image-description=\"&lt;p&gt;Creating and Using the CodeAgent&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Creating and Using the CodeAgent&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2025\/03\/code2-1024x544.png\" class=\"wp-image-3640\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/03\/code2.png?resize=550%2C292&#038;ssl=1\" alt=\"Creating and Using the CodeAgent\" width=\"550\" height=\"292\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/03\/code2.png?resize=300%2C159&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/03\/code2.png?resize=1024%2C544&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/03\/code2.png?resize=768%2C408&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/03\/code2.png?resize=1536%2C816&amp;ssl=1 1536w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/03\/code2.png?w=1694&amp;ssl=1 1694w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-3640\" class=\"wp-caption-text\"><strong>Creating and Using the CodeAgent<\/strong><\/figcaption><\/figure>\n<p>Another nice thing about <em><strong>Smolagents<\/strong><\/em> is its support for a variety of LLMs, not just those hosted on <em><strong>HuggingFace<\/strong><\/em>. In the above code, we can change the LLM by switching between <em><strong>LiteLLMModel<\/strong><\/em> and <em><strong>HfApiModel<\/strong><\/em> objects.<\/p>\n<p>Here is the output from the program:<\/p>\n<figure id=\"attachment_3641\" aria-describedby=\"caption-attachment-3641\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/03\/output.png?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"3641\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2025\/03\/12\/getting-started-with-huggingface-smolagents\/output-12\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2025\/03\/output.png\" data-orig-size=\"1012,188\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Program Output\" data-image-description=\"&lt;p&gt;Program Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Program Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2025\/03\/output.png\" class=\"wp-image-3641\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/03\/output.png?resize=550%2C102&#038;ssl=1\" alt=\"Program Output\" width=\"550\" height=\"102\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/03\/output.png?resize=300%2C56&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/03\/output.png?resize=768%2C143&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/03\/output.png?w=1012&amp;ssl=1 1012w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-3641\" class=\"wp-caption-text\"><strong>Program Output<\/strong><\/figcaption><\/figure>\n<p>You can see that the relevant tools are correctly called for the <em><strong>\u201cuniverse\u201d<\/strong><\/em> related questions. But for getting the population of <em><strong>Maldives<\/strong><\/em>, the <em><strong>\u201cgetPopulation\u201d<\/strong><\/em> tool is not called!<\/p>\n<p>This is just a basic example of how we can use <em><strong>Smolagents<\/strong><\/em>. It has other types of agents such as <em><strong>ToolCallingAgent<\/strong><\/em>, <em><strong>ManagedAgent<\/strong><\/em>, and <em><strong>MultiStepAgent<\/strong><\/em>, each of which has a specific role. I hope to write about those in future articles.<\/p>\n<p>You can download the code <a href=\"https:\/\/www.rangakrish.com\/downloads\/Example1.py\" target=\"_blank\" rel=\"noopener\"><em><strong>here<\/strong><\/em><\/a>.<\/p>\n<p>Have a nice week!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Agents and Agent frameworks are hot topics these days. LangChain, crewAI, LangGraph, Microsoft Semantic Kernel, and Microsoft Autogen are some of the popular agent frameworks. Smolagents is a relatively new entry in this arena. It is a lightweight agent framework from the well-known HuggingFace platform. In today\u2019s article, I want to show how easy it [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[415,388,17,103],"tags":[417,376,416],"class_list":["post-3638","post","type-post","status-publish","format-standard","hentry","category-agents","category-openai","category-programming","category-python","tag-agents","tag-llm","tag-smolagents"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-WG","jetpack-related-posts":[{"id":3650,"url":"https:\/\/www.rangakrish.com\/index.php\/2025\/03\/26\/exploring-openai-agent-sdk\/","url_meta":{"origin":3638,"position":0},"title":"Exploring OpenAI Agent SDK","author":"admin","date":"March 26, 2025","format":false,"excerpt":"OpenAI recently released its open-source Agents SDK. The documentation looked interesting, so I decided to give it a try. The SDK supports multiple agents working together using \u201chandoffs\u201d. The example I am using in today\u2019s article involves 3 agents: 1) Agent who specializes in answering questions on Planetary positions 2)\u2026","rel":"","context":"In &quot;OpenAI&quot;","block_context":{"text":"OpenAI","link":"https:\/\/www.rangakrish.com\/index.php\/category\/openai\/"},"img":{"alt_text":"Using Structured Output","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/03\/code1-1-300x121.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/03\/code1-1-300x121.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/03\/code1-1-300x121.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2994,"url":"https:\/\/www.rangakrish.com\/index.php\/2023\/01\/29\/wolframalpha-chatgpt-and-the-future-of-ai\/","url_meta":{"origin":3638,"position":1},"title":"WolframAlpha, ChatGPT, and the Future of AI","author":"admin","date":"January 29, 2023","format":false,"excerpt":"We all know that ChatGPT has taken the world by storm. True, it is a major advance of Artificial Intelligence in the area of Natural Language Processing. Many may not know that WolframAlpha, launched in 2009, allowed natural language queries. As a long time user of Wolfram Mathematica, I was\u2026","rel":"","context":"In &quot;Knowledge Representation&quot;","block_context":{"text":"Knowledge Representation","link":"https:\/\/www.rangakrish.com\/index.php\/category\/knowledge-representation\/"},"img":{"alt_text":"WolframAlpha Query","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/01\/walpha2-300x193.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/01\/walpha2-300x193.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/01\/walpha2-300x193.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1946,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/28\/stdis_empty\/","url_meta":{"origin":3638,"position":2},"title":"std::is_empty","author":"admin","date":"March 28, 2020","format":false,"excerpt":"In the previous post, we looked at the std::is_destructible<T> type trait. Today, let us try to understand another type trait std::is_empty<T>. As per the specification, is_empty<T>::value will return true in the following cases: - The class\/struct has no non-static data member - The class\/struct does not define a virtual function\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Example4: Union and Bit Field","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example4-1.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example4-1.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example4-1.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1349,"url":"https:\/\/www.rangakrish.com\/index.php\/2018\/12\/30\/natural-language-interaction-with-ilexicon-using-luis\/","url_meta":{"origin":3638,"position":3},"title":"Natural Language Interaction with iLexicon Using LUIS","author":"admin","date":"December 30, 2018","format":false,"excerpt":"Some time ago, I had written a series of articles on my iLexicon project. It is a Lisp package that supports many interesting queries on English words. When I was discussing this project with a client recently, she asked me if it was possible to query the system in natural\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"Talking to iLexicon","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/12\/Code2.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/12\/Code2.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/12\/Code2.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":3626,"url":"https:\/\/www.rangakrish.com\/index.php\/2025\/02\/21\/using-openai-from-mathematica-part-3\/","url_meta":{"origin":3638,"position":4},"title":"Using OpenAI from Mathematica: Part-3","author":"admin","date":"February 21, 2025","format":false,"excerpt":"Let us continue our discussion on using Mathematica to interact with OpenAI (you may want to go through the earlier article as well). The simplest function to interact with the LLM is LLMSynthesize[]. As you might have guessed, this is a \u201csync\u201d (non-streaming) call. What if you expect a long\u2026","rel":"","context":"In &quot;Mathematica&quot;","block_context":{"text":"Mathematica","link":"https:\/\/www.rangakrish.com\/index.php\/category\/mathematica\/"},"img":{"alt_text":"Basic LLMSynthesize","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/02\/fig1-300x21.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/02\/fig1-300x21.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/02\/fig1-300x21.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":558,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/07\/07\/clpython-python-in-common-lisp\/","url_meta":{"origin":3638,"position":5},"title":"CLPython &#8211; Python in Common Lisp","author":"admin","date":"July 7, 2017","format":false,"excerpt":"My work in the area of NLP requires\u00a0me to work with several frameworks across multiple languages such as Java, Python and Lisp. Sometime ago I got a chance to experiment with CLPython, an open-source implementation of Python in Common Lisp. Although CLPython is not under active development now, I found\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/3638","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/comments?post=3638"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/3638\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=3638"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=3638"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=3638"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}