run-llama / llama_index

LlamaIndex is a data framework for your LLM applications
https://docs.llamaindex.ai
MIT License
35.24k stars 4.95k forks source link

[Question]: Error #14793

Open medhsv opened 1 month ago

medhsv commented 1 month ago

Question Validation

Question

I want to use the following: LLM: Llama 2 7B chat Embed Model: sentence-transformers/all-mpnet-base-v2

can anybody provide me a fix

This is the Error: ValueError Traceback (most recent call last) /usr/local/lib/python3.10/dist-packages/llama_index/core/embeddings/utils.py in resolve_embed_model(embed_model, callback_manager) 58 embed_model = OpenAIEmbedding() ---> 59 validate_openai_api_key(embed_model.api_key) 60 except ImportError:

5 frames ValueError: No API key found for OpenAI. Please set either the OPENAI_API_KEY environment variable or openai.api_key prior to initialization. API keys can be found or created at https://platform.openai.com/account/api-keys

During handling of the above exception, another exception occurred:

ValueError Traceback (most recent call last) /usr/local/lib/python3.10/dist-packages/llama_index/core/embeddings/utils.py in resolve_embed_model(embed_model, callback_manager) 64 ) 65 except ValueError as e: ---> 66 raise ValueError( 67 "\n**\n" 68 "Could not load OpenAI embedding model. "

ValueError:


Could not load OpenAI embedding model. If you intended to use OpenAI, please check your OPENAI_API_KEY. Original error: No API key found for OpenAI. Please set either the OPENAI_API_KEY environment variable or openai.api_key prior to initialization. API keys can be found or created at https://platform.openai.com/account/api-keys

Consider using embed_model='local'. Visit our documentation for more embedding options: https://docs.llamaindex.ai/en/stable/module_guides/models/embeddings.html#modules


medhsv commented 1 month ago

This is where I am getting the error:

from llama_index.core import SummaryIndex, VectorStoreIndex

summary_index = SummaryIndex(nodes) vector_index = VectorStoreIndex(nodes)

Even though I have previously setup the llm and the embedding model

dosubot[bot] commented 1 month ago

To resolve the error related to the missing OpenAI API key when using Llama 2 7B chat as the LLM and sentence-transformers/all-mpnet-base-v2 as the embedding model, you need to ensure that your code does not default to using OpenAI embeddings. Here’s how you can explicitly set your embedding model:

  1. Ensure you are not defaulting to OpenAI embeddings: Make sure that your code explicitly sets the embedding model to sentence-transformers/all-mpnet-base-v2.

  2. Set up the embedding model correctly: You need to initialize the embedding model and pass it to the VectorStoreIndex and SummaryIndex.

Here is an example of how you can do this:

from llama_index import ServiceContext, VectorStoreIndex, SummaryIndex
from sentence_transformers import SentenceTransformer

# Initialize your embedding model
embed_model = SentenceTransformer('sentence-transformers/all-mpnet-base-v2')

# Create a ServiceContext with your embedding model
service_context = ServiceContext(embed_model=embed_model)

# Set the global service context to ensure it is used throughout your code
from llama_index import set_global_service_context
set_global_service_context(service_context)

# Now create your indices
summary_index = SummaryIndex(nodes, service_context=service_context)
vector_index = VectorStoreIndex(nodes, service_context=service_context)

By explicitly setting the embedding model and ensuring the global service context is used, you should avoid the fallback to OpenAI embeddings and the associated API key error [1][2].

To continue talking to Dosu, mention @dosu.

medhsv commented 1 month ago

I got the following error after running the above provided code:

TypeError Traceback (most recent call last) in <cell line: 7>() 5 6 # Create a ServiceContext with your embedding model ----> 7 service_context = ServiceContext(embed_model=embed_model) 8 9 # Set the global service context to ensure it is used throughout your code

1 frames /usr/local/lib/python3.10/dist-packages/pydantic/v1/dataclasses.py in handle_extra_init(self, *args, kwargs) 291 def handle_extra_init(self: 'Dataclass', *args: Any, *kwargs: Any) -> None: 292 if config.extra == Extra.ignore: --> 293 init(self, args, {k: v for k, v in kwargs.items() if k in self.__dataclass_fields__}) 294 295 elif config.extra == Extra.allow:

TypeError: ServiceContext.init() missing 5 required positional arguments: 'llm_predictor', 'prompt_helper', 'transformations', 'llama_logger', and 'callback_manager'

dosubot[bot] commented 1 month ago

The error indicates that the ServiceContext class requires additional positional arguments. To resolve this, you can use the from_defaults method to create a ServiceContext with default values for the other parameters.

Here is the updated code:

from llama_index import ServiceContext, VectorStoreIndex, SummaryIndex
from sentence_transformers import SentenceTransformer

# Initialize your embedding model
embed_model = SentenceTransformer('sentence-transformers/all-mpnet-base-v2')

# Create a ServiceContext with your embedding model using from_defaults
service_context = ServiceContext.from_defaults(embed_model=embed_model)

# Set the global service context to ensure it is used throughout your code
from llama_index import set_global_service_context
set_global_service_context(service_context)

# Now create your indices
summary_index = SummaryIndex(nodes, service_context=service_context)
vector_index = VectorStoreIndex(nodes, service_context=service_context)

This should resolve the TypeError by using the from_defaults method to initialize the ServiceContext with default values for the other required parameters [1].

To continue talking to Dosu, mention @dosu.

medhsv commented 1 month ago

I want to use Huggingface LLM Llama 2 7B chat. will this coode be able to support it

dosubot[bot] commented 1 month ago

Yes, the provided code will support using the Huggingface LLM Llama 2 7B chat. You can specify the model name for Llama 2 7B chat from Huggingface and use it in your setup. Here’s how you can do it:

from llama_index import ServiceContext, VectorStoreIndex, SummaryIndex
from sentence_transformers import SentenceTransformer
from llama_index.llms.openvino import OpenVINOLLM

# Initialize your embedding model
embed_model = SentenceTransformer('sentence-transformers/all-mpnet-base-v2')

# Create a ServiceContext with your embedding model using from_defaults
service_context = ServiceContext.from_defaults(embed_model=embed_model)

# Set the global service context to ensure it is used throughout your code
from llama_index import set_global_service_context
set_global_service_context(service_context)

# Initialize the Llama 2 7B chat model
ov_config = {
    "PERFORMANCE_HINT": "LATENCY",
    "NUM_STREAMS": "1",
    "CACHE_DIR": "",
}

llm = OpenVINOLLM(
    model_name="meta-llama/Llama-2-7b-chat-hf",
    tokenizer_name="meta-llama/Llama-2-7b-chat-hf",
    context_window=3900,
    max_new_tokens=256,
    model_kwargs={"ov_config": ov_config},
    generate_kwargs={"temperature": 0.7, "top_k": 50, "top_p": 0.95},
    device_map="auto",
)

# Now create your indices
summary_index = SummaryIndex(nodes, service_context=service_context)
vector_index = VectorStoreIndex(nodes, service_context=service_context)

# Example query
response = llm.complete("What is the meaning of life?")
print(str(response))

This setup ensures that the Llama 2 7B chat model from Huggingface is properly loaded and configured for use with the LlamaIndex framework [1][2][3].

To continue talking to Dosu, mention @dosu.

medhsv commented 1 month ago

still not solved still showing the openai error while trying to use huggingface llm

dosubot[bot] commented 1 month ago

To ensure that the Huggingface LLM and the specified embedding model are used correctly without defaulting to OpenAI embeddings, you need to explicitly set the embedding model and the LLM in the ServiceContext. Here’s how you can do it:

  1. Ensure you are not defaulting to OpenAI embeddings: Make sure that your code explicitly sets the embedding model to sentence-transformers/all-mpnet-base-v2.

  2. Set up the embedding model and LLM correctly: You need to initialize the embedding model and the LLM, and pass them to the ServiceContext.

Here is an example of how you can do this:

from llama_index import ServiceContext, VectorStoreIndex, SummaryIndex
from sentence_transformers import SentenceTransformer
from transformers import AutoModelForCausalLM, AutoTokenizer
from llama_index.llms.huggingface import HuggingFaceLLM

# Initialize your embedding model
embed_model = SentenceTransformer('sentence-transformers/all-mpnet-base-v2')

# Initialize the Llama 2 7B chat model
model_name = "meta-llama/Llama-2-7b-chat-hf"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)

llm = HuggingFaceLLM(model=model, tokenizer=tokenizer)

# Create a ServiceContext with your embedding model and LLM using from_defaults
service_context = ServiceContext.from_defaults(llm=llm, embed_model=embed_model)

# Set the global service context to ensure it is used throughout your code
from llama_index import set_global_service_context
set_global_service_context(service_context)

# Now create your indices
summary_index = SummaryIndex(nodes, service_context=service_context)
vector_index = VectorStoreIndex(nodes, service_context=service_context)

By explicitly setting the embedding model and the LLM, and ensuring the global service context is used, you should avoid the fallback to OpenAI embeddings and the associated API key error [1][2].

To continue talking to Dosu, mention @dosu.