Find answers from the community

Updated 2 years ago

I got llama index working with a

I got llama index working with a personal OpenAI key. Now I tried changing the base and key to use Azure.

Plain Text
os.environ["OPENAI_API_KEY"] = args.openai_key
os.environ["OPENAI_API_BASE"] = args.openai_host

openai.api_type = "azure"
openai.api_version = "2023-03-15"
openai.api_base = os.getenv('OPENAI_API_BASE')
openai.api_key = os.getenv("OPENAI_API_KEY")


But I'm getting this error, now:
openai.error.InvalidRequestError: Must provide an 'engine' or 'deployment_id' parameter to create a <class 'openai.api_resources.embedding.Embedding'>
O
L
33 comments
I also used:
Plain Text
llm = AzureOpenAI(temperature=0, model_name=args.model_name, deployment_name=args.model_name)

But that doesn't fix it.

The model name and deployment name are both text-davinci-003
The error seems to be occurring on:
Plain Text
index = GPTVectorStoreIndex.from_documents(documents, service_context=service_context)
I saw this is supposed to be fixed by https://github.com/jerryjliu/llama_index/pull/3140. I'm on 0.6.9 but it doesn't work still.
yea. I've just gone over the code 4 or 5 times and it matches everywhere from what I can tell. I'm still getting:
Plain Text
openai.error.InvalidRequestError: Must provide an 'engine' or 'deployment_id' parameter to create a <class 'openai.api_resources.embedding.Embedding'>
It happens when it runs this:
Plain Text
index = GPTVectorStoreIndex.from_documents(documents, service_context=service_context)
Here is how I create the service_context:
Plain Text
def create_service_context():
    # define LLM
    if openai.api_type == "azure":
        # Use Azure OpenAI API
        logging.info("Using Azure OpenAI.")
        #llm = AzureOpenAI(temperature=0, model_name=args.model_name, deployment_name=args.model_name)
        llm = AzureOpenAI(
            deployment_name=args.model_name,
            temperature=0,
            model_kwargs={
                "api_key": openai.api_key,
                "api_base": openai.api_base,
                "api_type": openai.api_type,
                "api_version": openai.api_version,
            }
        )
    else:
        # Using OpenAI API
        if args.model_name == "gpt-3.5-turbo":
            logging.info("Using ChatOpenAI.")
            llm = ChatOpenAI(temperature=0, model_name="gpt-3.5-turbo")
        else:
            logging.info("Using OpenAI.")
            llm = OpenAI(temperature=0, model_name=args.model_name)

    # Define LLMPredictor
    llm_predictor = LLMPredictor(llm=llm)

    # Define Embedding Model
    embedding_llm = LangchainEmbedding(
        OpenAIEmbeddings(
            model="text-embedding-ada-002",
            deployment="text-embedding-ada-002",
            openai_api_key=openai.api_key,
            openai_api_base=openai.api_base,
            openai_api_type=openai.api_type,
            openai_api_version=openai.api_version,
        ),
        embed_batch_size=1,
    )

    # define prompt helper
    # set maximum input size
    max_input_size = 4096

    # set number of output tokens
    num_output = 256
    
    # set maximum chunk overlap
    max_chunk_overlap = 20
    
    prompt_helper = PromptHelper(max_input_size, num_output, max_chunk_overlap)

    return ServiceContext.from_defaults(
        llm_predictor=llm_predictor,
        prompt_helper=prompt_helper,
        embed_model=embedding_llm
    )
I mean, that looks correct πŸ€” I don't have azure access to test though lol

Maybe @disiok or @jerryjliu0 see an obvious issue?
Been banging my head against this for a few days and was the primary reason for joining the channel as I'm at a loss.
Seems like it should just work
I even blew away my conda env and reinstalled to make sure it wasn't some residual issue.
Have you seen this thread?

https://github.com/jerryjliu/llama_index/issues/3264

Tbh idk why it's so complicated
lol reading that right now
I've copied their code nearly exactly the same and it still gives the same error
I just copied the code exactly as suggested there and it did not work
I think I may have discovered a bug
It seems like it unfortunately πŸ˜… you could also try downgrading llama index for now, because it seems like it worked at one point πŸ€”
I reproduced their code exactly (on 0.6.9) and this does not work:

Plain Text
from llama_index import ServiceContext, GPTVectorStoreIndex, LLMPredictor, LangchainEmbedding, SimpleDirectoryReader
from langchain.chat_models import AzureChatOpenAI
from langchain.embeddings import OpenAIEmbeddings

API_KEY = "<my key>"
API_BASE = "https://<resource name>.openai.azure.com/"
files_path = "/Users/abissell/az/files/"
embed_deployment_name = ""
chat_deployment_name = ""

embedding = OpenAIEmbeddings(
    deployment=embed_deployment_name,
    openai_api_key=API_KEY,
    openai_api_base=API_BASE,
    openai_api_type="azure",
    openai_api_version="2022-12-01",
)
embedding_llm = LangchainEmbedding(embedding, embed_batch_size=1)

llm = AzureChatOpenAI(
    deployment_name=chat_deployment_name,
    openai_api_key=API_KEY,
    openai_api_base=API_BASE,
    openai_api_type="azure",
    openai_api_version="2023-03-15-preview",
)
llm_predictor = LLMPredictor(llm=llm)

service_context = ServiceContext.from_defaults(
    llm_predictor=llm_predictor,
    embed_model=embedding_llm,
)
documents = SimpleDirectoryReader(files_path).load_data()
index = GPTVectorStoreIndex.from_documents(documents, service_context=service_context)
query_engine = index.as_query_engine()
print(query_engine.query("sample"))
downgraded to 0.6.5. Same error
Ahhh...downgraded langchain to 0.0.166 and now it works
Oooo interesting, a langchain issue! πŸ‘€
Ohhh another goofy bug.
So when you set the API version for the embedding, it looks like it changes the api_version of openai. Which means when you try to query the index, it uses the wrong api version and results in Resource Not Found
But, if you create the embedding before you create the AzureChatOpenAI object, you don't see the error
Man... classic microsoft making everything difficult :PepeHands:
I think it's a problem with everything referencing this global object under the hood. It breaks a lot of Object-Oriented Programming assumptions.
Yea, like all of that gets set on that global openai object right? Kinda messy
Yea. it wouldn't be so bad if they weren't modifying them too.
And, I moved langchain up to 0.0.173 and it still works now. So it seems like it's all an order of operations issue
0.0.175 (latest) also works.
Good to know! πŸ˜…
Add a reply
Sign up and join the conversation on Discord