sfang32 commited on
Commit
f4f92f9
·
verified ·
1 Parent(s): 9b5c1cb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -16
app.py CHANGED
@@ -1,24 +1,91 @@
1
- from openai import OpenAI
2
  import os
3
- base_url = "https://internlm-chat.intern-ai.org.cn/puyu/api/v1/"
 
 
 
 
 
 
 
 
 
 
4
  api_key = os.getenv('key_internlm')
5
- model="internlm2.5-latest"
6
 
7
- # base_url = "https://api.siliconflow.cn/v1"
8
- # api_key = "sk-请填写准确的 token!"
9
- # model="internlm/internlm2_5-7b-chat"
10
 
11
- question = "EMIC是什么?"
 
12
 
13
- client = OpenAI(
14
- api_key=api_key ,
15
- base_url=base_url,
 
 
 
16
  )
17
 
18
- chat_rsp = client.chat.completions.create(
19
- model=model,
20
- messages=[{"role": "user", "content": question}],
21
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
- for choice in chat_rsp.choices:
24
- print(choice.message.content)
 
 
 
 
 
 
 
 
 
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
+ # 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('key_internlm')
 
14
 
15
+ # api_base_url = "https://api.siliconflow.cn/v1"
16
+ # model = "internlm/internlm2_5-7b-chat"
17
+ # api_key = "请填写 API Key"
18
 
19
+ path_model_embedding = "sentence-transformer"
20
+ path_knowledge = "data_domain"
21
 
22
+ llm =OpenAILike(
23
+ model=model,
24
+ api_base=api_base_url,
25
+ api_key=api_key,
26
+ is_chat_model=True,
27
+ callback_manager=callback_manager
28
  )
29
 
30
+ st.set_page_config(page_title="llama_index_demo", page_icon="🦜🔗")
31
+ st.title("llama_index_demo")
32
+
33
+ # 初始化模型
34
+ @st.cache_resource
35
+ def init_models():
36
+ embed_model = HuggingFaceEmbedding(
37
+ model_name=path_model_embedding
38
+ )
39
+ Settings.embed_model = embed_model
40
+
41
+ #用初始化llm
42
+ Settings.llm = llm
43
+
44
+ documents = SimpleDirectoryReader(path_knowledge).load_data()
45
+ index = VectorStoreIndex.from_documents(documents)
46
+ query_engine = index.as_query_engine()
47
+
48
+ return query_engine
49
+
50
+ # 检查是否需要初始化模型
51
+ if 'query_engine' not in st.session_state:
52
+ st.session_state['query_engine'] = init_models()
53
+
54
+ def greet2(question):
55
+ response = st.session_state['query_engine'].query(question)
56
+ return response
57
+
58
+
59
+ # Store LLM generated responses
60
+ if "messages" not in st.session_state.keys():
61
+ st.session_state.messages = [{"role": "assistant", "content": "你好,我是你的助手,有什么我可以帮助你的吗?"}]
62
+
63
+ # Display or clear chat messages
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
+
71
+ st.sidebar.button('Clear Chat History', on_click=clear_chat_history)
72
+
73
+ # Function for generating LLaMA2 response
74
+ def generate_llama_index_response(prompt_input):
75
+ return greet2(prompt_input)
76
+
77
+ # User-provided prompt
78
+ if prompt := st.chat_input():
79
+ st.session_state.messages.append({"role": "user", "content": prompt})
80
+ with st.chat_message("user"):
81
+ st.write(prompt)
82
 
83
+ # Gegenerate_llama_index_response last message is not from assistant
84
+ if st.session_state.messages[-1]["role"] != "assistant":
85
+ with st.chat_message("assistant"):
86
+ with st.spinner("Thinking..."):
87
+ response = generate_llama_index_response(prompt)
88
+ placeholder = st.empty()
89
+ placeholder.markdown(response)
90
+ message = {"role": "assistant", "content": response}
91
+ st.session_state.messages.append(message)