-
Notifications
You must be signed in to change notification settings - Fork 343
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit e44855d
Showing
155 changed files
with
31,863 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Sphinx build info version 1 | ||
# This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done. | ||
config: 7dfe42599499bc692fbbf280cfd24230 | ||
tags: 645f666f9bcd5a90fca523b33c5a78b7 |
Empty file.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,222 @@ | ||
<!DOCTYPE html> | ||
<html class="writer-html5" lang="en" data-content_root="../../../"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>agentscope.agents.dialog_agent — AgentScope documentation</title> | ||
<link rel="stylesheet" type="text/css" href="../../../_static/pygments.css?v=80d5e7a1" /> | ||
<link rel="stylesheet" type="text/css" href="../../../_static/css/theme.css?v=19f00094" /> | ||
|
||
|
||
<!--[if lt IE 9]> | ||
<script src="../../../_static/js/html5shiv.min.js"></script> | ||
<![endif]--> | ||
|
||
<script src="../../../_static/jquery.js?v=5d32c60e"></script> | ||
<script src="../../../_static/_sphinx_javascript_frameworks_compat.js?v=2cd50e6c"></script> | ||
<script src="../../../_static/documentation_options.js?v=5929fcd5"></script> | ||
<script src="../../../_static/doctools.js?v=888ff710"></script> | ||
<script src="../../../_static/sphinx_highlight.js?v=dc90522c"></script> | ||
<script src="https://unpkg.com/[email protected]/dist/mermaid.min.js"></script> | ||
<script>mermaid.initialize({startOnLoad:true});</script> | ||
<script src="../../../_static/js/theme.js"></script> | ||
<link rel="index" title="Index" href="../../../genindex.html" /> | ||
<link rel="search" title="Search" href="../../../search.html" /> | ||
</head> | ||
|
||
<body class="wy-body-for-nav"> | ||
<div class="wy-grid-for-nav"> | ||
<nav data-toggle="wy-nav-shift" class="wy-nav-side"> | ||
<div class="wy-side-scroll"> | ||
<div class="wy-side-nav-search" > | ||
|
||
|
||
|
||
<a href="../../../index.html" class="icon icon-home"> | ||
AgentScope | ||
</a> | ||
<div role="search"> | ||
<form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get"> | ||
<input type="text" name="q" placeholder="Search docs" aria-label="Search docs" /> | ||
<input type="hidden" name="check_keywords" value="yes" /> | ||
<input type="hidden" name="area" value="default" /> | ||
</form> | ||
</div> | ||
</div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu"> | ||
<p class="caption" role="heading"><span class="caption-text">AgentScope Tutorial</span></p> | ||
<ul> | ||
<li class="toctree-l1"><a class="reference internal" href="../../../tutorial/quick_start.html">Getting Started</a></li> | ||
<li class="toctree-l1"><a class="reference internal" href="../../../tutorial/advance.html">Advanced Exploration</a></li> | ||
<li class="toctree-l1"><a class="reference internal" href="../../../tutorial/contribute.html">Get Involved</a></li> | ||
</ul> | ||
<p class="caption" role="heading"><span class="caption-text">AgentScope API Reference</span></p> | ||
<ul> | ||
<li class="toctree-l1"><a class="reference internal" href="../../../agentscope.agents.html">Agents package</a></li> | ||
<li class="toctree-l1"><a class="reference internal" href="../../../agentscope.configs.html">Configs package</a></li> | ||
<li class="toctree-l1"><a class="reference internal" href="../../../agentscope.memory.html">Memory package</a></li> | ||
<li class="toctree-l1"><a class="reference internal" href="../../../agentscope.models.html">Models package</a></li> | ||
<li class="toctree-l1"><a class="reference internal" href="../../../agentscope.pipelines.html">Pipelines package</a></li> | ||
<li class="toctree-l1"><a class="reference internal" href="../../../agentscope.service.html">Service package</a></li> | ||
<li class="toctree-l1"><a class="reference internal" href="../../../agentscope.rpc.html">RPC package</a></li> | ||
<li class="toctree-l1"><a class="reference internal" href="../../../agentscope.utils.html">Utils package</a></li> | ||
<li class="toctree-l1"><a class="reference internal" href="../../../agentscope.web_ui.html">Web UI package</a></li> | ||
<li class="toctree-l1"><a class="reference internal" href="../../../agentscope.html">Module contents</a></li> | ||
</ul> | ||
|
||
</div> | ||
</div> | ||
</nav> | ||
|
||
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" > | ||
<i data-toggle="wy-nav-top" class="fa fa-bars"></i> | ||
<a href="../../../index.html">AgentScope</a> | ||
</nav> | ||
|
||
<div class="wy-nav-content"> | ||
<div class="rst-content"> | ||
<div role="navigation" aria-label="Page navigation"> | ||
<ul class="wy-breadcrumbs"> | ||
<li><a href="../../../index.html" class="icon icon-home" aria-label="Home"></a></li> | ||
<li class="breadcrumb-item"><a href="../../index.html">Module code</a></li> | ||
<li class="breadcrumb-item active">agentscope.agents.dialog_agent</li> | ||
<li class="wy-breadcrumbs-aside"> | ||
</li> | ||
</ul> | ||
<hr/> | ||
</div> | ||
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article"> | ||
<div itemprop="articleBody"> | ||
|
||
<h1>Source code for agentscope.agents.dialog_agent</h1><div class="highlight"><pre> | ||
<span></span><span class="c1"># -*- coding: utf-8 -*-</span> | ||
<span class="sd">"""A general dialog agent."""</span> | ||
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Any</span><span class="p">,</span> <span class="n">Optional</span><span class="p">,</span> <span class="n">Union</span><span class="p">,</span> <span class="n">Callable</span> | ||
<span class="kn">from</span> <span class="nn">loguru</span> <span class="kn">import</span> <span class="n">logger</span> | ||
|
||
<span class="kn">from</span> <span class="nn">..message</span> <span class="kn">import</span> <span class="n">Msg</span> | ||
<span class="kn">from</span> <span class="nn">.agent</span> <span class="kn">import</span> <span class="n">AgentBase</span> | ||
<span class="kn">from</span> <span class="nn">..prompt</span> <span class="kn">import</span> <span class="n">PromptEngine</span> | ||
<span class="kn">from</span> <span class="nn">..prompt</span> <span class="kn">import</span> <span class="n">PromptType</span> | ||
|
||
|
||
<div class="viewcode-block" id="DialogAgent"> | ||
<a class="viewcode-back" href="../../../agentscope.agents.html#agentscope.agents.dialog_agent.DialogAgent">[docs]</a> | ||
<span class="k">class</span> <span class="nc">DialogAgent</span><span class="p">(</span><span class="n">AgentBase</span><span class="p">):</span> | ||
<span class="w"> </span><span class="sd">"""A simple agent used to perform a dialogue. Your can set its role by</span> | ||
<span class="sd"> `sys_prompt`."""</span> | ||
|
||
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span> | ||
<span class="bp">self</span><span class="p">,</span> | ||
<span class="n">name</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> | ||
<span class="n">config</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">dict</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> | ||
<span class="n">sys_prompt</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">str</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> | ||
<span class="n">model</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">Union</span><span class="p">[</span><span class="n">Callable</span><span class="p">[</span><span class="o">...</span><span class="p">,</span> <span class="n">Any</span><span class="p">],</span> <span class="nb">str</span><span class="p">]]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> | ||
<span class="n">use_memory</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">True</span><span class="p">,</span> | ||
<span class="n">memory_config</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">dict</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> | ||
<span class="n">prompt_type</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">PromptType</span><span class="p">]</span> <span class="o">=</span> <span class="n">PromptType</span><span class="o">.</span><span class="n">LIST</span><span class="p">,</span> | ||
<span class="p">)</span> <span class="o">-></span> <span class="kc">None</span><span class="p">:</span> | ||
<span class="w"> </span><span class="sd">"""Initialize the dialog agent.</span> | ||
|
||
<span class="sd"> Arguments:</span> | ||
<span class="sd"> name (`str`):</span> | ||
<span class="sd"> The name of the agent.</span> | ||
<span class="sd"> config (`Optional[dict]`):</span> | ||
<span class="sd"> The configuration of the agent, if provided, the agent will</span> | ||
<span class="sd"> be initialized from the config rather than the other</span> | ||
<span class="sd"> parameters.</span> | ||
<span class="sd"> sys_prompt (`Optional[str]`):</span> | ||
<span class="sd"> The system prompt of the agent, which can be passed by args</span> | ||
<span class="sd"> or hard-coded in the agent.</span> | ||
<span class="sd"> model (`Optional[Union[Callable[..., Any], str]]`, defaults to</span> | ||
<span class="sd"> None):</span> | ||
<span class="sd"> The callable model object or the model name, which is used to</span> | ||
<span class="sd"> load model from configuration.</span> | ||
<span class="sd"> use_memory (`bool`, defaults to `True`):</span> | ||
<span class="sd"> Whether the agent has memory.</span> | ||
<span class="sd"> memory_config (`Optional[dict]`):</span> | ||
<span class="sd"> The config of memory.</span> | ||
<span class="sd"> prompt_type (`Optional[PromptType]`, defaults to</span> | ||
<span class="sd"> `PromptType.LIST`):</span> | ||
<span class="sd"> The type of the prompt organization, chosen from</span> | ||
<span class="sd"> `PromptType.LIST` or `PromptType.STRING`.</span> | ||
<span class="sd"> """</span> | ||
<span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span> | ||
<span class="n">name</span><span class="p">,</span> | ||
<span class="n">config</span><span class="p">,</span> | ||
<span class="n">sys_prompt</span><span class="p">,</span> | ||
<span class="n">model</span><span class="p">,</span> | ||
<span class="n">use_memory</span><span class="p">,</span> | ||
<span class="n">memory_config</span><span class="p">,</span> | ||
<span class="p">)</span> | ||
|
||
<span class="c1"># init prompt engine</span> | ||
<span class="bp">self</span><span class="o">.</span><span class="n">engine</span> <span class="o">=</span> <span class="n">PromptEngine</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="n">prompt_type</span><span class="o">=</span><span class="n">prompt_type</span><span class="p">)</span> | ||
|
||
<div class="viewcode-block" id="DialogAgent.reply"> | ||
<a class="viewcode-back" href="../../../agentscope.agents.html#agentscope.agents.dialog_agent.DialogAgent.reply">[docs]</a> | ||
<span class="k">def</span> <span class="nf">reply</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">:</span> <span class="nb">dict</span> <span class="o">=</span> <span class="kc">None</span><span class="p">)</span> <span class="o">-></span> <span class="nb">dict</span><span class="p">:</span> | ||
<span class="w"> </span><span class="sd">"""Reply function of the agent. Processes the input data,</span> | ||
<span class="sd"> generates a prompt using the current dialogue memory and system</span> | ||
<span class="sd"> prompt, and invokes the language model to produce a response. The</span> | ||
<span class="sd"> response is then formatted and added to the dialogue memory.</span> | ||
|
||
<span class="sd"> Args:</span> | ||
<span class="sd"> x (`dict`, defaults to `None`):</span> | ||
<span class="sd"> A dictionary representing the user's input to the agent. This</span> | ||
<span class="sd"> input is added to the dialogue memory if provided. Defaults to</span> | ||
<span class="sd"> None.</span> | ||
<span class="sd"> Returns:</span> | ||
<span class="sd"> A dictionary representing the message generated by the agent in</span> | ||
<span class="sd"> response to the user's input.</span> | ||
<span class="sd"> """</span> | ||
<span class="c1"># record the input if needed</span> | ||
<span class="k">if</span> <span class="n">x</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span> | ||
<span class="bp">self</span><span class="o">.</span><span class="n">memory</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> | ||
|
||
<span class="c1"># prepare prompt</span> | ||
<span class="n">prompt</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">engine</span><span class="o">.</span><span class="n">join</span><span class="p">(</span> | ||
<span class="bp">self</span><span class="o">.</span><span class="n">sys_prompt</span><span class="p">,</span> | ||
<span class="bp">self</span><span class="o">.</span><span class="n">memory</span><span class="o">.</span><span class="n">get_memory</span><span class="p">(),</span> | ||
<span class="p">)</span> | ||
|
||
<span class="c1"># call llm and generate response</span> | ||
<span class="n">response</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">(</span><span class="n">prompt</span><span class="p">)</span> | ||
<span class="n">msg</span> <span class="o">=</span> <span class="n">Msg</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">response</span><span class="p">)</span> | ||
|
||
<span class="c1"># logging and record the message in memory</span> | ||
<span class="n">logger</span><span class="o">.</span><span class="n">chat</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span> | ||
<span class="bp">self</span><span class="o">.</span><span class="n">memory</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span> | ||
|
||
<span class="k">return</span> <span class="n">msg</span></div> | ||
</div> | ||
|
||
</pre></div> | ||
|
||
</div> | ||
</div> | ||
<footer> | ||
|
||
<hr/> | ||
|
||
<div role="contentinfo"> | ||
<p>© Copyright 2024, Alibaba Tongyi Lab.</p> | ||
</div> | ||
|
||
Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a | ||
<a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a> | ||
provided by <a href="https://readthedocs.org">Read the Docs</a>. | ||
|
||
|
||
</footer> | ||
</div> | ||
</div> | ||
</section> | ||
</div> | ||
<script> | ||
jQuery(function () { | ||
SphinxRtdTheme.Navigation.enable(true); | ||
}); | ||
</script> | ||
|
||
</body> | ||
</html> |
Oops, something went wrong.