Chen commited on
Commit
374292c
1 Parent(s): 6c20719

pylint and py format ready

Browse files
app.py CHANGED
@@ -1,5 +1,4 @@
1
  import os
2
- from dotenv import load_dotenv
3
  from llama_index import SimpleDirectoryReader
4
  from llama_index.node_parser import SimpleNodeParser
5
  from llama_index.data_structs.node import Node, DocumentRelationship
@@ -14,14 +13,13 @@ import logging
14
  import sys
15
 
16
 
17
- load_dotenv()
18
  logging.basicConfig(
19
  stream=sys.stdout, level=logging.DEBUG
20
  ) # logging.DEBUG for more verbose output
21
  logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))
22
 
23
 
24
- def main():
25
  documents = SimpleDirectoryReader("./data").load_data()
26
 
27
  # index = VectorStoreIndex.from_documents(documents)
@@ -31,12 +29,13 @@ def main():
31
  # index = VectorStoreIndex(nodes)
32
 
33
  # define embedding
34
- embedding = LangchainEmbedding(OpenAIEmbeddings(chunk_size=1))
35
  # define LLM
36
  llm_predictor = LLMPredictor(
37
  llm=AzureOpenAI(
38
- engine="text-davinci-003",
39
- model_name="text-davinci-003",
 
40
  )
41
  )
42
 
 
1
  import os
 
2
  from llama_index import SimpleDirectoryReader
3
  from llama_index.node_parser import SimpleNodeParser
4
  from llama_index.data_structs.node import Node, DocumentRelationship
 
13
  import sys
14
 
15
 
 
16
  logging.basicConfig(
17
  stream=sys.stdout, level=logging.DEBUG
18
  ) # logging.DEBUG for more verbose output
19
  logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))
20
 
21
 
22
+ def main() -> None:
23
  documents = SimpleDirectoryReader("./data").load_data()
24
 
25
  # index = VectorStoreIndex.from_documents(documents)
 
29
  # index = VectorStoreIndex(nodes)
30
 
31
  # define embedding
32
+ embedding = LangchainEmbedding(OpenAIEmbeddings(client=None, chunk_size=1))
33
  # define LLM
34
  llm_predictor = LLMPredictor(
35
  llm=AzureOpenAI(
36
+ client=None,
37
+ deployment_name="text-davinci-003",
38
+ model="text-davinci-003",
39
  )
40
  )
41
 
core/lifecycle.py CHANGED
@@ -1,6 +1,6 @@
1
  import enum
2
  from abc import ABC, abstractmethod
3
- from typing import TypeVar
4
 
5
  from core import logger_factory
6
 
@@ -30,19 +30,24 @@ class Disposable(ABC):
30
 
31
 
32
  class LifecycleAware(ABC):
33
- def __init__(self, state):
 
 
 
 
34
  self.state = state
35
 
36
- def get_lifecycle_state(self):
 
37
  return self.state
38
 
39
 
40
  class Lifecycle(Initializable, Startable, Stoppable, Disposable, LifecycleAware, ABC):
41
- def __init__(self):
42
  self.logger = logger_factory.get_logger(self.__class__.__name__)
43
  self.lifecycle_state = LifecycleState(lifecycle=self)
44
 
45
- def initialize(self):
46
  if not self.lifecycle_state.can_initialize(self.lifecycle_state.get_phase()):
47
  self.logger.warning("[{}]cannot initialize".format(self.__class__.__name__))
48
  return
