diff --git a/.gitignore b/.gitignore index 82f9275..7573b14 100644 --- a/.gitignore +++ b/.gitignore @@ -160,3 +160,5 @@ cython_debug/ # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ +/config.json +/.idea/ diff --git a/README.md b/README.md index bb4f16d..5453967 100644 --- a/README.md +++ b/README.md @@ -1 +1,9 @@ -# comfyUI-siliconflow-api-2lab \ No newline at end of file +# DeepSeek +Unofficial implementation of siliconflow API for ComfyUI + +# How to use +- apply api key in :https://cloud.siliconflow.cn/ +- add api key in config.json + +# example +see workflow folder \ No newline at end of file diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..5eb68b3 --- /dev/null +++ b/__init__.py @@ -0,0 +1,18 @@ + + +from .siliconflow import FreeChat,PaidChat + +# A dictionary that contains all nodes you want to export with their names +# NOTE: names should be globally unique +NODE_CLASS_MAPPINGS = { + FreeChat.NAME: FreeChat, + PaidChat.NAME: PaidChat, +} + +# display name +NODE_DISPLAY_NAME_MAPPINGS = { + PaidChat.NAME: "Siliconflow paid chat", + FreeChat.NAME: "Siliconflow free chat", +} + +__all__ = [NODE_CLASS_MAPPINGS, NODE_DISPLAY_NAME_MAPPINGS] diff --git a/config_template.json b/config_template.json new file mode 100644 index 0000000..1e6076d --- /dev/null +++ b/config_template.json @@ -0,0 +1,3 @@ +{ + "API_KEY": "your key" +} \ No newline at end of file diff --git a/constants.py b/constants.py new file mode 100644 index 0000000..0bd9c6c --- /dev/null +++ b/constants.py @@ -0,0 +1,18 @@ +# NAMESPACE +import os +from pathlib import Path + +PROJECT_NAME= '2lab' + +def get_project_name(name): + return '{} ({})'.format(name, PROJECT_NAME) + +def get_project_category(sub_dirs = None): + start = "🦊" + PROJECT_NAME + if sub_dirs is None: + return start + else: + return "{}/{}".format(start,sub_dirs) + +# PATH +project_root = Path(__file__).parent \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..f0dd0ae --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +openai \ No newline at end of file diff --git a/siliconflow.py b/siliconflow.py new file mode 100644 index 0000000..af3aa66 --- /dev/null +++ b/siliconflow.py @@ -0,0 +1,92 @@ + +import os +import json +from openai import OpenAI + +from .constants import get_project_category, get_project_name, project_root + +configFile = os.path.join(project_root, 'config.json') +with open(configFile, 'r') as file: + data = file.read() +json_data = json.loads(data) +key = json_data["API_KEY"] + +NODE_CATEGORY = get_project_category("llm") + +class FreeChat: + NAME = get_project_name('FreeChat') + CATEGORY = NODE_CATEGORY + RETURN_TYPES = ("STRING",) + RETURN_NAMES = ("text",) + FUNCTION = "doWork" + + @classmethod + def INPUT_TYPES(s): + return { + "required": { + "prompt": ("STRING", {"multiline": True}), + "model": (["Qwen/Qwen2-7B-Instruct", + "Qwen/Qwen2-1.5B-Instruct", + "Qwen/Qwen1.5-7B-Chat", + "THUDM/glm-4-9b-chat", + "THUDM/chatglm3-6b", + "01-ai/Yi-1.5-9B-Chat-16K", + "01-ai/Yi-1.5-6B-Chat" + ],{"default": "Qwen/Qwen2-7B-Instruct"}), + }, + } + + def doWork(self, prompt, model): + client = OpenAI(api_key=key, base_url="https://api.siliconflow.cn/v1") + + response = client.chat.completions.create( + model=model, + messages=[ + {"role": "system", "content": "You are a helpful assistant"}, + {"role": "user", "content": prompt}, + ], + stream=False + ) + + print(response.choices[0].message.content) + return {"result": (response.choices[0].message.content,)} + +class PaidChat: + NAME = get_project_name('PaidChat') + CATEGORY = NODE_CATEGORY + RETURN_TYPES = ("STRING",) + RETURN_NAMES = ("text",) + FUNCTION = "doWork" + + @classmethod + def INPUT_TYPES(s): + return { + "required": { + "prompt": ("STRING", {"multiline": True}), + "model": (["Qwen/Qwen2-72B-Instruct", + "Qwen/Qwen2-57B-A14B-Instruct", + "Qwen/Qwen1.5-110B-Chat", + "Qwen/Qwen1.5-32B-Chat", + "Qwen/Qwen1.5-14B-Chat", + "deepseek-ai/DeepSeek-Coder-V2-Instruct", + "deepseek-ai/DeepSeek-V2-Chat", + "deepseek-ai/deepseek-llm-67b-chat", + "01-ai/Yi-1.5-34B-Chat-16K" + ],{"default": "Qwen/Qwen2-72B-Instruct"}), + }, + } + + def doWork(self, prompt, model): + client = OpenAI(api_key=key, base_url="https://api.siliconflow.cn/v1") + + response = client.chat.completions.create( + model=model, + messages=[ + {"role": "system", "content": "You are a helpful assistant"}, + {"role": "user", "content": prompt}, + ], + stream=False + ) + + print(response.choices[0].message.content) + return {"result": (response.choices[0].message.content,)} diff --git a/workflow/freechat.png b/workflow/freechat.png new file mode 100644 index 0000000..06e3ce8 Binary files /dev/null and b/workflow/freechat.png differ