Code Steps in Relevance AI’s Tool Builder

relevance ai elite how to | Elite How To

Relevance.ai

Absolutely incredible! Relevance.ai’s Agents are a true game-changer for any team looking to automate workflows and enhance productivity. These AI teammates handle tasks on autopilot, becoming an extra pair of…

Key Take-aways

  • JavaScript and Python Code Integration: Relevance AI’s Tool Builder supports both JavaScript and Python code steps, enabling enhanced customization and functionality.
  • Advanced Web Scraping and Summarization: By leveraging these code steps, users can perform advanced tasks like scraping websites and summarizing content directly within the AI tools.
  • Dynamic User Inputs: Code steps can dynamically access user inputs, enabling more tailored outputs for specific AI applications.

In this tutorial, Dan from Relevance AI demonstrates how to use two code steps—JavaScript and Python—in the Tool Builder. The example showcases a tool that takes a news topic, scrapes articles from BBC.com, and summarizes them using an LLM (Large Language Model). This guide covers how to configure code steps, dynamically handle inputs, and optimize outputs.

Setting Up the AI Tool for Web Scraping

Dan introduces a dummy tool designed to take a user-provided news topic, search for related articles on BBC.com, and summarize the content.

  • User Inputs: The tool starts with a user input called news_topic, which is passed into a Google search step.
  • Filtering Results: The initial results are filtered to identify relevant articles, but not all results are actual articles (e.g., fixtures, tables, live streams).

Example: Searching for a Topic

Dan demonstrates by entering “Nations League” as the topic, triggering a Google search that yields results from BBC.com. However, the search results include non-article links, requiring additional filtering to identify the correct article link.

Using JavaScript Code Steps for Filtering Results

Dan explains how JavaScript code steps can filter search results and identify links that lead to actual articles.

  1. Accessing Variables:
    • In JavaScript code steps, variables such as Google_results and news_topic can be accessed directly by their names.
    • Dan uses JavaScript’s find function to filter the array of search results and identify links that include /articles/.
  2. Running the JavaScript Step:
    • Dan runs the JavaScript step, successfully identifying the correct article link: “Pitch in Bulgaria not acceptable.”
    • The JavaScript step efficiently filters results, setting the stage for further processing in the Python code step.

Advanced Web Scraping with Python Code Steps

Dan moves on to the Python code step, which allows for more granular data manipulation and advanced web scraping.

  1. Accessing Variables in Python:
    • Unlike JavaScript, variables in Python code steps are accessed using the steps and params objects.
    • Example: steps['javascript']['output'] is used to retrieve the output of the previous JavaScript step, while params['news_topic'] retrieves the user input.
  2. Importing Python Packages:
    • Relevance AI’s Python code step supports package imports, allowing users to add libraries like requests and beautifulsoup4.
    • Dan demonstrates how to import these packages and configure the Python environment (e.g., memory size, back-end selection).
  3. Scraping the Web Page:
    • The Python code step fetches the article link using the requests package, extracts HTML content, and uses BeautifulSoup to parse the content.
    • Dan retrieves text elements (e.g., paragraphs, headers) and maps them into a structured format for further processing.

Using Helper Functions in Python Code Steps

Dan highlights the use of built-in helper functions in the Python code step, which offer additional functionality.

  1. Prompt Completion:
    • The helper function prompt_completion allows users to perform LLM-based tasks directly within Python.
    • Dan uses this function to pass the article text to the LLM, requesting a summary of the content.
  2. Running the Python Step:
    • The Python code step processes the scraped content and generates a summary.
    • Example Output: “Northern Ireland manager Michael O’Neill expressed dissatisfaction with the poor playing surface at the stadium in Bulgaria after his team lost 1-0.”

Key Differences Between JavaScript and Python Code Steps

Dan summarizes the main differences between JavaScript and Python code steps in Relevance AI’s Tool Builder:

  • Variable Access:
    • JavaScript: Variables are accessed directly by their names.
    • Python: Variables are accessed via the steps or params objects, requiring additional keying for outputs.
  • Advanced Capabilities:
    • JavaScript: Best for straightforward filtering and simple data manipulation.
    • Python: Offers more power, including package imports, HTML parsing, and interaction with LLMs and knowledge tables.

Best Practices for Using Code Steps in Relevance AI

Dan shares some best practices for using code steps effectively in Relevance AI:

  1. Choose the Right Code Step:
    • Use JavaScript for simple tasks like filtering arrays or manipulating basic data.
    • Use Python for complex operations like web scraping, handling large data sets, or interacting with LLMs.
  2. Organize Code Efficiently:
    • Break tasks into multiple steps, using separate code steps for each distinct task (e.g., filtering, fetching, summarizing).
    • Utilize helper functions to reduce code complexity and improve readability.
  3. Test and Debug Frequently:
    • Regularly test code steps to ensure accuracy, especially when using helper functions or interacting with external services.

Real-World Applications of Code Steps in AI Tools

By integrating JavaScript and Python code steps, Relevance AI users can build powerful AI tools with diverse applications:

  • Content Aggregation: Automatically search, scrape, and summarize content from various sources, enabling streamlined content aggregation for news or research.
  • Data Analysis: Perform in-depth data manipulation and analysis using Python, with the ability to incorporate LLMs for summarization, categorization, or extraction.
  • Automated Research: Enhance AI tools with dynamic scraping and advanced summarization, enabling more precise and tailored research reports.

Is Code Integration Right for Your AI Projects?

Code integration in Relevance AI provides a versatile approach to building AI tools, combining the simplicity of no-code steps with the power of custom code. JavaScript and Python code steps enable users to:

  • Extend Tool Capabilities: Add advanced data processing, web scraping, and LLM-based summarization to AI workflows.
  • Enhance Automation: Automate complex tasks by leveraging built-in helper functions, package imports, and dynamic variables.

Ready to unlock advanced AI capabilities? Explore how Relevance AI’s JavaScript and Python code steps can enhance your automation projects today.

Posted in