Spaces:
Build error
Build error
Upload 2 files
Browse files- app.py +83 -0
- data/deepseek.txt +15 -0
app.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 = "请填写 API Key"
|
| 13 |
+
|
| 14 |
+
# api_base_url = "https://api.siliconflow.cn/v1"
|
| 15 |
+
# model = "internlm/internlm2_5-7b-chat"
|
| 16 |
+
# api_key = "请填写 API Key"
|
| 17 |
+
print(api_key)
|
| 18 |
+
llm =OpenAILike(model=model, api_base=api_base_url, api_key=api_key, is_chat_model=True,callback_manager=callback_manager)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
st.set_page_config(page_title="llama_index_demo", page_icon="🦜🔗")
|
| 23 |
+
st.title("llama_index_demo")
|
| 24 |
+
|
| 25 |
+
# 初始化模型
|
| 26 |
+
@st.cache_resource
|
| 27 |
+
def init_models():
|
| 28 |
+
embed_model = HuggingFaceEmbedding(
|
| 29 |
+
model_name="/root/model/sentence-transformer"
|
| 30 |
+
)
|
| 31 |
+
Settings.embed_model = embed_model
|
| 32 |
+
|
| 33 |
+
#用初始化llm
|
| 34 |
+
Settings.llm = llm
|
| 35 |
+
|
| 36 |
+
documents = SimpleDirectoryReader("/root/llamaindex_demo/data").load_data()
|
| 37 |
+
index = VectorStoreIndex.from_documents(documents)
|
| 38 |
+
query_engine = index.as_query_engine()
|
| 39 |
+
|
| 40 |
+
return query_engine
|
| 41 |
+
|
| 42 |
+
# 检查是否需要初始化模型
|
| 43 |
+
if 'query_engine' not in st.session_state:
|
| 44 |
+
st.session_state['query_engine'] = init_models()
|
| 45 |
+
|
| 46 |
+
def greet2(question):
|
| 47 |
+
response = st.session_state['query_engine'].query(question)
|
| 48 |
+
return response
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
# Store LLM generated responses
|
| 52 |
+
if "messages" not in st.session_state.keys():
|
| 53 |
+
st.session_state.messages = [{"role": "assistant", "content": "你好,我是你的助手,有什么我可以帮助你的吗?"}]
|
| 54 |
+
|
| 55 |
+
# Display or clear chat messages
|
| 56 |
+
for message in st.session_state.messages:
|
| 57 |
+
with st.chat_message(message["role"]):
|
| 58 |
+
st.write(message["content"])
|
| 59 |
+
|
| 60 |
+
def clear_chat_history():
|
| 61 |
+
st.session_state.messages = [{"role": "assistant", "content": "你好,我是你的助手,有什么我可以帮助你的吗?"}]
|
| 62 |
+
|
| 63 |
+
st.sidebar.button('Clear Chat History', on_click=clear_chat_history)
|
| 64 |
+
|
| 65 |
+
# Function for generating LLaMA2 response
|
| 66 |
+
def generate_llama_index_response(prompt_input):
|
| 67 |
+
return greet2(prompt_input)
|
| 68 |
+
|
| 69 |
+
# User-provided prompt
|
| 70 |
+
if prompt := st.chat_input():
|
| 71 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 72 |
+
with st.chat_message("user"):
|
| 73 |
+
st.write(prompt)
|
| 74 |
+
|
| 75 |
+
# Gegenerate_llama_index_response last message is not from assistant
|
| 76 |
+
if st.session_state.messages[-1]["role"] != "assistant":
|
| 77 |
+
with st.chat_message("assistant"):
|
| 78 |
+
with st.spinner("Thinking..."):
|
| 79 |
+
response = generate_llama_index_response(prompt)
|
| 80 |
+
placeholder = st.empty()
|
| 81 |
+
placeholder.markdown(response)
|
| 82 |
+
message = {"role": "assistant", "content": response}
|
| 83 |
+
st.session_state.messages.append(message)
|
data/deepseek.txt
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
DeepSeek R1是由杭州深度求索人工智能基础技术研究有限公司(DeepSeek)开发的高性能AI推理模型,具有强大的推理能力,尤其在数学、代码和自然语言推理等复杂任务中表现出色。
|
| 2 |
+
核心特点
|
| 3 |
+
强化学习驱动:DeepSeek R1通过大规模强化学习技术进行后训练,无需大量标注数据,即可显著提升推理能力。
|
| 4 |
+
多阶段训练:模型采用多阶段训练方法,包括冷启动数据的监督微调和强化学习阶段,以优化推理模式。
|
| 5 |
+
高性能推理:在数学任务中,DeepSeek R1在AIME 2024上达到79.8%的准确率,在MATH-500上达到97.3%。在编程任务中,它在Codeforces上获得2029 Elo评分,超过96.3%的人类参与者。
|
| 6 |
+
开源与灵活部署:DeepSeek R1遵循MIT License,完全开源,支持本地部署和云服务。
|
| 7 |
+
应用场景
|
| 8 |
+
复杂问题解决:适用于需要多步骤推理和长链思维的任务。
|
| 9 |
+
编程辅助:能够提供代码分析、优化和工程解决方案。
|
| 10 |
+
知识型任务:在跨领域知识测试中表现卓越,适合需要准确事实和知识整合的场景。
|
| 11 |
+
版本与架构
|
| 12 |
+
DeepSeek R1包括多个版本,如DeepSeek-R1-Zero(纯强化学习版本)和基于Qwen、Llama蒸馏出的多个密集模型。这些版本为不同需求的用户提供了灵活的选择。
|
| 13 |
+
开发者支持
|
| 14 |
+
DeepSeek R1支持一键部署功能,例如腾讯云HAI服务可在3分钟内完成模型启动配置。此外,其开源特性也降低了开发门槛。
|
| 15 |
+
总体而言,DeepSeek R1凭借其强大的推理能力和灵活的部署方式,正在成为AI领域的重要力量,尤其在推理任务中展现出与OpenAI的o1相当的性能。
|