ollama_llama.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import os
  2. import logging
  3. from lightrag import LightRAG, QueryParam
  4. from lightrag.llm import ollama_model_complete, ollama_embedding
  5. from lightrag.utils import EmbeddingFunc
  6. import pdfplumber
  7. WORKING_DIR = "./Legal_Documents"
  8. logging.basicConfig(format="%(levelname)s:%(message)s", level=logging.INFO)
  9. if not os.path.exists(WORKING_DIR):
  10. os.mkdir(WORKING_DIR)
  11. rag = LightRAG(
  12. working_dir=WORKING_DIR,
  13. chunk_token_size=1200,
  14. llm_model_func=ollama_model_complete,
  15. llm_model_name="llama3.1:latest",
  16. llm_model_max_async=4,
  17. llm_model_max_token_size=32768,
  18. llm_model_kwargs={"host": "http://localhost:11434", "options": {"num_ctx": 32768}},
  19. embedding_func=EmbeddingFunc(
  20. embedding_dim=768,
  21. max_token_size=8192,
  22. func=lambda texts: ollama_embedding(texts, embed_model="nomic-embed-text", host="http://localhost:11434"),
  23. ),
  24. )
  25. pdf_path = "../Constituion-of-India.pdf"
  26. pdf_text = ""
  27. with pdfplumber.open(pdf_path) as pdf:
  28. for page in pdf.pages:
  29. pdf_text += page.extract_text() + "\n"
  30. # rag.insert(pdf_text)
  31. # Define output file path
  32. os.makedirs(
  33. os.path.join(WORKING_DIR, "../outputs/"),
  34. exist_ok=True,
  35. )
  36. output_file = os.path.join(WORKING_DIR, "../outputs/output_queries_2.txt")
  37. # Function to write results to file
  38. def write_to_file(output_text):
  39. with open(output_file, "a", encoding="utf-8") as file:
  40. file.write(output_text + "\n")
  41. # Perform searches and save results
  42. write_to_file("------------------------------------------Naive--------------------------------------------------------")
  43. write_to_file(
  44. rag.query(
  45. "What does companies act mean?",
  46. param=QueryParam(mode="naive"),
  47. )
  48. )
  49. print("\033[92mNaive - Done ✔\033[0m")
  50. write_to_file("------------------------------------------Local------------------------------------------------------")
  51. write_to_file(
  52. rag.query(
  53. "What does companies act mean?",
  54. param=QueryParam(mode="local"),
  55. )
  56. )
  57. print("\033[92mLocal - Done ✔\033[0m")
  58. write_to_file("------------------------------------------Global----------------------------------------------------")
  59. write_to_file(
  60. rag.query(
  61. "What does companies act mean?",
  62. param=QueryParam(mode="global"),
  63. )
  64. )
  65. print("\033[92mGlobal - Done ✔\033[0m")
  66. write_to_file("------------------------------------------Hybrid------------------------------------------------")
  67. write_to_file(
  68. rag.query(
  69. "What does companies act mean?",
  70. param=QueryParam(mode="hybrid"),
  71. )
  72. )
  73. print("\033[92mHybrid - Done ✔\033[0m")
  74. # Output confirmation with a green checkmark
  75. print("\033[92mAll Queries Completed ✔✔✔\033[0m")