Skip to content

Commit

Permalink
增加stepfun文生图工具
Browse files Browse the repository at this point in the history
  • Loading branch information
chenlinfeng committed Aug 6, 2024
1 parent 7bafb8b commit d5953d1
Show file tree
Hide file tree
Showing 8 changed files with 303 additions and 2 deletions.
Empty file.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions api/core/tools/provider/builtin/stepfun/stepfun.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from typing import Any

from core.tools.errors import ToolProviderCredentialValidationError
from core.tools.provider.builtin.stepfun.tools.image import StepfunTool
from core.tools.provider.builtin_tool_provider import BuiltinToolProviderController


class StepfunProvider(BuiltinToolProviderController):
def _validate_credentials(self, credentials: dict[str, Any]) -> None:
try:
StepfunTool().fork_tool_runtime(
runtime={
"credentials": credentials,
}
).invoke(
user_id='',
tool_parameters={
"prompt": "cute girl, blue eyes, white hair, anime style",
"size": "1024x1024",
"n": 1
},
)
except Exception as e:
raise ToolProviderCredentialValidationError(str(e))

46 changes: 46 additions & 0 deletions api/core/tools/provider/builtin/stepfun/stepfun.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
identity:
author: Stepfun
name: stepfun
label:
en_US: Image-1X
zh_Hans: 阶跃星辰绘画
pt_BR: Image-1X
description:
en_US: Image-1X
zh_Hans: 阶跃星辰绘画
pt_BR: Image-1X
icon: icon.png
tags:
- image
- productivity
credentials_for_provider:
stepfun_api_key:
type: secret-input
required: true
label:
en_US: Stepfun API key
zh_Hans: 阶跃星辰API key
pt_BR: Stepfun API key
help:
en_US: Please input your stepfun API key
zh_Hans: 请输入你的阶跃星辰 API key
pt_BR: Please input your stepfun API key
placeholder:
en_US: Please input your stepfun API key
zh_Hans: 请输入你的阶跃星辰 API key
pt_BR: Please input your stepfun API key
stepfun_base_url:
type: text-input
required: false
label:
en_US: Stepfun base URL
zh_Hans: 阶跃星辰 base URL
pt_BR: Stepfun base URL
help:
en_US: Please input your Stepfun base URL
zh_Hans: 请输入你的阶跃星辰 base URL
pt_BR: Please input your Stepfun base URL
placeholder:
en_US: Please input your Stepfun base URL
zh_Hans: 请输入你的阶跃星辰 base URL
pt_BR: Please input your Stepfun base URL
72 changes: 72 additions & 0 deletions api/core/tools/provider/builtin/stepfun/tools/image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import random
from typing import Any, Union

from openai import OpenAI
from yarl import URL

from core.tools.entities.tool_entities import ToolInvokeMessage
from core.tools.tool.builtin_tool import BuiltinTool


class StepfunTool(BuiltinTool):
""" Stepfun Image Generation Tool """
def _invoke(self,
user_id: str,
tool_parameters: dict[str, Any],
) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
"""
invoke tools
"""
base_url = self.runtime.credentials.get('stepfun_base_url', None)
if not base_url:
base_url = None
else:
base_url = str(URL(base_url) / 'v1')

client = OpenAI(
api_key=self.runtime.credentials['stepfun_api_key'],
base_url=base_url,
)

extra_body = {}
model = tool_parameters.get('model', 'step-1X-medium')
if not model:
return self.create_text_message('Please input model name')
# prompt
prompt = tool_parameters.get('prompt', '')
if not prompt:
return self.create_text_message('Please input prompt')

seed = tool_parameters.get('seed', 0)
if seed > 0:
extra_body['seed'] = seed
steps = tool_parameters.get('steps', 0)
if steps > 0:
extra_body['steps'] = steps
negative_prompt = tool_parameters.get('negative_prompt', '')
if negative_prompt:
extra_body['negative_prompt'] = negative_prompt

# call openapi stepfun model
response = client.images.generate(
prompt=prompt,
model=model,
size=tool_parameters.get('size', '1024x1024'),
n=tool_parameters.get('n', 1),
extra_body= extra_body
)
print(response)

result = []
for image in response.data:
result.append(self.create_image_message(image=image.url))
result.append(self.create_json_message({
"url": image.url,
}))
return result

