{"id":399,"date":"2024-12-31T01:04:19","date_gmt":"2024-12-30T21:34:19","guid":{"rendered":"https:\/\/haghiri75.com\/en\/?p=399"},"modified":"2024-12-31T01:04:19","modified_gmt":"2024-12-30T21:34:19","slug":"you-only-need-python-to-make-ai-agents","status":"publish","type":"post","link":"https:\/\/haghiri75.com\/en\/you-only-need-python-to-make-ai-agents\/","title":{"rendered":"You only need Python to make AI agents."},"content":{"rendered":"<p>In 2022, <a href=\"https:\/\/ai.com\">ChatGPT<\/a> released and LLMs becoming the hot topic of pretty much every technology related press, event, YouTube video, etc. It was like finding the secret ingredient to a potion which can make you immortal.<\/p>\n<p>But <a href=\"https:\/\/meta.com\">Meta<\/a> didn&#8217;t let OpenAI becoming the one and only. They also started the game by releasing their well-named model\u00a0<em>Large Language Model Meta AI\u00a0<\/em>or\u00a0<em>LLaMA\u00a0<\/em>which we all know and love. Not only Meta, but our friends at <a href=\"https:\/\/mistral.ai\">Mistral AI<\/a> weren&#8217;t idle and they also released a good bunch of open source models and the result of their work even motivated me in making of my Persian LLM, <a href=\"https:\/\/haghiri75.com\/en\/maral-is-here-7-billion-parameters-bilingual-model-with-support-of-persian\/\">Maral.<\/a><\/p>\n<p>But nowadays, good LLM is not a big problem. With a quick search on the internet, we easily can find good LLMs. Base models and fine-tunes which are made for generic or specific purposes, models which are armed with reasoning, models which are made for programmers, etc.<\/p>\n<p>We have the text output, now we need action. This is what I&#8217;m going to discuss in this particular post and I also will love to hear back from you as well.<\/p>\n<p><a href=\"https:\/\/jabirproject.org\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full\" src=\"https:\/\/mann-e-images.storage.c2.liara.space\/319996af-289a-4617-8d0c-6580e4793747.png\" width=\"1280\" height=\"720\" \/><\/a><\/p>\n<h1>AI Agents add action to LLMs<\/h1>\n<p>Well, I remember when the make-shift Android rip-off of iPod touch or simply\u00a0<em>Rabbit R1\u00a0<\/em>was introduced, they just advertised the device to work on a\u00a0<em>Large Action Model\u00a0<\/em>or LAM. I always was thinking about how can we modify one of the open LLMs to have action? Then I got the answer.<\/p>\n<p>The simplest thing we can think of is an LLM tuned on JSON input for different API&#8217;s with different tones. It is what I believe\u00a0<em>function calling\u00a0<\/em>or\u00a0<em>tool calling\u00a0<\/em>is. But it still has the downside.<\/p>\n<p>Imagine I train LLaMA 3.2 on API&#8217;s from AirBnB, Shopify, Amazon, Uber and Spotify. What will happen if you ask for a YouTube video? You even won&#8217;t get rick-rolled and it won&#8217;t be a good sign for products such as Rabbit R1 (or any other competitors).<\/p>\n<p>Then I got familiar with\u00a0<em>Crew AI\u00a0<\/em>which is a framework for making agents. But honestly, I never understood these\u00a0<em>AI frameworks<\/em>. Most of them are making the process of making a simple application over complicated. But thanks to Crew AI, I finally could understand what an AI agent is.<\/p>\n<p>An AI agent, adds actions in a\u00a0<em>human understandable way\u00a0<\/em>to LLMs. Like when you ask ChatGPT to create a picture, it calls an API running Dall-E and then gives you the image. This is what an agent is&#8230;! (at least until it&#8217;s not called Smith).<\/p>\n<p><a href=\"https:\/\/jabirproject.org\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full\" src=\"https:\/\/mann-e-images.storage.c2.liara.space\/54618c0a-5e65-463b-bebf-d75cb4589a5e.png\" width=\"1280\" height=\"720\" \/><\/a><\/p>\n<h1>Making an AI Agent without the frameworks is possible!<\/h1>\n<p>Well, it is possible. You only need Python and probably OpenAI&#8217;s library to make an agent. First of all let&#8217;s see what an agent does. An agent simply gets a prompt from you. Something like\u00a0<em>Send an email to John Doe and explain why I will be late tomorrow.<\/em> The AI model has to understand some steps here.<\/p>\n<p>First, it has to call a function to search your contact list and find\u00a0<em>John Doe\u00a0<\/em>then it has to generate a text explaining why you will be late. Then the last part is to send the email over an email server (which can be a private mail server or a provider like Google&#8217;s Gmail).<\/p>\n<p>Also, you can make it one step more difficult for your own agent and ask it to do these in the GUI (basically you need to use a Vision model for this task).<\/p>\n<p>Let&#8217;s make it happen in Python. It will be easy and you will understand it better.<\/p>\n<h2>Python example<\/h2>\n<p><strong>Disclaimer:\u00a0<\/strong>Since I have a full working code example on github, this part of the blog will be just a simple example.<\/p>\n<p>First step is to find an LLM. I personally think any provider with an OpenAI compatible API works perfectly and for this particular project, I&#8217;m using my own LLM which is known as <a href=\"https:\/\/jabirproject.org\">Jabir Project<\/a>.<\/p>\n<p>Jabir Project is a finetune on LLaMA 3.1 405B and proven itself in many different tasks. If you don&#8217;t want to use Jabir LLMs, it&#8217;s fine. You may prefer OpenAI, <a href=\"https:\/\/deepinfra.com\">DeepInfra<\/a> or <a href=\"https:\/\/openrouter.ai\">OpenRouter<\/a>. Also you may want to go local, so why not using <a href=\"https:\/\/ollama.com\">Ollama<\/a>?<\/p>\n<p>Well, assuming you want to use Jabir&#8217;s API, you need to set up an OpenAI client like this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">from openai import OpenAI\r\n\r\nclient = OpenAI(api_key=\"FAKE\", base_url=\"https:\/\/openai.jabirpoject.org\/v1\")<\/pre>\n<p>This is as easy as typing one line of code! You may be wondering why I used &#8220;FAKE&#8221; as the API key? It was when I tried to add Ollama&#8217;s API to my code and I understood that OpenAI library requires a value for the API key.<\/p>\n<p>Then, we need to set up a simple agent class:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">class Agent:\r\n    \r\n    def __init__(self, system=\"\"):\r\n        self.system = system\r\n        self.messages = []\r\n        if self.system:\r\n            self.messages.append({\"role\" : \"system\", \"content\" : system})\r\n    \r\n    def __call__(self, message):\r\n        self.messages.append({\"role\" : \"user\", \"content\" : message})\r\n        result = self.execute()\r\n        self.messages.append({\"role\" : \"assistant\", \"content\" : result})\r\n        return result\r\n    \r\n    def execute(self):\r\n        completion = client.chat.completions.create(\r\n            model = \"jabir-400b\",\r\n            messages = self.messages,\r\n            temperature = 0.0\r\n        )\r\n        \r\n        return completion.choices[0].message.content<\/pre>\n<p>This agent class is what that matters a lot. Since it has a memory of what happened.<\/p>\n<p>You can run the agent like this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">sample_agent = Agent(\"You are a helpful assistant\")\r\nprint(sample_agent(\"What is 1+1?\"))<\/pre>\n<p>Now the main question is that how can we add actions to this agent?<\/p>\n<h2>The Sample Agent with real action<\/h2>\n<p>As I was working on a way to make agents with no frameworks, I came up with the idea of making each action a python function and then ask the AI to generate something for me which can be later parsed into inputs for those.<\/p>\n<p>I made it in form of a jupyter notebook and it is available through my <a href=\"https:\/\/github.com\/prp-e\/jabir_agent\">Github account<\/a>. You can write agents like this and be completely framework-independent.<\/p>\n<p><a href=\"https:\/\/jabirproject.org\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full\" src=\"https:\/\/mann-e-images.storage.c2.liara.space\/8f06f45f-dcc7-4029-bfc3-046a1e997747.png\" width=\"1280\" height=\"720\" \/><\/a><\/p>\n<h1>Conclusion<\/h1>\n<p>Almost three years ago I made a blog post here called<a href=\"https:\/\/haghiri75.com\/en\/i-was-to-cheap-to-pay-10-a-month-for-copilot-so-i-made-my-own\/\"> I was too cheap to pay $10 a month for Github&#8217;s copilot so I made my own<\/a> and it was a good start of my journey to generative AI. Although I abandoned text generation for a somehow long time and started <a href=\"https:\/\/mann-e.com\">Mann-E<\/a>, I got back to the world of NLP with Maral models.<\/p>\n<p>And Maral got abandoned because my personal life was getting a little rough and then I decided to start a personalization platform called <a href=\"https:\/\/atelierai.me\">Atelier AI<\/a>. Which lets you create your own LoRAs for Mann-E models.<\/p>\n<p>But when I restarted the Jabir Project, I thought an LLM is not enough. This model should be the foundation of something bigger. This is why I did a lot of research on AI agents, and now I completely am aware of what I&#8217;m going to do.<\/p>\n<p>I love to hear back from readers of my blog about what possible ideas we can implement using LLMs and agents, so I politely ask all of you participate in the discussion and let&#8217;s build the future together.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In 2022, ChatGPT released and LLMs becoming the hot topic of pretty much every technology related press, event, YouTube video, etc. It was like finding the secret ingredient to a potion which can make you immortal. But Meta didn&#8217;t let OpenAI becoming the one and only. They also started the game by releasing their well-named &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/haghiri75.com\/en\/you-only-need-python-to-make-ai-agents\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;You only need Python to make AI agents.&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_newsletter_tier_id":0,"footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[4],"tags":[36,23,37,11,22,45,46,24,25,57,49,50],"jetpack_publicize_connections":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8BkKn-6r","jetpack-related-posts":[{"id":318,"url":"https:\/\/haghiri75.com\/en\/frontbricks-my-llm-based-weekend-project-which-is-inspired-by-vercels-v0\/","url_meta":{"origin":399,"position":0},"title":"FrontBricks, my LLM-based weekend project which is inspired by Vercel&#8217;s V0","author":"prp-e","date":"May 31, 2024","format":false,"excerpt":"Since 2022, there is a hype of\u00a0generative artificial intelligence\u00a0and it resulted in a bunch of cool projects. Although a lot of us may remember that Github's copilot was much older. Those days, I wrote an article about how I was too cheap to pay $10 a month for copilot, so\u2026","rel":"","context":"In &quot;Computer Architecture &amp; Programming&quot;","block_context":{"text":"Computer Architecture &amp; Programming","link":"https:\/\/haghiri75.com\/en\/category\/computer\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/haghiri75.com\/en\/wp-content\/uploads\/frontbricks.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/haghiri75.com\/en\/wp-content\/uploads\/frontbricks.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/haghiri75.com\/en\/wp-content\/uploads\/frontbricks.png?resize=525%2C300 1.5x, https:\/\/i0.wp.com\/haghiri75.com\/en\/wp-content\/uploads\/frontbricks.png?resize=700%2C400 2x, https:\/\/i0.wp.com\/haghiri75.com\/en\/wp-content\/uploads\/frontbricks.png?resize=1050%2C600 3x"},"classes":[]},{"id":335,"url":"https:\/\/haghiri75.com\/en\/privacy-focused-ai-is-all-we-need\/","url_meta":{"origin":399,"position":1},"title":"Privacy-focused AI is all we need","author":"prp-e","date":"November 1, 2024","format":false,"excerpt":"I remember in 2020 and 2021, due to Elon Musk's interest in crypto and also\u00a0The Metaverse Hype\u00a0people, specially the ones who had no idea about crypto or blockchain, started investing in the crypto markets. Although it seemed a little bit of a failure, people made profit out of it. It\u2026","rel":"","context":"In &quot;Projects&quot;","block_context":{"text":"Projects","link":"https:\/\/haghiri75.com\/en\/category\/projects\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":258,"url":"https:\/\/haghiri75.com\/en\/re-creating-midjourney-with-only-10-technical-report-for-mann-e-5-development\/","url_meta":{"origin":399,"position":2},"title":"Re-creating Midjourney with only $10 &#8211; Technical Report for Mann-E 5 development","author":"prp-e","date":"April 8, 2023","format":false,"excerpt":"The year 2022 was an amazing year for generative AI market and no one can deny in this year, release of some cool models such as Midjourney, Stable Diffusion and ChatGPT made this market bigger, better and more competitive. You may also know Mann-E, the model I have developed on\u2026","rel":"","context":"In &quot;Computer Architecture &amp; Programming&quot;","block_context":{"text":"Computer Architecture &amp; Programming","link":"https:\/\/haghiri75.com\/en\/category\/computer\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":391,"url":"https:\/\/haghiri75.com\/en\/lets-build-metaverse-with-ai-building-asset-generator\/","url_meta":{"origin":399,"position":3},"title":"Let&#8217;s build Metaverse with AI: Building asset generator","author":"prp-e","date":"November 27, 2024","format":false,"excerpt":"Look at this: How do you think this apple has been made? Excellent question. After the previous post, I said we should put LLMs out of the picture for now. Also we needed to talk about 3D, because it is important in whole metaverse space, right? Today I just did\u2026","rel":"","context":"In &quot;Projects&quot;","block_context":{"text":"Projects","link":"https:\/\/haghiri75.com\/en\/category\/projects\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/mann-e-images.storage.c2.liara.space\/120c3554-8326-4625-a129-50e80bb215da_part_0003.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/mann-e-images.storage.c2.liara.space\/120c3554-8326-4625-a129-50e80bb215da_part_0003.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/mann-e-images.storage.c2.liara.space\/120c3554-8326-4625-a129-50e80bb215da_part_0003.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/mann-e-images.storage.c2.liara.space\/120c3554-8326-4625-a129-50e80bb215da_part_0003.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":192,"url":"https:\/\/haghiri75.com\/en\/i-was-to-cheap-to-pay-10-a-month-for-copilot-so-i-made-my-own\/","url_meta":{"origin":399,"position":4},"title":"I was too cheap to pay $10 a month for copilot, so I made my own","author":"prp-e","date":"September 4, 2022","format":false,"excerpt":"In mid 2021, there was a revolution in coding. As a lazy programmer who always needed a fast and smart assistant, I was really happy to have Github Copilot in my arsenal of coding tools. So I was one of the early adapters of the whole idea of AI pair\u2026","rel":"","context":"In &quot;Computer Architecture &amp; Programming&quot;","block_context":{"text":"Computer Architecture &amp; Programming","link":"https:\/\/haghiri75.com\/en\/category\/computer\/"},"img":{"alt_text":"My own copilot!","src":"https:\/\/i0.wp.com\/haghiri75.com\/en\/wp-content\/uploads\/Screen-Shot-2022-09-03-at-10.46.46-PM.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/haghiri75.com\/en\/wp-content\/uploads\/Screen-Shot-2022-09-03-at-10.46.46-PM.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/haghiri75.com\/en\/wp-content\/uploads\/Screen-Shot-2022-09-03-at-10.46.46-PM.png?resize=525%2C300 1.5x"},"classes":[]},{"id":202,"url":"https:\/\/haghiri75.com\/en\/severus-does-the-magic\/","url_meta":{"origin":399,"position":5},"title":"Severus does the magic","author":"prp-e","date":"September 11, 2022","format":false,"excerpt":"It is not too long after I told you that I was too cheap to pay $10 a month for github copilot and I came up with the idea for Severus, my own AI pair programmer. It was something that went boom. My blog usually doesn't have more than 20\u2026","rel":"","context":"In &quot;Computer Architecture &amp; Programming&quot;","block_context":{"text":"Computer Architecture &amp; Programming","link":"https:\/\/haghiri75.com\/en\/category\/computer\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/haghiri75.com\/en\/wp-content\/uploads\/warforged_wizard_by_captdiablo_de2mwm8-pre-1024x724.jpg?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/haghiri75.com\/en\/wp-content\/uploads\/warforged_wizard_by_captdiablo_de2mwm8-pre-1024x724.jpg?resize=350%2C200 1x, https:\/\/i0.wp.com\/haghiri75.com\/en\/wp-content\/uploads\/warforged_wizard_by_captdiablo_de2mwm8-pre-1024x724.jpg?resize=525%2C300 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/haghiri75.com\/en\/wp-json\/wp\/v2\/posts\/399"}],"collection":[{"href":"https:\/\/haghiri75.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/haghiri75.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/haghiri75.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/haghiri75.com\/en\/wp-json\/wp\/v2\/comments?post=399"}],"version-history":[{"count":5,"href":"https:\/\/haghiri75.com\/en\/wp-json\/wp\/v2\/posts\/399\/revisions"}],"predecessor-version":[{"id":404,"href":"https:\/\/haghiri75.com\/en\/wp-json\/wp\/v2\/posts\/399\/revisions\/404"}],"wp:attachment":[{"href":"https:\/\/haghiri75.com\/en\/wp-json\/wp\/v2\/media?parent=399"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/haghiri75.com\/en\/wp-json\/wp\/v2\/categories?post=399"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/haghiri75.com\/en\/wp-json\/wp\/v2\/tags?post=399"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}