Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
D
dify
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ai-tech
dify
Commits
3a7f58d2
Unverified
Commit
3a7f58d2
authored
Jun 09, 2023
by
crazywoola
Committed by
GitHub
Jun 09, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Feature/fix streaming mode (#324)
parent
6123bba9
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
131 additions
and
72 deletions
+131
-72
README.md
sdks/nodejs-client/README.md
+15
-4
index.js
sdks/nodejs-client/index.js
+116
-68
No files found.
sdks/nodejs-client/README.md
View file @
3a7f58d2
...
@@ -12,8 +12,12 @@ After installing the SDK, you can use it in your project like this:
...
@@ -12,8 +12,12 @@ After installing the SDK, you can use it in your project like this:
```
js
```
js
import
{
DifyClient
,
ChatClient
,
CompletionClient
}
from
'dify-client'
import
{
DifyClient
,
ChatClient
,
CompletionClient
}
from
'dify-client'
const
API_KEY
=
'your-api-key-here'
;
const
API_KEY
=
'your-api-key-here'
const
user
=
`random-user-id`
;
const
user
=
`random-user-id`
const
inputs
=
{
name
:
'test name a'
}
const
query
=
"Please tell me a short story in 10 words or less."
// Create a completion client
// Create a completion client
const
completionClient
=
new
CompletionClient
(
API_KEY
)
const
completionClient
=
new
CompletionClient
(
API_KEY
)
...
@@ -22,8 +26,15 @@ completionClient.createCompletionMessage(inputs, query, responseMode, user)
...
@@ -22,8 +26,15 @@ completionClient.createCompletionMessage(inputs, query, responseMode, user)
// Create a chat client
// Create a chat client
const
chatClient
=
new
ChatClient
(
API_KEY
)
const
chatClient
=
new
ChatClient
(
API_KEY
)
// Create a chat message
// Create a chat message in stream mode
chatClient
.
createChatMessage
(
inputs
,
query
,
responseMode
,
user
,
conversationId
)
const
response
=
await
chatClient
.
createChatMessage
(
inputs
,
query
,
user
,
true
,
null
)
const
stream
=
response
.
data
;
stream
.
on
(
'data'
,
data
=>
{
console
.
log
(
data
);
});
stream
.
on
(
'end'
,
()
=>
{
console
.
log
(
"stream done"
);
});
// Fetch conversations
// Fetch conversations
chatClient
.
getConversations
(
user
)
chatClient
.
getConversations
(
user
)
// Fetch conversation messages
// Fetch conversation messages
...
...
sdks/nodejs-client/index.js
View file @
3a7f58d2
import
axios
from
'axios'
import
axios
from
"axios"
;
export
const
BASE_URL
=
"https://api.dify.ai/v1"
;
export
const
BASE_URL
=
'https://api.dify.ai/v1'
export
const
routes
=
{
export
const
routes
=
{
application
:
{
application
:
{
method
:
'GET'
,
method
:
"GET"
,
url
:
()
=>
`/parameters`
url
:
()
=>
`/parameters`
,
},
},
feedback
:
{
feedback
:
{
method
:
'POST'
,
method
:
"POST"
,
url
:
(
message
Id
)
=>
`/messages/
${
messageI
d
}
/feedbacks`
,
url
:
(
message
_id
)
=>
`/messages/
${
message_i
d
}
/feedbacks`
,
},
},
createCompletionMessage
:
{
createCompletionMessage
:
{
method
:
'POST'
,
method
:
"POST"
,
url
:
()
=>
`/completion-messages`
,
url
:
()
=>
`/completion-messages`
,
},
},
createChatMessage
:
{
createChatMessage
:
{
method
:
'POST'
,
method
:
"POST"
,
url
:
()
=>
`/chat-messages`
,
url
:
()
=>
`/chat-messages`
,
},
},
getConversationMessages
:
{
getConversationMessages
:
{
method
:
'GET'
,
method
:
"GET"
,
url
:
()
=>
'/messages'
,
url
:
()
=>
"/messages"
,
},
},
getConversations
:
{
getConversations
:
{
method
:
'GET'
,
method
:
"GET"
,
url
:
()
=>
'/conversations'
,
url
:
()
=>
"/conversations"
,
},
},
renameConversation
:
{
renameConversation
:
{
method
:
'PATCH'
,
method
:
"PATCH"
,
url
:
(
conversationId
)
=>
`/conversations/
${
conversationId
}
`
,
url
:
(
conversation_id
)
=>
`/conversations/
${
conversation_id
}
`
,
}
},
};
}
export
class
DifyClient
{
export
class
DifyClient
{
constructor
(
apiKey
,
baseUrl
=
BASE_URL
)
{
constructor
(
apiKey
,
baseUrl
=
BASE_URL
)
{
this
.
apiKey
=
apiKey
this
.
apiKey
=
apiKey
;
this
.
baseUrl
=
baseUrl
this
.
baseUrl
=
baseUrl
;
}
}
updateApiKey
(
apiKey
)
{
updateApiKey
(
apiKey
)
{
this
.
apiKey
=
apiKey
this
.
apiKey
=
apiKey
;
}
}
async
sendRequest
(
method
,
endpoint
,
data
=
null
,
params
=
null
,
stream
=
false
)
{
async
sendRequest
(
method
,
endpoint
,
data
=
null
,
params
=
null
,
stream
=
false
)
{
const
headers
=
{
const
headers
=
{
'Authorization'
:
`Bearer
${
this
.
apiKey
}
`
,
Authorization
:
`Bearer
${
this
.
apiKey
}
`
,
'Content-Type'
:
'application/json'
,
"Content-Type"
:
"application/json"
,
}
}
;
const
url
=
`
${
this
.
baseUrl
}${
endpoint
}
`
const
url
=
`
${
this
.
baseUrl
}${
endpoint
}
`
;
let
response
let
response
;
if
(
!
stream
)
{
if
(
stream
)
{
response
=
await
axios
({
response
=
await
axios
({
method
,
method
,
url
,
url
,
data
,
data
,
params
,
params
,
headers
,
headers
,
responseType
:
stream
?
'stream'
:
'json'
,
responseType
:
"stream"
,
})
})
;
}
else
{
}
else
{
response
=
await
fetch
(
url
,
{
response
=
await
axios
({
headers
,
method
,
method
,
body
:
JSON
.
stringify
(
data
),
url
,
})
data
,
params
,
headers
,
responseType
:
"json"
,
});
}
}
return
response
return
response
;
}
}
messageFeedback
(
message
I
d
,
rating
,
user
)
{
messageFeedback
(
message
_i
d
,
rating
,
user
)
{
const
data
=
{
const
data
=
{
rating
,
rating
,
user
,
user
,
}
};
return
this
.
sendRequest
(
routes
.
feedback
.
method
,
routes
.
feedback
.
url
(
messageId
),
data
)
return
this
.
sendRequest
(
routes
.
feedback
.
method
,
routes
.
feedback
.
url
(
message_id
),
data
);
}
}
getApplicationParameters
(
user
)
{
getApplicationParameters
(
user
)
{
const
params
=
{
user
}
const
params
=
{
user
};
return
this
.
sendRequest
(
routes
.
application
.
method
,
routes
.
application
.
url
(),
null
,
params
)
return
this
.
sendRequest
(
routes
.
application
.
method
,
routes
.
application
.
url
(),
null
,
params
);
}
}
}
}
export
class
CompletionClient
extends
DifyClient
{
export
class
CompletionClient
extends
DifyClient
{
createCompletionMessage
(
inputs
,
query
,
user
,
responseMod
e
)
{
createCompletionMessage
(
inputs
,
query
,
user
,
stream
=
fals
e
)
{
const
data
=
{
const
data
=
{
inputs
,
inputs
,
query
,
query
,
responseMode
,
user
,
user
,
}
response_mode
:
stream
?
"streaming"
:
"blocking"
,
return
this
.
sendRequest
(
routes
.
createCompletionMessage
.
method
,
routes
.
createCompletionMessage
.
url
(),
data
,
null
,
responseMode
===
'streaming'
)
};
return
this
.
sendRequest
(
routes
.
createCompletionMessage
.
method
,
routes
.
createCompletionMessage
.
url
(),
data
,
null
,
stream
);
}
}
}
}
export
class
ChatClient
extends
DifyClient
{
export
class
ChatClient
extends
DifyClient
{
createChatMessage
(
inputs
,
query
,
user
,
responseMode
=
'blocking'
,
conversationId
=
null
)
{
createChatMessage
(
inputs
,
query
,
user
,
stream
=
false
,
conversation_id
=
null
)
{
const
data
=
{
const
data
=
{
inputs
,
inputs
,
query
,
query
,
user
,
user
,
responseMode
,
response_mode
:
stream
?
"streaming"
:
"blocking"
,
}
};
if
(
conversationId
)
if
(
conversation_id
)
data
.
conversation_id
=
conversation_id
;
data
.
conversation_id
=
conversationId
return
this
.
sendRequest
(
routes
.
createChatMessage
.
method
,
routes
.
createChatMessage
.
url
(),
data
,
null
,
responseMode
===
'streaming'
)
return
this
.
sendRequest
(
routes
.
createChatMessage
.
method
,
routes
.
createChatMessage
.
url
(),
data
,
null
,
stream
);
}
}
getConversationMessages
(
user
,
conversationId
=
''
,
firstId
=
null
,
limit
=
null
)
{
getConversationMessages
(
const
params
=
{
user
}
user
,
conversation_id
=
""
,
first_id
=
null
,
limit
=
null
)
{
const
params
=
{
user
};
if
(
conversationId
)
if
(
conversation_id
)
params
.
conversation_id
=
conversation_id
;
params
.
conversation_id
=
conversationId
if
(
firstId
)
if
(
first_id
)
params
.
first_id
=
first_id
;
params
.
first_id
=
firstId
if
(
limit
)
if
(
limit
)
params
.
limit
=
limit
;
params
.
limit
=
limit
return
this
.
sendRequest
(
routes
.
getConversationMessages
.
method
,
routes
.
getConversationMessages
.
url
(),
null
,
params
)
return
this
.
sendRequest
(
routes
.
getConversationMessages
.
method
,
routes
.
getConversationMessages
.
url
(),
null
,
params
);
}
}
getConversations
(
user
,
firstId
=
null
,
limit
=
null
,
pinned
=
null
)
{
getConversations
(
user
,
first_id
=
null
,
limit
=
null
,
pinned
=
null
)
{
const
params
=
{
user
,
first_id
:
firstId
,
limit
,
pinned
}
const
params
=
{
user
,
first_id
:
first_id
,
limit
,
pinned
};
return
this
.
sendRequest
(
routes
.
getConversations
.
method
,
routes
.
getConversations
.
url
(),
null
,
params
)
return
this
.
sendRequest
(
routes
.
getConversations
.
method
,
routes
.
getConversations
.
url
(),
null
,
params
);
}
}
renameConversation
(
conversationId
,
name
,
user
)
{
renameConversation
(
conversation_id
,
name
,
user
)
{
const
data
=
{
name
,
user
}
const
data
=
{
name
,
user
};
return
this
.
sendRequest
(
routes
.
renameConversation
.
method
,
routes
.
renameConversation
.
url
(
conversationId
),
data
)
return
this
.
sendRequest
(
routes
.
renameConversation
.
method
,
routes
.
renameConversation
.
url
(
conversation_id
),
data
);
}
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment