Spaces:
Sleeping
Sleeping
rewind version
Browse files
app.py
CHANGED
|
@@ -1,114 +1,84 @@
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
|
|
|
| 3 |
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settings
|
| 4 |
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
| 5 |
from llama_index.legacy.callbacks import CallbackManager
|
| 6 |
from llama_index.llms.openai_like import OpenAILike
|
|
|
|
|
|
|
| 7 |
# Create an instance of CallbackManager
|
| 8 |
callback_manager = CallbackManager()
|
| 9 |
-
|
| 10 |
-
|
|
|
|
| 11 |
api_key = os.environ.get('API_KEY')
|
| 12 |
-
|
|
|
|
| 13 |
os.system('git lfs install')
|
| 14 |
os.system('git clone https://www.modelscope.cn/Ceceliachenen/paraphrase-multilingual-MiniLM-L12-v2.git')
|
| 15 |
-
|
| 16 |
-
model =
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
st.
|
| 20 |
-
|
| 21 |
-
st.title("llama_index_demo")
|
| 22 |
# 初始化模型
|
| 23 |
@st.cache_resource
|
| 24 |
def init_models():
|
| 25 |
-
"""
|
| 26 |
-
初始化并缓存模型。
|
| 27 |
-
本函数通过加载预训练的嵌入模型和语言模型来初始化设置,并构建查询引擎。
|
| 28 |
-
使用缓存装饰器是为了提高效率,避免重复初始化模型。
|
| 29 |
-
返回:
|
| 30 |
-
query_engine: 用于查询的引擎。
|
| 31 |
-
"""
|
| 32 |
-
# 初始化嵌入模型
|
| 33 |
embed_model = HuggingFaceEmbedding(
|
| 34 |
model_name="./paraphrase-multilingual-MiniLM-L12-v2"
|
| 35 |
)
|
| 36 |
Settings.embed_model = embed_model
|
| 37 |
-
|
|
|
|
| 38 |
Settings.llm = llm
|
| 39 |
-
|
| 40 |
documents = SimpleDirectoryReader("./data").load_data()
|
| 41 |
index = VectorStoreIndex.from_documents(documents)
|
| 42 |
query_engine = index.as_query_engine()
|
|
|
|
| 43 |
return query_engine
|
|
|
|
| 44 |
# 检查是否需要初始化模型
|
| 45 |
if 'query_engine' not in st.session_state:
|
| 46 |
st.session_state['query_engine'] = init_models()
|
|
|
|
| 47 |
def greet2(question):
|
| 48 |
-
"""
|
| 49 |
-
使用预设的question参数调用session_state中的query_engine来生成响应。
|
| 50 |
-
参数:
|
| 51 |
-
question (str): 一个字符串,代表用户的问题或查询。
|
| 52 |
-
返回:
|
| 53 |
-
response: query_engine对question的响应结果,类型依据具体实现而定。
|
| 54 |
-
"""
|
| 55 |
-
# 从session_state字典中获取名为'query_engine'的引擎,并使用它来查询问题
|
| 56 |
response = st.session_state['query_engine'].query(question)
|
| 57 |
-
# 返回查询得到的响应结果
|
| 58 |
return response
|
| 59 |
-
|
| 60 |
-
|
|
|
|
| 61 |
if "messages" not in st.session_state.keys():
|
| 62 |
-
st.session_state.messages = [{"role": "assistant", "content": "你好,我是你的助手,有什么我可以帮助你的吗?"}]
|
| 63 |
-
|
|
|
|
| 64 |
for message in st.session_state.messages:
|
| 65 |
-
# 根据消息的角色类型创建聊天消息框
|
| 66 |
with st.chat_message(message["role"]):
|
| 67 |
-
|
| 68 |
-
|
| 69 |
def clear_chat_history():
|
| 70 |
-
"""
|
| 71 |
-
清除聊天记录并重置会话状态。
|
| 72 |
-
此函数将当前会话状态的消息清空,仅保留一条表示助手问候的初始消息。
|
| 73 |
-
这有助于为用户提供一个新的开始,并确保聊天记录不会变得过于冗长。
|
| 74 |
-
"""
|
| 75 |
st.session_state.messages = [{"role": "assistant", "content": "你好,我是你的助手,有什么我可以帮助你的吗?"}]
|
| 76 |
-
|
| 77 |
-
st.sidebar.button('
|
|
|
|
|
|
|
| 78 |
def generate_llama_index_response(prompt_input):
|
| 79 |
-
"""
|
| 80 |
-
根据输入的提示生成基于llama索引的响应。
|
| 81 |
-
此函数的作用是通过特定的提示输入,生成一个相应的响应。它调用了另一个函数greet2,
|
| 82 |
-
以完成响应的生成过程。这种封装方式允许在greet2函数中实现复杂的处理逻辑,
|
| 83 |
-
同时对外提供一个简单的接���。
|
| 84 |
-
参数:
|
| 85 |
-
prompt_input (str): 用于生成响应的输入提示。
|
| 86 |
-
返回:
|
| 87 |
-
str: 由greet2函数生成的响应。
|
| 88 |
-
"""
|
| 89 |
return greet2(prompt_input)
|
|
|
|
| 90 |
# User-provided prompt
|
| 91 |
-
# 如果用户通过聊天输入提供了信息,则执行以下操作
|
| 92 |
if prompt := st.chat_input():
|
| 93 |
-
# 将用户的聊天信息添加到会话状态的消息列表中
|
| 94 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 95 |
-
# 在聊天界面的用户消息区域显示用户输入的内容
|
| 96 |
with st.chat_message("user"):
|
| 97 |
-
st.
|
| 98 |
-
|
| 99 |
-
#
|
| 100 |
if st.session_state.messages[-1]["role"] != "assistant":
|
| 101 |
-
# 在助手的聊天消息框中
|
| 102 |
with st.chat_message("assistant"):
|
| 103 |
-
# 显示“Thinking...”动画,表示正在处理请求
|
| 104 |
with st.spinner("Thinking..."):
|
| 105 |
-
# 生成响应
|
| 106 |
response = generate_llama_index_response(prompt)
|
| 107 |
-
# 创建一个占位符,用于显示响应内容
|
| 108 |
placeholder = st.empty()
|
| 109 |
-
# 在占位符中显示响应内容
|
| 110 |
placeholder.markdown(response)
|
| 111 |
-
# 创建一个新的消息对象,表示助手的响应
|
| 112 |
message = {"role": "assistant", "content": response}
|
| 113 |
-
|
| 114 |
-
st.session_state.messages.append(message)
|
|
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
| 3 |
+
from configparser import ConfigParser
|
| 4 |
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settings
|
| 5 |
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
| 6 |
from llama_index.legacy.callbacks import CallbackManager
|
| 7 |
from llama_index.llms.openai_like import OpenAILike
|
| 8 |
+
|
| 9 |
+
|
| 10 |
# Create an instance of CallbackManager
|
| 11 |
callback_manager = CallbackManager()
|
| 12 |
+
|
| 13 |
+
api_base_url = "https://internlm-chat.intern-ai.org.cn/puyu/api/v1/"
|
| 14 |
+
model = "internlm2.5-latest"
|
| 15 |
api_key = os.environ.get('API_KEY')
|
| 16 |
+
|
| 17 |
+
# download embedding model
|
| 18 |
os.system('git lfs install')
|
| 19 |
os.system('git clone https://www.modelscope.cn/Ceceliachenen/paraphrase-multilingual-MiniLM-L12-v2.git')
|
| 20 |
+
|
| 21 |
+
llm = OpenAILike(model=model, api_base=api_base_url, api_key=api_key, is_chat_model=True, callback_manager=callback_manager)
|
| 22 |
+
|
| 23 |
+
st.set_page_config(page_title="llama_index_demo", page_icon="🦜🔗")
|
| 24 |
+
st.title("XTuner-Chat")
|
| 25 |
+
|
|
|
|
| 26 |
# 初始化模型
|
| 27 |
@st.cache_resource
|
| 28 |
def init_models():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
embed_model = HuggingFaceEmbedding(
|
| 30 |
model_name="./paraphrase-multilingual-MiniLM-L12-v2"
|
| 31 |
)
|
| 32 |
Settings.embed_model = embed_model
|
| 33 |
+
|
| 34 |
+
#用初始化llm
|
| 35 |
Settings.llm = llm
|
| 36 |
+
|
| 37 |
documents = SimpleDirectoryReader("./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.markdown(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.markdown(prompt)
|
| 75 |
+
|
| 76 |
+
# Gegenerate_llama_index_response if 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)
|
|
|