Pankaj Kaushal commited on
Commit
b22ee5e
·
1 Parent(s): fbf52be

Remove LlamaIndex implementation from llm directory as per @MdNazishArmanShorthillsAI

Browse files

- Deleted `lightrag/llm/llama_index_impl.py`
- Reorganization of the LlamaIndex wrapper location

Files changed (1) hide show
  1. lightrag/llm/llama_index_impl.py +0 -214
lightrag/llm/llama_index_impl.py DELETED
@@ -1,214 +0,0 @@
1
- import pipmaster as pm
2
- from core.logging_config import setup_logger
3
- from llama_index.core.llms import (
4
- ChatMessage,
5
- MessageRole,
6
- ChatResponse,
7
- )
8
- from typing import List, Optional
9
-
10
- # Install required dependencies
11
- if not pm.is_installed("llama-index"):
12
- pm.install("llama-index")
13
-
14
- from llama_index.core.embeddings import BaseEmbedding
15
- from llama_index.core.settings import Settings as LlamaIndexSettings
16
- from tenacity import (
17
- retry,
18
- stop_after_attempt,
19
- wait_exponential,
20
- retry_if_exception_type,
21
- )
22
- from lightrag.utils import (
23
- wrap_embedding_func_with_attrs,
24
- locate_json_string_body_from_string,
25
- )
26
- from lightrag.exceptions import (
27
- APIConnectionError,
28
- RateLimitError,
29
- APITimeoutError,
30
- )
31
- import numpy as np
32
-
33
- logger = setup_logger("lightrag.llm.llama_index_impl")
34
-
35
-
36
- def configure_llama_index(settings: LlamaIndexSettings = None, **kwargs):
37
- """
38
- Configure LlamaIndex settings.
39
-
40
- Args:
41
- settings: LlamaIndex Settings instance. If None, uses default settings.
42
- **kwargs: Additional settings to override/configure
43
- """
44
- if settings is None:
45
- settings = LlamaIndexSettings()
46
-
47
- # Update settings with any provided kwargs
48
- for key, value in kwargs.items():
49
- if hasattr(settings, key):
50
- setattr(settings, key, value)
51
- else:
52
- logger.warning(f"Unknown LlamaIndex setting: {key}")
53
-
54
- # Set as global settings
55
- LlamaIndexSettings.set_global(settings)
56
- return settings
57
-
58
-
59
- def format_chat_messages(messages):
60
- """Format chat messages into LlamaIndex format."""
61
- formatted_messages = []
62
-
63
- for msg in messages:
64
- role = msg.get("role", "user")
65
- content = msg.get("content", "")
66
-
67
- if role == "system":
68
- formatted_messages.append(
69
- ChatMessage(role=MessageRole.SYSTEM, content=content)
70
- )
71
- elif role == "assistant":
72
- formatted_messages.append(
73
- ChatMessage(role=MessageRole.ASSISTANT, content=content)
74
- )
75
- elif role == "user":
76
- formatted_messages.append(
77
- ChatMessage(role=MessageRole.USER, content=content)
78
- )
79
- else:
80
- logger.warning(f"Unknown role {role}, treating as user message")
81
- formatted_messages.append(
82
- ChatMessage(role=MessageRole.USER, content=content)
83
- )
84
-
85
- return formatted_messages
86
-
87
-
88
- @retry(
89
- stop=stop_after_attempt(3),
90
- wait=wait_exponential(multiplier=1, min=4, max=60),
91
- retry=retry_if_exception_type(
92
- (RateLimitError, APIConnectionError, APITimeoutError)
93
- ),
94
- )
95
- async def llama_index_complete_if_cache(
96
- model: str,
97
- prompt: str,
98
- system_prompt: Optional[str] = None,
99
- history_messages: List[dict] = [],
100
- **kwargs,
101
- ) -> str:
102
- """Complete the prompt using LlamaIndex."""
103
- try:
104
- # Format messages for chat
105
- formatted_messages = []
106
-
107
- # Add system message if provided
108
- if system_prompt:
109
- formatted_messages.append(
110
- ChatMessage(role=MessageRole.SYSTEM, content=system_prompt)
111
- )
112
-
113
- # Add history messages
114
- for msg in history_messages:
115
- formatted_messages.append(
116
- ChatMessage(
117
- role=MessageRole.USER
118
- if msg["role"] == "user"
119
- else MessageRole.ASSISTANT,
120
- content=msg["content"],
121
- )
122
- )
123
-
124
- # Add current prompt
125
- formatted_messages.append(ChatMessage(role=MessageRole.USER, content=prompt))
126
-
127
- # Get LLM instance from kwargs
128
- if "llm_instance" not in kwargs:
129
- raise ValueError("llm_instance must be provided in kwargs")
130
- llm = kwargs["llm_instance"]
131
-
132
- # Get response
133
- response: ChatResponse = await llm.achat(messages=formatted_messages)
134
-
135
- # In newer versions, the response is in message.content
136
- content = response.message.content
137
- return content
138
-
139
- except Exception as e:
140
- logger.error(f"Error in llama_index_complete_if_cache: {str(e)}")
141
- raise
142
-
143
-
144
- async def llama_index_complete(
145
- prompt,
146
- system_prompt=None,
147
- history_messages=None,
148
- keyword_extraction=False,
149
- settings: LlamaIndexSettings = None,
150
- **kwargs,
151
- ) -> str:
152
- """
153
- Main completion function for LlamaIndex
154
-
155
- Args:
156
- prompt: Input prompt
157
- system_prompt: Optional system prompt
158
- history_messages: Optional chat history
159
- keyword_extraction: Whether to extract keywords from response
160
- settings: Optional LlamaIndex settings
161
- **kwargs: Additional arguments
162
- """
163
- if history_messages is None:
164
- history_messages = []
165
-
166
- keyword_extraction = kwargs.pop("keyword_extraction", None)
167
- result = await llama_index_complete_if_cache(
168
- kwargs.get("llm_instance"),
169
- prompt,
170
- system_prompt=system_prompt,
171
- history_messages=history_messages,
172
- **kwargs,
173
- )
174
- if keyword_extraction:
175
- return locate_json_string_body_from_string(result)
176
- return result
177
-
178
-
179
- @wrap_embedding_func_with_attrs(embedding_dim=1536, max_token_size=8192)
180
- @retry(
181
- stop=stop_after_attempt(3),
182
- wait=wait_exponential(multiplier=1, min=4, max=60),
183
- retry=retry_if_exception_type(
184
- (RateLimitError, APIConnectionError, APITimeoutError)
185
- ),
186
- )
187
- async def llama_index_embed(
188
- texts: list[str],
189
- embed_model: BaseEmbedding = None,
190
- settings: LlamaIndexSettings = None,
191
- **kwargs,
192
- ) -> np.ndarray:
193
- """
194
- Generate embeddings using LlamaIndex
195
-
196
- Args:
197
- texts: List of texts to embed
198
- embed_model: LlamaIndex embedding model
199
- settings: Optional LlamaIndex settings
200
- **kwargs: Additional arguments
201
- """
202
- if settings:
203
- configure_llama_index(settings)
204
-
205
- if embed_model is None:
206
- raise ValueError("embed_model must be provided")
207
-
208
- # LlamaIndex's embed_query returns a list of floats
209
- embeddings = []
210
- for text in texts:
211
- embedding = await embed_model.aembed_query(text)
212
- embeddings.append(embedding)
213
-
214
- return np.array(embeddings)