Difference between revisions of "Dreamily API"

From Caiyun Wiki
Jump to: navigation, search
m (Chaosconst moved page 彩云小梦接口 Dreamily API to Dreamily API)
 
(15 intermediate revisions by 2 users not shown)
Line 1: Line 1:
大家好,在这里介绍彩云小梦 API 的调用。
+
Hello, everybody, and welcome to Colorfulclouds's Dreamily API. It's still in its early stages. The steps are as follows:
 +
大家好,在这里介绍彩云小梦 API 的调用。目前还非常初级,处于尝鲜版本。使用步骤如下:
 +
* send e-mail to support@dreamily.ai. To apply for a token, please specify: 发送邮件到 api@caiyunapp.com 申请 token,请写明:
 +
** Your Company/School and name 你的单位/学校和姓名
 +
** The purpose of the application API 申请 API 的用途
 +
** links to apps or websites App 或 网站的链接
 +
* follow the instructions below to make the call 按照下面的说明进行调用
  
* 账户注册:通过 http://if.caiyunai.com/dream 注册一个彩云小梦的普通用户
+
=python version=
* 注册完成后:在 chrome 浏览器地址栏输入(其中前缀 '''javascipt''' 需要手动输入):'''javascript:alert(localStorage.cy_dream_user)''',得到你的uid
+
colab link:https://colab.research.google.com/drive/1Ha0IEOm-VoPu6DbtbUkuYE5HycdfQ9Ax?usp=sharing
* 创建文章,通过如下命令
+
<syntaxhighlight lang="python">
<syntaxhighlight lang="bash">
+
def write(title, content):
#!/bin/bash
+
    global nid
export CY_UID=你刚才得到的UID
 
  
#创建新文章
+
    import requests
curl -d '{"content":"","title":"文章标题","text":""}' http://if.caiyunai.com/v1/dream/$CY_UID/novel_save
+
    import json
{"status":0,"data":{"nid":"6064092f660f63133b940725"}} 
+
   
 +
    url = "http://if.caiyunai.com/v1/dream/"
 +
   
 +
    #WARNING, this should be replaced by your token
 +
    token = #your token
  
#得到nid为你的文章,记下你的nid
 
export NID=6064092f660f63133b940725
 
</syntaxhighlight>
 
* 选择模型
 
<syntaxhighlight lang="bash">
 
#查看 model 详情(一般不用):curl -d '' http://if.caiyunai.com/v1/dream/model_list
 
  
#小梦 0 号:60094a2a9661080dc490f75a
+
    #创建文章,如果已经创建过就使用之前的文章id
#小梦 1 号:601ac4c9bd931db756e22da6
 
#纯爱:601f92f60c9aaf5f28a6f908
 
#言情:601f936f0c9aaf5f28a6f90a
 
#玄幻:60211134902769d45689bf75
 
  
#我们在本文例子里,选“小梦0号
+
    try:
export MID=60094a2a9661080dc490f75a
+
   
</syntaxhighlight>
+
        payload = {
* 发送续写请求
+
                "content" : content,
<syntaxhighlight lang="bash">
+
                "title" : title,
 +
                "nid": nid,
 +
              }
  
curl "http://if.caiyunai.com/v1/dream/$CY_UID/novel_ai" -d '{"nid":"'$NID'","content":"你好","uid":"'$CY_UID'","mid":"'$MID'","title":""}'
+
    except:
#返回例子如下:{"status":0,"msg":"ok","data":{"xid":"60640b0c9dda21a5586494a0"}}
 
#记录下续写id(xid)
 
export XID=60640b0c9dda21a5586494a0
 
</syntaxhighlight>
 
* 等待,然后获取续写结果
 
<syntaxhighlight lang="bash">
 
  
curl "http://if.caiyunai.com/v1/dream/$CY_UID/novel_dream_loop" -d '{"nid":"'$NID'","xid":"'$XID'"}'
+
        payload = {
 +
                "content" : content,  
 +
                "title" : title,
 +
              }
  
# 请求体样例:{"nid":"602f3a7cb499433a1a16a458","xid":"602f3dfe84f40329800a3760"}
 
# 返回体样例:
 
#  计算中:{"status":0,"data":{"rows":[],"count":1}}
 
#  计算完毕:{"status":0,"data":{"rows":[
 
#      {"content":"第一条联想结果","_id":"602f3dfe84f40329800a3761","mid":"60094a2a9661080dc490f75a"},
 
#      {"content":"第二条联想结果","_id":"602f3dfe84f40329800a3761","mid":"60094a2a9661080dc490f75a"},
 
#      {"content":"第三条联想结果","_id":"602f3dfe84f40329800a3761","mid":"60094a2a9661080dc490f75a"}
 