@@ -50,7 +55,7 @@ class Lifecycle(Initializable, Startable, Stoppable, Disposable, LifecycleAware,
50
  self.do_init()
51
  self.lifecycle_state.set_phase(LifecyclePhase.INITIALIZED)
52
 
53
- def start(self):
54
  if not self.lifecycle_state.can_start(self.lifecycle_state.get_phase()):
55
  self.logger.warning("[{}]cannot start".format(self.__class__.__name__))
56
  return
@@ -58,7 +63,7 @@ class Lifecycle(Initializable, Startable, Stoppable, Disposable, LifecycleAware,
58
  self.do_start()
59
  self.lifecycle_state.set_phase(LifecyclePhase.STARTED)
60
 
61
- def stop(self):
62
  if not self.lifecycle_state.can_stop(self.lifecycle_state.get_phase()):
63
  self.logger.warning("[{}]cannot stop".format(self.__class__.__name__))
64
  return
@@ -66,7 +71,7 @@ class Lifecycle(Initializable, Startable, Stoppable, Disposable, LifecycleAware,
66
  self.do_stop()
67
  self.lifecycle_state.set_phase(LifecyclePhase.STOPPED)
68
 
69
- def dispose(self):
70
  if not self.lifecycle_state.can_dispose(self.lifecycle_state.get_phase()):
71
  self.logger.warning("[{}]cannot dispose".format(self.__class__.__name__))
72
  return
@@ -75,19 +80,19 @@ class Lifecycle(Initializable, Startable, Stoppable, Disposable, LifecycleAware,
75
  self.lifecycle_state.set_phase(LifecyclePhase.DISPOSED)
76
 
77
  @abstractmethod
78
- def do_init(self):
79
  pass
80
 
81
  @abstractmethod
82
- def do_start(self):
83
  pass
84
 
85
  @abstractmethod
86
- def do_stop(self):
87
  pass
88
 
89
  @abstractmethod
90
- def do_dispose(self):
91
  pass
92
 
93
 
@@ -103,18 +108,18 @@ class LifecyclePhase(enum.Enum):
103
 
104
 
105
  class LifecycleController(ABC):
106
- def can_initialize(self, phase: [LifecyclePhase]) -> bool:
107
  return phase is None or phase == LifecyclePhase.DISPOSED
108
 
109
- def can_start(self, phase: [LifecyclePhase]) -> bool:
110
  return phase is not None and (
111
  phase == LifecyclePhase.INITIALIZED or phase == LifecyclePhase.STOPPED
112
  )
113
 
114
- def can_stop(self, phase: [LifecyclePhase]) -> bool:
115
  return phase is not None and phase == LifecyclePhase.STARTED
116
 
117
- def can_dispose(self, phase: [LifecyclePhase]) -> bool:
118
  return phase is not None and (
119
  phase == LifecyclePhase.INITIALIZED or phase == LifecyclePhase.STOPPED
120
  )
@@ -124,7 +129,9 @@ LS = TypeVar("LS", bound=Lifecycle)
124
 
125
 
126
  class LifecycleState(LifecycleController, ABC):
127
- def __init__(self, lifecycle: [LS]):
 
 
128
  self.phase = None
129
  self.prev_phase = None
130
  self.lifecycle = lifecycle
@@ -154,18 +161,25 @@ class LifecycleState(LifecycleController, ABC):
154
  def is_disposed(self) -> bool:
155
  return self.phase == LifecyclePhase.DISPOSED
156
 
157
- def get_phase(self) -> LifecyclePhase:
158
  return self.phase
159
 
160
- def set_phase(self, phase: [LifecyclePhase]) -> None:
161
- prev = "None" if self.phase is None else self.phase.name
 
 
 
 
 
162
  self.logger.info(
163
  "[setPhaseName][{}]{} --> {}".format(
164
- self.lifecycle.__class__.__name__, prev, phase.name
 
 
165
  )
166
  )
167
  self.phase = phase
168
 
169
- def rollback(self, err: [Exception]) -> None:
170
  self.phase = self.prev_phase
171
  self.prev_phase = None
 
1
  import enum
2
  from abc import ABC, abstractmethod
3
+ from typing import TypeVar, Optional
4
 
5
  from core import logger_factory
6
 
 
30
 
31
 
32
  class LifecycleAware(ABC):
33
+ def __init__(self, state: "LifecycleState") -> None:
34
+ """
35
+ Args:
36
+ state(LifecycleState): lifecycle state
37
+ """
38
  self.state = state
39
 
40
+ @property
41
+ def get_lifecycle_state(self) -> "LifecycleState":
42
  return self.state
43
 
44
 
45
  class Lifecycle(Initializable, Startable, Stoppable, Disposable, LifecycleAware, ABC):
46
+ def __init__(self) -> None:
47
  self.logger = logger_factory.get_logger(self.__class__.__name__)
48
  self.lifecycle_state = LifecycleState(lifecycle=self)
49
 
50
+ def initialize(self) -> None:
51
  if not self.lifecycle_state.can_initialize(self.lifecycle_state.get_phase()):
52
  self.logger.warning("[{}]cannot initialize".format(self.__class__.__name__))
53
  return
 
55
  self.do_init()
56
  self.lifecycle_state.set_phase(LifecyclePhase.INITIALIZED)
57
 
58
+ def start(self) -> None:
59
  if not self.lifecycle_state.can_start(self.lifecycle_state.get_phase()):
60
  self.logger.warning("[{}]cannot start".format(self.__class__.__name__))
61
  return
 
63
  self.do_start()
64
  self.lifecycle_state.set_phase(LifecyclePhase.STARTED)
65
 
66
+ def stop(self) -> None:
67
  if not self.lifecycle_state.can_stop(self.lifecycle_state.get_phase()):
68
  self.logger.warning("[{}]cannot stop".format(self.__class__.__name__))
69
  return
 
71
  self.do_stop()
72
  self.lifecycle_state.set_phase(LifecyclePhase.STOPPED)
73
 
74
+ def dispose(self) -> None:
75
  if not self.lifecycle_state.can_dispose(self.lifecycle_state.get_phase()):
76
  self.logger.warning("[{}]cannot dispose".format(self.__class__.__name__))
77
  return
 
80
  self.lifecycle_state.set_phase(LifecyclePhase.DISPOSED)
81
 
82
  @abstractmethod
83
+ def do_init(self) -> None:
84
  pass
85
 
86
  @abstractmethod
87
+ def do_start(self) -> None:
88
  pass
89
 
90
  @abstractmethod
91
+ def do_stop(self) -> None:
92
  pass
93
 
94
  @abstractmethod
95
+ def do_dispose(self) -> None:
96
  pass
97
 
98
 
 
108
 
109
 
110
  class LifecycleController(ABC):
111
+ def can_initialize(self, phase: Optional[LifecyclePhase]) -> bool:
112
  return phase is None or phase == LifecyclePhase.DISPOSED
113
 
114
+ def can_start(self, phase: Optional[LifecyclePhase]) -> bool:
115
  return phase is not None and (
116
  phase == LifecyclePhase.INITIALIZED or phase == LifecyclePhase.STOPPED
117
  )
118
 
119
+ def can_stop(self, phase: Optional[LifecyclePhase]) -> bool:
120
  return phase is not None and phase == LifecyclePhase.STARTED
121
 
122
+ def can_dispose(self, phase: Optional[LifecyclePhase]) -> bool:
123
  return phase is not None and (
124
  phase == LifecyclePhase.INITIALIZED or phase == LifecyclePhase.STOPPED
125
  )
 
129
 
130
 
131
  class LifecycleState(LifecycleController, ABC):
132
+ phase: Optional[LifecyclePhase]
133
+
134
+ def __init__(self, lifecycle: LS) -> None:
135
  self.phase = None
136
  self.prev_phase = None
137
  self.lifecycle = lifecycle
 
161
  def is_disposed(self) -> bool:
162
  return self.phase == LifecyclePhase.DISPOSED
163
 
164
+ def get_phase(self) -> Optional[LifecyclePhase]:
165
  return self.phase
166
 
167
+ def set_phase(self, phase: Optional[LifecyclePhase]) -> None:
168
+ prev = "None"
169
+ if self.phase is not None:
170
+ prev = self.phase.name
171
+ current = "None"
172
+ if phase is not None:
173
+ current = phase.name
174
  self.logger.info(
175
  "[setPhaseName][{}]{} --> {}".format(
176
+ self.lifecycle.__class__.__name__,
177
+ prev,
178
+ current,
179
  )
180
  )
181
  self.phase = phase
182
 
183
+ def rollback(self, err: Exception) -> None:
184
  self.phase = self.prev_phase
185
  self.prev_phase = None
core/logger_factory.py CHANGED
@@ -3,7 +3,7 @@ from logging import handlers
3
  from typing import Optional
4
 
5
 
6
- def get_logger(name: [str], file_name: Optional[str] = None) -> logging.Logger:
7
  logger = logging.getLogger(name)
8
  if file_name is None:
9
  file_name = "app-default.log"
 
3
  from typing import Optional
4
 
5
 
6
+ def get_logger(name: str, file_name: Optional[str] = None) -> logging.Logger:
7
  logger = logging.getLogger(name)
8
  if file_name is None:
9
  file_name = "app-default.log"
core/test_lifecycle.py CHANGED
@@ -7,52 +7,52 @@ logging.basicConfig()
7
 
8
 
9
  class SubLifecycle(Lifecycle):
10
- def __init__(self):
11
  super().__init__()
12
  self.init_counter = 0
13
 
14
- def do_init(self):
15
  self.init_counter += 1
16
 
17
- def do_start(self):
18
  self.init_counter += 1
19
 
20
- def do_stop(self):
21
  self.init_counter += 1
22
 
23
- def do_dispose(self):
24
  self.init_counter += 1
25
 
26
 
27
  class TestLifecycle(TestCase):
28
- def test_initialize(self):
29
  ls = SubLifecycle()
30
  ls.initialize()
31
- ls.logger.info(ls.lifecycle_state.get_phase().name)
32
  ls.start()
33
- ls.logger.info(ls.lifecycle_state.get_phase().name)
34
  ls.stop()
35
- ls.logger.info(ls.lifecycle_state.get_phase().name)
36
  ls.dispose()
37
- ls.logger.info(ls.lifecycle_state.get_phase().name)
38
 
39
- def test_start(self):
40
  self.fail()
41
 
42
- def test_stop(self):
43
  self.fail()
44
 
45
- def test_dispose(self):
46
  self.fail()
47
 
48
- def test_do_init(self):
49
  self.fail()
50
 
51
- def test_do_start(self):
52
  self.fail()
53
 
54
- def test_do_stop(self):
55
  self.fail()
56
 
57
- def test_do_dispose(self):
58
  self.fail()
 
7
 
8
 
9
  class SubLifecycle(Lifecycle):
10
+ def __init__(self) -> None:
11
  super().__init__()
12
  self.init_counter = 0
13
 
14
+ def do_init(self) -> None:
15
  self.init_counter += 1
16
 
17
+ def do_start(self) -> None:
18
  self.init_counter += 1
19
 
20
+ def do_stop(self) -> None:
21
  self.init_counter += 1
22
 
23
+ def do_dispose(self) -> None:
24
  self.init_counter += 1
25
 
26
 
27
  class TestLifecycle(TestCase):
28
+ def test_initialize(self) -> None:
29
  ls = SubLifecycle()
30
  ls.initialize()
31
+ ls.logger.info(ls.lifecycle_state.get_phase())
32
  ls.start()
33
+ ls.logger.info(ls.lifecycle_state.get_phase())
34
  ls.stop()
35
+ ls.logger.info(ls.lifecycle_state.get_phase())
36
  ls.dispose()
37
+ ls.logger.info(ls.lifecycle_state.get_phase())
38
 
39
+ def test_start(self) -> None:
40
  self.fail()
41
 
42
+ def test_stop(self) -> None:
43
  self.fail()
44
 
45
+ def test_dispose(self) -> None:
46
  self.fail()
47
 
48
+ def test_do_init(self) -> None:
49
  self.fail()
50
 
51
+ def test_do_start(self) -> None:
52
  self.fail()
53
 
54
+ def test_do_stop(self) -> None:
55
  self.fail()
56
 
57
+ def test_do_dispose(self) -> None:
58
  self.fail()
github_retriever.py CHANGED
@@ -10,7 +10,7 @@ import os
10
  import pickle
11
 
12
 
13
- def main():
14
  # define embedding
15
  embedding = LangchainEmbedding(OpenAIEmbeddings(chunk_size=1))
16
  # define LLM
 
10
  import pickle
11
 
12
 
13
+ def main() -> None:
14
  # define embedding
15
  embedding = LangchainEmbedding(OpenAIEmbeddings(chunk_size=1))
16
  # define LLM
langchain/manager.py CHANGED
@@ -8,8 +8,8 @@ from langchain.base_language import BaseLanguageModel
8
  from core.lifecycle import Lifecycle
9
 
10
 
11
- class LangChainManager(Lifecycle, ABC):
12
- def __init__(self):
13
  super().__init__()
14
 
15
  @abstractmethod
@@ -21,17 +21,19 @@ class LangChainManager(Lifecycle, ABC):
21
  pass
22
 
23
 
24
- class LangChainAzureManager(LangChainManager):
25
- def __init__(self):
26
  super().__init__()
27
 
28
  # Override
29
  def get_embedding(self) -> LCEmbeddings:
30
- return OpenAIEmbeddings(chunk_size=1)
31
 
32
  # Override
33
  def get_llm(self) -> BaseLanguageModel:
34
  return AzureOpenAI(
35
- engine="text-davinci-003",
36
- model_name="text-davinci-003",
 
 
37
  )
 
8
  from core.lifecycle import Lifecycle
9
 
10
 
11
+ class BaseLangChainManager(Lifecycle, ABC):
12
+ def __init__(self) -> None:
13
  super().__init__()
14
 
15
  @abstractmethod
 
21
  pass
22
 
23
 
24
+ class LangChainAzureManager(BaseLangChainManager):
25
+ def __init__(self) -> None:
26
  super().__init__()
27
 
28
  # Override
29
  def get_embedding(self) -> LCEmbeddings:
30
+ return OpenAIEmbeddings(client=None, chunk_size=1)
31
 
32
  # Override
33
  def get_llm(self) -> BaseLanguageModel:
34
  return AzureOpenAI(
35
+ deployment_name="text-davinci-003",
36
+ # model_name="text-davinci-003",
37
+ model="text-davinci-003",
38
+ client=None,
39
  )
llama/context.py CHANGED
@@ -1,23 +1,29 @@
1
  from llama_index import ServiceContext, LLMPredictor, LangchainEmbedding
2
-
3
  from core.lifecycle import Lifecycle
4
- from langchain.manager import LangChainManager
5
 
6
 
7
  class ServiceContextManager(Lifecycle):
8
- def __init__(self, manager: [LangChainManager]):
 
 
9
  super().__init__()
10
  self.manager = manager
11
  self.service_context = None
12
 
13
  def get_service_context(self) -> ServiceContext:
14
  if self.lifecycle_state.is_started():
15
- raise Exception(
16
- "incorrect lifecycle state: {}".format(self.get_lifecycle_state())
 
 
 
 
17
  )
18
  return self.service_context
19
 
20
- def do_init(self):
21
  # define embedding
22
  embedding = LangchainEmbedding(self.manager.get_embedding())
23
  # define LLM
@@ -27,29 +33,29 @@ class ServiceContextManager(Lifecycle):
27
  llm_predictor=llm_predictor, embed_model=embedding
28
  )
29
 
30
- def do_start(self):
31
  pass
32
 
33
- def do_stop(self):
34
  pass
35
 
36
- def do_dispose(self):
37
  pass
38
 
39
 
40
  class StorageContextManager(Lifecycle):
41
- def __init__(self, dataset_path: [str] = "./dataset"):
42
  super().__init__()
43
  self.dataset_path = dataset_path
44
 
45
- def do_init(self):
46
  pass
47
 
48
- def do_start(self):
49
  pass
50
 
51
- def do_stop(self):
52
  pass
53
 
54
- def do_dispose(self):
55
  pass
 
1
  from llama_index import ServiceContext, LLMPredictor, LangchainEmbedding
2
+ from type import Optional
3
  from core.lifecycle import Lifecycle
4
+ from langchain.manager import BaseLangChainManager
5
 
6
 
7
  class ServiceContextManager(Lifecycle):
8
+ service_context: Optional[ServiceContext]
9
+
10
+ def __init__(self, manager: BaseLangChainManager) -> None:
11
  super().__init__()
12
  self.manager = manager
13
  self.service_context = None
14
 
15
  def get_service_context(self) -> ServiceContext:
16
  if self.lifecycle_state.is_started():
17
+ raise KeyError(
18
+ "incorrect lifecycle state: {}".format(self.lifecycle_state.phase)
19
+ )
20
+ if self.service_context is None:
21
+ raise ValueError(
22
+ "service context is not ready, check for lifecycle statement"
23
  )
24
  return self.service_context
25
 
26
+ def do_init(self) -> None:
27
  # define embedding
28
  embedding = LangchainEmbedding(self.manager.get_embedding())
29
  # define LLM
 
33
  llm_predictor=llm_predictor, embed_model=embedding
34
  )
35
 
36
+ def do_start(self) -> None:
37
  pass
38
 
39
+ def do_stop(self) -> None:
40
  pass
41
 
42
+ def do_dispose(self) -> None:
43
  pass
44
 
45
 
46
  class StorageContextManager(Lifecycle):
47
+ def __init__(self, dataset_path: Optional[str] = "./dataset") -> None:
48
  super().__init__()
49
  self.dataset_path = dataset_path
50
 
51
+ def do_init(self) -> None:
52
  pass
53
 
54
+ def do_start(self) -> None:
55
  pass
56
 
57
+ def do_stop(self) -> None:
58
  pass
59
 
60
+ def do_dispose(self) -> None:
61
  pass
llama/index.py CHANGED
@@ -1,14 +1,18 @@
1
  from core.lifecycle import Lifecycle
2
  from llama.context import ServiceContextManager
 
 
3
 
4
 
5
  class IndexManager(Lifecycle):
6
- def __init__(self, context_manager: [ServiceContextManager]):
 
 
7
  super().__init__()
8
  self.index = None
9
  self.context_manager = context_manager
10
 
11
- def get_index(self):
12
  if not self.lifecycle_state.is_started():
13
  raise Exception("Lifecycle state is not correct")
14
  return self.index
 
1
  from core.lifecycle import Lifecycle
2
  from llama.context import ServiceContextManager
3
+ from llama_index.indices.vector_store import VectorStoreIndex
4
+ from typing import Optional
5
 
6
 
7
  class IndexManager(Lifecycle):
8
+ index: Optional[VectorStoreIndex]
9
+
10
+ def __init__(self, context_manager: ServiceContextManager) -> None:
11
  super().__init__()
12
  self.index = None
13
  self.context_manager = context_manager
14
 
15
+ def get_index(self) -> Optional[VectorStoreIndex]:
16
  if not self.lifecycle_state.is_started():
17
  raise Exception("Lifecycle state is not correct")
18
  return self.index
llama/vector_storage.py CHANGED
@@ -2,17 +2,17 @@ from core.lifecycle import Lifecycle
2
 
3
 
4
  class VectorStorageManager(Lifecycle):
5
- def __init__(self):
6
  super().__init__()
7
 
8
- def do_init(self):
9
  pass
10
 
11
- def do_start(self):
12
  pass
13
 
14
- def do_stop(self):
15
  pass
16
 
17
- def do_dispose(self):
18
  pass
 
2
 
3
 
4
  class VectorStorageManager(Lifecycle):
5
+ def __init__(self) -> None:
6
  super().__init__()
7
 
8
+ def do_init(self) -> None:
9
  pass
10
 
11
+ def do_start(self) -> None:
12
  pass
13
 
14
+ def do_stop(self) -> None:
15
  pass
16
 
17
+ def do_dispose(self) -> None:
18
  pass
pyproject.toml CHANGED
@@ -1,7 +1,7 @@
1
  [tool.mypy]
2
  ignore_missing_imports = "True"
3
  disallow_untyped_defs = "True"
4
- exclude = ["notebooks", "build", "examples"]
5
 
6
  [tool.ruff]
7
  exclude = [
@@ -14,4 +14,6 @@ exclude = [
14
  "notebooks",
15
  "docs",
16
  "dataset",
 
 
17
  ]
 
1
  [tool.mypy]
2
  ignore_missing_imports = "True"
3
  disallow_untyped_defs = "True"
4
+ exclude = ["notebooks", "build", "examples", "docs", "dataset", "app.py", "github_retriever.py"]
5
 
6
  [tool.ruff]
7
  exclude = [
 
14
  "notebooks",
15
  "docs",
16
  "dataset",
17
+ "app.py",
18
+ "github_retriever.py"
19
  ]
requirements.txt CHANGED
@@ -1,3 +1,7 @@
1
  llama_index
2
  llama_hub
3
- langchain
 
 
 
 
 
1
  llama_index
2
  llama_hub
3
+ langchain
4
+ dotenv
5
+ ruff
6
+ black
7
+ mypy