-
Notifications
You must be signed in to change notification settings - Fork 10
/
29_rag_integrated_llamaindex_demo.py
46 lines (32 loc) · 1.28 KB
/
29_rag_integrated_llamaindex_demo.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
# -*- coding: utf-8 -*-
"""
@author:XuMing(xuming624@qq.com)
@description: agentica integrated llamaindex db demo
pip install llamaindex
"""
import sys
sys.path.append('..')
from agentica import Agent
from agentica.knowledge.llamaindex_knowledge import LlamaIndexKnowledge
from llama_index.core import (
SimpleDirectoryReader,
StorageContext,
VectorStoreIndex,
)
from llama_index.core.retrievers import VectorIndexRetriever
from llama_index.core.node_parser import SentenceSplitter
# Define the path to the document to be loaded into the knowledge base
file_path = "data/news_docs.txt"
documents = SimpleDirectoryReader(input_files=[file_path]).load_data()
splitter = SentenceSplitter(chunk_size=1024)
nodes = splitter.get_nodes_from_documents(documents)
storage_context = StorageContext.from_defaults()
index = VectorStoreIndex(nodes=nodes, storage_context=storage_context)
retriever = VectorIndexRetriever(index)
# Create a knowledge base from the vector store
knowledge = LlamaIndexKnowledge(retriever=retriever)
# Create an agent with the knowledge base
agent = Agent(knowledge=knowledge, search_knowledge=True, debug_mode=True, show_tool_calls=True)
# Use the assistant to ask a question and print a response.
r = agent.run("2023年全国田径锦标赛在哪里举办的?")
print(r)