您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. from ragflow_sdk import RAGFlow
  2. from common import HOST_ADDRESS
  3. from time import sleep
  4. import pytest
  5. def test_parse_document_with_txt(get_api_key_fixture):
  6. API_KEY = get_api_key_fixture
  7. rag = RAGFlow(API_KEY, HOST_ADDRESS)
  8. ds = rag.create_dataset(name="test_parse_document")
  9. name = 'ragflow_test.txt'
  10. with open("test_data/ragflow_test.txt","rb") as file :
  11. blob = file.read()
  12. docs = ds.upload_documents([{"displayed_name": name, "blob": blob}])
  13. doc = docs[0]
  14. ds.async_parse_documents(document_ids=[doc.id])
  15. '''
  16. for n in range(100):
  17. if doc.progress == 1:
  18. break
  19. sleep(1)
  20. else:
  21. raise Exception("Run time ERROR: Document parsing did not complete in time.")
  22. '''
  23. def test_parse_and_cancel_document(get_api_key_fixture):
  24. API_KEY = get_api_key_fixture
  25. rag = RAGFlow(API_KEY, HOST_ADDRESS)
  26. ds = rag.create_dataset(name="test_parse_and_cancel_document")
  27. name = 'ragflow_test.txt'
  28. with open("test_data/ragflow_test.txt","rb") as file :
  29. blob = file.read()
  30. docs=ds.upload_documents([{"displayed_name": name, "blob": blob}])
  31. doc = docs[0]
  32. ds.async_parse_documents(document_ids=[doc.id])
  33. sleep(1)
  34. if 0 < doc.progress < 1:
  35. ds.async_cancel_parse_documents(document_ids=[doc.id])
  36. def test_bulk_parse_documents(get_api_key_fixture):
  37. API_KEY = get_api_key_fixture
  38. rag = RAGFlow(API_KEY, HOST_ADDRESS)
  39. ds = rag.create_dataset(name="test_bulk_parse_and_cancel_documents")
  40. with open("ragflow.txt","rb") as file:
  41. blob = file.read()
  42. documents = [
  43. {'displayed_name': 'test1.txt', 'blob': blob},
  44. {'displayed_name': 'test2.txt', 'blob': blob},
  45. {'displayed_name': 'test3.txt', 'blob': blob}
  46. ]
  47. docs = ds.upload_documents(documents)
  48. ids = [doc.id for doc in docs]
  49. ds.async_parse_documents(ids)
  50. '''
  51. for n in range(100):
  52. all_completed = all(doc.progress == 1 for doc in docs)
  53. if all_completed:
  54. break
  55. sleep(1)
  56. else:
  57. raise Exception("Run time ERROR: Bulk document parsing did not complete in time.")
  58. '''
  59. @pytest.mark.skip(reason="DocumentService.get_list() expects page and page_size")
  60. def test_list_chunks_with_success(get_api_key_fixture):
  61. API_KEY = get_api_key_fixture
  62. rag = RAGFlow(API_KEY, HOST_ADDRESS)
  63. ds = rag.create_dataset(name="test_list_chunks_with_success")
  64. with open("test_data/ragflow_test.txt", "rb") as file:
  65. blob = file.read()
  66. '''
  67. # chunk_size = 1024 * 1024
  68. # chunks = [blob[i:i + chunk_size] for i in range(0, len(blob), chunk_size)]
  69. documents = [
  70. {'displayed_name': f'chunk_{i}.txt', 'blob': chunk} for i, chunk in enumerate(chunks)
  71. ]
  72. '''
  73. documents =[{"displayed_name":"test_list_chunks_with_success.txt","blob":blob}]
  74. docs = ds.upload_documents(documents)
  75. ids = [doc.id for doc in docs]
  76. ds.async_parse_documents(ids)
  77. '''
  78. for n in range(100):
  79. all_completed = all(doc.progress == 1 for doc in docs)
  80. if all_completed:
  81. break
  82. sleep(1)
  83. else:
  84. raise Exception("Run time ERROR: Chunk document parsing did not complete in time.")
  85. '''
  86. doc = docs[0]
  87. doc.list_chunks()
  88. def test_add_chunk_with_success(get_api_key_fixture):
  89. API_KEY = get_api_key_fixture
  90. rag = RAGFlow(API_KEY, HOST_ADDRESS)
  91. ds = rag.create_dataset(name="test_add_chunk_with_success")
  92. with open("test_data/ragflow_test.txt", "rb") as file:
  93. blob = file.read()
  94. '''
  95. # chunk_size = 1024 * 1024
  96. # chunks = [blob[i:i + chunk_size] for i in range(0, len(blob), chunk_size)]
  97. documents = [
  98. {'displayed_name': f'chunk_{i}.txt', 'blob': chunk} for i, chunk in enumerate(chunks)
  99. ]
  100. '''
  101. documents =[{"displayed_name":"test_list_chunks_with_success.txt","blob":blob}]
  102. docs = ds.upload_documents(documents)
  103. doc = docs[0]
  104. doc.add_chunk(content="This is a chunk addition test")
  105. @pytest.mark.skip(reason="docs[0] is None")
  106. def test_delete_chunk_with_success(get_api_key_fixture):
  107. API_KEY = get_api_key_fixture
  108. rag = RAGFlow(API_KEY, HOST_ADDRESS)
  109. ds = rag.create_dataset(name="test_delete_chunk_with_success")
  110. with open("test_data/ragflow_test.txt", "rb") as file:
  111. blob = file.read()
  112. '''
  113. # chunk_size = 1024 * 1024
  114. # chunks = [blob[i:i + chunk_size] for i in range(0, len(blob), chunk_size)]
  115. documents = [
  116. {'displayed_name': f'chunk_{i}.txt', 'blob': chunk} for i, chunk in enumerate(chunks)
  117. ]
  118. '''
  119. documents =[{"displayed_name":"test_list_chunks_with_success.txt","blob":blob}]
  120. docs = ds.upload_documents(documents)
  121. doc = docs[0]
  122. chunk = doc.add_chunk(content="This is a chunk addition test")
  123. doc.delete_chunks([chunk.id])
  124. def test_update_chunk_content(get_api_key_fixture):
  125. API_KEY = get_api_key_fixture
  126. rag = RAGFlow(API_KEY, HOST_ADDRESS)
  127. ds = rag.create_dataset(name="test_update_chunk_content_with_success")
  128. with open("test_data/ragflow_test.txt", "rb") as file:
  129. blob = file.read()
  130. '''
  131. # chunk_size = 1024 * 1024
  132. # chunks = [blob[i:i + chunk_size] for i in range(0, len(blob), chunk_size)]
  133. documents = [
  134. {'displayed_name': f'chunk_{i}.txt', 'blob': chunk} for i, chunk in enumerate(chunks)
  135. ]
  136. '''
  137. documents =[{"displayed_name":"test_update_chunk_content_with_success.txt","blob":blob}]
  138. docs = ds.upload_documents(documents)
  139. doc = docs[0]
  140. chunk = doc.add_chunk(content="This is a chunk addition test")
  141. chunk.update({"content":"This is a updated content"})
  142. def test_update_chunk_available(get_api_key_fixture):
  143. API_KEY = get_api_key_fixture
  144. rag = RAGFlow(API_KEY, HOST_ADDRESS)
  145. ds = rag.create_dataset(name="test_update_chunk_available_with_success")
  146. with open("test_data/ragflow_test.txt", "rb") as file:
  147. blob = file.read()
  148. '''
  149. # chunk_size = 1024 * 1024
  150. # chunks = [blob[i:i + chunk_size] for i in range(0, len(blob), chunk_size)]
  151. documents = [
  152. {'displayed_name': f'chunk_{i}.txt', 'blob': chunk} for i, chunk in enumerate(chunks)
  153. ]
  154. '''
  155. documents =[{"displayed_name":"test_update_chunk_available_with_success.txt","blob":blob}]
  156. docs = ds.upload_documents(documents)
  157. doc = docs[0]
  158. chunk = doc.add_chunk(content="This is a chunk addition test")
  159. chunk.update({"available":False})
  160. def test_retrieve_chunks(get_api_key_fixture):
  161. API_KEY = get_api_key_fixture
  162. rag = RAGFlow(API_KEY, HOST_ADDRESS)
  163. ds = rag.create_dataset(name="retrieval")
  164. with open("test_data/ragflow_test.txt", "rb") as file:
  165. blob = file.read()
  166. '''
  167. # chunk_size = 1024 * 1024
  168. # chunks = [blob[i:i + chunk_size] for i in range(0, len(blob), chunk_size)]
  169. documents = [
  170. {'displayed_name': f'chunk_{i}.txt', 'blob': chunk} for i, chunk in enumerate(chunks)
  171. ]
  172. '''
  173. documents =[{"displayed_name":"test_retrieve_chunks.txt","blob":blob}]
  174. docs = ds.upload_documents(documents)
  175. doc = docs[0]
  176. doc.add_chunk(content="This is a chunk addition test")
  177. rag.retrieve(dataset_ids=[ds.id],document_ids=[doc.id])