@staticmethod
def _generate_random_id(length=8):
characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
random_id = ''.join(random.choices(characters, k=length))
return random_id
158 changes: 158 additions & 0 deletions api/core/tools/provider/builtin/stepfun/tools/image.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
identity:
name: stepfun
author: Stepfun
label:
en_US: step-1X
zh_Hans: 阶跃星辰绘画
pt_BR: step-1X
description:
en_US: step-1X is a powerful drawing tool by stepfun, you can draw the image based on your prompt
zh_Hans: step-1X 系列是阶跃星辰提供的强大的绘画工具,它可以根据您的提示词绘制出您想要的图像。
pt_BR: step-1X is a powerful drawing tool by stepfun, you can draw the image based on your prompt
description:
human:
en_US: step-1X is a text to image tool
zh_Hans: step-1X 是一个文本/图像到图像的工具
pt_BR: step-1X is a text to image tool
llm: step-1X is a tool used to generate images from text or image
parameters:
- name: prompt
type: string
required: true
label:
en_US: Prompt
zh_Hans: 提示词
pt_BR: Prompt
human_description:
en_US: Image prompt, you can check the official documentation of step-1X
zh_Hans: 图像提示词,您可以查看step-1X 的官方文档
pt_BR: Image prompt, you can check the official documentation of step-1X
llm_description: Image prompt of step-1X you should describe the image you want to generate as a list of words as possible as detailed
form: llm
- name: model
type: select
required: false
human_description:
en_US: used for selecting the model name
zh_Hans: 用于选择模型的名字
pt_BR: used for selecting the model name
label:
en_US: Model Name
zh_Hans: 模型名字
pt_BR: Model Name
form: form
options:
- value: step-1X-turbo
label:
en_US: turbo
zh_Hans: turbo
pt_BR: turbo
- value: step-1X-medium
label:
en_US: medium
zh_Hans: medium
pt_BR: medium
- value: step-1X-large
label:
en_US: large
zh_Hans: large
pt_BR: large
default: step-1X-medium
- name: size
type: select
required: false
human_description:
en_US: used for selecting the image size
zh_Hans: 用于选择图像大小
pt_BR: used for selecting the image size
label:
en_US: Image size
zh_Hans: 图像大小
pt_BR: Image size
form: form
options:
- value: 256x256
label:
en_US: 256x256
zh_Hans: 256x256
pt_BR: 256x256
- value: 512x512
label:
en_US: 512x512
zh_Hans: 512x512
pt_BR: 512x512
- value: 768x768
label:
en_US: 768x768
zh_Hans: 768x768
pt_BR: 768x768
- value: 1024x1024
label:
en_US: 1024x1024
zh_Hans: 1024x1024
pt_BR: 1024x1024
- value: 1280x800
label:
en_US: 1280x800
zh_Hans: 1280x800
pt_BR: 1280x800
- value: 800x1280
label:
en_US: 800x1280
zh_Hans: 800x1280
pt_BR: 800x1280
default: 1024x1024
- name: n
type: number
required: true
human_description:
en_US: used for selecting the number of images
zh_Hans: 用于选择图像数量
pt_BR: used for selecting the number of images
label:
en_US: Number of images
zh_Hans: 图像数量
pt_BR: Number of images
form: form
default: 1
min: 1
max: 10
- name: seed
type: number
required: false
label:
en_US: seed
zh_Hans: seed
pt_BR: seed
human_description:
en_US: seed
zh_Hans: seed
pt_BR: seed
form: form
default: 10
- name: steps
type: number
required: false
label:
en_US: Steps
zh_Hans: Steps
pt_BR: Steps
human_description:
en_US: Steps
zh_Hans: Steps
pt_BR: Steps
form: form
default: 10
- name: negative_prompt
type: string
required: false
label:
en_US: Negative prompt
zh_Hans: Negative prompt
pt_BR: Negative prompt
human_description:
en_US: Negative prompt
zh_Hans: Negative prompt
pt_BR: Negative prompt
form: form
default: (worst quality:1.3), (nsfw), low quality
2 changes: 1 addition & 1 deletion api/migrations/versions/4823da1d26cf_add_tool_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def upgrade():
sa.Column('conversation_id', postgresql.UUID(), nullable=False),
sa.Column('file_key', sa.String(length=255), nullable=False),
sa.Column('mimetype', sa.String(length=255), nullable=False),
sa.Column('original_url', sa.String(length=255), nullable=True),
sa.Column('original_url', sa.String(length=2048), nullable=True),
sa.PrimaryKeyConstraint('id', name='tool_file_pkey')
)
# ### end Alembic commands ###
Expand Down
2 changes: 1 addition & 1 deletion api/models/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,4 +299,4 @@ class ToolFile(db.Model):
# mime type
mimetype = db.Column(db.String(255), nullable=False)
# original url
original_url = db.Column(db.String(255), nullable=True)
original_url = db.Column(db.String(2048), nullable=True)

0 comments on commit d5953d1

Please sign in to comment.