Spaces:
Build error
Build error
Commit ·
461b215
1
Parent(s): df5d87b
Initial commit
Browse files
app.py
CHANGED
|
@@ -1,93 +1,88 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from llama_index 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 |
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
callback_manager = CallbackManager()
|
| 9 |
|
| 10 |
-
|
| 11 |
-
api_base_url = "https://internlm-chat.intern-ai.org.cn/puyu/api/v1/"
|
| 12 |
model = "internlm2.5-latest"
|
| 13 |
-
api_key = "eyJ0eXBlIjoiSldUIiwiYWxnIjoiSFM1MTIifQ.eyJqdGkiOiI4MDAwNzM0OCIsInJvbCI6IlJPTEVfUkVHSVNURVIiLCJpc3MiOiJPcGVuWExhYiIsImlhdCI6MTczNzczMjUwMCwiY2xpZW50SWQiOiJlYm1ydm9kNnlvMG5semFlazF5cCIsInBob25lIjoiMTk4MjEyMTUyNzEiLCJ1dWlkIjoiZjc2MDM3NTctMTU2Yy00MTM3LWE1YmEtNTk3MDljODRiNDRkIiwiZW1haWwiOiIiLCJleHAiOjE3NTMyODQ1MDB9.4zD9ixAv1JdP_AJLQhNW3tCgzCGquW6eFcbV0XNqmqCZ0pL5A4hIPVA0zeFleg-n04O1IsyIZZ0rmkATZ1V6_A"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
llm = OpenAILike(
|
| 17 |
-
model=model,
|
| 18 |
-
api_base=api_base_url,
|
| 19 |
-
api_key=api_key,
|
| 20 |
-
is_chat_model=True,
|
| 21 |
-
callback_manager=callback_manager,
|
| 22 |
-
)
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
st.
|
|
|
|
| 27 |
|
| 28 |
# 初始化模型
|
| 29 |
@st.cache_resource
|
| 30 |
def init_models():
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
|
| 43 |
-
|
| 44 |
-
query_engine = index.as_query_engine()
|
| 45 |
-
return query_engine
|
| 46 |
-
except Exception as e:
|
| 47 |
-
st.error(f"模型初始化失败:{e}")
|
| 48 |
-
return None
|
| 49 |
|
| 50 |
# 检查是否需要初始化模型
|
| 51 |
if 'query_engine' not in st.session_state:
|
| 52 |
st.session_state['query_engine'] = init_models()
|
| 53 |
|
| 54 |
-
# 模型查询函数
|
| 55 |
def greet2(question):
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
response = query_engine.query(question)
|
| 59 |
-
return response.response # 确保返回的内容是字符串
|
| 60 |
-
else:
|
| 61 |
-
return "模型初始化失败,请检查环境配置。"
|
| 62 |
-
|
| 63 |
-
# 清空聊天记录
|
| 64 |
-
def clear_chat_history():
|
| 65 |
-
st.session_state.messages = [{"role": "assistant", "content": "你好,我是你的助手,有什么我可以帮助你的吗?"}]
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
| 70 |
|
| 71 |
-
#
|
| 72 |
for message in st.session_state.messages:
|
| 73 |
with st.chat_message(message["role"]):
|
| 74 |
st.write(message["content"])
|
| 75 |
|
| 76 |
-
|
|
|
|
|
|
|
| 77 |
st.sidebar.button('Clear Chat History', on_click=clear_chat_history)
|
| 78 |
|
| 79 |
-
#
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 82 |
with st.chat_message("user"):
|
| 83 |
st.write(prompt)
|
| 84 |
|
| 85 |
-
|
|
|
|
| 86 |
with st.chat_message("assistant"):
|
| 87 |
-
with st.spinner("
|
| 88 |
-
response =
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
st.error("未能生成响应,请检查模型状态。")
|
|
|
|
| 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 |
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
# 下载模型
|
| 11 |
+
os.system('huggingface-cli download --resume-download sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2 --local-dir /root/model/sentence-transformer')
|
| 12 |
+
# Create an instance of CallbackManager
|
| 13 |
callback_manager = CallbackManager()
|
| 14 |
|
| 15 |
+
api_base_url = "https://internlm-chat.intern-ai.org.cn/puyu/api/v1/"
|
|
|
|
| 16 |
model = "internlm2.5-latest"
|
| 17 |
+
api_key = "eyJ0eXBlIjoiSldUIiwiYWxnIjoiSFM1MTIifQ.eyJqdGkiOiI4MDAwNzM0OCIsInJvbCI6IlJPTEVfUkVHSVNURVIiLCJpc3MiOiJPcGVuWExhYiIsImlhdCI6MTczNzczMjUwMCwiY2xpZW50SWQiOiJlYm1ydm9kNnlvMG5semFlazF5cCIsInBob25lIjoiMTk4MjEyMTUyNzEiLCJ1dWlkIjoiZjc2MDM3NTctMTU2Yy00MTM3LWE1YmEtNTk3MDljODRiNDRkIiwiZW1haWwiOiIiLCJleHAiOjE3NTMyODQ1MDB9.4zD9ixAv1JdP_AJLQhNW3tCgzCGquW6eFcbV0XNqmqCZ0pL5A4hIPVA0zeFleg-n04O1IsyIZZ0rmkATZ1V6_A"
|
| 18 |
+
|
| 19 |
+
# api_base_url = "https://api.siliconflow.cn/v1"
|
| 20 |
+
# model = "internlm/internlm2_5-7b-chat"
|
| 21 |
+
# api_key = "请填写 API Key"
|
| 22 |
|
| 23 |
+
llm =OpenAILike(model=model, api_base=api_base_url, api_key=api_key, is_chat_model=True,callback_manager=callback_manager)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
st.set_page_config(page_title="llama_index_demo", page_icon="🦜🔗")
|
| 28 |
+
st.title("llama_index_demo")
|
| 29 |
|
| 30 |
# 初始化模型
|
| 31 |
@st.cache_resource
|
| 32 |
def init_models():
|
| 33 |
+
embed_model = HuggingFaceEmbedding(
|
| 34 |
+
model_name="/root/model/sentence-transformer"
|
| 35 |
+
)
|
| 36 |
+
Settings.embed_model = embed_model
|
| 37 |
|
| 38 |
+
#用初始化llm
|
| 39 |
+
Settings.llm = llm
|
| 40 |
|
| 41 |
+
documents = SimpleDirectoryReader("/root/llamaindex_demo/data").load_data()
|
| 42 |
+
index = VectorStoreIndex.from_documents(documents)
|
| 43 |
+
query_engine = index.as_query_engine()
|
| 44 |
|
| 45 |
+
return query_engine
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
# 检查是否需要初始化模型
|
| 48 |
if 'query_engine' not in st.session_state:
|
| 49 |
st.session_state['query_engine'] = init_models()
|
| 50 |
|
|
|
|
| 51 |
def greet2(question):
|
| 52 |
+
response = st.session_state['query_engine'].query(question)
|
| 53 |
+
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
|
| 56 |
+
# Store LLM generated responses
|
| 57 |
+
if "messages" not in st.session_state.keys():
|
| 58 |
+
st.session_state.messages = [{"role": "assistant", "content": "你好,我是你的助手,有什么我可以帮助你的吗?"}]
|
| 59 |
|
| 60 |
+
# Display or clear chat messages
|
| 61 |
for message in st.session_state.messages:
|
| 62 |
with st.chat_message(message["role"]):
|
| 63 |
st.write(message["content"])
|
| 64 |
|
| 65 |
+
def clear_chat_history():
|
| 66 |
+
st.session_state.messages = [{"role": "assistant", "content": "你好,我是你的助手,有什么我可以帮助你的吗?"}]
|
| 67 |
+
|
| 68 |
st.sidebar.button('Clear Chat History', on_click=clear_chat_history)
|
| 69 |
|
| 70 |
+
# Function for generating LLaMA2 response
|
| 71 |
+
def generate_llama_index_response(prompt_input):
|
| 72 |
+
return greet2(prompt_input)
|
| 73 |
+
|
| 74 |
+
# User-provided prompt
|
| 75 |
+
if prompt := st.chat_input():
|
| 76 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 77 |
with st.chat_message("user"):
|
| 78 |
st.write(prompt)
|
| 79 |
|
| 80 |
+
# Gegenerate_llama_index_response last message is not from assistant
|
| 81 |
+
if st.session_state.messages[-1]["role"] != "assistant":
|
| 82 |
with st.chat_message("assistant"):
|
| 83 |
+
with st.spinner("Thinking..."):
|
| 84 |
+
response = generate_llama_index_response(prompt)
|
| 85 |
+
placeholder = st.empty()
|
| 86 |
+
placeholder.markdown(response)
|
| 87 |
+
message = {"role": "assistant", "content": response}
|
| 88 |
+
st.session_state.messages.append(message)
|
|
|