Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gitee AI tools #10314

Merged
merged 2 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions api/core/tools/provider/builtin/gitee_ai/_assets/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions api/core/tools/provider/builtin/gitee_ai/gitee_ai.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import requests

from core.tools.errors import ToolProviderCredentialValidationError
from core.tools.provider.builtin_tool_provider import BuiltinToolProviderController


class GiteeAIProvider(BuiltinToolProviderController):
def _validate_credentials(self, credentials: dict) -> None:
url = "https://ai.gitee.com/api/base/account/me"
headers = {
"accept": "application/json",
"authorization": f"Bearer {credentials.get('api_key')}",
}

response = requests.get(url, headers=headers)
if response.status_code != 200:
raise ToolProviderCredentialValidationError("GiteeAI API key is invalid")
22 changes: 22 additions & 0 deletions api/core/tools/provider/builtin/gitee_ai/gitee_ai.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
identity:
author: Gitee AI
name: gitee_ai
label:
en_US: Gitee AI
zh_Hans: Gitee AI
description:
en_US: 快速体验大模型,领先探索 AI 开源世界
zh_Hans: 快速体验大模型,领先探索 AI 开源世界
icon: icon.svg
tags:
- image
credentials_for_provider:
api_key:
type: secret-input
required: true
label:
en_US: API Key
placeholder:
zh_Hans: 在此输入您的 API Key
en_US: Enter your API Key
url: https://ai.gitee.com/dashboard/settings/tokens
33 changes: 33 additions & 0 deletions api/core/tools/provider/builtin/gitee_ai/tools/text-to-image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from typing import Any, Union

import requests

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


class GiteeAITool(BuiltinTool):
def _invoke(
self, user_id: str, tool_parameters: dict[str, Any]
) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
headers = {
"content-type": "application/json",
"authorization": f"Bearer {self.runtime.credentials['api_key']}",
}

payload = {
"inputs": tool_parameters.get("inputs"),
"width": tool_parameters.get("width", "720"),
"height": tool_parameters.get("height", "720"),
}
model = tool_parameters.get("model", "Kolors")
url = f"https://ai.gitee.com/api/serverless/{model}/text-to-image"

response = requests.post(url, json=payload, headers=headers)
if response.status_code != 200:
return self.create_text_message(f"Got Error Response:{response.text}")

# The returned image is base64 and needs to be mark as an image
result = [self.create_blob_message(blob=response.content, meta={"mime_type": "image/jpeg"})]

return result
72 changes: 72 additions & 0 deletions api/core/tools/provider/builtin/gitee_ai/tools/text-to-image.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
identity:
name: text to image
author: gitee_ai
label:
en_US: text to image
icon: icon.svg
description:
human:
en_US: generate images using a variety of popular models
llm: This tool is used to generate image from text.
parameters:
- name: model
type: select
required: true
options:
- value: flux-1-schnell
label:
en_US: flux-1-schnell
- value: Kolors
label:
en_US: Kolors
- value: stable-diffusion-3-medium
label:
en_US: stable-diffusion-3-medium
- value: stable-diffusion-xl-base-1.0
label:
en_US: stable-diffusion-xl-base-1.0
- value: stable-diffusion-v1-4
label:
en_US: stable-diffusion-v1-4
default: Kolors
label:
en_US: Choose Image Model
zh_Hans: 选择生成图片的模型
form: form
- name: inputs
type: string
required: true
label:
en_US: Input Text
zh_Hans: 输入文本
human_description:
en_US: The text input used to generate the image.
zh_Hans: 用于生成图片的输入文本。
llm_description: This text input will be used to generate image.
form: llm
- name: width
type: number
required: true
default: 720
min: 1
max: 1024
label:
en_US: Image Width
zh_Hans: 图片宽度
human_description:
en_US: The width of the generated image.
zh_Hans: 生成图片的宽度。
form: form
- name: height
type: number
required: true
default: 720
min: 1
max: 1024
label:
en_US: Image Height
zh_Hans: 图片高度
human_description:
en_US: The height of the generated image.
zh_Hans: 生成图片的高度。
form: form