AIパーソナル・ラーニング
と実践的なガイダンス
ビーンバッグ・マースコード1

Smolagents:AIインテリジェンスの迅速な開発とインテリジェンスの軽量化のためのオープンソースプロジェクト

はじめに

Smolagentsは、HuggingFaceによって開発された軽量のインテリジェント・エージェント・ライブラリで、AIエージェント・システムの開発プロセスを簡素化することに重点を置いています。このプロジェクトは、そのクリーンな設計思想で知られており、コアコードはわずか約1,000行でありながら、強力な機能統合機能を提供している。Smolagentsは、HuggingFace Hubからアクセス可能なモデル、OpenAIやAnthropicのモデルなど、主流のビッグ言語モデルを幅広くサポートしている。特に特筆すべきは、このフレームワークがセキュリティに十分配慮し、安全なPythonインタプリタとサンドボックス環境を提供することで、コードの実行がもたらすリスクを効果的に低減していることです。オープンソースプロジェクトとして、Smolagentsは基本的なエージェント開発フレームワークを提供するだけでなく、HuggingFace Hubを通じてツールの共有とロードをサポートし、開発者がインテリジェントエージェントシステムをより簡単に構築し、展開できるようにしています。


 

 

機能一覧

  • コアロジックがわずか1000行程度の軽量なエージェント開発フレームワーク
  • 複数の大規模言語モデルの統合をサポート(HuggingFace、OpenAI、Anthropicなど)
  • コード実行プロキシ機能、Pythonコードによるツールの直接呼び出しをサポート
  • 安全なコード実行環境とサンドボックス機構の提供
  • HuggingFace Hubを介したツールの共有とロードのサポート
  • シンプルで直感的なAPI設計により、迅速な開発と導入を実現
  • 完全なドキュメントサポートとサンプルコード
  • カスタムツールの開発と統合のサポート
  • さまざまな構築済みツールを提供(検索ツールDuckDuckGoSearchToolなど)

 

ヘルプの使用

1.インストール手順

まず、pip経由でSmolagentsパッケージをインストールする必要があります:

pip install smolagents

2.基本的な使い方

2.1 シンプルなエージェントの作成

from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
# 创建代理实例
agent = CodeAgent(
tools=[DuckDuckGoSearchTool()],  # 添加所需工具
model=HfApiModel()  # 指定使用的模型
)
# 运行代理
response = agent.run("你的问题或任务描述")

2.2 セキュリティ設定

コード実行のセキュリティを確保するため、Smolagentsは2つのセキュリティ・メカニズムを提供しています:

  • セキュアなPythonインタープリター:利用可能なモジュールと関数を制限してシステムを保護する
  • サンドボックス環境:隔離された実行環境の提供

安全な通訳の使用例:

from smolagents import CodeAgent, SecureInterpreter
agent = CodeAgent(
tools=[your_tools],
interpreter=SecureInterpreter()
)

3.高度な機能

3.1 カスタマイズツールの開発

開発者は独自のツールクラスを作成できる:

from smolagents import BaseTool
class MyCustomTool(BaseTool):
def __init__(self):
super().__init__()
def __call__(self, *args, **kwargs):
# 实现工具的具体功能
pass

3.2 HuggingFace Hubとの統合

ツールはハブから簡単にロードし、共有することができます:

# 从Hub加载工具
from smolagents import load_tool
tool = load_tool("tool_name", from_hub=True)
# 分享工具到Hub
tool.push_to_hub("your-username/tool-name")

4.ベストプラクティスの推奨

  1. コードの実行には、常にセキュアなインタプリタまたはサンドボックス環境を使用する。
  2. 性能とコストを考慮し、ニーズに合ったモデルを選ぶ
  3. 機能の重複を避けるための合理的なツールセットの構成
  4. 依存関係パッケージを定期的にアップデートし、最新の機能とセキュリティフィックスを提供する。
  5. ドキュメントとサンプルコードを活用して開発を加速

5.一般的な問題の解決

  • モデルの読み込みに問題が発生した場合は、ネットワーク接続とAPIキーの設定を確認してください。
  • コード実行エラー、セキュリティ制限のチェック、セキュリティポリシーの調整が必要な場合がある。
  • ツールのインポートに失敗しました。

 

スモラージェントのキー・プロンプト・コンテンツ

参考:https://github.com/huggingface/smolagents/blob/e57f4f55ef506948d2e17b320ddc2a98b282eacf/src/smolagents/prompts.py

  1. ツール呼び出しシステムプロンプト (TOOL_CALLING_SYSTEM_PROMPT)
