maccmaccmaccc commited on
Commit
a500343
·
verified ·
1 Parent(s): ecb7c81

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -2
app.py CHANGED
@@ -1,4 +1,84 @@
1
  import streamlit as st
 
 
 
 
 
2
 
3
- st.title("Test App")
4
- st.write("Hello World!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ llm = OpenAILike(
16
+ model=model,
17
+ api_base=api_base_url,
18
+ api_key=api_key,
19
+ is_chat_model=True,
20
+ callback_manager=callback_manager
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
+ # 使用 Hugging Face Hub 上的模型
30
+ embed_model = HuggingFaceEmbedding(
31
+ model_name="sentence-transformers/all-MiniLM-L6-v2"
32
+ )
33
+ Settings.embed_model = embed_model
34
+ Settings.llm = llm
35
+
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
+ 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)