Skip to content

Commit

Permalink
feat: dall-e
Browse files Browse the repository at this point in the history
  • Loading branch information
ubuntu committed May 28, 2024
1 parent 50fe324 commit 5764ca1
Show file tree
Hide file tree
Showing 2 changed files with 251 additions and 2 deletions.
118 changes: 117 additions & 1 deletion docs/openai.html
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,123 @@ <h2 id='fine-tunes'>Fine-tunes</h2>
<h2 id='edits'>Edits</h2>
<p>暂未支持</p>
<h2 id='images'>Images</h2>
<p>暂未支持</p>
<p>PGPT 支持 OpenAI DALL-E接口,这是OpenAI官方训练出的根据文本说明创建图像的人工智能系统。</p>

<p>接入的DALL-E模型包括以下两种:</p>

<ul>
<li>DALL-E-2: 通过自然语言描述创建逼真的图像和艺术。</li>
<li>DALL-E-3: 比DALL-E-2了解更多的细微差别和细节,能将想法转化为及其准确的图像。</li>
</ul>
<h3 id='api-create-images'>API - Create images</h3>
<p><code>POST https://openai.pgpt.cloud/v1/images/generations</code></p>
<h4 id='request-body-4'>Request body</h4>
<blockquote>
<p>Create image 请求示范 (openai &lt; 1.0)</p>
</blockquote>
<div class="highlight"><pre class="highlight python tab-python"><code><span class="kn">import</span> <span class="nn">openai</span>
<span class="n">openai</span><span class="p">.</span><span class="n">api_key</span> <span class="o">=</span> <span class="s">'&lt;YOUR_API_KEY&gt;'</span>
<span class="n">openai</span><span class="p">.</span><span class="n">api_base</span> <span class="o">=</span> <span class="s">'https://openai.pgpt.cloud/v1'</span>
<span class="n">generation_response</span> <span class="o">=</span> <span class="n">openai</span><span class="p">.</span><span class="n">Image</span><span class="p">.</span><span class="n">create</span><span class="p">(</span>
<span class="n">prompt</span><span class="o">=</span><span class="s">'一团火焰'</span><span class="p">,</span> <span class="c1"># Enter your prompt text here
</span> <span class="n">size</span><span class="o">=</span><span class="s">'1024x1024'</span><span class="p">,</span>
<span class="n">n</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span>
<span class="n">model</span><span class="o">=</span><span class="s">'dall-e-3'</span>
<span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">generation_response</span><span class="p">)</span>
</code></pre></div>
<blockquote>
<p>Create image 请求示范 (openai &gt; 1.0)</p>
</blockquote>
<div class="highlight"><pre class="highlight python tab-python"><code><span class="c1"># openai &gt;= 1.0
</span><span class="kn">from</span> <span class="nn">openai</span> <span class="kn">import</span> <span class="n">OpenAI</span>

<span class="n">api_key</span> <span class="o">=</span> <span class="s">'&lt;YOUR_API_KEY&gt;'</span>
<span class="n">base_url</span> <span class="o">=</span> <span class="s">'https://openai.pgpt.cloud/v1'</span>

<span class="n">client</span> <span class="o">=</span> <span class="n">OpenAI</span><span class="p">(</span><span class="n">api_key</span><span class="o">=</span><span class="n">api_key</span><span class="p">,</span> <span class="n">base_url</span><span class="o">=</span><span class="n">base_url</span><span class="p">)</span>
<span class="n">generation_response</span> <span class="o">=</span> <span class="n">client</span><span class="p">.</span><span class="n">images</span><span class="p">.</span><span class="n">generate</span><span class="p">(</span>
<span class="c1"># Enter your prompt text here
</span> <span class="n">prompt</span><span class="o">=</span><span class="s">'Long exposure image capturing the mesmerizing '</span>
<span class="s">'trails of stars above a tranquil mountain landscape.'</span><span class="p">,</span>
<span class="n">model</span><span class="o">=</span><span class="s">'dall-e-3'</span><span class="p">,</span>
<span class="n">size</span><span class="o">=</span><span class="s">'1024x1024'</span><span class="p">,</span>
<span class="n">n</span><span class="o">=</span><span class="mi">1</span>
<span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">generation_response</span><span class="p">)</span>
</code></pre></div><div class="highlight"><pre class="highlight shell tab-shell"><code>curl https://openai.pgpt.cloud/v1/images/generation <span class="se">\</span>
<span class="nt">-H</span> <span class="s2">"Content-Type: application/json"</span> <span class="se">\</span>
<span class="nt">-H</span> <span class="s2">"Authorization: Bearer &lt;APK_KEY&gt;"</span> <span class="se">\</span>
<span class="nt">-d</span> <span class="s1">'{
"model": "dall-e-3",
"prompt": "一团火焰",
"size": "1024x1024,
"n": 1
}'</span>
</code></pre></div>
<blockquote>
<p>Create image 请求示范打印结果</p>
</blockquote>
<div class="highlight"><pre class="highlight json tab-json"><code><span class="p">{</span><span class="w">
</span><span class="nl">"created"</span><span class="p">:</span><span class="w"> </span><span class="mi">1716862564</span><span class="p">,</span><span class="w">
</span><span class="nl">"data"</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="w">
</span><span class="p">{</span><span class="w">
</span><span class="nl">"b64_json"</span><span class="p">:</span><span class="w"> </span><span class="kc">null</span><span class="p">,</span><span class="w">
</span><span class="nl">"revised_prompt"</span><span class="p">:</span><span class="w"> </span><span class="s2">"a ball of fire"</span><span class="p">,</span><span class="w">
</span><span class="nl">"url"</span><span class="p">:</span><span class="w"> </span><span class="s2">"https://dalleprodsec.blob.core.windows.net/private/images/0d06c6d3-5279-44a7-a596-0941573cd02a/generated_00.png?se=2024-05-29T02%3A16%3A13Z&amp;sig=a4vKF1k8oVqlNmij8EISHkFI6dhPcTe9h5Qvb2U4s9U%3D&amp;ske=2024-05-31T00%3A01%3A35Z&amp;skoid=e52d5ed7-0657-4f62-bc12-7e5dbb260a96&amp;sks=b&amp;skt=2024-05-24T00%3A01%3A35Z&amp;sktid=33e01921-4d64-4f8c-a055-5bdaffd5e33d&amp;skv=2020-10-02&amp;sp=r&amp;spr=https&amp;sr=b&amp;sv=2020-10-02"</span><span class="p">,</span><span class="w">
</span><span class="nl">"content_filter_results"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="nl">"hate"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="nl">"filtered"</span><span class="p">:</span><span class="w"> </span><span class="kc">false</span><span class="p">,</span><span class="w">
</span><span class="nl">"severity"</span><span class="p">:</span><span class="w"> </span><span class="s2">"safe"</span><span class="w">
</span><span class="p">},</span><span class="w">
</span><span class="nl">"self_harm"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="nl">"filtered"</span><span class="p">:</span><span class="w"> </span><span class="kc">false</span><span class="p">,</span><span class="w">
</span><span class="nl">"severity"</span><span class="p">:</span><span class="w"> </span><span class="s2">"safe"</span><span class="w">
</span><span class="p">},</span><span class="w">
</span><span class="nl">"sexual"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="nl">"filtered"</span><span class="p">:</span><span class="w"> </span><span class="kc">false</span><span class="p">,</span><span class="w">
</span><span class="nl">"severity"</span><span class="p">:</span><span class="w"> </span><span class="s2">"safe"</span><span class="w">
</span><span class="p">},</span><span class="w">
</span><span class="nl">"violence"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="nl">"filtered"</span><span class="p">:</span><span class="w"> </span><span class="kc">false</span><span class="p">,</span><span class="w">
</span><span class="nl">"severity"</span><span class="p">:</span><span class="w"> </span><span class="s2">"safe"</span><span class="w">
</span><span class="p">}</span><span class="w">
</span><span class="p">},</span><span class="w">
</span><span class="nl">"prompt_filter_results"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="nl">"hate"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="nl">"filtered"</span><span class="p">:</span><span class="w"> </span><span class="kc">false</span><span class="p">,</span><span class="w">
</span><span class="nl">"severity"</span><span class="p">:</span><span class="w"> </span><span class="s2">"safe"</span><span class="w">
</span><span class="p">},</span><span class="w">
</span><span class="nl">"profanity"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="nl">"detected"</span><span class="p">:</span><span class="w"> </span><span class="kc">false</span><span class="p">,</span><span class="w">
</span><span class="nl">"filtered"</span><span class="p">:</span><span class="w"> </span><span class="kc">false</span><span class="w">
</span><span class="p">},</span><span class="w">
</span><span class="nl">"self_harm"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="nl">"filtered"</span><span class="p">:</span><span class="w"> </span><span class="kc">false</span><span class="p">,</span><span class="w">
</span><span class="nl">"severity"</span><span class="p">:</span><span class="w"> </span><span class="s2">"safe"</span><span class="w">
</span><span class="p">},</span><span class="w">
</span><span class="nl">"sexual"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="nl">"filtered"</span><span class="p">:</span><span class="w"> </span><span class="kc">false</span><span class="p">,</span><span class="w">
</span><span class="nl">"severity"</span><span class="p">:</span><span class="w"> </span><span class="s2">"safe"</span><span class="w">
</span><span class="p">},</span><span class="w">
</span><span class="nl">"violence"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
</span><span class="nl">"filtered"</span><span class="p">:</span><span class="w"> </span><span class="kc">false</span><span class="p">,</span><span class="w">
</span><span class="nl">"severity"</span><span class="p">:</span><span class="w"> </span><span class="s2">"safe"</span><span class="w">
</span><span class="p">}</span><span class="w">
</span><span class="p">}</span><span class="w">
</span><span class="p">}</span><span class="w">
</span><span class="p">]</span><span class="w">
</span><span class="p">}</span><span class="w">
</span></code></pre></div><h5 id='model-string-required-4'>参数 - model <code>string</code> Required</h5>
<p>要使用的模型ID。目前我们支持并推荐用 <code>dall-e-2</code>, <code>dall-e-3</code></p>
<h5 id='prompt-string-required'>参数 - prompt <code>string</code> Required</h5>
<p>提示词,用于生成图片</p>
<h5 id='size-string-required'>参数 - size <code>string</code> Required</h5>
<p>指定图像的尺寸,目前仅支持 1024x1024、1024x1792 或 1792x1024 像素</p>
<h5 id='n-int-optional-default-t0-1'>参数 - n <code>int</code> Optional default t0 1</h5>
<p>指定生成的图片数量。</p>

