> ## Documentation Index
> Fetch the complete documentation index at: https://ikun.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 自定义工具

此示例展示了如何使用 Agno 创建和使用自己的自定义工具。
您可以将 Hacker News 的功能替换为您想要的任何 API 或服务！

您自己工具的一些想法：

* 天气数据获取器
* 股票价格分析器
* 个人日历集成
* 自定义数据库查询
* 本地文件操作

## 代码

```python custom_tools.py theme={null}
import json
from textwrap import dedent

import httpx
from agno.agent import Agent
from agno.models.openai import OpenAIChat


def get_top_hackernews_stories(num_stories: int = 10) -> str:
    """使用此函数从 Hacker News 获取热门故事。

    Args:
        num_stories (int): 要返回的故事数量。默认为 10。

    Returns:
        str: 热门故事的 JSON 字符串。
    """

    # 获取热门故事 ID
    response = httpx.get("https://hacker-news.firebaseio.com/v0/topstories.json")
    story_ids = response.json()

    # 获取故事详情
    stories = []
    for story_id in story_ids[:num_stories]:
        story_response = httpx.get(
            f"https://hacker-news.firebaseio.com/v0/item/{story_id}.json"
        )
        story = story_response.json()
        if "text" in story:
            story.pop("text", None)
        stories.append(story)
    return json.dumps(stories)


# 创建一个具有硅谷个性的科技新闻报道代理
agent = Agent(
    model=OpenAIChat(id="gpt-4o"),
    instructions=dedent("""\
        你是一位对所有科技事物都充满热情的、懂技术的 Hacker News 记者！🤖
        将自己想象成硅谷内部人士和科技记者的结合体。

        你的风格指南：
        - 用吸引人的科技头条和表情符号开头
        - 用热情和前沿的科技态度呈现 Hacker News 故事
        - 保持回复简洁但信息丰富
        - 在适当的时候使用科技行业的参考和创业公司的行话
        - 以时髦的科技主题结束语结尾，例如“回到终端！”或“生产部署！”

        记住要彻底分析 HN 故事，同时保持高涨的科技热情！\
    """),
    tools=[get_top_hackernews_stories],
    show_tool_calls=True,
    markdown=True,
)

# 尝试示例问题：
# - "目前 HN 上哪些科技话题最热门？"
# - "总结一下 Hacker News 的前 5 个故事"
# - "今天获得最多投票的故事是什么？"
agent.print_response("Summarize the top 5 stories on hackernews?", stream=True)
```

## 用法

<Steps>
  <Snippet file="create-venv-step.mdx" />

  <Step title="安装库">
    ```bash theme={null}
    pip install openai httpx agno
    ```
  </Step>

  <Step title="运行代理">
    ```bash theme={null}
    python custom_tools.py
    ```
  </Step>
</Steps>
