index.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. import axios from "axios";
  2. export const BASE_URL = "https://api.dify.ai/v1";
  3. export const routes = {
  4. application: {
  5. method: "GET",
  6. url: () => `/parameters`,
  7. },
  8. feedback: {
  9. method: "POST",
  10. url: (message_id) => `/messages/${message_id}/feedbacks`,
  11. },
  12. createCompletionMessage: {
  13. method: "POST",
  14. url: () => `/completion-messages`,
  15. },
  16. createChatMessage: {
  17. method: "POST",
  18. url: () => `/chat-messages`,
  19. },
  20. getConversationMessages: {
  21. method: "GET",
  22. url: () => `/messages`,
  23. },
  24. getConversations: {
  25. method: "GET",
  26. url: () => `/conversations`,
  27. },
  28. renameConversation: {
  29. method: "POST",
  30. url: (conversation_id) => `/conversations/${conversation_id}/name`,
  31. },
  32. deleteConversation: {
  33. method: "DELETE",
  34. url: (conversation_id) => `/conversations/${conversation_id}`,
  35. },
  36. fileUpload: {
  37. method: "POST",
  38. url: () => `/files/upload`,
  39. }
  40. };
  41. export class DifyClient {
  42. constructor(apiKey, baseUrl = BASE_URL) {
  43. this.apiKey = apiKey;
  44. this.baseUrl = baseUrl;
  45. }
  46. updateApiKey(apiKey) {
  47. this.apiKey = apiKey;
  48. }
  49. async sendRequest(
  50. method,
  51. endpoint,
  52. data = null,
  53. params = null,
  54. stream = false,
  55. headerParams = {}
  56. ) {
  57. const headers = {
  58. ...{
  59. Authorization: `Bearer ${this.apiKey}`,
  60. "Content-Type": "application/json",
  61. },
  62. ...headerParams
  63. };
  64. const url = `${this.baseUrl}${endpoint}`;
  65. let response;
  66. if (stream) {
  67. response = await axios({
  68. method,
  69. url,
  70. data,
  71. params,
  72. headers,
  73. responseType: "stream",
  74. });
  75. } else {
  76. response = await axios({
  77. method,
  78. url,
  79. data,
  80. params,
  81. headers,
  82. responseType: "json",
  83. });
  84. }
  85. return response;
  86. }
  87. messageFeedback(message_id, rating, user) {
  88. const data = {
  89. rating,
  90. user,
  91. };
  92. return this.sendRequest(
  93. routes.feedback.method,
  94. routes.feedback.url(message_id),
  95. data
  96. );
  97. }
  98. getApplicationParameters(user) {
  99. const params = { user };
  100. return this.sendRequest(
  101. routes.application.method,
  102. routes.application.url(),
  103. null,
  104. params
  105. );
  106. }
  107. fileUpload(data) {
  108. return this.sendRequest(
  109. routes.fileUpload.method,
  110. routes.fileUpload.url(),
  111. data,
  112. null,
  113. false,
  114. {
  115. "Content-Type": 'multipart/form-data'
  116. }
  117. );
  118. }
  119. }
  120. export class CompletionClient extends DifyClient {
  121. createCompletionMessage(inputs, user, stream = false, files = null) {
  122. const data = {
  123. inputs,
  124. user,
  125. response_mode: stream ? "streaming" : "blocking",
  126. files,
  127. };
  128. return this.sendRequest(
  129. routes.createCompletionMessage.method,
  130. routes.createCompletionMessage.url(),
  131. data,
  132. null,
  133. stream
  134. );
  135. }
  136. }
  137. export class ChatClient extends DifyClient {
  138. createChatMessage(
  139. inputs,
  140. query,
  141. user,
  142. stream = false,
  143. conversation_id = null,
  144. files = null
  145. ) {
  146. const data = {
  147. inputs,
  148. query,
  149. user,
  150. response_mode: stream ? "streaming" : "blocking",
  151. files,
  152. };
  153. if (conversation_id) data.conversation_id = conversation_id;
  154. return this.sendRequest(
  155. routes.createChatMessage.method,
  156. routes.createChatMessage.url(),
  157. data,
  158. null,
  159. stream
  160. );
  161. }
  162. getConversationMessages(
  163. user,
  164. conversation_id = "",
  165. first_id = null,
  166. limit = null
  167. ) {
  168. const params = { user };
  169. if (conversation_id) params.conversation_id = conversation_id;
  170. if (first_id) params.first_id = first_id;
  171. if (limit) params.limit = limit;
  172. return this.sendRequest(
  173. routes.getConversationMessages.method,
  174. routes.getConversationMessages.url(),
  175. null,
  176. params
  177. );
  178. }
  179. getConversations(user, first_id = null, limit = null, pinned = null) {
  180. const params = { user, first_id: first_id, limit, pinned };
  181. return this.sendRequest(
  182. routes.getConversations.method,
  183. routes.getConversations.url(),
  184. null,
  185. params
  186. );
  187. }
  188. renameConversation(conversation_id, name, user, auto_generate) {
  189. const data = { name, user, auto_generate };
  190. return this.sendRequest(
  191. routes.renameConversation.method,
  192. routes.renameConversation.url(conversation_id),
  193. data
  194. );
  195. }
  196. deleteConversation(conversation_id, user) {
  197. const data = { user };
  198. return this.sendRequest(
  199. routes.deleteConversation.method,
  200. routes.deleteConversation.url(conversation_id),
  201. data
  202. );
  203. }
  204. }