Spaces:
Sleeping
Sleeping
Add Chinese-Large embedding optimize
#5
by
NickNYU
- opened
- .gitattributes +35 -0
- README.md +2 -1
- app.py +44 -44
- langchain_manager/__pycache__/__init__.cpython-310.pyc +0 -0
- llama/context.py +125 -0
- llama/index.py +18 -0
- llama/vector_storage.py +18 -0
- requirements.txt +2 -1
.gitattributes
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
|
@@ -12,6 +12,7 @@ license: apache-2.0
|
|
| 12 |
|
| 13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
| 14 |
|
|
|
|
| 15 |
Then, create a new Python virtual environment. The command below creates an environment in `.venv`,
|
| 16 |
and activates it:
|
| 17 |
|
|
@@ -54,4 +55,4 @@ You can also make use of our pre-commit hooks by setting up git hook scripts:
|
|
| 54 |
pre-commit install
|
| 55 |
```
|
| 56 |
|
| 57 |
-
We run an assortment of linters: `black`, `ruff`, `mypy`.
|
|
|
|
| 12 |
|
| 13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
| 14 |
|
| 15 |
+
|
| 16 |
Then, create a new Python virtual environment. The command below creates an environment in `.venv`,
|
| 17 |
and activates it:
|
| 18 |
|
|
|
|
| 55 |
pre-commit install
|
| 56 |
```
|
| 57 |
|
| 58 |
+
We run an assortment of linters: `black`, `ruff`, `mypy`.
|
app.py
CHANGED
|
@@ -1,44 +1,44 @@
|
|
| 1 |
-
import logging
|
| 2 |
-
import sys
|
| 3 |
-
|
| 4 |
-
import streamlit as st
|
| 5 |
-
from dotenv import load_dotenv
|
| 6 |
-
|
| 7 |
-
from xpipe_wiki.manager_factory import XPipeRobotManagerFactory, XPipeRobotRevision
|
| 8 |
-
|
| 9 |
-
logging.basicConfig(
|
| 10 |
-
stream=sys.stdout, level=logging.INFO
|
| 11 |
-
) # logging.DEBUG for more verbose output
|
| 12 |
-
logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))
|
| 13 |
-
|
| 14 |
-
# Sidebar contents
|
| 15 |
-
with st.sidebar:
|
| 16 |
-
st.title("🤗💬 LLM Chat App")
|
| 17 |
-
st.markdown(
|
| 18 |
-
"""
|
| 19 |
-
## About
|
| 20 |
-
This app is an LLM-powered chatbot built using:
|
| 21 |
-
- [Streamlit](https://streamlit.io/)
|
| 22 |
-
- [LangChain](https://python.langchain.com/)
|
| 23 |
-
- [X-Pipe](https://github.com/ctripcorp/x-pipe)
|
| 24 |
-
"""
|
| 25 |
-
)
|
| 26 |
-
# add_vertical_space(5)
|
| 27 |
-
st.write("Made by Nick")
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
def main() -> None:
|
| 31 |
-
st.header("X-Pipe Wiki 机器人 💬")
|
| 32 |
-
robot_manager = XPipeRobotManagerFactory.get_or_create(
|
| 33 |
-
XPipeRobotRevision.HUGGINGFACE_VERSION_0
|
| 34 |
-
)
|
| 35 |
-
robot = robot_manager.get_robot()
|
| 36 |
-
query = st.text_input("X-Pipe Wiki 问题:")
|
| 37 |
-
if query:
|
| 38 |
-
response = robot.ask(question=query)
|
| 39 |
-
st.write(response)
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
if __name__ == "__main__":
|
| 43 |
-
load_dotenv()
|
| 44 |
-
main()
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
import sys
|
| 3 |
+
|
| 4 |
+
import streamlit as st
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
|
| 7 |
+
from xpipe_wiki.manager_factory import XPipeRobotManagerFactory, XPipeRobotRevision
|
| 8 |
+
|
| 9 |
+
logging.basicConfig(
|
| 10 |
+
stream=sys.stdout, level=logging.INFO
|
| 11 |
+
) # logging.DEBUG for more verbose output
|
| 12 |
+
logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))
|
| 13 |
+
|
| 14 |
+
# Sidebar contents
|
| 15 |
+
with st.sidebar:
|
| 16 |
+
st.title("🤗💬 LLM Chat App")
|
| 17 |
+
st.markdown(
|
| 18 |
+
"""
|
| 19 |
+
## About
|
| 20 |
+
This app is an LLM-powered chatbot built using:
|
| 21 |
+
- [Streamlit](https://streamlit.io/)
|
| 22 |
+
- [LangChain](https://python.langchain.com/)
|
| 23 |
+
- [X-Pipe](https://github.com/ctripcorp/x-pipe)
|
| 24 |
+
"""
|
| 25 |
+
)
|
| 26 |
+
# add_vertical_space(5)
|
| 27 |
+
st.write("Made by Nick")
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def main() -> None:
|
| 31 |
+
st.header("X-Pipe Wiki 机器人 💬")
|
| 32 |
+
robot_manager = XPipeRobotManagerFactory.get_or_create(
|
| 33 |
+
XPipeRobotRevision.HUGGINGFACE_VERSION_0
|
| 34 |
+
)
|
| 35 |
+
robot = robot_manager.get_robot()
|
| 36 |
+
query = st.text_input("X-Pipe Wiki 问题:")
|
| 37 |
+
if query:
|
| 38 |
+
response = robot.ask(question=query)
|
| 39 |
+
st.write(response)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
if __name__ == "__main__":
|
| 43 |
+
load_dotenv()
|
| 44 |
+
main()
|
langchain_manager/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (154 Bytes). View file
|
|
|
llama/context.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from abc import abstractmethod, ABC
|
| 2 |
+
from typing import List
|
| 3 |
+
|
| 4 |
+
from llama_index import ServiceContext, LLMPredictor, LangchainEmbedding, Document
|
| 5 |
+
from llama_index import StorageContext
|
| 6 |
+
|
| 7 |
+
from core.lifecycle import Lifecycle
|
| 8 |
+
from langchain_manager.manager import BaseLangChainManager
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class ServiceContextManager(Lifecycle, ABC):
|
| 12 |
+
@abstractmethod
|
| 13 |
+
def get_service_context(self) -> ServiceContext:
|
| 14 |
+
pass
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class AzureServiceContextManager(ServiceContextManager):
|
| 18 |
+
lc_manager: BaseLangChainManager
|
| 19 |
+
service_context: ServiceContext
|
| 20 |
+
|
| 21 |
+
def __init__(self, lc_manager: BaseLangChainManager):
|
| 22 |
+
super().__init__()
|
| 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"
|
| 29 |
+
)
|
| 30 |
+
return self.service_context
|
| 31 |
+
|
| 32 |
+
def do_init(self) -> None:
|
| 33 |
+
# define embedding
|
| 34 |
+
embedding = LangchainEmbedding(self.lc_manager.get_embedding())
|
| 35 |
+
# define LLM
|
| 36 |
+
llm_predictor = LLMPredictor(llm=self.lc_manager.get_llm())
|
| 37 |
+
# configure service context
|
| 38 |
+
self.service_context = ServiceContext.from_defaults(
|
| 39 |
+
llm_predictor=llm_predictor, embed_model=embedding
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
def do_start(self) -> None:
|
| 43 |
+
self.logger.info(
|
| 44 |
+
"[do_start][embedding] last used usage: %d",
|
| 45 |
+
self.service_context.embed_model.total_tokens_used,
|
| 46 |
+
)
|
| 47 |
+
self.logger.info(
|
| 48 |
+
"[do_start][predict] last used usage: %d",
|
| 49 |
+
self.service_context.llm_predictor.total_tokens_used,
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
def do_stop(self) -> None:
|
| 53 |
+
self.logger.info(
|
| 54 |
+
"[do_stop][embedding] last used usage: %d",
|
| 55 |
+
self.service_context.embed_model.total_tokens_used,
|
| 56 |
+
)
|
| 57 |
+
self.logger.info(
|
| 58 |
+
"[do_stop][predict] last used usage: %d",
|
| 59 |
+
self.service_context.llm_predictor.total_tokens_used,
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
def do_dispose(self) -> None:
|
| 63 |
+
self.logger.info(
|
| 64 |
+
"[do_dispose] total used token: %d",
|
| 65 |
+
self.service_context.llm_predictor.total_tokens_used,
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
class StorageContextManager(Lifecycle, ABC):
|
| 70 |
+
@abstractmethod
|
| 71 |
+
def get_storage_context(self) -> StorageContext:
|
| 72 |
+
pass
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
class LocalStorageContextManager(StorageContextManager):
|
| 76 |
+
storage_context: StorageContext
|
| 77 |
+
|
| 78 |
+
def __init__(
|
| 79 |
+
self,
|
| 80 |
+
service_context_manager: ServiceContextManager,
|
| 81 |
+
dataset_path: str = "./dataset",
|
| 82 |
+
) -> None:
|
| 83 |
+
super().__init__()
|
| 84 |
+
self.dataset_path = dataset_path
|
| 85 |
+
self.service_context_manager = service_context_manager
|
| 86 |
+
|
| 87 |
+
def get_storage_context(self) -> StorageContext:
|
| 88 |
+
return self.storage_context
|
| 89 |
+
|
| 90 |
+
def do_init(self) -> None:
|
| 91 |
+
from llama.utils import is_local_storage_files_ready
|
| 92 |
+
|
| 93 |
+
if is_local_storage_files_ready(self.dataset_path):
|
| 94 |
+
self.storage_context = StorageContext.from_defaults(
|
| 95 |
+
persist_dir=self.dataset_path
|
| 96 |
+
)
|
| 97 |
+
else:
|
| 98 |
+
docs = self._download()
|
| 99 |
+
self._indexing(docs)
|
| 100 |
+
|
| 101 |
+
def do_start(self) -> None:
|
| 102 |
+
# self.logger.info("[do_start]%", **self.storage_context.to_dict())
|
| 103 |
+
pass
|
| 104 |
+
|
| 105 |
+
def do_stop(self) -> None:
|
| 106 |
+
# self.logger.info("[do_stop]%", **self.storage_context.to_dict())
|
| 107 |
+
pass
|
| 108 |
+
|
| 109 |
+
def do_dispose(self) -> None:
|
| 110 |
+
self.storage_context.persist(self.dataset_path)
|
| 111 |
+
|
| 112 |
+
def _download(self) -> List[Document]:
|
| 113 |
+
from llama.data_loader import GithubLoader
|
| 114 |
+
|
| 115 |
+
loader = GithubLoader()
|
| 116 |
+
return loader.load()
|
| 117 |
+
|
| 118 |
+
def _indexing(self, docs: List[Document]) -> None:
|
| 119 |
+
from llama_index import GPTVectorStoreIndex
|
| 120 |
+
|
| 121 |
+
index = GPTVectorStoreIndex.from_documents(
|
| 122 |
+
docs, service_context=self.service_context_manager.get_service_context()
|
| 123 |
+
)
|
| 124 |
+
index.storage_context.persist(persist_dir=self.dataset_path)
|
| 125 |
+
self.storage_context = index.storage_context
|
llama/index.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from core.lifecycle import Lifecycle
|
| 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
|
requirements.txt
CHANGED
|
@@ -4,4 +4,5 @@ streamlit
|
|
| 4 |
ruff
|
| 5 |
black
|
| 6 |
mypy
|
| 7 |
-
accelerate
|
|
|
|
|
|
| 4 |
ruff
|
| 5 |
black
|
| 6 |
mypy
|
| 7 |
+
accelerate
|
| 8 |
+
python-dotenv
|