update api setting
This commit is contained in:
@@ -61,17 +61,17 @@ So we decided to develop some general-purpose nodes based on our needs to help y
|
||||
</details>
|
||||
|
||||
<details>
|
||||
<summary>Step 2: Get Comflowy API Key</summary>
|
||||
<summary>Step 2: Obtain Comflowy API Key</summary>
|
||||
|
||||
Next, you need to get the Comflowy API Key. Click on the avatar in the lower left corner (Figure 1), then click on Settings (Figure 2), and finally find the API Key (Figure 3) and copy it.
|
||||
Next, you need to obtain the Comflowy API Key. Click on the avatar in the bottom left corner (Figure ①), then click on Settings (Figure ②), and finally find the API Key (Figure ③) and copy it. **Note: For security reasons in future use, please do not disclose your API Key to others.**
|
||||
|
||||

|
||||
</details>
|
||||
|
||||
<details>
|
||||
<summary>Step 3: Configure Comflowy extension</summary>
|
||||
<summary>Step 3: Enter Comflowy API Key</summary>
|
||||
|
||||
Finally, you need to input the API Key into the node. For example, there's an API Key input box in the LLM node where you need to paste the API Key you just copied:
|
||||
Lastly, you need to input the API Key into the Comflowy Set API Key node. After entering it, you can delete this node. Then you can use other Comflowy nodes. If you don't input this node, you won't be able to use Comflowy nodes.
|
||||
|
||||

