Skip to content

Commit

Permalink
[Feat] Integrate HugginFace smolagents into docs (#622)
Browse files Browse the repository at this point in the history
* add hf logo

* add examples and docs for smolagents

* update page reference
  • Loading branch information
the-praxs authored Jan 4, 2025
1 parent e153547 commit 6bf8bd1
Show file tree
Hide file tree
Showing 6 changed files with 735 additions and 0 deletions.
Binary file added docs/images/external/huggingface/hf-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions docs/mint.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
"v1/integrations/ollama",
"v1/integrations/openai",
"v1/integrations/rest",
"v1/integrations/smolagents",
"v1/integrations/swarmzero",
"v1/integrations/taskweaver",
"v1/integrations/xai"
Expand Down
4 changes: 4 additions & 0 deletions docs/v1/examples/examples.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ mode: "wide"
Create a REST server that performs and observes agent tasks
</Card>

<Card title="smolagents" icon={<img src="https://github.com/AgentOps-AI/agentops/blob/main/docs/images/external/huggingface/hf-logo.png?raw=true" alt="smolagents" />} iconType="image" iconType="solid" href="/v1/integrations/smolagents">
Track HuggingFace's smolagents with AgentOps seamlessly
</Card>

<Card title="SwarmZero" icon={<img src="https://www.github.com/agentops-ai/agentops/blob/main/docs/images/external/swarmzero/swarmzero_logo.png?raw=true" alt="SwarmZero" />} iconType="image" href="/v1/integrations/swarmzero">
SwarmZero multi-agent framework for AI Agents and AI Swarms with AgentOps support
</Card>
Expand Down
82 changes: 82 additions & 0 deletions docs/v1/integrations/smolagents.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
---
title: smolagents
description: "Track HuggingFace's smolagents with AgentOps seamlessly"
---

import EnvTooltip from '/snippets/add-env-tooltip.mdx'

[smolagents](https://github.com/huggingface/smolagents) is a framework for building and running AI agents from [HuggingFace](https://huggingface.co/).
Checkout their [docs](https://huggingface.co/docs/smolagents/index) to get started with creating your own agents.

## Integrating AgentOps with smolagents

<Steps>
<Step title="Install the AgentOps SDK">
<CodeGroup>
```bash pip
pip install agentops
```
```bash poetry
poetry add agentops
```
</CodeGroup>
</Step>
<Step title="Install smolagents">
<CodeGroup>
```bash pip
pip install smolagents
```
```bash poetry
poetry add smolagents
```
</CodeGroup>
</Step>
<Step title="Initialize AgentOps and use LiteLLM in your code">
<Note>
AgentOps tracks calls from smolagents via LiteLLM currently.
</Note>
<CodeGroup>
```python python
import agentops
from smolagents import LiteLLMModel

agentops.init(<INSERT YOUR API KEY HERE>)
model = LiteLLMModel(<model_name>)

# Your code here...

agentops.end_session('Success')
```
</CodeGroup>
<EnvTooltip />
<CodeGroup>
```python .env
AGENTOPS_API_KEY=<YOUR API KEY>
```
</CodeGroup>
Read more about environment variables in [Advanced Configuration](/v1/usage/advanced-configuration)
</Step>
<Step title="Run your Agent">
Execute your program and visit [app.agentops.ai/drilldown](https://app.agentops.ai/drilldown) to observe your Agent! 🕵️
<Tip>
After your run, AgentOps prints a clickable url to console linking directly to your session in the Dashboard
</Tip>
<div/>
<Frame type="glass" caption="Clickable link to session">
<img height="200" src="https://raw.githubusercontent.com/AgentOps-AI/agentops/refs/heads/main/docs/images/external/app_screenshots/session-replay.png?raw=true" />
</Frame>
</Step>
</Steps>

## Full Examples

You can refer to the following examples -

- [Text to SQL](https://github.com/AgentOps-AI/agentops/blob/main/examples/smolagents_examples/text_to_sql.ipynb)
- [Multi-Agent System](https://github.com/AgentOps-AI/agentops/blob/main/examples/smolagents_examples/multi_smolagents_system.ipynb)

<script type="module" src="/scripts/github_stars.js"></script>
<script type="module" src="/scripts/scroll-img-fadein-animation.js"></script>
<script type="module" src="/scripts/button_heartbeat_animation.js"></script>
<script type="css" src="/styles/styles.css"></script>
<script type="module" src="/scripts/adjust_api_dynamically.js"></script>
282 changes: 282 additions & 0 deletions examples/smolagents_examples/multi_smolagents_system.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,282 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "7d4c41ff",
"metadata": {},
"source": [
"# Orchestrate a Multi-Agent System\n",
"\n",
"In this notebook, we will make a multi-agent web browser: an agentic system with several agents collaborating to solve problems using the web!\n",
"\n",
"It will be a simple hierarchy, using a `ManagedAgent` object to wrap the managed web search agent:"
]
},
{
"cell_type": "markdown",
"id": "446d088d",
"metadata": {},
"source": [
"```\n",
"+----------------+\n",
"| Manager agent |\n",
"+----------------+\n",
" |\n",
"_________|______________\n",
"| |\n",
"Code interpreter +--------------------------------+\n",
" tool | Managed agent |\n",
" | +------------------+ |\n",
" | | Web Search agent | |\n",
" | +------------------+ |\n",
" | | | |\n",
" | Web Search tool | |\n",
" | Visit webpage tool |\n",
" +--------------------------------+\n",
"```\n",
"Let’s set up this system.\n",
"\n",
"Run the line below to install the required dependencies:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "015b0a87",
"metadata": {},
"outputs": [],
"source": [
"%pip install markdownify\n",
"%pip install duckduckgo-search\n",
"%pip install smolagents\n",
"%pip install agentops"
]
},
{
"cell_type": "markdown",
"id": "00509499",
"metadata": {},
"source": [
"🖇️ Now we initialize the AgentOps client and load the environment variables to use the API keys."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "330770fd",
"metadata": {},
"outputs": [],
"source": [
"import agentops\n",
"from dotenv import load_dotenv\n",
"import os\n",
"\n",
"load_dotenv()\n",
"AGENTOPS_API_KEY = os.getenv(\"AGENTOPS_API_KEY\") or \"<your_agentops_api_key>\"\n",
"OPENAI_API_KEY = os.getenv(\"OPENAI_API_KEY\") or \"<your_openai_api_key>\""
]
},
{
"cell_type": "markdown",
"id": "9516d2a7",
"metadata": {},
"source": [
"⚡️ Our agent will be powered by `openai/gpt-4o-mini` using the `LiteLLMModel` class."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5f78927c",
"metadata": {},
"outputs": [],
"source": [
"from smolagents import LiteLLMModel\n",
"\n",
"agentops.init(api_key=AGENTOPS_API_KEY, default_tags=[\"smolagents\", \"example\", \"multi-agent\"])\n",
"model = LiteLLMModel(\"openai/gpt-4o-mini\")"
]
},
{
"cell_type": "markdown",
"id": "a08cc376",
"metadata": {},
"source": [
"## Create a Web Search Tool\n",
"\n",
"For web browsing, we can already use our pre-existing `DuckDuckGoSearchTool`. However, we will also create a `VisitWebpageTool` from scratch using `markdownify`. Here’s how:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "01689447",
"metadata": {},
"outputs": [],
"source": [
"import re\n",
"import requests\n",
"from markdownify import markdownify\n",
"from requests.exceptions import RequestException\n",
"from smolagents import tool\n",
"\n",
"@tool\n",
"def visit_webpage(url: str) -> str:\n",
" \"\"\"Visits a webpage at the given URL and returns its content as a markdown string.\n",
"\n",
" Args:\n",
" url: The URL of the webpage to visit.\n",
"\n",
" Returns:\n",
" The content of the webpage converted to Markdown, or an error message if the request fails.\n",
" \"\"\"\n",
" try:\n",
" # Send a GET request to the URL\n",
" response = requests.get(url)\n",
" response.raise_for_status() # Raise an exception for bad status codes\n",
"\n",
" # Convert the HTML content to Markdown\n",
" markdown_content = markdownify(response.text).strip()\n",
"\n",
" # Remove multiple line breaks\n",
" markdown_content = re.sub(r\"\\n{3,}\", \"\\n\\n\", markdown_content)\n",
"\n",
" return markdown_content\n",
"\n",
" except RequestException as e:\n",
" return f\"Error fetching the webpage: {str(e)}\"\n",
" except Exception as e:\n",
" return f\"An unexpected error occurred: {str(e)}\""
]
},
{
"cell_type": "markdown",
"id": "3c45517b",
"metadata": {},
"source": [
"Let’s test our tool:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "51cc54f1",
"metadata": {},
"outputs": [],
"source": [
"print(visit_webpage(\"https://en.wikipedia.org/wiki/Hugging_Face\")[:500])"
]
},
{
"cell_type": "markdown",
"id": "921df68d",
"metadata": {},
"source": [
"## Build Our Multi-Agent System\n",
"\n",
"We will now use the tools `search` and `visit_webpage` to create the web agent."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "f274b34f",
"metadata": {},
"outputs": [],
"source": [
"from smolagents import (\n",
" CodeAgent,\n",
" ToolCallingAgent,\n",
" ManagedAgent,\n",
" DuckDuckGoSearchTool,\n",
")\n",
"\n",
"web_agent = ToolCallingAgent(\n",
" tools=[DuckDuckGoSearchTool(), visit_webpage],\n",
" model=model,\n",
" max_iterations=10,\n",
")\n",
"\n",
"managed_web_agent = ManagedAgent(\n",
" agent=web_agent,\n",
" name=\"search\",\n",
" description=\"Runs web searches for you. Give it your query as an argument.\",\n",
")\n",
"\n",
"manager_agent = CodeAgent(\n",
" tools=[],\n",
" model=model,\n",
" managed_agents=[managed_web_agent],\n",
" additional_authorized_imports=[\"time\", \"numpy\", \"pandas\"],\n",
")"
]
},
{
"cell_type": "markdown",
"id": "d5977883",
"metadata": {},
"source": [
"Let’s run our system with the following query:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e1e497c1",
"metadata": {},
"outputs": [],
"source": [
"answer = manager_agent.run(\"If LLM trainings continue to scale up at the current rhythm until 2030, what would be the electric power in GW required to power the biggest training runs by 2030? What does that correspond to, compared to some countries? Please provide a source for any number used.\")\n",
"\n",
"print(answer)"
]
},
{
"cell_type": "markdown",
"id": "169583c6",
"metadata": {},
"source": [
"Awesome! We've successfully run a multi-agent system. Let's end the agentops session with a \"Success\" state. You can also end the session with a \"Failure\" or \"Indeterminate\" state, which is set as default."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f82fafac",
"metadata": {},
"outputs": [],
"source": [
"agentops.end_session(\"Success\")"
]
},
{
"cell_type": "markdown",
"id": "d373e4ea",
"metadata": {},
"source": [
"You can view the session in the [AgentOps dashboard](https://app.agentops.ai/sessions) by clicking the link provided after ending the session."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "test",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading

0 comments on commit 6bf8bd1

Please sign in to comment.