Find answers from the community

b
bixqu
Offline, last seen 3 months ago
Joined September 25, 2024
b
bixqu
·

```

Plain Text
base_index = VectorStoreIndex.from_documents(documents, 
                                        storage_context=storage_context,
                                        service_context=service_context,
                                        show_progress=True)


Does this line embed my documents and save them to my custom vector store? If so, can I call it without passing it to a variable, and then simply query the vector store directly, something like

Plain Text
VectorStoreIndex.from_documents(documents, 
                                        storage_context=storage_context,
                                        service_context=service_context,
                                        show_progress=True)
index = VectorStoreIndex.from_vector_store(vector_store=vector_store, 
                                               service_context = service_context)
retriever = index.as_retriever(similarity_top_k = similarity_top_k)


or do I need to pass the from_documents part to a variable even if I don't use it ?
6 comments
b
L
b
bixqu
·

```

Plain Text
query_engine_base = RetrieverQueryEngine.from_args(retriever, 
                                                   service_context=service_context)
        response = query_engine_base.query(query)

I have two questions here. By default, how many chunks get sent as a context to my llm ? And second, can I pass in the above code a similarity_top_k argument that lets me control how many of the most similar chunks get appended to the context ?
4 comments
b
L
I am building a retriever using llamaindex's display_source_node function :

Plain Text
for node in nodes:
    display_source_node(node, source_length=10000)

and it returns a structure like so:

Plain Text
Node ID: d4d67180-71c8-4328-b3f1-1e98fa42ab69
Similarity: 0.8694979150607424
Text: We also list two qualitative examples where safety [...]


This is great, but can I somehow return the Source Document (name of the file + extension) to which the text belongs?

Plain Text
Node ID:
Source Document: example5.pdf
Similarity:
Text: 

Something like this ?
2 comments
b
R
How can I specify the chunk size when creating a neo4j knowledge graph rag script? Before the v010 update, you specified chunk_size in the service context. Now I see you write settings.chunk_size but this seems to have no effect on the graph stored in neo4j. How can I explicitly provide the chunk size to one of the classes (possibly the KnowlegeGraphIndex)?
2 comments
b
L
b
bixqu
·

Index

