Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
# Create an instance of CallbackManager
|
| 8 |
+
callback_manager = CallbackManager()
|
| 9 |
+
|
| 10 |
+
api_base_url = "https://internlm-chat.intern-ai.org.cn/puyu/api/v1/"
|
| 11 |
+
model = "internlm2.5-latest"
|
| 12 |
+
api_key = "请填写 API Key"
|
| 13 |
+
|
| 14 |
+
# api_base_url = "https://api.siliconflow.cn/v1"
|
| 15 |
+
# model = "internlm/internlm2_5-7b-chat"
|
| 16 |
+
# api_key = "eyJ0eXBlIjoiSldUIiwiYWxnIjoiSFM1MTIifQ.eyJqdGkiOiI2NDIwNzQyMiIsInJvbCI6IlJPTEVfUkVHSVNURVIiLCJpc3MiOiJPcGVuWExhYiIsImlhdCI6MTczMDgwNTgyNiwiY2xpZW50SWQiOiJlYm1ydm9kNnlvMG5semFlazF5cCIsInBob25lIjoiMTM0NTUxMjAyOTciLCJ1dWlkIjoiYTA4N2JhNjQtYjI0Zi00NGM3LTlhYWQtMjZmNDRkZDljNDBmIiwiZW1haWwiOiIiLCJleHAiOjE3NDYzNTc4MjZ9._5eSNmjRIkWLjoGDCLaL26urtRP45f6x-cWXjZdC0DrN9svKGegFMbUj04v0cz_rXvOBYSgMzRoXGEk2KEoskA"
|
| 17 |
+
|
| 18 |
+
llm =OpenAILike(model=model, api_base=api_base_url, api_key=api_key, is_chat_model=True,callback_manager=callback_manager)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
st.set_page_config(page_title="llama_index_demo", page_icon="🦜🔗")
|
| 23 |
+
st.title("llama_index_demo")
|
| 24 |
+
|
| 25 |
+
# 初始化模型
|
| 26 |
+
@st.cache_resource
|
| 27 |
+
def init_models():
|
| 28 |
+
embed_model = HuggingFaceEmbedding(
|
| 29 |
+
model_name="/root/model/sentence-transformer"
|
| 30 |
+
)
|
| 31 |
+
Settings.embed_model = embed_model
|
| 32 |
+
|
| 33 |
+
#用初始化llm
|
| 34 |
+
Settings.llm = llm
|
| 35 |
+
|
| 36 |
+
documents = SimpleDirectoryReader("/root/llamaindex_demo/data").load_data()
|
| 37 |
+
index = VectorStoreIndex.from_documents(documents)
|
| 38 |
+
query_engine = index.as_query_engine()
|
| 39 |
+
|
| 40 |
+
return query_engine
|
| 41 |
+
|
| 42 |
+
# 检查是否需要初始化模型
|
| 43 |
+
if 'query_engine' not in st.session_state:
|
| 44 |
+
st.session_state['query_engine'] = init_models()
|
| 45 |
+
|
| 46 |
+
def greet2(question):
|
| 47 |
+
response = st.session_state['query_engine'].query(question)
|
| 48 |
+
return response
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
# Store LLM generated responses
|
| 52 |
+
if "messages" not in st.session_state.keys():
|
| 53 |
+
st.session_state.messages = [{"role": "assistant", "content": "你好,我是你的助手,有什么我可以帮助你的吗?"}]
|
| 54 |
+
|
| 55 |
+
# Display or clear chat messages
|
| 56 |
+
for message in st.session_state.messages:
|
| 57 |
+
with st.chat_message(message["role"]):
|
| 58 |
+
st.write(message["content"])
|
| 59 |
+
|
| 60 |
+
def clear_chat_history():
|
| 61 |
+
st.session_state.messages = [{"role": "assistant", "content": "你好,我是你的助手,有什么我可以帮助你的吗?"}]
|
| 62 |
+
|
| 63 |
+
st.sidebar.button('Clear Chat History', on_click=clear_chat_history)
|
| 64 |
+
|
| 65 |
+
# Function for generating LLaMA2 response
|
| 66 |
+
def generate_llama_index_response(prompt_input):
|
| 67 |
+
return greet2(prompt_input)
|
| 68 |
+
|
| 69 |
+
# User-provided prompt
|
| 70 |
+
if prompt := st.chat_input():
|
| 71 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 72 |
+
with st.chat_message("user"):
|
| 73 |
+
st.write(prompt)
|
| 74 |
+
|
| 75 |
+
# Gegenerate_llama_index_response last message is not from assistant
|
| 76 |
+
if st.session_state.messages[-1]["role"] != "assistant":
|
| 77 |
+
with st.chat_message("assistant"):
|
| 78 |
+
with st.spinner("Thinking..."):
|
| 79 |
+
response = generate_llama_index_response(prompt)
|
| 80 |
+
placeholder = st.empty()
|
| 81 |
+
placeholder.markdown(response)
|
| 82 |
+
message = {"role": "assistant", "content": response}
|
| 83 |
+
st.session_state.messages.append(message)
|