alazarchuk commited on
Commit
3aa449a
·
1 Parent(s): 3d203c4

Add ability to passadditional parameters to ollama library like host and timeout

Browse files
.gitignore ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ env/
12
+ venv/
13
+ ENV/
14
+ env.bak/
15
+ venv.bak/
16
+ *.egg
17
+ *.egg-info/
18
+ dist/
19
+ build/
20
+ *.whl
21
+
22
+ # PyInstaller
23
+ # Usually these files are written by a python script from a template
24
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
25
+ *.manifest
26
+ *.spec
27
+
28
+ # Installer logs
29
+ pip-log.txt
30
+ pip-delete-this-directory.txt
31
+
32
+ # Unit test / coverage reports
33
+ htmlcov/
34
+ .tox/
35
+ .nox/
36
+ .coverage
37
+ .coverage.*
38
+ .cache
39
+ nosetests.xml
40
+ coverage.xml
41
+ *.cover
42
+ *.py,cover
43
+ .hypothesis/
44
+
45
+ # Translations
46
+ *.mo
47
+ *.pot
48
+
49
+ # Django stuff:
50
+ *.log
51
+ local_settings.py
52
+ db.sqlite3
53
+ db.sqlite3-journal
54
+
55
+ # Flask stuff:
56
+ instance/
57
+ .webassets-cache
58
+
59
+ # Scrapy stuff:
60
+ .scrapy
61
+
62
+ # Sphinx documentation
63
+ docs/_build/
64
+
65
+ # PyBuilder
66
+ target/
67
+
68
+ # Jupyter Notebook
69
+ .ipynb_checkpoints
70
+
71
+ # IPython
72
+ profile_default/
73
+ ipython_config.py
74
+
75
+ # pyenv
76
+ .python-version
77
+
78
+ # celery beat schedule file
79
+ celerybeat-schedule
80
+
81
+ # SageMath parsed files
82
+ *.sage.py
83
+
84
+ # Environments
85
+ .env
86
+ .env.*
87
+ .venv
88
+ .venv.*
89
+ env/
90
+ venv/
91
+ ENV/
92
+ env.bak/
93
+ venv.bak/
94
+
95
+ # Spyder project settings
96
+ .spyderproject
97
+ .spyderworkspace
98
+
99
+ # Rope project settings
100
+ .ropeproject
101
+
102
+ # mkdocs documentation
103
+ /site
104
+
105
+ # mypy
106
+ .mypy_cache/
107
+ .dmypy.json
108
+ dmypy.json
109
+
110
+ # Pyre type checker
111
+ .pyre/
112
+
113
+ # pytype static type analyzer
114
+ .pytype/
115
+
116
+ # Cython debug symbols
117
+ cython_debug/
118
+
119
+ # Example files
120
+ book.txt
121
+ dickens/
examples/lightrag_ollama_demo.py CHANGED
@@ -1,4 +1,7 @@
1
  import os
 
 
 
2
 
3
  from lightrag import LightRAG, QueryParam
4
  from lightrag.llm import ollama_model_complete, ollama_embedding
@@ -11,15 +14,17 @@ if not os.path.exists(WORKING_DIR):
11
 
12
  rag = LightRAG(
13
  working_dir=WORKING_DIR,
14
- llm_model_func=ollama_model_complete,
15
- llm_model_name='your_model_name',
 
 
 
16
  embedding_func=EmbeddingFunc(
17
  embedding_dim=768,
18
  max_token_size=8192,
19
  func=lambda texts: ollama_embedding(
20
- texts,
21
- embed_model="nomic-embed-text"
22
- )
23
  ),
24
  )
25
 
@@ -28,13 +33,21 @@ with open("./book.txt") as f:
28
  rag.insert(f.read())
29
 
30
  # Perform naive search
31
- print(rag.query("What are the top themes in this story?", param=QueryParam(mode="naive")))
 
 
32
 
33
  # Perform local search
34
- print(rag.query("What are the top themes in this story?", param=QueryParam(mode="local")))
 
 
35
 
36
  # Perform global search
37
- print(rag.query("What are the top themes in this story?", param=QueryParam(mode="global")))
 
 
38
 
39
  # Perform hybrid search
40
- print(rag.query("What are the top themes in this story?", param=QueryParam(mode="hybrid")))
 
 
 
1
  import os
2
+ import logging
3
+
4
+ logging.basicConfig(format="%(levelname)s:%(message)s", level=logging.DEBUG)
5
 
6
  from lightrag import LightRAG, QueryParam
7
  from lightrag.llm import ollama_model_complete, ollama_embedding
 
14
 
