mirror of
https://github.com/langgenius/dify-docs-archived.git
synced 2026-07-01 20:35:52 -04:00
update: agent memory
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
An **Agent Strategy Plugin** helps an LLM carry out tasks like reasoning or decision-making, including choosing and calling tools, as well as handling results. This allows the system to address problems more autonomously.
|
||||
|
||||
Below, you’ll see how to develop a plugin that supports **Function Calling** to automatically fetch the current time.
|
||||
Below, you'll see how to develop a plugin that supports **Function Calling** to automatically fetch the current time.
|
||||
|
||||
### Prerequisites
|
||||
|
||||
@@ -21,50 +21,60 @@ For details on preparing the plugin development tool, see [Initializing the Deve
|
||||
|
||||
Run the following command to create a development template for your Agent plugin:
|
||||
|
||||
```
|
||||
```bash
|
||||
dify plugin init
|
||||
```
|
||||
|
||||
Follow the on-screen prompts and refer to the sample comments for guidance.
|
||||
|
||||
```bash
|
||||
➜ Dify Plugins Developing dify plugin init
|
||||
➜ ./dify-plugin-darwin-arm64 plugin init ─╯
|
||||
Edit profile of the plugin
|
||||
Plugin name (press Enter to next step): # 填写插件的名称
|
||||
Author (press Enter to next step): Author name # 填写插件作者
|
||||
Description (press Enter to next step): Description # 填写插件的描述
|
||||
Plugin name (press Enter to next step): # Enter the plugin name
|
||||
Author (press Enter to next step): # Enter the plugin author
|
||||
Description (press Enter to next step): # Enter the plugin description
|
||||
---
|
||||
Select the language you want to use for plugin development, and press Enter to con
|
||||
Select the language you want to use for plugin development, and press Enter to continue,
|
||||
BTW, you need Python 3.12+ to develop the Plugin if you choose Python.
|
||||
-> python # 选择 Python 环境
|
||||
-> python # Select Python environment
|
||||
go (not supported yet)
|
||||
---
|
||||
Based on the ability you want to extend, we have divided the Plugin into four type
|
||||
Based on the ability you want to extend, we have divided the Plugin into four types: Tool, Model, Extension, and Agent Strategy.
|
||||
|
||||
- Tool: It's a tool provider, but not only limited to tools, you can implement an
|
||||
- Tool: It's a tool provider, but not only limited to tools, you can implement an endpoint there, for example, you need both Sending Message and Receiving Message if you are
|
||||
- Model: Just a model provider, extending others is not allowed.
|
||||
- Extension: Other times, you may only need a simple http service to extend the fu
|
||||
- Extension: Other times, you may only need a simple http service to extend the functionalities, Extension is the right choice for you.
|
||||
- Agent Strategy: Implement your own logics here, just by focusing on Agent itself
|
||||
|
||||
What's more, we have provided the template for you, you can choose one of them b
|
||||
What's more, we have provided the template for you, you can choose one of them below:
|
||||
tool
|
||||
-> agent-strategy # 选择 Agent 策略模板
|
||||
-> agent-strategy # Select Agent strategy template
|
||||
llm
|
||||
text-embedding
|
||||
---
|
||||
Configure the permissions of the plugin, use up and down to navigate, tab to sel
|
||||
Configure the permissions of the plugin, use up and down to navigate, tab to select, after selection, press enter to finish
|
||||
Backwards Invocation:
|
||||
Tools:
|
||||
Enabled: [✔] You can invoke tools inside Dify if it's enabled # 默认开启
|
||||
Enabled: [✔] You can invoke tools inside Dify if it's enabled # Enabled by default
|
||||
Models:
|
||||
Enabled: [✔] You can invoke models inside Dify if it's enabled # 默认开启
|
||||
LLM: [✔] You can invoke LLM models inside Dify if it's enabled # 默认开启
|
||||
Text Embedding: [✘] You can invoke text embedding models inside Dify if it'
|
||||
Enabled: [✔] You can invoke models inside Dify if it's enabled # Enabled by default
|
||||
LLM: [✔] You can invoke LLM models inside Dify if it's enabled # Enabled by default
|
||||
→ Text Embedding: [✘] You can invoke text embedding models inside Dify if it's enabled
|
||||
Rerank: [✘] You can invoke rerank models inside Dify if it's enabled
|
||||
...
|
||||
TTS: [✘] You can invoke TTS models inside Dify if it's enabled
|
||||
Speech2Text: [✘] You can invoke speech2text models inside Dify if it's enabled
|
||||
Moderation: [✘] You can invoke moderation models inside Dify if it's enabled
|
||||
Apps:
|
||||
Enabled: [✘] Ability to invoke apps like BasicChat/ChatFlow/Agent/Workflow etc.
|
||||
Resources:
|
||||
Storage:
|
||||
Enabled: [✘] Persistence storage for the plugin
|
||||
Size: N/A The maximum size of the storage
|
||||
Endpoints:
|
||||
Enabled: [✘] Ability to register endpoints
|
||||
```
|
||||
|
||||
After initialization, you’ll get a folder containing all the resources needed for plugin development. Familiarizing yourself with the overall structure of an Agent Strategy Plugin will streamline the development process:
|
||||
After initialization, you'll get a folder containing all the resources needed for plugin development. Familiarizing yourself with the overall structure of an Agent Strategy Plugin will streamline the development process:
|
||||
|
||||
```text
|
||||
├── GUIDE.md # User guide and documentation
|
||||
@@ -95,12 +105,12 @@ Agent Strategy Plugin development revolves around two files:
|
||||
|
||||
#### 2.1 Defining Parameters
|
||||
|
||||
To build an Agent plugin, start by specifying the necessary parameters in `strategies/basic_agent.yaml`. These parameters define the plugin’s core features, such as calling an LLM or using tools.
|
||||
To build an Agent plugin, start by specifying the necessary parameters in `strategies/basic_agent.yaml`. These parameters define the plugin's core features, such as calling an LLM or using tools.
|
||||
|
||||
We recommend including the following four parameters first:
|
||||
|
||||
1. **model**: The large language model to call (e.g., GPT-4, GPT-4o-mini).
|
||||
2. **tools**: A list of tools that enhance your plugin’s functionality.
|
||||
2. **tools**: A list of tools that enhance your plugin's functionality.
|
||||
3. **query**: The user input or prompt content sent to the model.
|
||||
4. **maximum_iterations**: The maximum iteration count to prevent excessive computation.
|
||||
|
||||
@@ -152,7 +162,7 @@ extra:
|
||||
source: strategies/basic_agent.py
|
||||
```
|
||||
|
||||
Once you’ve configured these parameters, the plugin will automatically generate a user-friendly interface so you can easily manage them:
|
||||
Once you've configured these parameters, the plugin will automatically generate a user-friendly interface so you can easily manage them:
|
||||
|
||||

|
||||
|
||||
@@ -182,16 +192,16 @@ class BasicAgentAgentStrategy(AgentStrategy):
|
||||
params = BasicParams(**parameters)
|
||||
```
|
||||
|
||||
### 3. Invoking the Model
|
||||
### 2.3 Invoking the Model
|
||||
|
||||
In an Agent Strategy Plugin, **invoking the model** is central to the workflow. You can invoke an LLM efficiently using `session.model.llm.invoke()` from the SDK, handling text generation, dialogue, and so forth.
|
||||
|
||||
If you want the LLM **handle tools**, ensure it outputs structured parameters to match a tool’s interface. In other words, the LLM must produce input arguments that the tool can accept based on the user’s instructions.
|
||||
If you want the LLM **handle tools**, ensure it outputs structured parameters to match a tool's interface. In other words, the LLM must produce input arguments that the tool can accept based on the user's instructions.
|
||||
|
||||
Construct the following parameters:
|
||||
|
||||
* model
|
||||
* prompt\_messages
|
||||
* prompt_messages
|
||||
* tools
|
||||
* stop
|
||||
* stream
|
||||
@@ -215,7 +225,95 @@ This code achieves the following functionality: after a user inputs a command, t
|
||||
|
||||

|
||||
|
||||
### 4. Handle a Tool
|
||||
### 2.4 Adding Memory to the Model
|
||||
|
||||
Adding **Memory** to your Agent plugin allows the model to remember previous conversations, making interactions more natural and effective. With memory enabled, the model can maintain context and provide more relevant responses.
|
||||
|
||||
Steps:
|
||||
|
||||
1. Configure Memory Functionality
|
||||
|
||||
Add the `history-messages` feature to the Agent plugin’s YAML configuration file `strategies/agent.yaml`:
|
||||
|
||||
```yaml
|
||||
identity:
|
||||
name: basic_agent # Agent strategy name
|
||||
author: novice # Author
|
||||
label:
|
||||
en_US: BasicAgent # English label
|
||||
description:
|
||||
en_US: BasicAgent # English description
|
||||
features:
|
||||
- history-messages # Enable history messages feature
|
||||
...
|
||||
```
|
||||
|
||||
2. Enable Memory Settings
|
||||
|
||||
After modifying the plugin configuration and restarting, you'll see the **Memory** toggle in the node configuration interface. Click the toggle button on the right to enable memory functionality.
|
||||
|
||||
<p align="center">
|
||||
<img src="https://assets-docs.dify.ai/2025/04/4dc804a2f93a030d3a94ef1465b2e359.png" width="400" alt="Memory">
|
||||
</p>
|
||||
|
||||
Once enabled, you can adjust the memory window size using the slider, which determines how many previous conversation turns the model can “remember”.
|
||||
|
||||
3. Debug History Messages
|
||||
|
||||
Add the following code to inspect history message contents:
|
||||
|
||||
```python
|
||||
class BasicAgentAgentStrategy(AgentStrategy):
|
||||
def _invoke(self, parameters: dict[str, Any]) -> Generator[AgentInvokeMessage]:
|
||||
params = BasicParams(**parameters)
|
||||
print(f"history_messages: {params.model.history_prompt_messages}")
|
||||
...
|
||||
```
|
||||
|
||||

|
||||
|
||||
The console will display output similar to:
|
||||
|
||||
```
|
||||
history_messages: []
|
||||
history_messages: [UserPromptMessage(role=<PromptMessageRole.USER: 'user'>, content='hello, my name is novice', name=None), AssistantPromptMessage(role=<PromptMessageRole.ASSISTANT: 'assistant'>, content='Hello, Novice! How can I assist you today?', name=None, tool_calls=[])]
|
||||
```
|
||||
|
||||
4. Integrate History Messages into Model Calls
|
||||
|
||||
Finally, modify the model invocation code to concatenate history messages with the current query:
|
||||
|
||||
```python
|
||||
class BasicAgentAgentStrategy(AgentStrategy):
|
||||
def _invoke(self, parameters: dict[str, Any]) -> Generator[AgentInvokeMessage]:
|
||||
params = BasicParams(**parameters)
|
||||
|
||||
chunks: Generator[LLMResultChunk, None, None] | LLMResult = (
|
||||
self.session.model.llm.invoke(
|
||||
model_config=LLMModelConfig(**params.model.model_dump(mode="json")),
|
||||
# Add history messages
|
||||
prompt_messages=params.model.history_prompt_messages
|
||||
+ [UserPromptMessage(content=params.query)],
|
||||
tools=[
|
||||
self._convert_tool_to_prompt_message_tool(tool)
|
||||
for tool in params.tools
|
||||
],
|
||||
stop=params.model.completion_params.get("stop", [])
|
||||
if params.model.completion_params
|
||||
else [],
|
||||
stream=True,
|
||||
)
|
||||
)
|
||||
...
|
||||
```
|
||||
|
||||
5. Check the Outcome
|
||||
|
||||
After implementing memory, the model can respond based on conversation history. In the example below, the model successfully remembers the user’s name mentioned in previous conversation.
|
||||
|
||||

|
||||
|
||||
### 2.5 Handling a Tool
|
||||
|
||||
After specifying the tool parameters, the Agent Strategy Plugin must actually call these tools. Use `session.tool.invoke()` to make those requests.
|
||||
|
||||
@@ -237,7 +335,7 @@ Example code for method definition:
|
||||
) -> Generator[ToolInvokeMessage, None, None]:...
|
||||
```
|
||||
|
||||
If you’d like the LLM itself to generate the parameters needed for tool calls, you can do so by combining the model’s output with your tool-calling code.
|
||||
If you'd like the LLM itself to generate the parameters needed for tool calls, you can do so by combining the model's output with your tool-calling code.
|
||||
|
||||
```python
|
||||
tool_instances = (
|
||||
@@ -257,13 +355,13 @@ With this in place, your Agent Strategy Plugin can automatically perform **Funct
|
||||
|
||||

|
||||
|
||||
### 5. Creating Logs
|
||||
### 2.6 Creating Logs
|
||||
|
||||
Often, multiple steps are necessary to complete a complex task in an **Agent Strategy Plugin**. It’s crucial for developers to track each step’s results, analyze the decision process, and optimize strategy. Using `create_log_message` and `finish_log_message` from the SDK, you can log real-time states before and after calls, aiding in quick problem diagnosis.
|
||||
Often, multiple steps are necessary to complete a complex task in an **Agent Strategy Plugin**. It's crucial for developers to track each step's results, analyze the decision process, and optimize strategy. Using `create_log_message` and `finish_log_message` from the SDK, you can log real-time states before and after calls, aiding in quick problem diagnosis.
|
||||
|
||||
For example:
|
||||
- Log a “starting model call” message before calling the model, clarifying the task’s execution progress.
|
||||
- Log a “call succeeded” message once the model responds, ensuring the model’s output can be traced end to end.
|
||||
- Log a "starting model call" message before calling the model, clarifying the task's execution progress.
|
||||
- Log a "call succeeded" message once the model responds, ensuring the model's output can be traced end to end.
|
||||
|
||||
```python
|
||||
model_log = self.create_log_message(
|
||||
@@ -318,7 +416,7 @@ model_log = self.create_log_message(
|
||||
yield model_log
|
||||
```
|
||||
|
||||
#### Sample code for agent-plugin functions
|
||||
### Sample code for agent-plugin functions
|
||||
|
||||
{% tabs %}
|
||||
{% tab title="Invoke Model" %}
|
||||
@@ -1036,7 +1134,7 @@ class BasicAgentAgentStrategy(AgentStrategy):
|
||||
|
||||
### 3. Debugging the Plugin
|
||||
|
||||
After finalizing the plugin’s declaration file and implementation code, run `python -m main` in the plugin directory to restart it. Next, confirm the plugin runs correctly. Dify offers remote debugging—go to [“Plugin Management”](https://console-plugin.dify.dev/plugins) to obtain your debug key and remote server address.
|
||||
After finalizing the plugin's declaration file and implementation code, run `python -m main` in the plugin directory to restart it. Next, confirm the plugin runs correctly. Dify offers remote debugging—go to ["Plugin Management"](https://console-plugin.dify.dev/plugins) to obtain your debug key and remote server address.
|
||||
|
||||
<figure><img src="https://assets-docs.dify.ai/2024/12/053415ef127f1f4d6dd85dd3ae79626a.png" alt=""><figcaption></figcaption></figure>
|
||||
|
||||
@@ -1056,7 +1154,7 @@ Then run:
|
||||
python -m main
|
||||
```
|
||||
|
||||
You’ll see the plugin installed in your Workspace, and team members can also access it.
|
||||
You'll see the plugin installed in your Workspace, and team members can also access it.
|
||||
|
||||
<figure><img src="https://assets-docs.dify.ai/2025/01/c82ec0202e5bf914b36e06c796398dd6.png" alt=""><figcaption><p>Browser Plugins</p></figcaption></figure>
|
||||
|
||||
@@ -1072,7 +1170,7 @@ dify plugin package ./basic_agent/
|
||||
|
||||
A file named `google.difypkg` (for example) appears in your current folder—this is your final plugin package.
|
||||
|
||||
**Congratulations!** You’ve fully developed, tested, and packaged your Agent Strategy Plugin.
|
||||
**Congratulations!** You've fully developed, tested, and packaged your Agent Strategy Plugin.
|
||||
|
||||
### Publishing the Plugin (Optional)
|
||||
|
||||
|
||||
@@ -25,41 +25,51 @@ dify plugin init
|
||||
|
||||
表示される指示に従い、必要な情報を入力します。以下のコードのコメントを参考に設定してください。
|
||||
|
||||
```
|
||||
➜ Dify Plugins Developing dify plugin init
|
||||
```bash
|
||||
➜ ./dify-plugin-darwin-arm64 plugin init ─╯
|
||||
Edit profile of the plugin
|
||||
Plugin name (press Enter to next step): # プラグインの名前を入力
|
||||
Author (press Enter to next step): Author name # プラグインの作者を入力
|
||||
Description (press Enter to next step): Description # プラグインの説明を入力
|
||||
Plugin name (press Enter to next step): # プラグイン名を入力
|
||||
Author (press Enter to next step): # プラグイン作者を入力
|
||||
Description (press Enter to next step): # プラグインの説明を入力
|
||||
---
|
||||
Select the language you want to use for plugin development, and press Enter to con
|
||||
Select the language you want to use for plugin development, and press Enter to continue,
|
||||
BTW, you need Python 3.12+ to develop the Plugin if you choose Python.
|
||||
-> python # Python環境を選択
|
||||
go (not supported yet)
|
||||
---
|
||||
Based on the ability you want to extend, we have divided the Plugin into four type
|
||||
Based on the ability you want to extend, we have divided the Plugin into four types: Tool, Model, Extension, and Agent Strategy.
|
||||
|
||||
- Tool: It's a tool provider, but not only limited to tools, you can implement an
|
||||
- Tool: It's a tool provider, but not only limited to tools, you can implement an endpoint there, for example, you need both Sending Message and Receiving Message if you are
|
||||
- Model: Just a model provider, extending others is not allowed.
|
||||
- Extension: Other times, you may only need a simple http service to extend the fu
|
||||
- Extension: Other times, you may only need a simple http service to extend the functionalities, Extension is the right choice for you.
|
||||
- Agent Strategy: Implement your own logics here, just by focusing on Agent itself
|
||||
|
||||
What's more, we have provided the template for you, you can choose one of them b
|
||||
What's more, we have provided the template for you, you can choose one of them below:
|
||||
tool
|
||||
-> agent-strategy # エージェント戦略テンプレートを選択
|
||||
-> agent-strategy # Agent戦略テンプレートを選択
|
||||
llm
|
||||
text-embedding
|
||||
---
|
||||
Configure the permissions of the plugin, use up and down to navigate, tab to sel
|
||||
Configure the permissions of the plugin, use up and down to navigate, tab to select, after selection, press enter to finish
|
||||
Backwards Invocation:
|
||||
Tools:
|
||||
Enabled: [✔] You can invoke tools inside Dify if it's enabled # デフォルトで有効
|
||||
Models:
|
||||
Enabled: [✔] You can invoke models inside Dify if it's enabled # デフォルトで有効
|
||||
LLM: [✔] You can invoke LLM models inside Dify if it's enabled # デフォルトで有効
|
||||
Text Embedding: [✘] You can invoke text embedding models inside Dify if it'
|
||||
→ Text Embedding: [✘] You can invoke text embedding models inside Dify if it's enabled
|
||||
Rerank: [✘] You can invoke rerank models inside Dify if it's enabled
|
||||
...
|
||||
TTS: [✘] You can invoke TTS models inside Dify if it's enabled
|
||||
Speech2Text: [✘] You can invoke speech2text models inside Dify if it's enabled
|
||||
Moderation: [✘] You can invoke moderation models inside Dify if it's enabled
|
||||
Apps:
|
||||
Enabled: [✘] Ability to invoke apps like BasicChat/ChatFlow/Agent/Workflow etc.
|
||||
Resources:
|
||||
Storage:
|
||||
Enabled: [✘] Persistence storage for the plugin
|
||||
Size: N/A The maximum size of the storage
|
||||
Endpoints:
|
||||
Enabled: [✘] Ability to register endpoints
|
||||
```
|
||||
|
||||
プラグインテンプレートを初期化すると、プラグインの開発に必要なすべてのリソースを含むコードフォルダが生成されます。エージェント戦略プラグインのコード構造を理解することで、開発プロセスがスムーズになります。
|
||||
@@ -178,7 +188,7 @@ class BasicAgentAgentStrategy(AgentStrategy):
|
||||
params = BasicParams(**parameters)
|
||||
```
|
||||
|
||||
#### 3. モデルの呼び出し
|
||||
#### 2.3 モデルの呼び出し
|
||||
|
||||
エージェント戦略プラグインでは、**モデルの呼び出し**が中心的な処理ロジックの1つです。SDKが提供する `session.model.llm.invoke()` メソッドを使用すると、LLMモデルを効率的に呼び出して、テキスト生成や対話処理などの機能を実現できます。
|
||||
|
||||
@@ -211,7 +221,95 @@ def invoke(
|
||||
|
||||

|
||||
|
||||
#### 4. ツールの呼び出し
|
||||
#### 2.4 モデルへのメモリ機能の追加
|
||||
|
||||
Agentプラグインを使用してモデルを呼び出す際、メモリ機能を追加することで対話体験が大幅に向上します。メモリ機能により、モデルは完全な対話コンテキストを理解し、一貫性のある対話と正確なツール呼び出しを実現できます。
|
||||
|
||||
実装手順:
|
||||
|
||||
1. メモリ機能の設定
|
||||
|
||||
AgentプラグインのYAML設定ファイル`strategies/agent.yaml`に`history-messages`機能を追加します:
|
||||
|
||||
```yaml
|
||||
identity:
|
||||
name: basic_agent # Agent戦略名
|
||||
author: novice # 作者
|
||||
label:
|
||||
en_US: BasicAgent # 英語ラベル
|
||||
description:
|
||||
en_US: BasicAgent # 英語説明
|
||||
features:
|
||||
- history-messages # 履歴メッセージ機能を有効化
|
||||
...
|
||||
```
|
||||
|
||||
2. メモリ設定の有効化
|
||||
|
||||
プラグイン設定ファイルを修正して再起動後、ノード設定画面に**メモリ**トグルが表示されます。右側のトグルボタンをクリックしてメモリ機能を有効にします。
|
||||
|
||||
<p align="center">
|
||||
<img src="https://assets-docs.dify.ai/2025/04/4dc804a2f93a030d3a94ef1465b2e359.png" width="400" alt="Memory">
|
||||
</p>
|
||||
|
||||
有効化後、**ウィンドウサイズ**スライダーでメモリウィンドウを調整できます。これはモデルが「記憶」できる過去の対話の量を決定します。
|
||||
|
||||
3. 履歴メッセージのデバッグ
|
||||
|
||||
以下のコードを追加して、履歴メッセージの内容を確認します:
|
||||
|
||||
```python
|
||||
class BasicAgentAgentStrategy(AgentStrategy):
|
||||
def _invoke(self, parameters: dict[str, Any]) -> Generator[AgentInvokeMessage]:
|
||||
params = BasicParams(**parameters)
|
||||
print(f"history_messages: {params.model.history_prompt_messages}")
|
||||
...
|
||||
```
|
||||
|
||||

|
||||
|
||||
コンソールには以下のような出力が表示されます:
|
||||
|
||||
```
|
||||
history_messages: []
|
||||
history_messages: [UserPromptMessage(role=<PromptMessageRole.USER: 'user'>, content='hello, my name is novice', name=None), AssistantPromptMessage(role=<PromptMessageRole.ASSISTANT: 'assistant'>, content='Hello, Novice! How can I assist you today?', name=None, tool_calls=[])]
|
||||
```
|
||||
|
||||
4. 履歴メッセージのモデル呼び出しへの統合
|
||||
|
||||
最後に、モデル呼び出しコードを修正して、履歴メッセージを現在のクエリに連結します:
|
||||
|
||||
```python
|
||||
class BasicAgentAgentStrategy(AgentStrategy):
|
||||
def _invoke(self, parameters: dict[str, Any]) -> Generator[AgentInvokeMessage]:
|
||||
params = BasicParams(**parameters)
|
||||
|
||||
chunks: Generator[LLMResultChunk, None, None] | LLMResult = (
|
||||
self.session.model.llm.invoke(
|
||||
model_config=LLMModelConfig(**params.model.model_dump(mode="json")),
|
||||
# 履歴メッセージを追加
|
||||
prompt_messages=params.model.history_prompt_messages
|
||||
+ [UserPromptMessage(content=params.query)],
|
||||
tools=[
|
||||
self._convert_tool_to_prompt_message_tool(tool)
|
||||
for tool in params.tools
|
||||
],
|
||||
stop=params.model.completion_params.get("stop", [])
|
||||
if params.model.completion_params
|
||||
else [],
|
||||
stream=True,
|
||||
)
|
||||
)
|
||||
...
|
||||
```
|
||||
|
||||
5. 効果の検証
|
||||
|
||||
メモリ機能を追加後、モデルは履歴に基づいて応答できるようになります。以下の例では、モデルは以前の対話で言及されたユーザー名を正しく記憶し、対話の一貫性を実現しています。
|
||||
|
||||

|
||||
|
||||
#### 2.5 ツールの呼び出し
|
||||
|
||||
ツールパラメータを設定した後、エージェント戦略プラグインに実際にツールを呼び出す機能を追加する必要があります。これは、SDKの `session.tool.invoke()` 関数を使用して行えます。
|
||||
|
||||
@@ -255,7 +353,7 @@ for tool_call_id, tool_call_name, tool_call_args in tool_calls:
|
||||
|
||||

|
||||
|
||||
#### 5. ログの作成
|
||||
#### 2.6 ログの作成
|
||||
|
||||
**エージェント戦略プラグイン**では、複雑なタスクを完了するために、通常、複数回の操作が必要です。各操作の実行結果を記録することは、開発者にとって非常に重要です。Agentの実行プロセスを追跡し、各ステップの意思決定の根拠を分析することで、戦略の効果をより適切に評価し、最適化できます。
|
||||
|
||||
|
||||
@@ -19,47 +19,57 @@ Agent 策略插件能够帮助 LLM 执行推理或决策逻辑,包括工具选
|
||||
|
||||
运行以下命令,初始化 Agent 插件开发模板。
|
||||
|
||||
```
|
||||
```bash
|
||||
dify plugin init
|
||||
```
|
||||
|
||||
按照页面提示,填写对应信息。参考以下代码中的备注信息,进行设置。
|
||||
|
||||
```
|
||||
➜ Dify Plugins Developing dify plugin init
|
||||
```bash
|
||||
➜ ./dify-plugin-darwin-arm64 plugin init ─╯
|
||||
Edit profile of the plugin
|
||||
Plugin name (press Enter to next step): # 填写插件的名称
|
||||
Author (press Enter to next step): Author name # 填写插件作者
|
||||
Description (press Enter to next step): Description # 填写插件的描述
|
||||
Author (press Enter to next step): # 填写插件作者
|
||||
Description (press Enter to next step): # 填写插件的描述
|
||||
---
|
||||
Select the language you want to use for plugin development, and press Enter to con
|
||||
Select the language you want to use for plugin development, and press Enter to continue,
|
||||
BTW, you need Python 3.12+ to develop the Plugin if you choose Python.
|
||||
-> python # 选择 Python 环境
|
||||
go (not supported yet)
|
||||
---
|
||||
Based on the ability you want to extend, we have divided the Plugin into four type
|
||||
Based on the ability you want to extend, we have divided the Plugin into four types: Tool, Model, Extension, and Agent Strategy.
|
||||
|
||||
- Tool: It's a tool provider, but not only limited to tools, you can implement an
|
||||
- Tool: It's a tool provider, but not only limited to tools, you can implement an endpoint there, for example, you need both Sending Message and Receiving Message if you are
|
||||
- Model: Just a model provider, extending others is not allowed.
|
||||
- Extension: Other times, you may only need a simple http service to extend the fu
|
||||
- Extension: Other times, you may only need a simple http service to extend the functionalities, Extension is the right choice for you.
|
||||
- Agent Strategy: Implement your own logics here, just by focusing on Agent itself
|
||||
|
||||
What's more, we have provided the template for you, you can choose one of them b
|
||||
What's more, we have provided the template for you, you can choose one of them below:
|
||||
tool
|
||||
-> agent-strategy # 选择 Agent 策略模板
|
||||
llm
|
||||
text-embedding
|
||||
---
|
||||
Configure the permissions of the plugin, use up and down to navigate, tab to sel
|
||||
Configure the permissions of the plugin, use up and down to navigate, tab to select, after selection, press enter to finish
|
||||
Backwards Invocation:
|
||||
Tools:
|
||||
Enabled: [✔] You can invoke tools inside Dify if it's enabled # 默认开启
|
||||
Models:
|
||||
Enabled: [✔] You can invoke models inside Dify if it's enabled # 默认开启
|
||||
LLM: [✔] You can invoke LLM models inside Dify if it's enabled # 默认开启
|
||||
Text Embedding: [✘] You can invoke text embedding models inside Dify if it'
|
||||
→ Text Embedding: [✘] You can invoke text embedding models inside Dify if it's enabled
|
||||
Rerank: [✘] You can invoke rerank models inside Dify if it's enabled
|
||||
...
|
||||
TTS: [✘] You can invoke TTS models inside Dify if it's enabled
|
||||
Speech2Text: [✘] You can invoke speech2text models inside Dify if it's enabled
|
||||
Moderation: [✘] You can invoke moderation models inside Dify if it's enabled
|
||||
Apps:
|
||||
Enabled: [✘] Ability to invoke apps like BasicChat/ChatFlow/Agent/Workflow etc.
|
||||
Resources:
|
||||
Storage:
|
||||
Enabled: [✘] Persistence storage for the plugin
|
||||
Size: N/A The maximum size of the storage
|
||||
Endpoints:
|
||||
Enabled: [✘] Ability to register endpoints
|
||||
```
|
||||
|
||||
初始化插件模板后将生成一个代码文件夹,包含插件开发过程中所需的完整资源。熟悉 Agent 策略插件的整体代码结构有助于插件的开发过程。
|
||||
@@ -183,7 +193,7 @@ class BasicAgentAgentStrategy(AgentStrategy):
|
||||
params = BasicParams(**parameters)
|
||||
```
|
||||
|
||||
#### 3. 调用模型
|
||||
#### 2.3 调用模型
|
||||
|
||||
在 Agent 策略插件中,**调用模型**是核心执行逻辑之一。可以通过 SDK 提供的 `session.model.llm.invoke()` 方法高效地调用 LLM 模型,实现文本生成、对话处理等功能。
|
||||
|
||||
@@ -216,7 +226,95 @@ def invoke(
|
||||
|
||||

|
||||
|
||||
#### 4. 调用工具
|
||||
#### 2.4 为模型添加记忆
|
||||
|
||||
当使用 Agent 插件调用模型时,为模型添加记忆功能能够极大提升对话体验。通过记忆功能,模型可以理解完整的对话上下文,实现连贯的交互体验和精准的工具调用。
|
||||
|
||||
具体实现步骤:
|
||||
|
||||
1. 配置记忆功能
|
||||
|
||||
在 Agent 插件的 YAML 配置文件 `strategies/agent.yaml` 中添加 `history-messages` 特性:
|
||||
|
||||
```yaml
|
||||
identity:
|
||||
name: basic_agent # Agent策略名称
|
||||
author: novice # 作者
|
||||
label:
|
||||
en_US: BasicAgent # 英文标签
|
||||
description:
|
||||
en_US: BasicAgent # 英文描述
|
||||
features:
|
||||
- history-messages # 启用历史消息功能
|
||||
...
|
||||
```
|
||||
|
||||
2. 启用记忆设置
|
||||
|
||||
修改插件配置文件并重启后,你将在节点配置界面中看到 **记忆** 开关。点击右侧的开关按钮,启用记忆功能。
|
||||
|
||||
<p align="center">
|
||||
<img src="https://assets-docs.dify.ai/2025/04/4dc804a2f93a030d3a94ef1465b2e359.png" width="400" alt="Memory">
|
||||
</p>
|
||||
|
||||
启用后,您可以通过 **窗口大小** 滑块调整记忆窗口,它决定了模型能够“记住”多少之前的对话内容。
|
||||
|
||||
3. 调试历史消息
|
||||
|
||||
添加以下代码,查看历史消息内容:
|
||||
|
||||
```python
|
||||
class BasicAgentAgentStrategy(AgentStrategy):
|
||||
def _invoke(self, parameters: dict[str, Any]) -> Generator[AgentInvokeMessage]:
|
||||
params = BasicParams(**parameters)
|
||||
print(f"history_messages: {params.model.history_prompt_messages}")
|
||||
...
|
||||
```
|
||||
|
||||

|
||||
|
||||
控制台将显示类似如下输出:
|
||||
|
||||
```
|
||||
history_messages: []
|
||||
history_messages: [UserPromptMessage(role=<PromptMessageRole.USER: 'user'>, content='hello, my name is novice', name=None), AssistantPromptMessage(role=<PromptMessageRole.ASSISTANT: 'assistant'>, content='Hello, Novice! How can I assist you today?', name=None, tool_calls=[])]
|
||||
```
|
||||
|
||||
4. 将历史消息集成到模型调用
|
||||
|
||||
最后,修改模型调用代码,将历史消息拼接到当前查询:
|
||||
|
||||
```python
|
||||
class BasicAgentAgentStrategy(AgentStrategy):
|
||||
def _invoke(self, parameters: dict[str, Any]) -> Generator[AgentInvokeMessage]:
|
||||
params = BasicParams(**parameters)
|
||||
|
||||
chunks: Generator[LLMResultChunk, None, None] | LLMResult = (
|
||||
self.session.model.llm.invoke(
|
||||
model_config=LLMModelConfig(**params.model.model_dump(mode="json")),
|
||||
# 添加历史消息
|
||||
prompt_messages=params.model.history_prompt_messages
|
||||
+ [UserPromptMessage(content=params.query)],
|
||||
tools=[
|
||||
self._convert_tool_to_prompt_message_tool(tool)
|
||||
for tool in params.tools
|
||||
],
|
||||
stop=params.model.completion_params.get("stop", [])
|
||||
if params.model.completion_params
|
||||
else [],
|
||||
stream=True,
|
||||
)
|
||||
)
|
||||
...
|
||||
```
|
||||
|
||||
5. 效果验证
|
||||
|
||||
添加加记忆功能后,模型能够基于历史对话进行回复。在下图示例中,模型成功地记住了之前对话中提及的用户名称,实现了对话的连贯性。
|
||||
|
||||

|
||||
|
||||
#### 2.5 调用工具
|
||||
|
||||
填写工具参数后,需赋予 Agent 策略插件实际调用工具的能力。可以通过 SDK 中的`session.tool.invoke()` 函数进行工具调用。
|
||||
|
||||
@@ -260,7 +358,7 @@ for tool_call_id, tool_call_name, tool_call_args in tool_calls:
|
||||
|
||||

|
||||
|
||||
#### 5. 日志创建
|
||||
#### 2.6 日志创建
|
||||
|
||||
在 **Agent 策略插件**中,通常需要执行多轮操作才能完成复杂任务。记录每轮操作的执行结果对于开发者来说非常重要,有助于追踪 Agent 的执行过程、分析每一步的决策依据,从而更好地评估和优化策略效果。
|
||||
|
||||
@@ -323,7 +421,7 @@ model_log = self.create_log_message(
|
||||
yield model_log
|
||||
```
|
||||
|
||||
#### 插件功能示例代码
|
||||
### 插件功能示例代码
|
||||
|
||||
{% tabs %}
|
||||
{% tab title="调用模型" %}
|
||||
|
||||
Reference in New Issue
Block a user