BinZhang commited on
Commit
c0a63e6
·
1 Parent(s): cbcef3f
Files changed (3) hide show
  1. app.py +14 -68
  2. b.py +22 -0
  3. requirements.txt +5 -11
app.py CHANGED
@@ -1,72 +1,18 @@
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.llms.huggingface import HuggingFaceLLM
5
 
6
- st.set_page_config(page_title="llama_index_demo", page_icon="🦜🔗")
7
- st.title("llama_index_demo")
 
8
 
9
- # 初始化模型
10
- @st.cache_resource
11
- def init_models():
12
- embed_model = HuggingFaceEmbedding(
13
- model_name="/root/model/sentence-transformer"
14
- )
15
- Settings.embed_model = embed_model
16
 
17
- llm = HuggingFaceLLM(
18
- model_name="/root/model/internlm2-chat-1_8b",
19
- tokenizer_name="/root/model/internlm2-chat-1_8b",
20
- model_kwargs={"trust_remote_code": True},
21
- tokenizer_kwargs={"trust_remote_code": True}
22
- )
23
- Settings.llm = llm
24
 
25
- documents = SimpleDirectoryReader("/root/llamaindex_demo/data").load_data()
26
- index = VectorStoreIndex.from_documents(documents)
27
- query_engine = index.as_query_engine()
28
-
29
- return query_engine
30
-
31
- # 检查是否需要初始化模型
32
- if 'query_engine' not in st.session_state:
33
- st.session_state['query_engine'] = init_models()
34
-
35
- def greet2(question):
36
- response = st.session_state['query_engine'].query(question)
37
- return response
38
-
39
-
40
- # Store LLM generated responses
41
- if "messages" not in st.session_state.keys():
42
- st.session_state.messages = [{"role": "assistant", "content": "你好,我是你的助手,有什么我可以帮助你的吗?"}]
43
-
44
- # Display or clear chat messages
45
- for message in st.session_state.messages:
46
- with st.chat_message(message["role"]):
47
- st.write(message["content"])
48
-
49
- def clear_chat_history():
50
- st.session_state.messages = [{"role": "assistant", "content": "你好,我是你的助手,有什么我可以帮助你的吗?"}]
51
-
52
- st.sidebar.button('Clear Chat History', on_click=clear_chat_history)
53
-
54
- # Function for generating LLaMA2 response
55
- def generate_llama_index_response(prompt_input):
56
- return greet2(prompt_input)
57
-
58
- # User-provided prompt
59
- if prompt := st.chat_input():
60
- st.session_state.messages.append({"role": "user", "content": prompt})
61
- with st.chat_message("user"):
62
- st.write(prompt)
63
-
64
- # Gegenerate_llama_index_response last message is not from assistant
65
- if st.session_state.messages[-1]["role"] != "assistant":
66
- with st.chat_message("assistant"):
67
- with st.spinner("Thinking..."):
68
- response = generate_llama_index_response(prompt)
69
- placeholder = st.empty()
70
- placeholder.markdown(response)
71
- message = {"role": "assistant", "content": response}
72
- st.session_state.messages.append(message)
 
1
+ from openai import OpenAI
 
 
 
2
 
3
+ base_url = "https://internlm-chat.intern-ai.org.cn/puyu/api/v1/"
4
+ api_key = "eyJ0eXBlIjoiSldUIiwiYWxnIjoiSFM1MTIifQ.eyJqdGkiOiIxMTIwNDk3OSIsInJvbCI6IlJPTEVfUkVHSVNURVIiLCJpc3MiOiJPcGVuWExhYiIsImlhdCI6MTczMzQxMjU1NCwiY2xpZW50SWQiOiJlYm1ydm9kNnlvMG5semFlazF5cCIsInBob25lIjoiMTUxMzcxMTY1MzEiLCJ1dWlkIjoiYmVlYTk0NTQtNWE5OS00OGNkLTgxNzctZDdjZWYzNmQwNTAxIiwiZW1haWwiOiIiLCJleHAiOjE3NDg5NjQ1NTR9.0-DNSkviINNJhGmx49-kUfTSRvyXNrT4LXU1sB01FprErwGCVinJStN5aNsaHjF2K95Pl7B15SQ_fa2l8cIT3Q"
5
+ model="internlm2.5-latest"
6
 
7
+ client = OpenAI(
8
+ api_key=api_key ,
9
+ base_url=base_url,
10
+ )
 
 
 
11
 
12
+ chat_rsp = client.chat.completions.create(
13
+ model=model,
14
+ messages=[{"role": "user", "content": "xtuner是什么?"}],
15
+ )
 
 
 
16
 
17
+ for choice in chat_rsp.choices:
18
+ print(choice.message.content)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from openai import OpenAI
2
+
3
+ base_url = "https://internlm-chat.intern-ai.org.cn/puyu/api/v1/"
4
+ api_key = "sk-请填写准确的 token!"
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
+ client = OpenAI(
12
+ api_key=api_key ,
13
+ base_url=base_url,
14
+ )
15
+
16
+ chat_rsp = client.chat.completions.create(
17
+ model=model,
18
+ messages=[{"role": "user", "content": "xtuner是什么?"}],
19
+ )
20
+
21
+ for choice in chat_rsp.choices:
22
+ print(choice.message.content)
requirements.txt CHANGED
@@ -1,11 +1,5 @@
1
- einops==0.7.0
2
- protobuf==5.26.1
3
- llama-index==0.10.38
4
- llama-index-llms-huggingface==0.2.0
5
- transformers[torch]==4.41.1
6
- huggingface_hub[inference]==0.23.1
7
- huggingface_hub==0.23.1
8
- sentence-transformers==2.7.0
9
- sentencepiece==0.2.0
10
- llama-index-embeddings-huggingface==0.2.0
11
- llama-index-embeddings-instructor==0.1.3
 
1
+ llama-index==0.11.20
2
+ llama-index-llms-replicate==0.3.0
3
+ llama-index-llms-openai-like==0.2.0
4
+ llama-index-embeddings-huggingface==0.3.1
5
+ llama-index-embeddings-instructor==0.2.1