15
  rag = LightRAG(
16
  working_dir=WORKING_DIR,
17
+ tiktoken_model_name="mistral:7b",
18
+ llm_model_func=ollama_model_complete,
19
+ llm_model_name="mistral:7b",
20
+ llm_model_max_async=2,
21
+ llm_model_kwargs={"host": "http://localhost:11434"},
22
  embedding_func=EmbeddingFunc(
23
  embedding_dim=768,
24
  max_token_size=8192,
25
  func=lambda texts: ollama_embedding(
26
+ texts, embed_model="nomic-embed-text", host="http://localhost:11434"
27
+ ),
 
28
  ),
29
  )
30
 
 
33
  rag.insert(f.read())
34
 
35
  # Perform naive search
36
+ print(
37
+ rag.query("What are the top themes in this story?", param=QueryParam(mode="naive"))
38
+ )
39
 
40
  # Perform local search
41
+ print(
42
+ rag.query("What are the top themes in this story?", param=QueryParam(mode="local"))
43
+ )
44
 
45
  # Perform global search
46
+ print(
47
+ rag.query("What are the top themes in this story?", param=QueryParam(mode="global"))
48
+ )
49
 
50
  # Perform hybrid search
51
+ print(
52
+ rag.query("What are the top themes in this story?", param=QueryParam(mode="hybrid"))
53
+ )
lightrag/lightrag.py CHANGED
@@ -86,6 +86,7 @@ class LightRAG:
86
  llm_model_name: str = 'meta-llama/Llama-3.2-1B-Instruct'#'meta-llama/Llama-3.2-1B'#'google/gemma-2-2b-it'
87
  llm_model_max_token_size: int = 32768
88
  llm_model_max_async: int = 16
 
89
 
90
  # storage
91
  key_string_value_json_storage_cls: Type[BaseKVStorage] = JsonKVStorage
@@ -158,7 +159,7 @@ class LightRAG:
158
  )
159
 
160
  self.llm_model_func = limit_async_func_call(self.llm_model_max_async)(
161
- partial(self.llm_model_func, hashing_kv=self.llm_response_cache)
162
  )
163
 
164
  def insert(self, string_or_strings):
 
86
  llm_model_name: str = 'meta-llama/Llama-3.2-1B-Instruct'#'meta-llama/Llama-3.2-1B'#'google/gemma-2-2b-it'
87
  llm_model_max_token_size: int = 32768
88
  llm_model_max_async: int = 16
89
+ llm_model_kwargs: dict = field(default_factory=dict)
90
 
91
  # storage
92
  key_string_value_json_storage_cls: Type[BaseKVStorage] = JsonKVStorage
 
159
  )
160
 
161
  self.llm_model_func = limit_async_func_call(self.llm_model_max_async)(
162
+ partial(self.llm_model_func, hashing_kv=self.llm_response_cache, **self.llm_model_kwargs)
163
  )
164
 
165
  def insert(self, string_or_strings):
lightrag/llm.py CHANGED
@@ -98,8 +98,10 @@ async def ollama_model_if_cache(
98
  ) -> str:
99
  kwargs.pop("max_tokens", None)
100
  kwargs.pop("response_format", None)
 
 
101
 
102
- ollama_client = ollama.AsyncClient()
103
  messages = []
104
  if system_prompt:
105
  messages.append({"role": "system", "content": system_prompt})
@@ -193,10 +195,11 @@ async def hf_embedding(texts: list[str], tokenizer, embed_model) -> np.ndarray:
193
  embeddings = outputs.last_hidden_state.mean(dim=1)
194
  return embeddings.detach().numpy()
195
 
196
- async def ollama_embedding(texts: list[str], embed_model) -> np.ndarray:
197
  embed_text = []
 
198
  for text in texts:
199
- data = ollama.embeddings(model=embed_model, prompt=text)
200
  embed_text.append(data["embedding"])
201
 
202
  return embed_text
 
98
  ) -> str:
99
  kwargs.pop("max_tokens", None)
100
  kwargs.pop("response_format", None)
101
+ host = kwargs.pop("host", None)
102
+ timeout = kwargs.pop("timeout", None)
103
 
104
+ ollama_client = ollama.AsyncClient(host=host, timeout=timeout)
105
  messages = []
106
  if system_prompt:
107
  messages.append({"role": "system", "content": system_prompt})
 
195
  embeddings = outputs.last_hidden_state.mean(dim=1)
196
  return embeddings.detach().numpy()
197
 
198
+ async def ollama_embedding(texts: list[str], embed_model, **kwargs) -> np.ndarray:
199
  embed_text = []
200
+ ollama_client = ollama.Client(**kwargs)
201
  for text in texts:
202
+ data = ollama_client.embeddings(model=embed_model, prompt=text)
203
  embed_text.append(data["embedding"])
204
 
205
  return embed_text