Update app.py
Browse files
app.py
CHANGED
|
@@ -1,79 +1,92 @@
|
|
|
|
|
| 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 |
-
|
| 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 |
-
|
| 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="
|
| 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.write(message["content"])
|
| 60 |
-
|
| 61 |
def clear_chat_history():
|
| 62 |
st.session_state.messages = [{"role": "assistant", "content": "你好,我是你的助手,有什么我可以帮助你的吗?"}]
|
| 63 |
-
|
| 64 |
-
|
| 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..."):
|
|
@@ -81,4 +94,4 @@ if st.session_state.messages[-1]["role"] != "assistant":
|
|
| 81 |
placeholder = st.empty()
|
| 82 |
placeholder.markdown(response)
|
| 83 |
message = {"role": "assistant", "content": response}
|
| 84 |
-
st.session_state.messages.append(message)
|
|
|
|
| 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 |
+
|
|
|
|
|
|
|
| 8 |
callback_manager = CallbackManager()
|
| 9 |
+
|
| 10 |
+
from configparser import ConfigParser
|
| 11 |
+
|
| 12 |
+
api_key = os.environ.get('API_KEY')
|
| 13 |
+
|
| 14 |
+
os.system('git lfs install')
|
| 15 |
+
os.system('git clone https://www.modelscope.cn/Ceceliachenen/paraphrase-multilingual-MiniLM-L12-v2.git')
|
| 16 |
+
|
| 17 |
api_base_url = "https://internlm-chat.intern-ai.org.cn/puyu/api/v1/"
|
| 18 |
model = "internlm2.5-latest"
|
| 19 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
llm =OpenAILike(model=model, api_base=api_base_url, api_key=api_key, is_chat_model=True,callback_manager=callback_manager)
|
| 21 |
+
|
| 22 |
+
st.set_page_config(page_title="由llama_index构建的RAG应用demo", page_icon="🦜🔗")
|
| 23 |
+
|
|
|
|
| 24 |
st.title("llama_index_demo")
|
| 25 |
+
|
|
|
|
| 26 |
@st.cache_resource
|
| 27 |
def init_models():
|
| 28 |
+
"""
|
| 29 |
+
初始化并缓存模型。
|
| 30 |
+
|
| 31 |
+
本函数通过加载预训练的嵌入模型和语言模型来初始化设置,并构建查询引擎。
|
| 32 |
+
使用缓存装饰器是为了提高效率,避免重复初始化模型。
|
| 33 |
+
|
| 34 |
+
返回:
|
| 35 |
+
query_engine: 用于查询的引擎。
|
| 36 |
+
"""
|
| 37 |
embed_model = HuggingFaceEmbedding(
|
| 38 |
+
model_name="./paraphrase-multilingual-MiniLM-L12-v2"
|
| 39 |
)
|
| 40 |
Settings.embed_model = embed_model
|
|
|
|
|
|
|
| 41 |
Settings.llm = llm
|
|
|
|
| 42 |
documents = SimpleDirectoryReader("./data").load_data()
|
| 43 |
index = VectorStoreIndex.from_documents(documents)
|
| 44 |
query_engine = index.as_query_engine()
|
| 45 |
+
|
| 46 |
return query_engine
|
|
|
|
|
|
|
| 47 |
if 'query_engine' not in st.session_state:
|
| 48 |
st.session_state['query_engine'] = init_models()
|
| 49 |
+
|
| 50 |
def greet2(question):
|
| 51 |
+
"""
|
| 52 |
+
使用预设的question参数调用session_state中的query_engine来生成响应。
|
| 53 |
+
参数:
|
| 54 |
+
question (str): 一个字符串,代表用户的问题或查询。
|
| 55 |
+
返回:
|
| 56 |
+
response: query_engine对question的响应结果,类型依据具体实现而定。
|
| 57 |
+
"""
|
| 58 |
response = st.session_state['query_engine'].query(question)
|
| 59 |
return response
|
| 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 |
with st.chat_message(message["role"]):
|
| 66 |
st.write(message["content"])
|
| 67 |
+
|
| 68 |
def clear_chat_history():
|
| 69 |
st.session_state.messages = [{"role": "assistant", "content": "你好,我是你的助手,有什么我可以帮助你的吗?"}]
|
| 70 |
+
st.sidebar.button('清空聊天历史', on_click=clear_chat_history)
|
| 71 |
+
|
|
|
|
|
|
|
| 72 |
def generate_llama_index_response(prompt_input):
|
| 73 |
+
"""
|
| 74 |
+
根据输入的提示生成基于llama索引的响应。
|
| 75 |
+
此函数的作用是通过特定的提示输入,生成一个相应的响应。它调用了另一个函数greet2,
|
| 76 |
+
以完成响应的生成过程。这种封装方式允许在greet2函数中实现复杂的处理逻辑,
|
| 77 |
+
同时对外提供一个简单的接口。
|
| 78 |
+
参数:
|
| 79 |
+
prompt_input (str): 用于生成响应的输入提示。
|
| 80 |
+
返回:
|
| 81 |
+
str: 由greet2函数生成的响应。
|
| 82 |
+
"""
|
| 83 |
return greet2(prompt_input)
|
| 84 |
+
|
|
|
|
| 85 |
if prompt := st.chat_input():
|
| 86 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 87 |
with st.chat_message("user"):
|
| 88 |
st.write(prompt)
|
| 89 |
+
|
|
|
|
| 90 |
if st.session_state.messages[-1]["role"] != "assistant":
|
| 91 |
with st.chat_message("assistant"):
|
| 92 |
with st.spinner("Thinking..."):
|
|
|
|
| 94 |
placeholder = st.empty()
|
| 95 |
placeholder.markdown(response)
|
| 96 |
message = {"role": "assistant", "content": response}
|
| 97 |
+
st.session_state.messages.append(message)
|