-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature] SDK llama index 0.10.x migration (#19)
* Llama index migration changes * Service context deprecation * Remove service_context file * Fix for token usage * Add comments * Change signature * Remove Optional/None type * Avoid nesting * Update llala-index version --------- Signed-off-by: Gayathri <[email protected]>
- Loading branch information
1 parent
4fa116b
commit f2c9bf1
Showing
13 changed files
with
1,435 additions
and
1,369 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
__version__ = "0.18.2" | ||
__version__ = "0.19.0" | ||
|
||
|
||
def get_sdk_version(): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import logging | ||
from typing import Callable, Optional, Union | ||
|
||
import tiktoken | ||
from llama_index.core.callbacks import ( | ||
CallbackManager as LlamaIndexCallbackManager, | ||
) | ||
from llama_index.core.callbacks import TokenCountingHandler | ||
from llama_index.core.embeddings import BaseEmbedding | ||
from llama_index.core.llms import LLM | ||
from transformers import AutoTokenizer | ||
|
||
from unstract.sdk.utils.usage_handler import UsageHandler | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class CallbackManager: | ||
"""Class representing the CallbackManager to manage callbacks. | ||
Use this over the default service context of llama index | ||
This class supports a tokenizer, token counter, | ||
usage handler, and callback manager. | ||
Attributes: | ||
None | ||
Methods: | ||
set_callback_manager: Returns a standard callback manager | ||
Example: | ||
callback_manager = CallbackManager. | ||
set_callback_manager( | ||
llm="default", | ||
embedding="default") | ||
""" | ||
|
||
@staticmethod | ||
def set_callback_manager( | ||
platform_api_key: str, | ||
llm: Optional[LLM] = None, | ||
embedding: Optional[BaseEmbedding] = None, | ||
workflow_id: str = "", | ||
execution_id: str = "", | ||
) -> LlamaIndexCallbackManager: | ||
"""Sets the standard callback manager for the llm. This is to be called | ||
explicitly whenever there is a need for the callback handling defined | ||
here as handlers is to be invoked. | ||
Parameters: | ||
llm (LLM): The LLM type | ||
Returns: | ||
CallbackManager tyoe of llama index | ||
Example: | ||
UNCallbackManager.set_callback_manager( | ||
platform_api_key: "abc", | ||
llm=llm, | ||
embedding=embedding | ||
) | ||
""" | ||
|
||
if llm: | ||
tokenizer = CallbackManager.get_tokenizer(llm) | ||
elif embedding: | ||
tokenizer = CallbackManager.get_tokenizer(embedding) | ||
|
||
token_counter = TokenCountingHandler(tokenizer=tokenizer, verbose=True) | ||
usage_handler = UsageHandler( | ||
token_counter=token_counter, | ||
platform_api_key=platform_api_key, | ||
llm_model=llm, | ||
embed_model=embedding, | ||
workflow_id=workflow_id, | ||
execution_id=execution_id, | ||
) | ||
|
||
callback_manager: LlamaIndexCallbackManager = LlamaIndexCallbackManager( | ||
handlers=[token_counter, usage_handler] | ||
) | ||
|
||
if llm is not None: | ||
llm.callback_manager = callback_manager | ||
if embedding is not None: | ||
embedding.callback_manager = callback_manager | ||
|
||
return callback_manager | ||
|
||
@staticmethod | ||
def get_tokenizer( | ||
model: Optional[Union[LLM, BaseEmbedding, None]], | ||
fallback_tokenizer: Callable[[str], list] = tiktoken.encoding_for_model( | ||
"gpt-3.5-turbo" | ||
).encode, | ||
) -> Callable[[str], list]: | ||
"""Returns a tokenizer function based on the provided model. | ||
Args: | ||
model (Optional[Union[LLM, BaseEmbedding]]): The model to use for | ||
tokenization. | ||
Returns: | ||
Callable[[str], List]: The tokenizer function. | ||
Raises: | ||
OSError: If an error occurs while loading the tokenizer. | ||
""" | ||
|
||
try: | ||
if isinstance(model, LLM): | ||
model_name: str = model.metadata.model_name | ||
elif isinstance(model, BaseEmbedding): | ||
model_name = model.model_name | ||
|
||
tokenizer: Callable[[str], list] = AutoTokenizer.from_pretrained( | ||
model_name | ||
).encode | ||
return tokenizer | ||
except OSError as e: | ||
logger.warning(str(e)) | ||
return fallback_tokenizer |
Oops, something went wrong.