lightrag_lmdeploy_demo.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import os
  2. from lightrag import LightRAG, QueryParam
  3. from lightrag.llm.lmdeploy import lmdeploy_model_if_cache
  4. from lightrag.llm.hf import hf_embed
  5. from lightrag.utils import EmbeddingFunc
  6. from transformers import AutoModel, AutoTokenizer
  7. import asyncio
  8. import nest_asyncio
  9. nest_asyncio.apply()
  10. WORKING_DIR = "./dickens"
  11. if not os.path.exists(WORKING_DIR):
  12. os.mkdir(WORKING_DIR)
  13. async def lmdeploy_model_complete(
  14. prompt=None,
  15. system_prompt=None,
  16. history_messages=[],
  17. keyword_extraction=False,
  18. **kwargs,
  19. ) -> str:
  20. model_name = kwargs["hashing_kv"].global_config["llm_model_name"]
  21. return await lmdeploy_model_if_cache(
  22. model_name,
  23. prompt,
  24. system_prompt=system_prompt,
  25. history_messages=history_messages,
  26. ## please specify chat_template if your local path does not follow original HF file name,
  27. ## or model_name is a pytorch model on huggingface.co,
  28. ## you can refer to https://github.com/InternLM/lmdeploy/blob/main/lmdeploy/model.py
  29. ## for a list of chat_template available in lmdeploy.
  30. chat_template="llama3",
  31. # model_format ='awq', # if you are using awq quantization model.
  32. # quant_policy=8, # if you want to use online kv cache, 4=kv int4, 8=kv int8.
  33. **kwargs,
  34. )
  35. async def initialize_rag():
  36. rag = LightRAG(
  37. working_dir=WORKING_DIR,
  38. llm_model_func=lmdeploy_model_complete,
  39. llm_model_name="meta-llama/Llama-3.1-8B-Instruct", # please use definite path for local model
  40. embedding_func=EmbeddingFunc(
  41. embedding_dim=384,
  42. max_token_size=5000,
  43. func=lambda texts: hf_embed(
  44. texts,
  45. tokenizer=AutoTokenizer.from_pretrained(
  46. "sentence-transformers/all-MiniLM-L6-v2"
  47. ),
  48. embed_model=AutoModel.from_pretrained(
  49. "sentence-transformers/all-MiniLM-L6-v2"
  50. ),
  51. ),
  52. ),
  53. )
  54. await rag.initialize_storages() # Auto-initializes pipeline_status
  55. return rag
  56. def main():
  57. # Initialize RAG instance
  58. rag = asyncio.run(initialize_rag())
  59. # Insert example text
  60. with open("./book.txt", "r", encoding="utf-8") as f:
  61. rag.insert(f.read())
  62. # Test different query modes
  63. print("\nNaive Search:")
  64. print(
  65. rag.query(
  66. "What are the top themes in this story?", param=QueryParam(mode="naive")
  67. )
  68. )
  69. print("\nLocal Search:")
  70. print(
  71. rag.query(
  72. "What are the top themes in this story?", param=QueryParam(mode="local")
  73. )
  74. )
  75. print("\nGlobal Search:")
  76. print(
  77. rag.query(
  78. "What are the top themes in this story?", param=QueryParam(mode="global")
  79. )
  80. )
  81. print("\nHybrid Search:")
  82. print(
  83. rag.query(
  84. "What are the top themes in this story?", param=QueryParam(mode="hybrid")
  85. )
  86. )
  87. if __name__ == "__main__":
  88. main()