Skip to content

Commit

Permalink
Merge pull request modelscope#3 from FredericW/main
Browse files Browse the repository at this point in the history
persist function added.
  • Loading branch information
ZiTao-Li authored Apr 16, 2024
2 parents 7b8b1fe + 8daaf4b commit 47a9e22
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 21 deletions.
12 changes: 7 additions & 5 deletions examples/conversation_with_RAG_agents/agent_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"class": "LlamaIndexAgent",
"args": {
"name": "AgentScope Tutorial Assistant",
"sys_prompt": "You're a helpful assistant. You need to generate answers based on the provided context.",
"sys_prompt": "You're an assistant helping new users to use AgentScope. You need to generate answers based on the provided context. You only provide the most relevant information. The answer is limited to be less than 100 words. If the key words of the question can be found in the provided context, the answer should contain the section which contains the answer, for example, saying 'You may refer to SECTION_NAME for more details.' The answers must to be itemized.",
"model_config_name": "qwen_config",
"emb_model_config_name": "qwen_emb_config",
"rag_config": {
Expand All @@ -22,15 +22,16 @@
"chunk_overlap": 40,
"similarity_top_k": 10,
"log_retrieval": false,
"recent_n_mem": 1
"recent_n_mem": 1,
"persist_dir": "../../rag_storage/tutorial_assist"
}
}
},
{
"class": "LlamaIndexAgent",
"args": {
"name": "AgentScope Framework Code Assistant",
"sys_prompt": "You're a helpful assistant about coding. You can very familiar with the framework code of AgentScope.",
"sys_prompt": "You're an assistant answering coding questions about AgentScope. The answer is sturucted as follows. (1) the key features, (2) an example showing how to use it, (3) where to find the example, and source codes.",
"model_config_name": "qwen_config",
"emb_model_config_name": "qwen_emb_config",
"rag_config": {
Expand Down Expand Up @@ -63,15 +64,16 @@
"chunk_overlap": 40,
"similarity_top_k": 10,
"log_retrieval": false,
"recent_n_mem": 1
"recent_n_mem": 1,
"persist_dir": "../../rag_storage/code_assist"
}
}
},
{
"class": "DialogAgent",
"args": {
"name": "Summarize Assistant",
"sys_prompt": "You are a helpful assistant that can summarize the answers of the previous two messages.",
"sys_prompt": "You summarize the answers of the previous two messages and remove the redundant information. The answer need to be simple and itemized. The answer needs to be less than 100 words.ex .",
"model_config_name": "qwen_config",
"use_memory": true
}
Expand Down
52 changes: 36 additions & 16 deletions examples/conversation_with_RAG_agents/rag/llama_index_rag.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from typing import Any, Optional, List, Union
from loguru import logger
import os.path

try:
from llama_index.core.readers.base import BaseReader
Expand All @@ -21,6 +22,8 @@
from llama_index.core.node_parser import SentenceSplitter
from llama_index.core import (
VectorStoreIndex,
StorageContext,
load_index_from_storage,
)
except ImportError:
BaseReader, BaseRetriever = None, None
Expand Down Expand Up @@ -136,9 +139,8 @@ def __init__(
super().__init__(model, emb_model, config, **kwargs)
self.retriever = None
self.index = None
self.persist_dir = kwargs.get("persist_dir", "/")
self.persist_dir = self.config.get("persist_dir", "/")
self.emb_model = emb_model
print(self.config)

# ensure the emb_model is compatible with LlamaIndex
if isinstance(emb_model, ModelWrapperBase):
Expand Down Expand Up @@ -251,21 +253,39 @@ def store_and_index(
# https://docs.llamaindex.ai/en/stable/module_guides/loading/ingestion_pipeline/root.html
transformations.append(self.emb_model)

if vector_store is not None:
pipeline = IngestionPipeline(
transformations=transformations,
vector_store=vector_store,
)
_ = pipeline.run(docs)
self.index = VectorStoreIndex.from_vector_store(vector_store)
if not os.path.exists(self.persist_dir):
# check if index is persisted, if not, calculate the index
if vector_store is None:
# No vector_store is provide,
# use in memory to construct an index
pipeline = IngestionPipeline(
transformations=transformations,
)
nodes = pipeline.run(documents=docs)
self.index = VectorStoreIndex(
nodes=nodes,
embed_model=self.emb_model,
)
else:
# use vector_store to construct an index
pipeline = IngestionPipeline(
transformations=transformations,
vector_store=vector_store,
)
_ = pipeline.run(docs)
self.index = VectorStoreIndex.from_vector_store(
vector_store=vector_store,
)
# persist the calculated index
self.index.storage_context.persist(persist_dir=self.persist_dir)
else:
# No vector store is provide, use simple in memory
pipeline = IngestionPipeline(
transformations=transformations,
)
nodes = pipeline.run(documents=docs)
self.index = VectorStoreIndex(
nodes=nodes,
# load the storage_context
storage_context = StorageContext.from_defaults(
persist_dir=self.persist_dir,
)
# construct index from
self.index = load_index_from_storage(
storage_context=storage_context,
embed_model=self.emb_model,
)

Expand Down

0 comments on commit 47a9e22

Please sign in to comment.