index.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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: "PATCH",
  30. url: (conversation_id) => `/conversations/${conversation_id}`,
  31. },
  32. deleteConversation: {
  33. method: "DELETE",
  34. url: (conversation_id) => `/conversations/${conversation_id}`,
  35. },
  36. };
  37. export class DifyClient {
  38. constructor(apiKey, baseUrl = BASE_URL) {
  39. this.apiKey = apiKey;
  40. this.baseUrl = baseUrl;
  41. }
  42. updateApiKey(apiKey) {
  43. this.apiKey = apiKey;
  44. }
  45. async sendRequest(
  46. method,
  47. endpoint,
  48. data = null,
  49. params = null,
  50. stream = false
  51. ) {
  52. const headers = {
  53. Authorization: `Bearer ${this.apiKey}`,
  54. "Content-Type": "application/json",
  55. };
  56. const url = `${this.baseUrl}${endpoint}`;
  57. let response;
  58. if (stream) {
  59. response = await axios({
  60. method,
  61. url,
  62. data,
  63. params,
  64. headers,
  65. responseType: "stream",
  66. });
  67. } else {
  68. response = await axios({
  69. method,
  70. url,
  71. data,
  72. params,
  73. headers,
  74. responseType: "json",
  75. });
  76. }
  77. return response;
  78. }
  79. messageFeedback(message_id, rating, user) {
  80. const data = {
  81. rating,
  82. user,
  83. };
  84. return this.sendRequest(
  85. routes.feedback.method,
  86. routes.feedback.url(message_id),
  87. data
  88. );
  89. }
  90. getApplicationParameters(user) {
  91. const params = { user };
  92. return this.sendRequest(
  93. routes.application.method,
  94. routes.application.url(),
  95. null,
  96. params
  97. );
  98. }
  99. }
  100. export class CompletionClient extends DifyClient {
  101. createCompletionMessage(inputs, query, user, stream = false) {
  102. const data = {
  103. inputs,
  104. query,
  105. user,
  106. response_mode: stream ? "streaming" : "blocking",
  107. };
  108. return this.sendRequest(
  109. routes.createCompletionMessage.method,
  110. routes.createCompletionMessage.url(),
  111. data,
  112. null,
  113. stream
  114. );
  115. }
  116. }
  117. export class ChatClient extends DifyClient {
  118. createChatMessage(
  119. inputs,
  120. query,
  121. user,
  122. stream = false,
  123. conversation_id = null
  124. ) {
  125. const data = {
  126. inputs,
  127. query,
  128. user,
  129. response_mode: stream ? "streaming" : "blocking",
  130. };
  131. if (conversation_id) data.conversation_id = conversation_id;
  132. return this.sendRequest(
  133. routes.createChatMessage.method,
  134. routes.createChatMessage.url(),
  135. data,
  136. null,
  137. stream
  138. );
  139. }
  140. getConversationMessages(
  141. user,
  142. conversation_id = "",
  143. first_id = null,
  144. limit = null
  145. ) {
  146. const params = { user };
  147. if (conversation_id) params.conversation_id = conversation_id;
  148. if (first_id) params.first_id = first_id;
  149. if (limit) params.limit = limit;
  150. return this.sendRequest(
  151. routes.getConversationMessages.method,
  152. routes.getConversationMessages.url(),
  153. null,
  154. params
  155. );
  156. }
  157. getConversations(user, first_id = null, limit = null, pinned = null) {
  158. const params = { user, first_id: first_id, limit, pinned };
  159. return this.sendRequest(
  160. routes.getConversations.method,
  161. routes.getConversations.url(),
  162. null,
  163. params
  164. );
  165. }
  166. renameConversation(conversation_id, name, user) {
  167. const data = { name, user };
  168. return this.sendRequest(
  169. routes.renameConversation.method,
  170. routes.renameConversation.url(conversation_id),
  171. data
  172. );
  173. }
  174. deleteConversation(conversation_id, user) {
  175. const data = { user };
  176. return this.sendRequest(
  177. routes.deleteConversation.method,
  178. routes.deleteConversation.url(conversation_id),
  179. data
  180. );
  181. }
  182. }