Skip to content

Commit

Permalink
deploy: 2854904
Browse files Browse the repository at this point in the history
  • Loading branch information
DavdGao committed May 14, 2024
1 parent b5e42a1 commit b7af68e
Show file tree
Hide file tree
Showing 30 changed files with 546 additions and 71 deletions.
Binary file modified en/.doctrees/agentscope.models.doctree
Binary file not shown.
Binary file modified en/.doctrees/agentscope.models.openai_model.doctree
Binary file not shown.
Binary file modified en/.doctrees/environment.pickle
Binary file not shown.
Binary file modified en/.doctrees/index.doctree
Binary file not shown.
Binary file modified en/.doctrees/tutorial/206-prompt.doctree
Binary file not shown.
4 changes: 3 additions & 1 deletion en/_modules/agentscope/agents/user_agent.html
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,9 @@ <h1>Source code for agentscope.agents.user_agent</h1><div class="highlight"><pre
<span class="c1"># Input url of file, image, video, audio or website</span>
<span class="n">url</span> <span class="o">=</span> <span class="kc">None</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">require_url</span><span class="p">:</span>
<span class="n">url</span> <span class="o">=</span> <span class="nb">input</span><span class="p">(</span><span class="s2">&quot;URL: &quot;</span><span class="p">)</span>
<span class="n">url</span> <span class="o">=</span> <span class="nb">input</span><span class="p">(</span><span class="s2">&quot;URL (or Enter to skip): &quot;</span><span class="p">)</span>
<span class="k">if</span> <span class="n">url</span> <span class="o">==</span> <span class="s2">&quot;&quot;</span><span class="p">:</span>
<span class="n">url</span> <span class="o">=</span> <span class="kc">None</span>

<span class="c1"># Add additional keys</span>
<span class="n">msg</span> <span class="o">=</span> <span class="n">Msg</span><span class="p">(</span>
Expand Down
97 changes: 87 additions & 10 deletions en/_modules/agentscope/models/openai_model.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ <h1>Source code for agentscope.models.openai_model</h1><div class="highlight"><p
<span></span><span class="c1"># -*- coding: utf-8 -*-</span>
<span class="sd">&quot;&quot;&quot;Model wrapper for OpenAI models&quot;&quot;&quot;</span>
<span class="kn">from</span> <span class="nn">abc</span> <span class="kn">import</span> <span class="n">ABC</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Union</span><span class="p">,</span> <span class="n">Any</span><span class="p">,</span> <span class="n">List</span><span class="p">,</span> <span class="n">Sequence</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Union</span><span class="p">,</span> <span class="n">Any</span><span class="p">,</span> <span class="n">List</span><span class="p">,</span> <span class="n">Sequence</span><span class="p">,</span> <span class="n">Dict</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">.model</span> <span class="kn">import</span> <span class="n">ModelWrapperBase</span><span class="p">,</span> <span class="n">ModelResponse</span>
<span class="kn">from</span> <span class="nn">..file_manager</span> <span class="kn">import</span> <span class="n">file_manager</span>
<span class="kn">from</span> <span class="nn">..message</span> <span class="kn">import</span> <span class="n">MessageBase</span>
<span class="kn">from</span> <span class="nn">..utils.tools</span> <span class="kn">import</span> <span class="n">_convert_to_str</span>
<span class="kn">from</span> <span class="nn">..utils.tools</span> <span class="kn">import</span> <span class="n">_convert_to_str</span><span class="p">,</span> <span class="n">_to_openai_image_url</span>

<span class="k">try</span><span class="p">:</span>
<span class="kn">import</span> <span class="nn">openai</span>
Expand Down Expand Up @@ -218,6 +218,9 @@ <h1>Source code for agentscope.models.openai_model</h1><div class="highlight"><p

<span class="n">deprecated_model_type</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s2">&quot;openai&quot;</span>

<span class="n">substrings_in_vision_models_names</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;gpt-4-turbo&quot;</span><span class="p">,</span> <span class="s2">&quot;vision&quot;</span><span class="p">,</span> <span class="s2">&quot;gpt-4o&quot;</span><span class="p">]</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;The substrings in the model names of vision models.&quot;&quot;&quot;</span>