|
||||
</details>
|
||||
|
||||
+7
-5
@@ -39,7 +39,7 @@
|
||||
## 二、如何使用
|
||||
|
||||
> [!NOTE]
|
||||
> 需要注意,使用 Comflowy 插件的时候,有可能会出现因为网络问题导致无法正常使用的情况。如果遇到类似 `Failed to get response from LLM model with https://app.comflowy.com/api/open/v0/prompt` 的报错,需要你检查一下网络状态。
|
||||
> 需要注意,在使用 Comflowy 插件的时候,有可能会出现因为网络问题导致无法正常使用的情况。如果遇到类似 `Failed to get response from LLM model with https://app.comflowy.com/api/open/v0/prompt` 的报错,请检查一下网络状态。
|
||||
|
||||
<details>
|
||||
<summary>Step 1: 安装 Comflowy 插件</summary>
|
||||
@@ -53,24 +53,26 @@
|
||||
git clone git clone https://github.com/6174/comflowy-nodes.git
|
||||
```
|
||||
|
||||
然后启动 ComfyUI。
|
||||
|
||||
- 方法三:下载zip文件
|
||||
|
||||
或者下载解压zip文件,将得到的文件夹复制到 ```ComfyUI\custom_nodes\``` 目录下。
|
||||
或者下载解压zip文件,将得到的文件夹复制到 ```ComfyUI\custom_nodes\``` 目录下,然后启动 ComfyUI。
|
||||
|
||||
</details>
|
||||
|
||||
<details>
|
||||
<summary>Step 2: 获取 Comflowy API Key</summary>
|
||||
|
||||
接着你需要获取 Comflowy 的 API Key,点击左下角的头像(图①),再点击设置(图②), 最后找到 API Key(图③) 并复制它。
|
||||
接着你需要获取 Comflowy 的 API Key,点击左下角的头像(图①),再点击设置(图②), 最后找到 API Key(图③) 并复制它。**注意,为了后续使用的安全性,请不要将你的 API Key 泄露给他人。**
|
||||
|
||||

|
||||
</details>
|
||||
|
||||
<details>
|
||||
<summary>Step 3: 配置 Comflowy 插件</summary>
|
||||
<summary>Step 3: 输入 Comflowy API Key</summary>
|
||||
|
||||
最后需要将 API Key 输入到节点里,比如 LLM 节点里就有一个 API Key 的输入框,你需要将刚才复制的 API Key 粘贴到这里:
|
||||
最后需要将 API Key 输入到 Comflowy Set API Key 节点里。输入完后,你可以删除此节点。然后使用 Comflowy 的其他节点。如果你没有输入这个节点,那么你将无法使用 Comflowy 的节点。
|
||||
|
||||

|
||||
</details>
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
import os
|
||||
import json
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
API_KEY_FILE = os.path.join(os.path.dirname(__file__), "api_key.json")
|
||||
|
||||
def save_api_key(api_key):
|
||||
"""
|
||||
Save the API key to a JSON file.
|
||||
|
||||
Args:
|
||||
api_key (str): The API key to be saved.
|
||||
"""
|
||||
try:
|
||||
with open(API_KEY_FILE, "w") as f:
|
||||
json.dump({"api_key": api_key}, f)
|
||||
logger.debug(f"API Key saved to {API_KEY_FILE}")
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to save API Key: {str(e)}")
|
||||
|
||||
def load_api_key():
|
||||
"""
|
||||
Load the API key from the JSON file.
|
||||
|
||||
Returns:
|
||||
str or None: The loaded API key, or None if not found or an error occurred.
|
||||
"""
|
||||
try:
|
||||
if os.path.exists(API_KEY_FILE):
|
||||
with open(API_KEY_FILE, "r") as f:
|
||||
data = json.load(f)
|
||||
return data.get("api_key")
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to load API Key: {str(e)}")
|
||||
return None
|
||||
@@ -1,3 +1,12 @@
|
||||
import os
|
||||
import json
|
||||
import logging
|
||||
from .types import STRING
|
||||
from .api_key_manager import save_api_key
|
||||
|
||||
# 设置日志
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
from .nodes_omost import (
|
||||
OmostLLMNode,
|
||||
@@ -11,6 +20,40 @@ from .nodes_json import FlowyPreviewJSON, FlowyExtractJSON, ComflowyLoadJSON
|
||||
from .nodes_http import FlowyHttpRequest
|
||||
from .nodes_llm import FlowyLLM
|
||||
|
||||
API_KEY_FILE = os.path.join(os.path.dirname(__file__), "api_key.json")
|
||||
|
||||
class ComflowySetAPIKey:
|
||||
"""
|
||||
A node for setting the global Comflowy API Key.
|
||||
"""
|
||||
@classmethod
|
||||
def INPUT_TYPES(s):
|
||||
return {"required": {"api_key": STRING}}
|
||||
|
||||
RETURN_TYPES = ()
|
||||
FUNCTION = "set_api_key"
|
||||
OUTPUT_NODE = True
|
||||
CATEGORY = "Comflowy"
|
||||
|
||||
def set_api_key(self, api_key):
|
||||
"""
|
||||
Set the global API key for Comflowy.
|
||||
|
||||
Args:
|
||||
api_key (str): The API key to be set.
|
||||
|
||||
Returns:
|
||||
tuple: An empty tuple as this node doesn't produce any output.
|
||||
|
||||
Raises:
|
||||
ValueError: If the provided API key is empty.
|
||||
"""
|
||||
if not api_key.strip():
|
||||
raise ValueError("API Key cannot be empty")
|
||||
save_api_key(api_key)
|
||||
print("Comflowy API Key has been set globally")
|
||||
return ()
|
||||
|
||||
NODE_CLASS_MAPPINGS = {
|
||||
"Comflowy_Http_Request": FlowyHttpRequest,
|
||||
"Comflowy_LLM": FlowyLLM,
|
||||
@@ -22,6 +65,7 @@ NODE_CLASS_MAPPINGS = {
|
||||
"Comflowy_Omost_Preview": ComflowyOmostPreviewNode,
|
||||
"Comflowy_Omost_Load_Canvas_Python_Code": ComflowyOmostLoadCanvasPythonCodeNode,
|
||||
"Comflowy_Omost_Load_Canvas_Conditioning": ComflowyOmostLoadCanvasConditioningNode,
|
||||
"Comflowy_Set_API_Key": ComflowySetAPIKey
|
||||
}
|
||||
|
||||
NODE_DISPLAY_NAME_MAPPINGS = {
|
||||
@@ -35,4 +79,5 @@ NODE_DISPLAY_NAME_MAPPINGS = {
|
||||
"Comflowy_Omost_Preview": "Comflowy Omost Preview",
|
||||
"Comflowy_Omost_Load_Canvas_Python_Code": "Comflowy Omost Load Canvas Python Code",
|
||||
"Comflowy_Omost_Load_Canvas_Conditioning": "Comflowy Omost Load Canvas Conditioning",
|
||||
"Comflowy_Set_API_Key": "Comflowy Set API Key"
|
||||
}
|
||||
|
||||
+32
-11
@@ -1,20 +1,19 @@
|
||||
import requests
|
||||
|
||||
# You can use this node to save full size images through the websocket, the
|
||||
# images will be sent in exactly the same format as the image previews: as
|
||||
# binary images on the websocket with a 8 byte header indicating the type
|
||||
# of binary message (first 4 bytes) and the image format (next 4 bytes).
|
||||
|
||||
# Note that no metadata will be put in the images saved with this node.
|
||||
import logging
|
||||
from .api_key_manager import load_api_key
|
||||
from .types import (
|
||||
API_HOST,
|
||||
LLM_MODELS,
|
||||
STRING,
|
||||
STRING_ML,
|
||||
)
|
||||
from .utils import llm_request, logger
|
||||
from .utils import llm_request
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class FlowyLLM:
|
||||
"""
|
||||
A node for making requests to the Comflowy LLM service.
|
||||
"""
|
||||
@classmethod
|
||||
def INPUT_TYPES(s):
|
||||
return {
|
||||
@@ -22,7 +21,6 @@ class FlowyLLM:
|
||||
"prompt": STRING_ML,
|
||||
"system_prompt": STRING_ML,
|
||||
"llm_model": (LLM_MODELS,),
|
||||
"api_key": STRING,
|
||||
"seed": ("INT", {"default": 0, "min": 0, "max": 0xFFFFFFFFFFFFFFFF}),
|
||||
}
|
||||
}
|
||||
@@ -37,13 +35,35 @@ Nodes from https://comflowy.com:
|
||||
- How to use:
|
||||
- Provide a prompt and a system prompt to generate a response from the LLM model.
|
||||
- Choose the LLM model from the available options.
|
||||
- Make sure to set your API Key using the 'Comflowy Set API Key' node before using this node.
|
||||
- Output: Return the generated text from the LLM model.
|
||||
"""
|
||||
|
||||
def llm_request(self, prompt, system_prompt, llm_model, api_key, seed, timeout=10):
|
||||
def llm_request(self, prompt, system_prompt, llm_model, seed, timeout=10):
|
||||
"""
|
||||
Make a request to the Comflowy LLM service.
|
||||
|
||||
Args:
|
||||
prompt (str): The main prompt for the LLM.
|
||||
system_prompt (str): The system prompt for the LLM.
|
||||
llm_model (str): The LLM model to use.
|
||||
seed (int): The seed for random number generation.
|
||||
timeout (int, optional): Timeout for the request in seconds. Defaults to 10.
|
||||
|
||||
Returns:
|
||||
dict: A dictionary containing the UI output and the result.
|
||||
"""
|
||||
if seed > 0xFFFFFFFF:
|
||||
seed = seed & 0xFFFFFFFF
|
||||
logger.warning("Seed is too large. Truncating to 32-bit: %d", seed)
|
||||
|
||||
api_key = load_api_key()
|
||||
|
||||
if not api_key:
|
||||
error_msg = "API Key is not set. Please use the 'Comflowy Set API Key' node to set a global API Key before using this node."
|
||||
logger.error(error_msg)
|
||||
raise ValueError(error_msg)
|
||||
|
||||
try:
|
||||
generated_text = llm_request(
|
||||
prompt=prompt,
|
||||
@@ -55,4 +75,5 @@ Nodes from https://comflowy.com:
|
||||
)
|
||||
return {"ui": {"text": [generated_text]}, "result": (generated_text,)}
|
||||
except Exception as e:
|
||||
logger.error(f"Error in LLM request: {str(e)}")
|
||||
return {"ui": {"text": [str(e)]}, "result": (str(e),)}
|
||||
+21
-26
@@ -1,25 +1,30 @@
|
||||
import logging
|
||||
import requests
|
||||
|
||||
from .types import API_HOST
|
||||
|
||||
logger = logging.getLogger("Comflowy")
|
||||
|
||||
def get_nested_value(data, json_path):
|
||||
keys = json_path.split(".")
|
||||
for key in keys:
|
||||
if key.isdigit():
|
||||
key = int(key)
|
||||
data = data[key]
|
||||
return data
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
def llm_request(prompt, system_prompt, llm_model, api_key, max_tokens=3000, timeout=10):
|
||||
"""
|
||||
Send a request to the Comflowy LLM API.
|
||||
|
||||
Args:
|
||||
prompt (str): The main prompt for the LLM.
|
||||
system_prompt (str): The system prompt for the LLM.
|
||||
llm_model (str): The LLM model to use.
|
||||
api_key (str): The API key for authentication.
|
||||
max_tokens (int, optional): Maximum number of tokens to generate. Defaults to 3000.
|
||||
timeout (int, optional): Timeout for the request in seconds. Defaults to 10.
|
||||
|
||||
Returns:
|
||||
str: The generated text from the LLM.
|
||||
|
||||
Raises:
|
||||
Exception: If there's an error in the API request or response.
|
||||
"""
|
||||
try:
|
||||
response = None
|
||||
url = f"{API_HOST}/api/open/v0/prompt"
|
||||
response = requests.post(
|
||||
url,
|
||||
f"{API_HOST}/api/open/v0/prompt",
|
||||
headers={
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": f"Bearer {api_key}",
|
||||
@@ -37,18 +42,8 @@ def llm_request(prompt, system_prompt, llm_model, api_key, max_tokens=3000, time
|
||||
|
||||
ret = response.json()
|
||||
if ret.get("success"):
|
||||
text = ret.get("text")
|
||||
return text
|
||||
return ret.get("text")
|
||||
else:
|
||||
raise Exception(f"Error: {ret.get('error')}")
|
||||
except Exception as e:
|
||||
error_message = str(e)
|
||||
if response is not None and response.status_code == 500:
|
||||
try:
|
||||
error_json = response.json()
|
||||
error_message = error_json.get("error", error_message)
|
||||
except ValueError:
|
||||
pass # response is not in JSON format
|
||||
print("LLM request error:", error_message)
|
||||
|
||||
raise Exception(f"Failed to get response from LLM model with {url}, error: {error_message}")
|
||||
raise Exception(f"Failed to get response from LLM model with {API_HOST}/api/open/v0/prompt, error: {str(e)}")
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 144 KiB After Width: | Height: | Size: 1.4 MiB |
Reference in New Issue
Block a user