HyperAIHyperAI

Command Palette

Search for a command to run...

AI Tools for Creativity: Building Agents and Storybooks

The field of Artificial Intelligence is advancing at breakneck speed, transitioning from basic chatbots to powerful AI agents capable of performing a wide array of tasks. These agents, built on large language models (LLMs) and equipped with various tools, can significantly enhance productivity and automation in numerous domains. This article explores the basics of AI agents, their different types, and a practical example of an AI tool for creative storytelling. The Basics of AI Agents AI agents are constructed by augmenting large language models with the ability to interact with external tools, thereby expanding their functionalities. These models can be used to handle a range of tasks, from scheduling appointments and managing calendars to more complex actions like content generation and data analysis. Popular Python packages for building AI agents include Langchain, Agno (formerly PhiData), and CrewAI. For this guide, we focus on Agno, a user-friendly library with extensive documentation. Setting Up Your First AI Agent To get started, create a virtual environment and install the necessary packages. Here’s a quick setup guide: bash pip install agno google-generativeai agno groq lancedb sentence-transformers tantivy youtube-transcript-api Next, obtain a Google Gemini API key. This key is essential for accessing the language model. Building a Simple Chat Agent Creating a basic chat agent with Agno involves initializing an Agent class, specifying the model, and defining instructions. Below is a simple example: ```python from agno.agent import Agent from agno.models.google import Gemini import os Initialize the agent agent = Agent( model=Gemini(id="gemini-1.5-flash", api_key=os.environ.get("GEMINI_API_KEY")), description="An assistant agent", instructions=["Be succinct. Answer in a maximum of 2 sentences."], markdown=True ) Query the agent response = agent.run("What's the weather like in NYC in May?") print(response.content) ``` This agent, using the Gemini 1.5 model, provides a succinct weather report based on its training data. For more current and specific information, you can add tools to interact with external APIs or databases. Enhancing Agents with Tools Tools allow AI agents to interact with the real world and perform tasks beyond basic text manipulation. Common tools include web search, SQL queries, and file management. For instance, consider a solopreneur in the healthy living business who wants to automate content generation for social media: ```python from agno.agent import Agent from agno.models.google import Gemini from agno.tools.file import FileTools Initialize the agent with tools agent = Agent( model=Gemini(id="gemini-1.5-flash", api_key=os.environ.get("GEMINI_API_KEY")), description="A social media marketer specialized in creating engaging content.", tools=[FileTools(read_files=True, save_files=True)], show_tool_calls=True ) Generate and save a social media post agent.print_response("""Write a short post for Instagram with tips and tricks that positions me as an authority in healthy eating and save it to a file named 'post.txt'.""", markdown=True) ``` This reduces the content creation process from multiple steps to just reviewing and posting. Advanced Tools: Function and Reasoning Function tools allow you to incorporate custom Python functions, such as fetching YouTube transcripts and summarizing them: ```python import os from agno.agent import Agent from agno.models.google import Gemini from youtube_transcript_api import YouTubeTranscriptApi Define a function to fetch YouTube transcripts def get_yt_transcript(video_id: str) -> str: ytt_api = YouTubeTranscriptApi() yt = ytt_api.fetch(video_id) return ''.join([line.text for line in yt]) Initialize the agent with the get_yt_transcript tool agent = Agent( model=Gemini(id="gemini-1.5-flash", api_key=os.environ.get("GEMINI_API_KEY")), description="An assistant that summarizes YouTube videos.", tools=[get_yt_transcript], expected_output="A summary of the video with the 5 main points and 2 questions for me to test my understanding.", markdown=True, show_tool_calls=True ) Query the agent to summarize a video agent.print_response("Summarize the text of the video with the id 'hrZSfMly_Ck'", markdown=True) ``` Reasoning tools enable agents to analyze situations and provide more context-aware responses. For example, using Alibaba’s Qwen-qwq-32b model: ```python from agno.agent import Agent from agno.models.groq import Groq from agno.tools.reasoning import ReasoningTools Initialize the reasoning agent agent = Agent( model=Groq(id="qwen-qwq-32b", api_key=os.environ.get("GROQ_API_KEY")), description="An experienced math teacher.", tools=[ReasoningTools(add_instructions=True)], show_tool_calls=True ) Query the agent agent.print_response("Explain the concept of sine and cosine in simple terms.", stream=True, show_full_reasoning=True, markdown=True) ``` Agents with Knowledge and Memory Retrieval Augmented Generation (RAG): This feature allows agents to search specific websites or data sources for relevant information and incorporate it into their responses. For instance, creating an RAG agent to summarize content from your website: ```python from agno.agent import Agent from agno.models.google import Gemini from agno.knowledge.url import UrlKnowledge from agno.vectordb.lancedb import LanceDb, SearchType from agno.embedder.sentence_transformer import SentenceTransformerEmbedder Load webpage to knowledge base agent_knowledge = UrlKnowledge( urls=["https://gustavorsantos.me/?page_id=47"], vector_db=LanceDb(uri="tmp/lancedb", table_name="projects", search_type=SearchType.hybrid, embedder=SentenceTransformerEmbedder()) ) Initialize the agent with knowledge agent = Agent( model=Gemini(id="gemini-2.0-flash", api_key=os.environ.get("GEMINI_API_KEY")), instructions=[ "Use tables to display data.", "Search your knowledge before answering the question.", "Only include the content from the agent_knowledge base table 'projects'.", "Only include the output in your response." ], knowledge=agent_knowledge, add_datetime_to_instructions=True, markdown=True ) Query the agent agent.print_response("What are the two books listed in the 'agent_knowledge'?", stream=True, show_full_reasoning=True, stream_intermediate_steps=True) ``` Agents with Memory: These agents can store and retrieve user-specific information, making interactions more personalized. Here’s an example: ```python from agno.agent import Agent from agno.memory.v2.db.sqlite import SqliteMemoryDb from agno.memory.v2.memory import Memory from agno.models.google import Gemini User ID user_id = "data_scientist" Create a memory database memory = Memory(db=SqliteMemoryDb(table_name="memory", db_file="tmp/memory.db"), model=Gemini(id="gemini-2.0-flash", api_key=os.environ.get("GEMINI_API_KEY"))) Clear the memory before starting memory.clear() Initialize the agent with memory agent = Agent( model=Gemini(id="gemini-2.0-flash", api_key=os.environ.get("GEMINI_API_KEY")), user_id=user_id, memory=memory, enable_agentic_memory=True, add_datetime_to_instructions=True, markdown=True ) Interact with the agent agent.print_response("My name is Gustavo and I am a Data Scientist learning about AI Agents.") memories = memory.get_user_memories(user_id=user_id) print(f"Memories about {user_id}:") pprint(memories) agent.print_response("What topic should I study about?") agent.print_response("I write articles for Towards Data Science.") memories = memory.get_user_memories(user_id=user_id) print(f"Memories about {user_id}:") pprint(memories) agent.print_response("Where should I post my next article?") ``` Evaluation and Industry Insights Industry experts like Gustavo Santos, a seasoned Data Scientist and AI enthusiast, emphasize the importance of continuous learning in the rapidly evolving AI landscape. He suggests focusing on a few selected packages and progressively building more sophisticated agents. This approach ensures a solid foundation and keeps developers ahead of the curve. Agno, with its comprehensive suite of tools and ease of use, is gaining popularity among AI developers. Its ability to create agents with memory, reasoning, and knowledge retrieval makes it a versatile choice for a wide range of applications, from personal assistants to complex data science projects. By exploring and experimenting with these tools, developers can harness the full potential of AI agents, transforming manual, time-consuming tasks into automated, efficient processes. The future of data science and AI automation looks promising, and getting familiar with these tools is a strategic move for anyone aiming to stay relevant and innovative. Additional Resources Agno Documentation: Link Google Gemini API Docs: Link YouTube Transcript API: Link Agno GitHub Cookbook: Link Gustavo Santos’ Website: Link These resources provide further insights and detailed guides, helping you build and refine your AI agents. Stay tuned for more advanced topics and evaluations in upcoming posts.

Related Links