index.js 3.7 KB

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