Spaces:
Sleeping
Sleeping
NickNYU
commited on
Commit
·
de35b2d
1
Parent(s):
b404642
refactor
Browse files- langchain_manager/manager.py +5 -5
- llama/context.py +24 -44
- llama/data_loader.py +5 -5
- xpipe_wiki/manager_factory.py +1 -1
- xpipe_wiki/robot_manager.py +1 -1
langchain_manager/manager.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
from abc import abstractmethod, ABC
|
| 2 |
|
| 3 |
-
from
|
| 4 |
-
from
|
| 5 |
-
from
|
| 6 |
-
from
|
| 7 |
|
| 8 |
from core.lifecycle import Lifecycle
|
| 9 |
|
|
@@ -36,4 +36,4 @@ class LangChainAzureManager(BaseLangChainManager):
|
|
| 36 |
# model_name="text-davinci-003",
|
| 37 |
model="text-davinci-003",
|
| 38 |
client=None,
|
| 39 |
-
)
|
|
|
|
| 1 |
from abc import abstractmethod, ABC
|
| 2 |
|
| 3 |
+
from langchain.base_language import BaseLanguageModel
|
| 4 |
+
from langchain.embeddings.base import Embeddings as LCEmbeddings
|
| 5 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
| 6 |
+
from langchain.llms import AzureOpenAI
|
| 7 |
|
| 8 |
from core.lifecycle import Lifecycle
|
| 9 |
|
|
|
|
| 36 |
# model_name="text-davinci-003",
|
| 37 |
model="text-davinci-003",
|
| 38 |
client=None,
|
| 39 |
+
)
|
llama/context.py
CHANGED
|
@@ -5,10 +5,10 @@ from llama_index import StorageContext
|
|
| 5 |
|
| 6 |
from core.lifecycle import Lifecycle
|
| 7 |
from langchain_manager.manager import BaseLangChainManager
|
| 8 |
-
from typing import List
|
| 9 |
|
| 10 |
|
| 11 |
class ServiceContextManager(Lifecycle, ABC):
|
|
|
|
| 12 |
@abstractmethod
|
| 13 |
def get_service_context(self) -> ServiceContext:
|
| 14 |
pass
|
|
@@ -23,10 +23,6 @@ class AzureServiceContextManager(ServiceContextManager):
|
|
| 23 |
self.lc_manager = lc_manager
|
| 24 |
|
| 25 |
def get_service_context(self) -> ServiceContext:
|
| 26 |
-
if self.lifecycle_state.is_started():
|
| 27 |
-
raise KeyError(
|
| 28 |
-
"incorrect lifecycle state: {}".format(self.lifecycle_state.phase)
|
| 29 |
-
)
|
| 30 |
if self.service_context is None:
|
| 31 |
raise ValueError(
|
| 32 |
"service context is not ready, check for lifecycle statement"
|
|
@@ -44,33 +40,23 @@ class AzureServiceContextManager(ServiceContextManager):
|
|
| 44 |
)
|
| 45 |
|
| 46 |
def do_start(self) -> None:
|
| 47 |
-
self.logger.info(
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
self.logger.info(
|
| 52 |
-
"[do_start][predict] last used usage: %d",
|
| 53 |
-
self.service_context.llm_predictor.total_tokens_used,
|
| 54 |
-
)
|
| 55 |
|
| 56 |
def do_stop(self) -> None:
|
| 57 |
-
self.logger.info(
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
self.logger.info(
|
| 62 |
-
"[do_stop][predict] last used usage: %d",
|
| 63 |
-
self.service_context.llm_predictor.total_tokens_used,
|
| 64 |
-
)
|
| 65 |
|
| 66 |
def do_dispose(self) -> None:
|
| 67 |
-
self.logger.info(
|
| 68 |
-
"[do_dispose] total used token: %d",
|
| 69 |
-
self.service_context.llm_predictor.total_tokens_used,
|
| 70 |
-
)
|
| 71 |
|
| 72 |
|
| 73 |
class StorageContextManager(Lifecycle, ABC):
|
|
|
|
| 74 |
@abstractmethod
|
| 75 |
def get_storage_context(self) -> StorageContext:
|
| 76 |
pass
|
|
@@ -79,11 +65,9 @@ class StorageContextManager(Lifecycle, ABC):
|
|
| 79 |
class LocalStorageContextManager(StorageContextManager):
|
| 80 |
storage_context: StorageContext
|
| 81 |
|
| 82 |
-
def __init__(
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
dataset_path: str = "./dataset",
|
| 86 |
-
) -> None:
|
| 87 |
super().__init__()
|
| 88 |
self.dataset_path = dataset_path
|
| 89 |
self.service_context_manager = service_context_manager
|
|
@@ -93,35 +77,31 @@ class LocalStorageContextManager(StorageContextManager):
|
|
| 93 |
|
| 94 |
def do_init(self) -> None:
|
| 95 |
from llama.utils import is_local_storage_files_ready
|
| 96 |
-
|
| 97 |
if is_local_storage_files_ready(self.dataset_path):
|
| 98 |
-
self.storage_context = StorageContext.from_defaults(
|
| 99 |
-
persist_dir=self.dataset_path
|
| 100 |
-
)
|
| 101 |
else:
|
| 102 |
docs = self._download()
|
| 103 |
self._indexing(docs)
|
| 104 |
|
| 105 |
def do_start(self) -> None:
|
| 106 |
-
self.logger.info("[do_start]%", **self.storage_context.to_dict())
|
|
|
|
| 107 |
|
| 108 |
def do_stop(self) -> None:
|
| 109 |
-
self.logger.info("[do_stop]%", **self.storage_context.to_dict())
|
|
|
|
| 110 |
|
| 111 |
def do_dispose(self) -> None:
|
| 112 |
self.storage_context.persist(self.dataset_path)
|
| 113 |
|
| 114 |
-
def _download(self) ->
|
| 115 |
from llama.data_loader import GithubLoader
|
| 116 |
-
|
| 117 |
loader = GithubLoader()
|
| 118 |
return loader.load()
|
| 119 |
|
| 120 |
-
def _indexing(self, docs:
|
| 121 |
from llama_index import GPTVectorStoreIndex
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
docs, service_context=self.service_context_manager.get_service_context()
|
| 125 |
-
)
|
| 126 |
index.storage_context.persist(persist_dir=self.dataset_path)
|
| 127 |
-
self.storage_context = index.storage_context
|
|
|
|
| 5 |
|
| 6 |
from core.lifecycle import Lifecycle
|
| 7 |
from langchain_manager.manager import BaseLangChainManager
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
class ServiceContextManager(Lifecycle, ABC):
|
| 11 |
+
|
| 12 |
@abstractmethod
|
| 13 |
def get_service_context(self) -> ServiceContext:
|
| 14 |
pass
|
|
|
|
| 23 |
self.lc_manager = lc_manager
|
| 24 |
|
| 25 |
def get_service_context(self) -> ServiceContext:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
if self.service_context is None:
|
| 27 |
raise ValueError(
|
| 28 |
"service context is not ready, check for lifecycle statement"
|
|
|
|
| 40 |
)
|
| 41 |
|
| 42 |
def do_start(self) -> None:
|
| 43 |
+
self.logger.info("[do_start][embedding] last used usage: %d",
|
| 44 |
+
self.service_context.embed_model.total_tokens_used)
|
| 45 |
+
self.logger.info("[do_start][predict] last used usage: %d",
|
| 46 |
+
self.service_context.llm_predictor.total_tokens_used)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
def do_stop(self) -> None:
|
| 49 |
+
self.logger.info("[do_stop][embedding] last used usage: %d",
|
| 50 |
+
self.service_context.embed_model.total_tokens_used)
|
| 51 |
+
self.logger.info("[do_stop][predict] last used usage: %d",
|
| 52 |
+
self.service_context.llm_predictor.total_tokens_used)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
def do_dispose(self) -> None:
|
| 55 |
+
self.logger.info("[do_dispose] total used token: %d", self.service_context.llm_predictor.total_tokens_used)
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
|
| 58 |
class StorageContextManager(Lifecycle, ABC):
|
| 59 |
+
|
| 60 |
@abstractmethod
|
| 61 |
def get_storage_context(self) -> StorageContext:
|
| 62 |
pass
|
|
|
|
| 65 |
class LocalStorageContextManager(StorageContextManager):
|
| 66 |
storage_context: StorageContext
|
| 67 |
|
| 68 |
+
def __init__(self,
|
| 69 |
+
dataset_path: str = "./dataset",
|
| 70 |
+
service_context_manager: ServiceContextManager = None) -> None:
|
|
|
|
|
|
|
| 71 |
super().__init__()
|
| 72 |
self.dataset_path = dataset_path
|
| 73 |
self.service_context_manager = service_context_manager
|
|
|
|
| 77 |
|
| 78 |
def do_init(self) -> None:
|
| 79 |
from llama.utils import is_local_storage_files_ready
|
|
|
|
| 80 |
if is_local_storage_files_ready(self.dataset_path):
|
| 81 |
+
self.storage_context = StorageContext.from_defaults(persist_dir=self.dataset_path)
|
|
|
|
|
|
|
| 82 |
else:
|
| 83 |
docs = self._download()
|
| 84 |
self._indexing(docs)
|
| 85 |
|
| 86 |
def do_start(self) -> None:
|
| 87 |
+
# self.logger.info("[do_start]%", **self.storage_context.to_dict())
|
| 88 |
+
pass
|
| 89 |
|
| 90 |
def do_stop(self) -> None:
|
| 91 |
+
# self.logger.info("[do_stop]%", **self.storage_context.to_dict())
|
| 92 |
+
pass
|
| 93 |
|
| 94 |
def do_dispose(self) -> None:
|
| 95 |
self.storage_context.persist(self.dataset_path)
|
| 96 |
|
| 97 |
+
def _download(self) -> [Document]:
|
| 98 |
from llama.data_loader import GithubLoader
|
|
|
|
| 99 |
loader = GithubLoader()
|
| 100 |
return loader.load()
|
| 101 |
|
| 102 |
+
def _indexing(self, docs: [Document]):
|
| 103 |
from llama_index import GPTVectorStoreIndex
|
| 104 |
+
index = GPTVectorStoreIndex.from_documents(docs,
|
| 105 |
+
service_context=self.service_context_manager.get_service_context())
|
|
|
|
|
|
|
| 106 |
index.storage_context.persist(persist_dir=self.dataset_path)
|
| 107 |
+
self.storage_context = index.storage_context
|
llama/data_loader.py
CHANGED
|
@@ -16,10 +16,10 @@ class WikiLoader(ABC):
|
|
| 16 |
|
| 17 |
class GithubLoader(WikiLoader):
|
| 18 |
def __init__(
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
):
|
| 24 |
super().__init__()
|
| 25 |
self.owner = (
|
|
@@ -57,4 +57,4 @@ class GithubLoader(WikiLoader):
|
|
| 57 |
with open("docs/docs.pkl", "wb") as f:
|
| 58 |
pickle.dump(docs, f)
|
| 59 |
|
| 60 |
-
return docs
|
|
|
|
| 16 |
|
| 17 |
class GithubLoader(WikiLoader):
|
| 18 |
def __init__(
|
| 19 |
+
self,
|
| 20 |
+
github_owner: Optional[str] = None,
|
| 21 |
+
repo: Optional[str] = None,
|
| 22 |
+
dirs: Optional[Sequence[str]] = None,
|
| 23 |
):
|
| 24 |
super().__init__()
|
| 25 |
self.owner = (
|
|
|
|
| 57 |
with open("docs/docs.pkl", "wb") as f:
|
| 58 |
pickle.dump(docs, f)
|
| 59 |
|
| 60 |
+
return docs
|
xpipe_wiki/manager_factory.py
CHANGED
|
@@ -44,4 +44,4 @@ class XPipeRobotManagerFactory:
|
|
| 44 |
)
|
| 45 |
LifecycleHelper.initialize_if_possible(robot_manager)
|
| 46 |
LifecycleHelper.start_if_possible(robot_manager)
|
| 47 |
-
return robot_manager
|
|
|
|
| 44 |
)
|
| 45 |
LifecycleHelper.initialize_if_possible(robot_manager)
|
| 46 |
LifecycleHelper.start_if_possible(robot_manager)
|
| 47 |
+
return robot_manager
|
xpipe_wiki/robot_manager.py
CHANGED
|
@@ -69,4 +69,4 @@ class AzureXPipeWikiRobotManager(XPipeWikiRobotManager):
|
|
| 69 |
|
| 70 |
def do_dispose(self) -> None:
|
| 71 |
LifecycleHelper.dispose_if_possible(self.storage_context_manager)
|
| 72 |
-
LifecycleHelper.dispose_if_possible(self.service_context_manager)
|
|
|
|
| 69 |
|
| 70 |
def do_dispose(self) -> None:
|
| 71 |
LifecycleHelper.dispose_if_possible(self.storage_context_manager)
|
| 72 |
+
LifecycleHelper.dispose_if_possible(self.service_context_manager)
|