<span class="k">def</span> <span class="nf">_register_default_metrics</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
<span class="c1"># Set monitor accordingly</span>
<span class="c1"># TODO: set quota to the following metrics</span>
Expand Down Expand Up @@ -323,6 +326,77 @@ <h1>Source code for agentscope.models.openai_model</h1><div class="highlight"><p
<span class="n">raw</span><span class="o">=</span><span class="n">response</span><span class="o">.</span><span class="n">model_dump</span><span class="p">(),</span>
<span class="p">)</span>

<span class="k">def</span> <span class="nf">_format_msg_with_url</span><span class="p">(</span>
<span class="bp">self</span><span class="p">,</span>
<span class="n">msg</span><span class="p">:</span> <span class="n">MessageBase</span><span class="p">,</span>
<span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Dict</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;Format a message with image urls into openai chat format.</span>
<span class="sd"> This format method is used for gpt-4o, gpt-4-turbo, gpt-4-vision and</span>
<span class="sd"> other vision models.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="c1"># Check if the model is a vision model</span>
<span class="k">if</span> <span class="ow">not</span> <span class="nb">any</span><span class="p">(</span>
<span class="n">_</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">model_name</span>
<span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">substrings_in_vision_models_names</span>
<span class="p">):</span>
<span class="n">logger</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span>
<span class="sa">f</span><span class="s2">&quot;The model </span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">model_name</span><span class="si">}</span><span class="s2"> is not a vision model. &quot;</span>
<span class="sa">f</span><span class="s2">&quot;Skip the url in the message.&quot;</span><span class="p">,</span>
<span class="p">)</span>
<span class="k">return</span> <span class="p">{</span>
<span class="s2">&quot;role&quot;</span><span class="p">:</span> <span class="n">msg</span><span class="o">.</span><span class="n">role</span><span class="p">,</span>
<span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="n">msg</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
<span class="s2">&quot;content&quot;</span><span class="p">:</span> <span class="n">_convert_to_str</span><span class="p">(</span><span class="n">msg</span><span class="o">.</span><span class="n">content</span><span class="p">),</span>
<span class="p">}</span>

<span class="c1"># Put all urls into a list</span>
<span class="n">urls</span> <span class="o">=</span> <span class="p">[</span><span class="n">msg</span><span class="o">.</span><span class="n">url</span><span class="p">]</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">msg</span><span class="o">.</span><span class="n">url</span><span class="p">,</span> <span class="nb">str</span><span class="p">)</span> <span class="k">else</span> <span class="n">msg</span><span class="o">.</span><span class="n">url</span>

<span class="c1"># Check if the url refers to an image</span>
<span class="n">checked_urls</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">url</span> <span class="ow">in</span> <span class="n">urls</span><span class="p">:</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">checked_urls</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">_to_openai_image_url</span><span class="p">(</span><span class="n">url</span><span class="p">))</span>
<span class="k">except</span> <span class="ne">TypeError</span><span class="p">:</span>
<span class="n">logger</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span>
<span class="sa">f</span><span class="s2">&quot;The url </span><span class="si">{</span><span class="n">url</span><span class="si">}</span><span class="s2"> is not a valid image url for &quot;</span>
<span class="sa">f</span><span class="s2">&quot;OpenAI Chat API, skipped.&quot;</span><span class="p">,</span>
<span class="p">)</span>