#    ],"count":0}}
 
  
</syntaxhighlight>
+
        response = requests.request("POST", url+token+"/novel_save", data=json.dumps(payload))
  
* 创作完毕后,可以使用novel_save把文章保存
+
        nid = json.loads(response.text)['data']['nid']
<syntaxhighlight lang="bash">
 
  
curl -d '{"content":"","title":"文章标题","text":"", "nid":"'$NID'"}' http://if.caiyunai.com/v1/dream/$CY_UID/novel_save
+
    #选择模型
  
</syntaxhighlight>
+
    #小梦0号:60094a2a9661080dc490f75a
 +
    #小梦1号:601ac4c9bd931db756e22da6
 +
    #纯爱:601f92f60c9aaf5f28a6f908
 +
    #言情:601f936f0c9aaf5f28a6f90a
 +
    #玄幻:60211134902769d45689bf75
  
* 下一次开始续写前,可以使用list函数调出已经保存的文章
+
    #我们在本文例子里,选“小梦0号“
 +
    mid="60094a2a9661080dc490f75a"
  
<syntaxhighlight lang="bash">
+
    #发起续写
  
curl -d '{"nid":"'$NID'" }' "http://if.caiyunai.com/v1/dream/$CY_UID/list"  
+
    payload = {
 +
            "content" : content,
 +
            "title" : title,
 +
            "mid": mid,
 +
            "nid": nid
 +
            }
  
 +
    response = requests.request("POST", url+token+"/novel_ai", data=json.dumps(payload))
 +
 +
    try:
 +
        xid=json.loads(response.text)['data']['xid']
 +
    except:
 +
        print(response.text)
 +
        return;
 +
       
 +
    #等待结果
 +
    import time
 +
 +
    while True:
 +
 +
      time.sleep(1)
 +
 +
      #获取结果
 +
      payload = {
 +
              "xid": xid,
 +
              "nid": nid
 +
              }
 +
 +
      response = requests.request("POST", url+token+"/novel_dream_loop", data=json.dumps(payload))
 +
 +
      if json.loads(response.text)['data']['count']==0:
 +
        res = json.loads(response.text)['data']['rows']
 +
        break
 +
 +
    return res
 +
 +
write("星球大战","地球联合舰队")
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 02:40, 16 October 2022

Hello, everybody, and welcome to Colorfulclouds's Dreamily API. It's still in its early stages. The steps are as follows: 大家好,在这里介绍彩云小梦 API 的调用。目前还非常初级,处于尝鲜版本。使用步骤如下:

  • send e-mail to support@dreamily.ai. To apply for a token, please specify: 发送邮件到 api@caiyunapp.com 申请 token,请写明:
    • Your Company/School and name 你的单位/学校和姓名
    • The purpose of the application API 申请 API 的用途
    • links to apps or websites App 或 网站的链接
  • follow the instructions below to make the call 按照下面的说明进行调用

python version

colab link:https://colab.research.google.com/drive/1Ha0IEOm-VoPu6DbtbUkuYE5HycdfQ9Ax?usp=sharing

def write(title, content):
    global nid

    import requests
    import json
    
    url = "http://if.caiyunai.com/v1/dream/"
    
    #WARNING, this should be replaced by your token
    token = #your token


    #创建文章,如果已经创建过就使用之前的文章id

    try:
    
        payload = {
                "content" : content, 
                "title" : title,
                "nid": nid,
               }

    except:

        payload = {
                "content" : content, 
                "title" : title,
               }


        response = requests.request("POST", url+token+"/novel_save", data=json.dumps(payload))

        nid = json.loads(response.text)['data']['nid']

    #选择模型

    #小梦0号:60094a2a9661080dc490f75a
    #小梦1号:601ac4c9bd931db756e22da6
    #纯爱:601f92f60c9aaf5f28a6f908
    #言情:601f936f0c9aaf5f28a6f90a
    #玄幻:60211134902769d45689bf75

    #我们在本文例子里,选“小梦0号“
    mid="60094a2a9661080dc490f75a"

    #发起续写

    payload = {
            "content" : content, 
            "title" : title,
            "mid": mid,
            "nid": nid
            }

    response = requests.request("POST", url+token+"/novel_ai", data=json.dumps(payload))

    try:
        xid=json.loads(response.text)['data']['xid']
    except:
        print(response.text)
        return;
        
    #等待结果
    import time

    while True:

      time.sleep(1) 

      #获取结果
      payload = {
              "xid": xid,
              "nid": nid
              }

      response = requests.request("POST", url+token+"/novel_dream_loop", data=json.dumps(payload))

      if json.loads(response.text)['data']['count']==0:
        res = json.loads(response.text)['data']['rows']
        break

    return res

write("星球大战","地球联合舰队")