Saturday, May 25, 2024

How to Create AI Agents Locally with Custom Data

 This video is a step-by-step tutorial to locally create AI agents with your own data with LlamaIndex and Ollama.




import json
from llama_index.core.tools import QueryEngineTool, ToolMetadata
from llama_index.core.agent import ReActAgent
from llama_index.llms.ollama import Ollama
from llama_index.core import Settings

from llama_index.core import (
    SimpleDirectoryReader,
    VectorStoreIndex,
    StorageContext,
    load_index_from_storage,
)


llm = Ollama(model="llama3:latest", request_timeout=120.0)
Settings.llm = llm


try:
    storage_context = StorageContext.from_defaults(persist_dir="./storage/oracle")
    dba_index = load_index_from_storage(storage_context)
    index_loaded = True
except:
    index_loaded = False

print (index_loaded)    

oracle_docs = SimpleDirectoryReader(input_files=["oracledocs.pdf"]).load_data()

oracle_index = VectorStoreIndex.from_documents(oracle_docs)

# persist index
oracle_index.storage_context.persist(persist_dir="./storage/oracle")

oracle_engine = oracle_index.as_query_engine(similarity_top_k=3)

query_engine_tools = [
    QueryEngineTool(
        query_engine=oracle_engine,
        metadata=ToolMetadata(
            name="oracledocs",
            description=(
                "Provides information about Oracle. "
                "Use a detailed plain text question as input to the tool."
            ),
        ),
    ),
]


agent = ReActAgent.from_tools(query_engine_tools,llm=llm,verbose=True)

response = agent.chat("What is Oracle?")
print(str(response))

response = agent.chat(
    "What backup options are available in Oracle database?"
)
print(str(response))

No comments: