Spaces:
No application file
No application file
BinZhang
commited on
Commit
·
3e029de
1
Parent(s):
8a19836
dftmsg
Browse files- a.py +48 -0
- app.py +1 -1
- requirements.txt +0 -1
a.py
CHANGED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
os.environ['NLTK_DATA'] = '/root/nltk_data'
|
3 |
+
|
4 |
+
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
|
5 |
+
from llama_index.core.settings import Settings
|
6 |
+
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
7 |
+
from llama_index.legacy.callbacks import CallbackManager
|
8 |
+
from llama_index.llms.openai_like import OpenAILike
|
9 |
+
|
10 |
+
|
11 |
+
# Create an instance of CallbackManager
|
12 |
+
callback_manager = CallbackManager()
|
13 |
+
|
14 |
+
api_base_url = "https://internlm-chat.intern-ai.org.cn/puyu/api/v1/"
|
15 |
+
model = "internlm2.5-latest"
|
16 |
+
api_key = "请填写 API Key"
|
17 |
+
|
18 |
+
# api_base_url = "https://api.siliconflow.cn/v1"
|
19 |
+
# model = "internlm/internlm2_5-7b-chat"
|
20 |
+
# api_key = "请填写 API Key"
|
21 |
+
|
22 |
+
|
23 |
+
|
24 |
+
llm =OpenAILike(model=model, api_base=api_base_url, api_key=api_key, is_chat_model=True,callback_manager=callback_manager)
|
25 |
+
|
26 |
+
|
27 |
+
#初始化一个HuggingFaceEmbedding对象,用于将文本转换为向量表示
|
28 |
+
embed_model = HuggingFaceEmbedding(
|
29 |
+
#指定了一个预训练的sentence-transformer模型的路径
|
30 |
+
model_name="/root/model/sentence-transformer"
|
31 |
+
)
|
32 |
+
#将创建的嵌入模型赋值给全局设置的embed_model属性,
|
33 |
+
#这样在后续的索引构建过程中就会使用这个模型。
|
34 |
+
Settings.embed_model = embed_model
|
35 |
+
|
36 |
+
#初始化llm
|
37 |
+
Settings.llm = llm
|
38 |
+
|
39 |
+
#从指定目录读取所有文档,并加载数据到内存中
|
40 |
+
documents = SimpleDirectoryReader("/root/llamaindex_demo/data").load_data()
|
41 |
+
#创建一个VectorStoreIndex,并使用之前加载的文档来构建索引。
|
42 |
+
# 此索引将文档转换为向量,并存储这些向量以便于快速检索。
|
43 |
+
index = VectorStoreIndex.from_documents(documents)
|
44 |
+
# 创建一个查询引擎,这个引擎可以接收查询并返回相关文档的响应。
|
45 |
+
query_engine = index.as_query_engine()
|
46 |
+
response = query_engine.query("xtuner是什么?")
|
47 |
+
|
48 |
+
print(response)
|
app.py
CHANGED
@@ -29,7 +29,7 @@ Settings.embed_model = embed_model
|
|
29 |
Settings.llm = llm
|
30 |
|
31 |
# 从指定目录读取所有文档,并加载数据到内存中
|
32 |
-
documents = SimpleDirectoryReader("
|
33 |
|
34 |
# 创建一个 VectorStoreIndex,并使用之前加载的文档来构建索引
|
35 |
index = VectorStoreIndex.from_documents(documents)
|
|
|
29 |
Settings.llm = llm
|
30 |
|
31 |
# 从指定目录读取所有文档,并加载数据到内存中
|
32 |
+
documents = SimpleDirectoryReader("./data").load_data()
|
33 |
|
34 |
# 创建一个 VectorStoreIndex,并使用之前加载的文档来构建索引
|
35 |
index = VectorStoreIndex.from_documents(documents)
|
requirements.txt
CHANGED
@@ -5,4 +5,3 @@ llama-index-embeddings-huggingface
|
|
5 |
llama-index-embeddings-instructor
|
6 |
llama-index-core
|
7 |
python-dotenv
|
8 |
-
openai
|
|
|
5 |
llama-index-embeddings-instructor
|
6 |
llama-index-core
|
7 |
python-dotenv
|
|