<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">checked_urls</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
<span class="c1"># If no valid image url is provided, return the normal message dict</span>
<span class="k">return</span> <span class="p">{</span>
<span class="s2">&quot;role&quot;</span><span class="p">:</span> <span class="n">msg</span><span class="o">.</span><span class="n">role</span><span class="p">,</span>
<span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="n">msg</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
<span class="s2">&quot;content&quot;</span><span class="p">:</span> <span class="n">_convert_to_str</span><span class="p">(</span><span class="n">msg</span><span class="o">.</span><span class="n">content</span><span class="p">),</span>
<span class="p">}</span>
<span class="k">else</span><span class="p">:</span>
<span class="c1"># otherwise, use the vision format message</span>
<span class="n">returned_msg</span> <span class="o">=</span> <span class="p">{</span>
<span class="s2">&quot;role&quot;</span><span class="p">:</span> <span class="n">msg</span><span class="o">.</span><span class="n">role</span><span class="p">,</span>
<span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="n">msg</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
<span class="s2">&quot;content&quot;</span><span class="p">:</span> <span class="p">[</span>
<span class="p">{</span>
<span class="s2">&quot;type&quot;</span><span class="p">:</span> <span class="s2">&quot;text&quot;</span><span class="p">,</span>
<span class="s2">&quot;text&quot;</span><span class="p">:</span> <span class="n">_convert_to_str</span><span class="p">(</span><span class="n">msg</span><span class="o">.</span><span class="n">content</span><span class="p">),</span>
<span class="p">},</span>
<span class="p">],</span>
<span class="p">}</span>

<span class="n">image_dicts</span> <span class="o">=</span> <span class="p">[</span>
<span class="p">{</span>
<span class="s2">&quot;type&quot;</span><span class="p">:</span> <span class="s2">&quot;image_url&quot;</span><span class="p">,</span>
<span class="s2">&quot;image_url&quot;</span><span class="p">:</span> <span class="p">{</span>
<span class="s2">&quot;url&quot;</span><span class="p">:</span> <span class="n">_</span><span class="p">,</span>
<span class="p">},</span>
<span class="p">}</span>
<span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="n">checked_urls</span>
<span class="p">]</span>

<span class="n">returned_msg</span><span class="p">[</span><span class="s2">&quot;content&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">image_dicts</span><span class="p">)</span>

<span class="k">return</span> <span class="n">returned_msg</span>

<div class="viewcode-block" id="OpenAIChatWrapper.format">
<a class="viewcode-back" href="../../../agentscope.models.openai_model.html#agentscope.models.OpenAIChatWrapper.format">[docs]</a>
<span class="k">def</span> <span class="nf">format</span><span class="p">(</span>
Expand All @@ -343,19 +417,22 @@ <h1>Source code for agentscope.models.openai_model</h1><div class="highlight"><p
<span class="sd"> The formatted messages in the format that OpenAI Chat API</span>
<span class="sd"> required.</span>
<span class="sd"> &quot;&quot;&quot;</span>

<span class="n">messages</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">arg</span> <span class="ow">in</span> <span class="n">args</span><span class="p">:</span>
<span class="k">if</span> <span class="n">arg</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="k">continue</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="n">MessageBase</span><span class="p">):</span>
<span class="n">messages</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
<span class="p">{</span>
<span class="s2">&quot;role&quot;</span><span class="p">:</span> <span class="n">arg</span><span class="o">.</span><span class="n">role</span><span class="p">,</span>
<span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="n">arg</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
<span class="s2">&quot;content&quot;</span><span class="p">:</span> <span class="n">_convert_to_str</span><span class="p">(</span><span class="n">arg</span><span class="o">.</span><span class="n">content</span><span class="p">),</span>
<span class="p">},</span>
<span class="p">)</span>
<span class="k">if</span> <span class="n">arg</span><span class="o">.</span><span class="n">url</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">messages</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_format_msg_with_url</span><span class="p">(</span><span class="n">arg</span><span class="p">))</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">messages</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
<span class="p">{</span>
<span class="s2">&quot;role&quot;</span><span class="p">:</span> <span class="n">arg</span><span class="o">.</span><span class="n">role</span><span class="p">,</span>
<span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="n">arg</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
<span class="s2">&quot;content&quot;</span><span class="p">:</span> <span class="n">_convert_to_str</span><span class="p">(</span><span class="n">arg</span><span class="o">.</span><span class="n">content</span><span class="p">),</span>
<span class="p">},</span>
<span class="p">)</span>

<span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="nb">list</span><span class="p">):</span>
<span class="n">messages</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="o">*</span><span class="n">arg</span><span class="p">))</span>
<span class="k">else</span><span class="p">:</span>
Expand Down
Loading

0 comments on commit b7af68e

Please sign in to comment.