<p>在一次请求中,dall-e-2支持最多10张图片,dall-e-3仅支持生成1张图片</p>
<h2 id='audio'>Audio</h2>
<p>暂未支持</p>
<h2 id='files'>Files</h2>
Expand Down
135 changes: 134 additions & 1 deletion source/openai.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,140 @@ curl https://openai.pgpt.cloud/v1/embeddings \

## Images

暂未支持
PGPT 支持 OpenAI DALL-E接口,这是OpenAI官方训练出的根据文本说明创建图像的人工智能系统。

接入的DALL-E模型包括以下两种:

- DALL-E-2: 通过自然语言描述创建逼真的图像和艺术。
- DALL-E-3: 比DALL-E-2了解更多的细微差别和细节,能将想法转化为及其准确的图像。


### API - Create images

`POST https://openai.pgpt.cloud/v1/images/generations`

#### Request body

> Create image 请求示范 (openai < 1.0)
```python
import openai
openai.api_key = '<YOUR_API_KEY>'
openai.api_base = 'https://openai.pgpt.cloud/v1'
generation_response = openai.Image.create(
prompt='一团火焰', # Enter your prompt text here
size='1024x1024',
n=1,
model='dall-e-3'
)
print(generation_response)
```

> Create image 请求示范 (openai > 1.0)
```python
# openai >= 1.0
from openai import OpenAI

api_key = '<YOUR_API_KEY>'
base_url = 'https://openai.pgpt.cloud/v1'

client = OpenAI(api_key=api_key, base_url=base_url)
generation_response = client.images.generate(
# Enter your prompt text here
prompt='Long exposure image capturing the mesmerizing '
'trails of stars above a tranquil mountain landscape.',
model='dall-e-3',
size='1024x1024',
n=1
)
print(generation_response)
```

