-
Notifications
You must be signed in to change notification settings - Fork 3
/
cozeapi.py
64 lines (54 loc) · 1.91 KB
/
cozeapi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import requests
import random
import json
url = "https://api.coze.cn/open_api/v2/chat"
conversation_id = random.randint(32768, 65536)
user = random.randint(32768, 65536)
class ComfyUI_Coze:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"text_positive": ("STRING", {"default": "给我个有创意的提示词", "multiline": True}),
"bot_id": ("STRING", {"default": "需要填写Coze的BotID", "multiline": True}),
"token": ("STRING", {"default": "需要填写Coze的Token", "multiline": True}),
"seed": ("INT", {"default": 0}),
"log_prompt": (["No", "Yes"], {"default":"Yes"}),
},
}
RETURN_TYPES = ('STRING',)
RETURN_NAMES = ('text_positive',)
FUNCTION = "cozeApi"
OUTPUT_NODE = True
CATEGORY = "ComfyUI_Mexx"
def cozeApi(self, text_positive, bot_id, token, seed, log_prompt):
payload = json.dumps({
"conversation_id": "mexx_" + str(conversation_id),
"bot_id": bot_id,
"user": "mexx_" + str(user),
"query": text_positive,
"stream": False
})
headers = {
'Authorization': 'Bearer ' + token,
'Accept': '*/*',
'Host': 'api.coze.cn',
'Connection': 'keep-alive',
'Content-Type': 'application/json'
}
r = requests.request("POST", url, headers=headers, data=payload)
result = r.json()
if log_prompt == "Yes":
print(f"resp: {result}")
messages = result["messages"]
filtered_messages = [message for message in messages if message.get('type') == 'answer']
content = filtered_messages[0]["content"]
return [content]
NODE_CLASS_MAPPINGS = {
"ComfyUI_Coze": ComfyUI_Coze
}
NODE_DISPLAY_NAME_MAPPINGS = {
"ComfyUI_Coze": "ComfyUI_Coze"
}