From this llmsherpa example (https://github.com/nlmatics/llmsherpa), I am trying to run this chunk:

Plain Text
from llama_index.core import Document
from llama_index.core import VectorStoreIndex

index = VectorStoreIndex(embed_model=embed_model)
for chunk in doc.chunks():
    index.insert(Document(text=chunk.to_context_text(), extra_info={}))
query_engine = index.as_query_engine()

# Let's run one query
response = query_engine.query("Tell me about Europe.")
print(response)


and I get

Plain Text
ValueError                                Traceback (most recent call last)
Cell In[18], line 4
      1 from llama_index.core import Document
      2 from llama_index.core import VectorStoreIndex
----> 4 index = VectorStoreIndex(embed_model=embed_model)
      5 for chunk in doc.chunks():
      6     index.insert(Document(text=chunk.to_context_text(), extra_info={}))

...
---> 59     raise ValueError("One of nodes, objects, or index_struct must be provided.")
     60 if index_struct is not None and nodes is not None:
     61     raise ValueError("Only one of nodes or index_struct can be provided.")

ValueError: One of nodes, objects, or index_struct must be provided.


Is this something that has changed in the newest llamaindex version? Any ideas on how I can fix it?
1 comment
L
KnowledgeGraphIndex in LLamaindex seems to be broken. Here is the complete code to generate a knowledge graph:

Plain Text
from llama_index.core import SummaryIndex, Document

index = SummaryIndex([])
text_chunks = ["cars are red", "cars are big", "elephants are red"]

doc_chunks = []
for i, text in enumerate(text_chunks):
    doc = Document(text=text, id_=f"doc_id_{i}")
    doc_chunks.append(doc)

kg_index = KnowledgeGraphIndex.from_documents(documents=doc_chunks,
                                            storage_context=storage_context,
                                            max_triplets_per_chunk=3,
                                            space_name=space_name,
                                            edge_types=edge_types,
                                            rel_prop_names=rel_prop_names,
                                            tags=tags,
                                            llm=llm,
                                            embed_model=embed_model,
                                            include_embeddings=True)

import networkx as nx
G = kg_index.get_networkx_graph()
nx.draw(G, with_labels=True, font_weight='bold')


And I get this:
34 comments
b
L
k
d
Nebula Graph seems to break in the v010 Llamaindex version. The code below:

Plain Text
documents = SimpleDirectoryReader("./data").load_data()


kg_index = KnowledgeGraphIndex.from_documents(documents=documents,
                                            storage_context=storage_context,
                                            max_triplets_per_chunk=2,
                                            space_name=space_name,
                                            edge_types=edge_types,
                                            llm=llm,
                                            embed_model=embed_model,
                                            rel_prop_names=rel_prop_names,
                                            tags=tags,
                                            include_embeddings=True)

kg_index.storage_context.persist(persist_dir='./storage_graph')
hybrid_query_engine = kg_index.as_query_engine(include_text=True,
                                                llm=llm,
                                                response_mode="tree_summarize",
                                                embedding_mode="hybrid",
                                                similarity_top_k=3,
                                                explore_global_knowledge=True)

query_text = "What is education?"
response = hybrid_query_engine.query(query_text)


throws an error:

Plain Text
Query failed. Query: WITH map{`true`: '-[', `false`: '<-['} AS arrow_l,     map{`true`: ']->', `false`: ']-'} AS arrow_r,     map{`relationship`: "relationship"} AS edge_type_map MATCH p=(start)-[e:`relationship`*..2]-()   WHERE id(start) IN $subjs WITH start, id(start) AS vid, nodes(p) AS nodes, e AS rels [...]


and the knowledge graph it generates has 0 nodes and 0 edges. Anyone has an idea on how this can be solved 😦 ?
10 comments
L
b
Any idea why this happens in llama_index 0.10.15 ?
7 comments
W
b
Hi, I am using a server that is configured with litellm, and I have an SSH tunnel that hits my localhost. For defining an llm I use :

Plain Text
from llama_index.llms import OpenAILike

llm = OpenAILike(api_base="http://localhost:8000",\
                 model="gpt_35_turbo",
                 api_key="sk-xxx")  

and it works perfectly. The problem is I can't seem to use OpenAILike to get my embedding model as well. How can I do this?
25 comments
L
I
b
Any idea why the FaithfulnessEvaluator returns an answer to the question instead of a Yes or No ?

Plain Text
import pandas as pd

def display_eval_df(query, response, eval_result):
    eval_df = pd.DataFrame(
        {
            "Query": str(query),
            "Response": str(response),
            "Source": response.source_nodes[0].node.get_content()[:500] + "...",
            "Evaluation Result": eval_result.feedback
        },
        index=[0],
    )
    eval_df = eval_df.style.set_properties(
        **{
            "inline-size": "600px",
            "overflow-wrap": "break-word",
        },
        subset=["Response", "Source"]
    )
    display(eval_df)

prompt_helper = PromptHelper(context_window=4096, 
                            num_output=256, 
                            chunk_overlap_ratio=0.1, 
                            chunk_size_limit=None)

service_context = ServiceContext.from_defaults(llm=llm,
                                                embed_model=embed_model, 
                                                prompt_helper=prompt_helper)
query_engine = rag.custom_retriever(query=None, 
                                    documents=documents,
                                    method='top_k',
                                    rag=True,
                                    chunk_size=512,
                                    similarity_top_k=1,
                                    eval=True)

faithfulness_evaluator = FaithfulnessEvaluator(service_context=service_context)
response_vector = query_engine.query(eval_questions[1])
eval_result = faithfulness_evaluator.evaluate_response(response=response_vector)

display_eval_df(eval_questions[1], response_vector, eval_result)
3 comments
b
W
b
bixqu
·

Ipenai

Hi, I am going crazy with finding a solution to this. I am working with a proxy server for OpenAI models. I'm using an ssh tunnel to hit the server on my localhost.

Plain Text
from openai import OpenAI

client = OpenAI(base_url="http://localhost:8000", api_key="sk-xxx")

response = client.chat.completions.create(model="gpt_35_turbo", messages = [
    {
        "role": "user",
        "content": "this is a test request, write a short poem"
    }
])

print(response)

This works perfectly. I need to use this with llamaindex, so I need to define my llm and embedding_model, and serve them to my service_context like so:

Plain Text
llm = OpenAI(model="text-davinci-003", temperature=0, max_tokens=256)
embed_model = OpenAIEmbedding()
text_splitter = SentenceSplitter(chunk_size=1024, chunk_overlap=20)
prompt_helper = PromptHelper(
    context_window=4096,
    num_output=256,
    chunk_overlap_ratio=0.1,
    chunk_size_limit=None,
)

service_context = ServiceContext.from_defaults(
    llm=llm,
    embed_model=embed_model,
    text_splitter=text_splitter,
    prompt_helper=prompt_helper,
)

where I would be calling my own "ada-02" model through the server as well. How can I make this work with my setup? I am totally unable to find any answer to this anywhere and I've already wasted days trying to fix it.
6 comments
b
L
When doing small-to-big retrieval, how do I store my vectors in a custom vector database?

If I do regular RAG (top-k, similar sized chunks) I simply write:

Plain Text
VectorStoreIndex.from_documents(documents,
                                        storage_context=self.storage_context,
                                        service_context=service_context,
                                        show_progress=False)

where I provide my storage context directing me to the custom vector DB I am running locally (PGVector).
3 comments
b
I am trying to implement small-to-big retrieval and in this chunk of code

Plain Text
vector_index_chunk = VectorStoreIndex(all_nodes, 
                                              service_context=self.service_context,
                                              storage_context=self.storage_context)
index = VectorStoreIndex.from_vector_store(vector_store=self.vector_store, 
                                           service_context=self.service_context)
vector_retriever_chunk = vector_index_chunk.as_retriever(similarity_top_k=2)
retriever_chunk = RecursiveRetriever("vector",
                                     retriever_dict={"vector": vector_retriever_chunk},
                                     node_dict=all_nodes_dict,
                                     verbose=True)

if rag:
    query_engine_chunk = RetrieverQueryEngine.from_args(retriever_chunk, 
                                                        service_context=self.service_context)
    response = query_engine_chunk.query(query)
    return str(response)
else:
    result = retriever_chunk.retrieve(query)
    res = []
    for node in result:
        res += [display_source_node_custom(node, 200, True)]
    return res

I want the function to store the vector_index_chunks and then to read them from the vector store. When I run this function (the whole thing not just this snippet) with rag = False, I get:

Plain Text
ValueError: Query id c092cd56-9404-43b8-84a0-d591c2cc2dc9 not found in either `retriever_dict` or `query_engine_dict`.

I'm guessing it's an error that the relevant node was not saved somehow, but I can't figure out what exactly causes the error.
11 comments
b
s
L
I have the weirdest problem I've encountered so far. This piece of code works perfectly:

Plain Text
from openai import OpenAI
client = OpenAI(base_url="http://localhost:xxx", api_key="xxx")
response = client.embeddings.create(input=["Your string here"], model="Azure-Text-Embedding-ada-002")
embedding_vector = response.data[0].embedding


however, if I try to pass client through a class, like this:

Plain Text
class CustomEmbedding:
    def __init__(self, client, model="Azure-Text-Embedding-ada-002"):
        self.client = client
        self.model = model

    def embed(self, texts):
        response = self.client.embeddings.create(input=texts, model=self.model)
        return [r.embedding for r in response.data]
    [...]

client = OpenAI(base_url="http://localhost:xxx", api_key="xxx")
embed_model = CustomEmbedding(client, model="Azure-Text-Embedding-ada-002")

I get that :

Plain Text
AttributeError: 'OpenAI' object has no attribute 'embeddings'

And I checked. self.client really does not have an embeddings attribute inside the class ,but it does have it outside of it. I literally have no clue what is going on. I need to wrap the client in this CustomEmbedding class, as I have no idea how else to define a llamaindex service_context (without a local openai key). Any ideas?
4 comments
b
L
I have a documents object made with SimpleDirectoryReader, from multiple files, and I am passing it to VectorStoreIndex.from_documents, like so:

Plain Text
from llama_index import VectorStoreIndex
index = VectorStoreIndex.from_documents(documents, 
                                        storage_context=storage_context,
                                        service_context=service_context, 
                                        show_progress=True)

and I get

Plain Text
AttributeError: 'str' object has no attribute 'get_doc_id'


Why does it see my documents object as a string? It's a dictionary structure.
14 comments
b
R