```shell
curl https://openai.pgpt.cloud/v1/images/generation \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <APK_KEY>" \
-d '{
"model": "dall-e-3",
"prompt": "一团火焰",
"size": "1024x1024,
"n": 1
}'
```

> Create image 请求示范打印结果
```json
{
"created": 1716862564,
"data": [
{
"b64_json": null,
"revised_prompt": "a ball of fire",
"url": "https://dalleprodsec.blob.core.windows.net/private/images/0d06c6d3-5279-44a7-a596-0941573cd02a/generated_00.png?se=2024-05-29T02%3A16%3A13Z&sig=a4vKF1k8oVqlNmij8EISHkFI6dhPcTe9h5Qvb2U4s9U%3D&ske=2024-05-31T00%3A01%3A35Z&skoid=e52d5ed7-0657-4f62-bc12-7e5dbb260a96&sks=b&skt=2024-05-24T00%3A01%3A35Z&sktid=33e01921-4d64-4f8c-a055-5bdaffd5e33d&skv=2020-10-02&sp=r&spr=https&sr=b&sv=2020-10-02",
"content_filter_results": {
"hate": {
"filtered": false,
"severity": "safe"
},
"self_harm": {
"filtered": false,
"severity": "safe"
},
"sexual": {
"filtered": false,
"severity": "safe"
},
"violence": {
"filtered": false,
"severity": "safe"
}
},
"prompt_filter_results": {
"hate": {
"filtered": false,
"severity": "safe"
},
"profanity": {
"detected": false,
"filtered": false
},
"self_harm": {
"filtered": false,
"severity": "safe"
},
"sexual": {
"filtered": false,
"severity": "safe"
},
"violence": {
"filtered": false,
"severity": "safe"
}
}
}
]
}
```

##### 参数 - model `string` Required

要使用的模型ID。目前我们支持并推荐用 `dall-e-2`, `dall-e-3`

##### 参数 - prompt `string` Required

提示词,用于生成图片

##### 参数 - size `string` Required

指定图像的尺寸,目前仅支持 1024x1024、1024x1792 或 1792x1024 像素

##### 参数 - n `int` Optional default t0 1

指定生成的图片数量。

在一次请求中,dall-e-2支持最多10张图片,dall-e-3仅支持生成1张图片

## Audio

Expand Down

0 comments on commit 5764ca1

Please sign in to comment.