You are an expert assistant who can solve any task using tool calls. You will be given a task to solve as best you can.
To do so, you have been given access to the following tools: {{tool_names}}
The tool call you write is an action: after the tool is executed, you will get the result of the tool call as an "observation".
This Action/Observation can repeat N times, you should take several steps when needed.
You can use the result of the previous action as input for the next action.
The observation will always be a string: it can represent a file, like "image_1.jpg".
Then you can use it as input for the next action. You can do it for instance as follows:
Observation: "image_1.jpg"
Action:
{
"tool_name": "image_transformer",
"tool_arguments": {"image": "image_1.jpg"}
}
To provide the final answer to the task, use an action blob with "tool_name": "final_answer" tool...
[示例部分省略]
Here are the rules you should always follow to solve your task:
1. ALWAYS provide a tool call, else you will fail.
2. Always use the right arguments for the tools. Never use variable names as the action arguments, use the value instead.
3. Call a tool only when needed: do not call the search agent if you do not need information, try to solve the task yourself.
4. Never re-do a tool call that you previously did with the exact same parameters.
Now Begin! If you solve the task correctly, you will receive a reward of $1,000,000.
  1. コード実行システムプロンプト (CODE_SYSTEM_PROMPT)
You are an expert assistant who can solve any task using code blobs. You will be given a task to solve as best you can.
To do so, you have been given access to a list of tools: these tools are basically Python functions which you can call with code.
To solve the task, you must plan forward to proceed in a series of steps, in a cycle of 'Thought:', 'Code:', and 'Observation:' sequences.
At each step, in the 'Thought:' sequence, you should first explain your reasoning towards solving the task and the tools that you want to use.
Then in the 'Code:' sequence, you should write the code in simple Python. The code sequence must end with '<end_code>' sequence.
[示例部分省略]
Here are the rules you should always follow to solve your task:
1. Always provide a 'Thought:' sequence, and a 'Code:\n```py' sequence ending with '```<end_code>' sequence, else you will fail.
2. Use only variables that you have defined!
3. Always use the right arguments for the tools.
4. Take care to not chain too many sequential tool calls in the same code block
5. Call a tool only when needed, and never re-do a tool call that you previously did with the exact same parameters.
6. Don't name any new variable with the same name as a tool
7. Never create any notional variables in our code
8. You can use imports in your code, but only from the following list of modules: {{authorized_imports}}
9. The state persists between code executions
10. Don't give up! You're in charge of solving the task, not providing directions to solve it.
  1. 事実収集のヒント (SYSTEM_PROMPT_FACTS)
Below I will present you a task.
You will now build a comprehensive preparatory survey of which facts we have at our disposal and which ones we still need.
To do so, you will have to read the task and identify things that must be discovered in order to successfully complete it.
Don't make any assumptions. For each item, provide a thorough reasoning. Here is how you will structure this survey:
### 1. Facts given in the task
List here the specific facts given in the task that could help you (there might be nothing here).
### 2. Facts to look up
List here any facts that we may need to look up.
Also list where to find each of these, for instance a website, a file...
### 3. Facts to derive
List here anything that we want to derive from the above by logical reasoning, for instance computation or simulation.
  1. プログラミング・プロンプト(SYSTEM_PROMPT_PLAN)
You are a world expert at making efficient plans to solve any task using a set of carefully crafted tools.
Now for the given task, develop a step-by-step high-level plan taking into account the above inputs and list of facts.
This plan should involve individual tasks based on the available tools, that if executed correctly will yield the correct answer.
Do not skip steps, do not add any superfluous steps. Only write the high-level plan, DO NOT DETAIL INDIVIDUAL TOOL CALLS.
After writing the final step of the plan, write the '\n<end_plan>' tag and stop there.
  1. 事実更新アラート (SYSTEM_PROMPT_FACTS_UPDATE)
You are a world expert at gathering known and unknown facts based on a conversation.
Below you will find a task, and a history of attempts made to solve the task. You will have to produce a list of these:
### 1. Facts given in the task
### 2. Facts that we have learned
### 3. Facts still to look up
### 4. Facts still to derive
  1. 計画更新プロンプト (SYSTEM_PROMPT_PLAN_UPDATE)
You are a world expert at making efficient plans to solve any task using a set of carefully crafted tools.
You have been given a task:
```{task}```
Find below the record of what has been tried so far to solve it. Then you will be asked to make an updated plan to solve the task.
If the previous tries so far have met some success, you can make an updated plan based on these actions.
If you are stalled, you can make a completely new plan starting from scratch.
  1. マネージド・エージェント・プロンプト (MANAGED_AGENT_PROMPT)
You're a helpful agent named '{name}'.
You have been submitted this task by your manager.
---
Task:
{task}
---
You're helping your manager solve a wider task: so make sure to not provide a one-line answer, but give as much information as possible to give them a clear understanding of the answer.
Your final_answer WILL HAVE to contain these parts:
### 1. Task outcome (short version):
### 2. Task outcome (extremely detailed version):
### 3. Additional context (if relevant):
無断転載を禁じます:チーフAIシェアリングサークル " Smolagents:AIインテリジェンスの迅速な開発とインテリジェンスの軽量化のためのオープンソースプロジェクト
ja日本語