test_client.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import os
  2. import unittest
  3. from dify_client.client import ChatClient, CompletionClient, DifyClient
  4. API_KEY = os.environ.get("API_KEY")
  5. APP_ID = os.environ.get("APP_ID")
  6. class TestChatClient(unittest.TestCase):
  7. def setUp(self):
  8. self.chat_client = ChatClient(API_KEY)
  9. def test_create_chat_message(self):
  10. response = self.chat_client.create_chat_message({}, "Hello, World!", "test_user")
  11. self.assertIn("answer", response.text)
  12. def test_create_chat_message_with_vision_model_by_remote_url(self):
  13. files = [{
  14. "type": "image",
  15. "transfer_method": "remote_url",
  16. "url": "your_image_url"
  17. }]
  18. response = self.chat_client.create_chat_message({}, "Describe the picture.", "test_user", files=files)
  19. self.assertIn("answer", response.text)
  20. def test_create_chat_message_with_vision_model_by_local_file(self):
  21. files = [{
  22. "type": "image",
  23. "transfer_method": "local_file",
  24. "upload_file_id": "your_file_id"
  25. }]
  26. response = self.chat_client.create_chat_message({}, "Describe the picture.", "test_user", files=files)
  27. self.assertIn("answer", response.text)
  28. def test_get_conversation_messages(self):
  29. response = self.chat_client.get_conversation_messages("test_user", "your_conversation_id")
  30. self.assertIn("answer", response.text)
  31. def test_get_conversations(self):
  32. response = self.chat_client.get_conversations("test_user")
  33. self.assertIn("data", response.text)
  34. class TestCompletionClient(unittest.TestCase):
  35. def setUp(self):
  36. self.completion_client = CompletionClient(API_KEY)
  37. def test_create_completion_message(self):
  38. response = self.completion_client.create_completion_message({"query": "What's the weather like today?"},
  39. "blocking", "test_user")
  40. self.assertIn("answer", response.text)
  41. def test_create_completion_message_with_vision_model_by_remote_url(self):
  42. files = [{
  43. "type": "image",
  44. "transfer_method": "remote_url",
  45. "url": "your_image_url"
  46. }]
  47. response = self.completion_client.create_completion_message(
  48. {"query": "Describe the picture."}, "blocking", "test_user", files)
  49. self.assertIn("answer", response.text)
  50. def test_create_completion_message_with_vision_model_by_local_file(self):
  51. files = [{
  52. "type": "image",
  53. "transfer_method": "local_file",
  54. "upload_file_id": "your_file_id"
  55. }]
  56. response = self.completion_client.create_completion_message(
  57. {"query": "Describe the picture."}, "blocking", "test_user", files)
  58. self.assertIn("answer", response.text)
  59. class TestDifyClient(unittest.TestCase):
  60. def setUp(self):
  61. self.dify_client = DifyClient(API_KEY)
  62. def test_message_feedback(self):
  63. response = self.dify_client.message_feedback("your_message_id", 'like', "test_user")
  64. self.assertIn("success", response.text)
  65. def test_get_application_parameters(self):
  66. response = self.dify_client.get_application_parameters("test_user")
  67. self.assertIn("user_input_form", response.text)
  68. def test_file_upload(self):
  69. file_path = "your_image_file_path"
  70. file_name = "panda.jpeg"
  71. mime_type = "image/jpeg"
  72. with open(file_path, "rb") as file:
  73. files = {
  74. "file": (file_name, file, mime_type)
  75. }
  76. response = self.dify_client.file_upload("test_user", files)
  77. self.assertIn("name", response.text)
  78. if __name__ == "__main__":
  79. unittest.main()