maccmaccmaccc commited on
Commit
d3c0ee9
·
verified ·
1 Parent(s): 1225acd

Create app.py

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