You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 5.2KB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. # dify-client
  2. A Dify App Service-API Client, using for build a webapp by request Service-API
  3. ## Usage
  4. First, install `dify-client` python sdk package:
  5. ```
  6. pip install dify-client
  7. ```
  8. Write your code with sdk:
  9. - completion generate with `blocking` response_mode
  10. ```python
  11. from dify_client import CompletionClient
  12. api_key = "your_api_key"
  13. # Initialize CompletionClient
  14. completion_client = CompletionClient(api_key)
  15. # Create Completion Message using CompletionClient
  16. completion_response = completion_client.create_completion_message(inputs={"query": "What's the weather like today?"},
  17. response_mode="blocking", user="user_id")
  18. completion_response.raise_for_status()
  19. result = completion_response.json()
  20. print(result.get('answer'))
  21. ```
  22. - completion using vision model, like gpt-4-vision
  23. ```python
  24. from dify_client import CompletionClient
  25. api_key = "your_api_key"
  26. # Initialize CompletionClient
  27. completion_client = CompletionClient(api_key)
  28. files = [{
  29. "type": "image",
  30. "transfer_method": "remote_url",
  31. "url": "your_image_url"
  32. }]
  33. # files = [{
  34. # "type": "image",
  35. # "transfer_method": "local_file",
  36. # "upload_file_id": "your_file_id"
  37. # }]
  38. # Create Completion Message using CompletionClient
  39. completion_response = completion_client.create_completion_message(inputs={"query": "Describe the picture."},
  40. response_mode="blocking", user="user_id", files=files)
  41. completion_response.raise_for_status()
  42. result = completion_response.json()
  43. print(result.get('answer'))
  44. ```
  45. - chat generate with `streaming` response_mode
  46. ```python
  47. import json
  48. from dify_client import ChatClient
  49. api_key = "your_api_key"
  50. # Initialize ChatClient
  51. chat_client = ChatClient(api_key)
  52. # Create Chat Message using ChatClient
  53. chat_response = chat_client.create_chat_message(inputs={}, query="Hello", user="user_id", response_mode="streaming")
  54. chat_response.raise_for_status()
  55. for line in chat_response.iter_lines(decode_unicode=True):
  56. line = line.split('data:', 1)[-1]
  57. if line.strip():
  58. line = json.loads(line.strip())
  59. print(line.get('answer'))
  60. ```
  61. - chat using vision model, like gpt-4-vision
  62. ```python
  63. from dify_client import ChatClient
  64. api_key = "your_api_key"
  65. # Initialize ChatClient
  66. chat_client = ChatClient(api_key)
  67. files = [{
  68. "type": "image",
  69. "transfer_method": "remote_url",
  70. "url": "your_image_url"
  71. }]
  72. # files = [{
  73. # "type": "image",
  74. # "transfer_method": "local_file",
  75. # "upload_file_id": "your_file_id"
  76. # }]
  77. # Create Chat Message using ChatClient
  78. chat_response = chat_client.create_chat_message(inputs={}, query="Describe the picture.", user="user_id",
  79. response_mode="blocking", files=files)
  80. chat_response.raise_for_status()
  81. result = chat_response.json()
  82. print(result.get("answer"))
  83. ```
  84. - upload file when using vision model
  85. ```python
  86. from dify_client import DifyClient
  87. api_key = "your_api_key"
  88. # Initialize Client
  89. dify_client = DifyClient(api_key)
  90. file_path = "your_image_file_path"
  91. file_name = "panda.jpeg"
  92. mime_type = "image/jpeg"
  93. with open(file_path, "rb") as file:
  94. files = {
  95. "file": (file_name, file, mime_type)
  96. }
  97. response = dify_client.file_upload("user_id", files)
  98. result = response.json()
  99. print(f'upload_file_id: {result.get("id")}')
  100. ```
  101. - Others
  102. ```python
  103. from dify_client import ChatClient
  104. api_key = "your_api_key"
  105. # Initialize Client
  106. client = ChatClient(api_key)
  107. # Get App parameters
  108. parameters = client.get_application_parameters(user="user_id")
  109. parameters.raise_for_status()
  110. print('[parameters]')
  111. print(parameters.json())
  112. # Get Conversation List (only for chat)
  113. conversations = client.get_conversations(user="user_id")
  114. conversations.raise_for_status()
  115. print('[conversations]')
  116. print(conversations.json())
  117. # Get Message List (only for chat)
  118. messages = client.get_conversation_messages(user="user_id", conversation_id="conversation_id")
  119. messages.raise_for_status()
  120. print('[messages]')
  121. print(messages.json())
  122. # Rename Conversation (only for chat)
  123. rename_conversation_response = client.rename_conversation(conversation_id="conversation_id",
  124. name="new_name", user="user_id")
  125. rename_conversation_response.raise_for_status()
  126. print('[rename result]')
  127. print(rename_conversation_response.json())
  128. ```
  129. * Using the Workflow Client
  130. ```python
  131. import json
  132. import requests
  133. from dify_client import WorkflowClient
  134. api_key = "your_api_key"
  135. # Initialize Workflow Client
  136. client = WorkflowClient(api_key)
  137. # Prepare parameters for Workflow Client
  138. user_id = "your_user_id"
  139. context = "previous user interaction / metadata"
  140. user_prompt = "What is the capital of France?"
  141. inputs = {
  142. "context": context,
  143. "user_prompt": user_prompt,
  144. # Add other input fields expected by your workflow (e.g., additional context, task parameters)
  145. }
  146. # Set response mode (default: streaming)
  147. response_mode = "blocking"
  148. # Run the workflow
  149. response = client.run(inputs=inputs, response_mode=response_mode, user=user_id)
  150. response.raise_for_status()
  151. # Parse result
  152. result = json.loads(response.text)
  153. answer = result.get("data").get("outputs")
  154. print(answer["answer"])
  155. ```