-
Notifications
You must be signed in to change notification settings - Fork 345
/
Copy pathtest_rag.py
199 lines (158 loc) · 6.62 KB
/
test_rag.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import os
import pytest
from modelscope_agent.memory import MemoryWithRag
IS_FORKED_PR = os.getenv('IS_FORKED_PR', 'false') == 'true'
@pytest.mark.skipif(IS_FORKED_PR, reason='only run modelscope-agent main repo')
def test_memory_with_rag_simple():
memory = MemoryWithRag(
urls=['tests/samples/常见QA.pdf'],
use_knowledge_cache=False,
)
summary_str = memory.run(query='高德天气api怎么申请')
print(summary_str)
assert 'https://lbs.amap.com/api/javascript-api-v2/guide/services/weather' in summary_str
@pytest.mark.skipif(IS_FORKED_PR, reason='only run modelscope-agent main repo')
def test_memory_with_rag_update():
memory = MemoryWithRag(use_knowledge_cache=False)
summary_str = memory.run(
query='模型大文件上传失败怎么办', url=['tests/samples/modelscope_qa_2.txt'])
print(summary_str)
assert 'git-lfs' in summary_str
@pytest.mark.skipif(IS_FORKED_PR, reason='only run modelscope-agent main repo')
def test_memory_with_rag_multi_sources():
memory = MemoryWithRag(
urls=['tests/samples/modelscope_qa_2.txt', 'tests/samples/常见QA.pdf'],
use_knowledge_cache=False,
)
summary_str = memory.run('高德天气api怎么申请')
print(summary_str)
assert 'https://lbs.amap.com/api/javascript-api-v2/guide/services/weather' in summary_str
@pytest.mark.skipif(IS_FORKED_PR, reason='only run modelscope-agent main repo')
def test_memory_with_rag_cache():
MemoryWithRag(
urls=['tests/samples/modelscope_qa_2.txt', 'tests/samples/常见QA.pdf'],
storage_path='./tmp/',
use_knowledge_cache=False,
)
memory = MemoryWithRag(
storage_path='./tmp/',
use_knowledge_cache=True,
)
summary_str = memory.run('模型大文件上传失败怎么办')
print(summary_str)
assert 'git-lfs' in summary_str
@pytest.mark.skipif(IS_FORKED_PR, reason='only run modelscope-agent main repo')
def test_memory_with_rag_custom():
from llama_index.retrievers.bm25 import BM25Retriever
from llama_index.readers.json import JSONReader
from llama_index.core.extractors import TitleExtractor
memory = MemoryWithRag(
urls=['tests/samples/modelscope_qa_2.txt'],
storage_path='./tmp/',
memory_path='./tmp/',
use_knowledge_cache=False,
retriever=BM25Retriever,
loaders={'.json': JSONReader},
post_processors=[],
transformations=[TitleExtractor])
summary_str = memory.run('模型大文件上传失败怎么办')
print(summary_str)
assert 'git-lfs' in summary_str
@pytest.mark.skipif(IS_FORKED_PR, reason='only run modelscope-agent main repo')
def test_memory_with_rag_multi_modal():
memory = MemoryWithRag(
urls=['tests/samples/rag.png'],
use_knowledge_cache=False,
)
summary_str = memory.run('根据rag的流程图,loading的后一步是什么?')
print(summary_str)
assert 'indexing' in summary_str or 'Indexing' in summary_str or '索引' in summary_str
@pytest.mark.skipif(IS_FORKED_PR, reason='only run modelscope-agent main repo')
def test_memory_with_rag_multi_modal_ms():
from modelscope_agent.rag.reader.image import OcrParser
memory = MemoryWithRag(
urls=['tests/samples/rag.png'],
use_knowledge_cache=False,
image_parser=OcrParser(),
)
summary_str = memory.run('根据rag的流程图,loading的后一步是什么?')
print(summary_str)
assert 'indexing' in summary_str or 'Indexing' in summary_str or '索引' in summary_str
@pytest.mark.skipif(IS_FORKED_PR, reason='only run modelscope-agent main repo')
def test_memory_with_rag_no_use_llm():
memory = MemoryWithRag(use_knowledge_cache=False)
summary_str = memory.run(
query='模型大文件上传失败怎么办',
url=['tests/samples/modelscope_qa_2.txt'],
use_llm=False)
print(summary_str)
assert 'file_path' in summary_str
assert 'git-lfs' in summary_str
@pytest.mark.skipif(IS_FORKED_PR, reason='only run modelscope-agent main repo')
def test_memory_with_rag_mongodb_storage():
# $ mongod --dbpath ./mongodb --logpath ./mongo.log --fork
import os
import shutil
from llama_index.storage.docstore.mongodb import MongoDocumentStore
from llama_index.storage.index_store.mongodb import MongoIndexStore
MONGO_URI = os.environ.get('MONGO_URI', 'mongodb://localhost')
cache_dir = './tmp'
MemoryWithRag(
urls=['tests/samples/modelscope_qa_1.txt'],
storage_path=cache_dir,
use_knowledge_cache=True,
docstore=MongoDocumentStore.from_uri(MONGO_URI),
index_store=MongoIndexStore.from_uri(MONGO_URI))
# clean local cache
if os.path.exists(cache_dir):
shutil.rmtree(cache_dir)
memory = MemoryWithRag(
use_knowledge_cache=True,
storage_path=cache_dir,
docstore=MongoDocumentStore.from_uri(MONGO_URI),
index_store=MongoIndexStore.from_uri(MONGO_URI))
summary_str = memory.run('环境安装报错 missing xcrun 怎么办?')
print(summary_str)
assert 'xcode-select --install' in summary_str
@pytest.mark.skipif(IS_FORKED_PR, reason='only run modelscope-agent main repo')
def test_memory_with_rag_mongodb_reader():
# $ mongod --dbpath ./mongodb --logpath ./mongo.log --fork
# insert msg
import pymongo
client = pymongo.MongoClient()
db = client['test_db']
collection = db['myCollection']
collection.insert_one({
'content':
'Alice decided to compile a book of interviews with startup founders.'
})
# read from mongodb
from llama_index.readers.mongodb import SimpleMongoReader
MONGO_URI = 'mongodb://localhost'
reader = SimpleMongoReader(uri=MONGO_URI)
documents = reader.load_data(
db_name='test_db',
collection_name='myCollection',
field_names=['content'])
memory = MemoryWithRag(use_knowledge_cache=False, documents=documents)
res = memory.run('Who decided to compile a book?', use_llm=False)
print(res)
assert 'Alice' in res
@pytest.mark.skipif(IS_FORKED_PR, reason='only run modelscope-agent main repo')
def test_memory_with_rag_files():
MemoryWithRag(
urls=[
'tests/samples/modelscope_qa_2.txt', 'tests/samples/常见QA.pdf',
'tests/samples/modelscope_qa_1.txt'
],
storage_path='./tmp/',
use_knowledge_cache=False,
)
memory = MemoryWithRag(
storage_path='./tmp/',
use_knowledge_cache=True,
)
files = ['modelscope_qa_2.txt', '常见QA.pdf']
summary_str = memory.run('多卡环境,如何指定卡推理?', url=files)
print(summary_str)
assert 'gpu:0' in summary_str