Spaces:
Sleeping
Sleeping
依赖冲突太麻烦了
Browse files
.ipynb_checkpoints/app-checkpoint.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settings
|
3 |
+
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
4 |
+
from llama_index.legacy.callbacks import CallbackManager
|
5 |
+
from llama_index.llms.openai_like import OpenAILike
|
6 |
+
import os
|
7 |
+
|
8 |
+
# Create an instance of CallbackManager
|
9 |
+
callback_manager = CallbackManager()
|
10 |
+
|
11 |
+
api_base_url = "https://internlm-chat.intern-ai.org.cn/puyu/api/v1/"
|
12 |
+
model = "internlm2.5-latest"
|
13 |
+
api_key = os.getenv("API_KEY")
|
14 |
+
|
15 |
+
# api_base_url = "https://api.siliconflow.cn/v1"
|
16 |
+
# model = "internlm/internlm2_5-7b-chat"
|
17 |
+
# api_key = "请填写 API Key"
|
18 |
+
|
19 |
+
llm =OpenAILike(model=model, api_base=api_base_url, api_key=api_key, is_chat_model=True,callback_manager=callback_manager)
|
20 |
+
|
21 |
+
|
22 |
+
|
23 |
+
st.set_page_config(page_title="llama_index_demo", page_icon="🦜🔗")
|
24 |
+
st.title("llama_index_demo")
|
25 |
+
|
26 |
+
# 初始化模型
|
27 |
+
@st.cache_resource
|
28 |
+
def init_models():
|
29 |
+
embed_model = HuggingFaceEmbedding(
|
30 |
+
model_name="/root/model/sentence-transformer"
|
31 |
+
)
|
32 |
+
Settings.embed_model = embed_model
|
33 |
+
|
34 |
+
#用初始化llm
|
35 |
+
Settings.llm = llm
|
36 |
+
|
37 |
+
documents = SimpleDirectoryReader("/root/llamaindex_demo/data").load_data()
|
38 |
+
index = VectorStoreIndex.from_documents(documents)
|
39 |
+
query_engine = index.as_query_engine()
|
40 |
+
|
41 |
+
return query_engine
|
42 |
+
|
43 |
+
# 检查是否需要初始化模型
|
44 |
+
if 'query_engine' not in st.session_state:
|
45 |
+
st.session_state['query_engine'] = init_models()
|
46 |
+
|
47 |
+
def greet2(question):
|
48 |
+
response = st.session_state['query_engine'].query(question)
|
49 |
+
return response
|
50 |
+
|
51 |
+
|
52 |
+
# Store LLM generated responses
|
53 |
+
if "messages" not in st.session_state.keys():
|
54 |
+
st.session_state.messages = [{"role": "assistant", "content": "你好,我是你的助手,有什么我可以帮助你的吗?"}]
|
55 |
+
|
56 |
+
# Display or clear chat messages
|
57 |
+
for message in st.session_state.messages:
|
58 |
+
with st.chat_message(message["role"]):
|
59 |
+
st.write(message["content"])
|
60 |
+
|
61 |
+
def clear_chat_history():
|
62 |
+
st.session_state.messages = [{"role": "assistant", "content": "你好,我是你的助手,有什么我可以帮助你的吗?"}]
|
63 |
+
|
64 |
+
st.sidebar.button('Clear Chat History', on_click=clear_chat_history)
|
65 |
+
|
66 |
+
# Function for generating LLaMA2 response
|
67 |
+
def generate_llama_index_response(prompt_input):
|
68 |
+
return greet2(prompt_input)
|
69 |
+
|
70 |
+
# User-provided prompt
|
71 |
+
if prompt := st.chat_input():
|
72 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
73 |
+
with st.chat_message("user"):
|
74 |
+
st.write(prompt)
|
75 |
+
|
76 |
+
# Gegenerate_llama_index_response last message is not from assistant
|
77 |
+
if st.session_state.messages[-1]["role"] != "assistant":
|
78 |
+
with st.chat_message("assistant"):
|
79 |
+
with st.spinner("Thinking..."):
|
80 |
+
response = generate_llama_index_response(prompt)
|
81 |
+
placeholder = st.empty()
|
82 |
+
placeholder.markdown(response)
|
83 |
+
message = {"role": "assistant", "content": response}
|
84 |
+
st.session_state.messages.append(message)
|
.ipynb_checkpoints/requirements-checkpoint.txt
CHANGED
@@ -102,14 +102,14 @@ sympy==1.13.1
|
|
102 |
tenacity==8.5.0
|
103 |
threadpoolctl==3.5.0
|
104 |
tiktoken==0.8.0
|
105 |
-
tokenizers==0.
|
106 |
-
torch==2.
|
107 |
-
torchaudio==2.
|
108 |
-
torchvision==0.
|
109 |
tornado==6.4.2
|
110 |
tqdm==4.67.1
|
111 |
transformers==4.46.3
|
112 |
-
triton
|
113 |
typing-extensions==4.12.2
|
114 |
typing-inspect==0.9.0
|
115 |
tzdata==2024.2
|
|
|
102 |
tenacity==8.5.0
|
103 |
threadpoolctl==3.5.0
|
104 |
tiktoken==0.8.0
|
105 |
+
tokenizers==0.13.3
|
106 |
+
torch==2.1.2
|
107 |
+
torchaudio==2.1.2
|
108 |
+
torchvision==0.16.0
|
109 |
tornado==6.4.2
|
110 |
tqdm==4.67.1
|
111 |
transformers==4.46.3
|
112 |
+
triton
|
113 |
typing-extensions==4.12.2
|
114 |
typing-inspect==0.9.0
|
115 |
tzdata==2024.2
|
requirements.txt
DELETED
@@ -1,119 +0,0 @@
|
|
1 |
-
aiohappyeyeballs==2.4.3
|
2 |
-
aiohttp==3.11.7
|
3 |
-
aiosignal==1.3.1
|
4 |
-
altair==5.5.0
|
5 |
-
annotated-types==0.7.0
|
6 |
-
anyio==4.6.2.post1
|
7 |
-
async-timeout==5.0.1
|
8 |
-
attrs==24.2.0
|
9 |
-
beautifulsoup4==4.12.3
|
10 |
-
blinker==1.9.0
|
11 |
-
cachetools==5.5.0
|
12 |
-
certifi==2024.8.30
|
13 |
-
charset-normalizer==3.4.0
|
14 |
-
click==8.1.7
|
15 |
-
dataclasses-json==0.6.7
|
16 |
-
deprecated==1.2.15
|
17 |
-
dirtyjson==1.0.8
|
18 |
-
distro==1.9.0
|
19 |
-
einops==0.7.0
|
20 |
-
exceptiongroup==1.2.2
|
21 |
-
filelock==3.16.1
|
22 |
-
filetype==1.2.0
|
23 |
-
frozenlist==1.5.0
|
24 |
-
fsspec==2024.10.0
|
25 |
-
gitdb==4.0.11
|
26 |
-
gitpython==3.1.43
|
27 |
-
greenlet==3.1.1
|
28 |
-
h11==0.14.0
|
29 |
-
httpcore==1.0.7
|
30 |
-
httpx==0.27.2
|
31 |
-
huggingface-hub==0.26.2
|
32 |
-
idna==3.10
|
33 |
-
instructorembedding==1.0.1
|
34 |
-
jinja2==3.1.4
|
35 |
-
jiter==0.7.1
|
36 |
-
joblib==1.4.2
|
37 |
-
jsonschema==4.23.0
|
38 |
-
jsonschema-specifications==2024.10.1
|
39 |
-
llama-cloud==0.1.5
|
40 |
-
llama-index==0.11.20
|
41 |
-
llama-index-agent-openai==0.3.4
|
42 |
-
llama-index-cli==0.3.1
|
43 |
-
llama-index-core==0.11.23
|
44 |
-
llama-index-embeddings-huggingface==0.3.1
|
45 |
-
llama-index-embeddings-instructor==0.2.1
|
46 |
-
llama-index-embeddings-openai==0.2.5
|
47 |
-
llama-index-indices-managed-llama-cloud==0.6.0
|
48 |
-
llama-index-legacy==0.9.48.post4
|
49 |
-
llama-index-llms-openai==0.2.16
|
50 |
-
llama-index-llms-openai-like==0.2.0
|
51 |
-
llama-index-llms-replicate==0.3.0
|
52 |
-
llama-index-multi-modal-llms-openai==0.2.3
|
53 |
-
llama-index-program-openai==0.2.0
|
54 |
-
llama-index-question-gen-openai==0.2.0
|
55 |
-
llama-index-readers-file==0.2.2
|
56 |
-
llama-index-readers-llama-parse==0.3.0
|
57 |
-
llama-parse==0.5.15
|
58 |
-
markdown-it-py==3.0.0
|
59 |
-
markupsafe==3.0.2
|
60 |
-
marshmallow==3.23.1
|
61 |
-
mdurl==0.1.2
|
62 |
-
mpmath==1.3.0
|
63 |
-
multidict==6.1.0
|
64 |
-
mypy-extensions==1.0.0
|
65 |
-
narwhals==1.14.2
|
66 |
-
nest-asyncio==1.6.0
|
67 |
-
networkx==3.4.2
|
68 |
-
nltk==3.9.1
|
69 |
-
numpy==1.26.4
|
70 |
-
openai==1.55.0
|
71 |
-
packaging==24.2
|
72 |
-
pandas==2.2.3
|
73 |
-
pillow==10.4.0
|
74 |
-
propcache==0.2.0
|
75 |
-
protobuf==5.26.1
|
76 |
-
pyarrow==18.0.0
|
77 |
-
pydantic==2.10.1
|
78 |
-
pydantic-core==2.27.1
|
79 |
-
pydeck==0.9.1
|
80 |
-
pygments==2.18.0
|
81 |
-
pypdf==4.3.1
|
82 |
-
python-dateutil==2.9.0.post0
|
83 |
-
pytz==2024.2
|
84 |
-
pyyaml==6.0.2
|
85 |
-
referencing==0.35.1
|
86 |
-
regex==2024.11.6
|
87 |
-
requests==2.32.3
|
88 |
-
rich==13.9.4
|
89 |
-
rpds-py==0.21.0
|
90 |
-
safetensors==0.4.5
|
91 |
-
scikit-learn==1.5.2
|
92 |
-
scipy==1.14.1
|
93 |
-
sentence-transformers==2.7.0
|
94 |
-
six==1.16.0
|
95 |
-
smmap==5.0.1
|
96 |
-
sniffio==1.3.1
|
97 |
-
soupsieve==2.6
|
98 |
-
sqlalchemy==2.0.36
|
99 |
-
streamlit==1.39.0
|
100 |
-
striprtf==0.0.26
|
101 |
-
sympy==1.13.1
|
102 |
-
tenacity==8.5.0
|
103 |
-
threadpoolctl==3.5.0
|
104 |
-
tiktoken==0.8.0
|
105 |
-
tokenizers==0.13.3
|
106 |
-
torch==2.1.2
|
107 |
-
torchaudio==2.1.2
|
108 |
-
torchvision==0.16.0
|
109 |
-
tornado==6.4.2
|
110 |
-
tqdm==4.67.1
|
111 |
-
transformers==4.46.3
|
112 |
-
triton
|
113 |
-
typing-extensions==4.12.2
|
114 |
-
typing-inspect==0.9.0
|
115 |
-
tzdata==2024.2
|
116 |
-
urllib3==2.2.3
|
117 |
-
watchdog==5.0.3
|
118 |
-
wrapt==1.17.0
|
119 |
-
yarl==1.18.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|