llama_index: replicate with llama-index and streamlit - unable to find documents[Question]:

Question Validation

  • I have searched both the documentation and discord for an answer.

Question

I am running code below on a GPU via:

streamlit run replicate_lama2.py

import os
import traceback
import sys
import streamlit as st

os.environ["REPLICATE_API_TOKEN"] = "my_key"

from llama_index.llms import Replicate

llama2_7b_chat = "meta/llama-2-7b-chat:8e6975e5ed6174911a6ff3d60540dfd4844201974602551e10e9e87ab143d81e"
llm = Replicate(
    model=llama2_7b_chat,
    temperature=0.01,
    additional_kwargs={"top_p": 1, "max_new_tokens": 300},
)

from llama_index import VectorStoreIndex, SimpleDirectoryReader
from llama_index.embeddings import HuggingFaceEmbedding
from llama_index import ServiceContext

embed_model = HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5")
service_context = ServiceContext.from_defaults(
    llm=llm, embed_model=embed_model
)

documents = SimpleDirectoryReader("data").load_data()
index = VectorStoreIndex.from_documents(
    documents, service_context=service_context
)


#index = VectorStoreIndex.from_documents(documents)

# Get the query engine
query_engine = index.as_query_engine(streaming=True)

# Create a Streamlit web app
#st.title("LLM Query Interface")
query = st.text_input("Enter your query:")
submit_button = st.button("Submit")

if submit_button:
    # Query the engine with the defined query
    response = query_engine.query(query)
    st.write("### Query Result:")
    st.write(response)

My directory “data” that contains different files I want to perform my queries is in the same directory where replicate_lama2.py script it. When I run this I am getting streamlit to open this chat in web browser (firefox) and I can ask a question (which is definitely answerable from documents in /data dir) but instead of any answer I am getting output in attach, that says “No doc available”. How to make this work? Python version I am using is 3.10.4. Screen Shot 2024-01-19 at 11 45 48 AM

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 15 (4 by maintainers)

Most upvoted comments

I run the code below and again I am getting the same error: “No docs available” (see attached)

import os import traceback import sys import streamlit as st from pathlib import Path from llama_index.llms import Replicate from llama_index import VectorStoreIndex, SimpleDirectoryReader from llama_index.embeddings import HuggingFaceEmbedding from llama_index import ServiceContext from llama_index.readers.file.docs_reader import PDFReader, DocxReader

os.environ[“REPLICATE_API_TOKEN”] = @.***"

from llama_index.llms import Replicate

llama2_7b_chat = “meta/llama-2-7b-chat:8e6975e5ed6174911a6ff3d60540dfd4844201974602551e10e9e87ab143d81e” llm = Replicate( model=llama2_7b_chat, temperature=0.01, additional_kwargs={“top_p”: 1, “max_new_tokens”: 300}, )

embed_model = HuggingFaceEmbedding(model_name=“BAAI/bge-small-en-v1.5”) service_context = ServiceContext.from_defaults( llm=llm, embed_model=embed_model )

def read_files_from_directory(directory_path): pdf_reader = PDFReader() docx_reader = DocxReader()

documents = []  # Initialize an empty list to store loaded documents

directory = Path(directory_path)

for file in directory.iterdir():
    if file.suffix == '.pdf':
        documents.extend(pdf_reader.load_data(file))
    elif file.suffix == '.docx':
        documents.extend(docx_reader.load_data(file))

return documents

Use the function to load documents

documents = read_files_from_directory(“data”)

Create VectorStoreIndex with the loaded documents

index = VectorStoreIndex.from_documents(documents, service_context=service_context)

Get the query engine

query_engine = index.as_query_engine(streaming=True)

Create a Streamlit web app

st.title(“LLM Query Interface”) query = st.text_input(“Enter your query:”) submit_button = st.button(“Submit”)

if submit_button: # Query the engine with the defined query response = query_engine.query(query) st.write(“### Query Result:”) st.write(response)

On Jan 19, 2024, at 4:38 PM, dosubot[bot] @.@.>> wrote:

from pathlib import Path from llama_index.readers.file.docs_reader import PDFReader, DocxReader

Assuming you have a TXTReader class

from llama_index.readers.file.docs_reader import TXTReader

def read_files_from_directory(directory_path): pdf_reader = PDFReader() docx_reader = DocxReader() # txt_reader = TXTReader()

directory = Path(directory_path)

for file in directory.iterdir():
    if file.suffix == '.pdf':
        documents = pdf_reader.load_data(file)
    elif file.suffix == '.docx':
        documents = docx_reader.load_data(file)
    # elif file.suffix == '.txt':
    #     documents = txt_reader.load_data(file)

    # Process documents here

read_files_from_directory(“/path/to/your/directory”)