NotFoundError: Error code: 404 – {‘error’: {‘code’: ‘404’, ‘message’: ‘Resource not found’}} when using Azure Openai with Langchain

I am facing an issue resource not found error.
This is what I have tried:

  1. Checked the version, azure_openai_api_key, modelname, version and
    everything is correct.
  2. The model was deployed yesterday so more than
    5 minutes have passed.
  3. I already have an embeddings model in Azure
    openAI and embeddings are created and stored in local.

I have cross checked the logs in Azure and the console says that the requests are reached.

I am not sure why I am getting this error even though everything seems to be correct and in line with Langchain and Azure OpenAI documentation.

import os
import dill  # Import dill instead of pickle
import streamlit as st
from dotenv import load_dotenv
from langchain_community.document_loaders import PyPDFLoader
from langchain_community.vectorstores import FAISS
from azure.storage.blob import BlobServiceClient
from langchain_openai import AzureOpenAIEmbeddings
from langchain_openai import AzureOpenAI
from langchain.chains import RetrievalQA
from langchain.prompts import PromptTemplate

load_dotenv()

//ex: entries in .env
AZURE_OPENAI_ENDPOINT="https://jkazureopenaiservice.openai.azure.com/"
AZURE_OPENAI_API_KEY = "key"
DEPLOYMENT_NAME = "gpt-4o"  # The name of your model deployment in Azure
AZURE_OPENAI_API_VERSION = "2024-05-13"
AZURE_OPENAI_DEPLOYMENT = "gpt-4o"


# Azure OpenAI configuration
AZURE_OPENAI_API_BASE = os.getenv("AZURE_OPENAI_API_BASE")
AZURE_OPENAI_API_KEY = os.getenv("AZURE_OPENAI_API_KEY")
AZURE_OPENAI_API_VERSION = "2024-05-13"
DEPLOYMENT_NAME = os.getenv("DEPLOYMENT_NAME")


# Path to the folder containing your PDFs
pdf_folder_path = './localKnowledgeHub'

# Initialize a list to hold all documents
all_documents = []

# Load each PDF file
for filename in os.listdir(pdf_folder_path):
    if filename.endswith('.pdf'):
        file_path = os.path.join(pdf_folder_path, filename)
        loader = PyPDFLoader(file_path)
        documents = loader.load()
        all_documents.extend(documents)

# Initialize the embeddings model
embeddings = AzureOpenAIEmbeddings(
    model="text-embedding-3-large"
)

# Create the FAISS vector store
#vector_store = FAISS.from_documents(all_documents, embeddings)
#vector_store.save_local("vectorstore")

# Load the vector store locally
vector_store = FAISS.load_local("vectorstore", embeddings, allow_dangerous_deserialization=True)

# Initialize the Azure OpenAI language model
llm = AzureOpenAI(
    api_key=AZURE_OPENAI_API_KEY,
    api_version=AZURE_OPENAI_API_VERSION,
    deployment_name=DEPLOYMENT_NAME,
    temperature=70
)

# Define the prompt template
PROMPT_TEMPLATE = """You are an AI Assistant. Given the following context:
{context}
Answer the following question:
{question}
Assistant:"""

PROMPT = PromptTemplate(
    template=PROMPT_TEMPLATE, input_variables=["context", "question"]
)
# Create a retrieval QA chain using LangChain with the custom prompt
qa_chain = RetrievalQA.from_chain_type(
    llm=llm,
    chain_type="stuff",  # 'stuff' means directly use documents retrieved
    retriever=vector_store.as_retriever(),
    chain_type_kwargs={"prompt": PROMPT}
)

# Set up the Streamlit UI
st.title("Chatbot")
st.write("Ask me anything a.")

# Input box for user questions
user_question = st.text_input("Question:")

# When the user submits a question
if user_question:    
    # Get the answer from the QA chain using the invoke method
    try:
        result = qa_chain.invoke({"query": user_question})
        answer = result['output_text']
    except Exception as e:
        answer = f"An error occurred: {str(e)}"
    
    # Display the answer
    st.write("Answer:", answer)

0

Instead of AzureOpenAI, try using AzureChatOpenAI.

from langchain_openai import AzureChatOpenAI

llm = AzureChatOpenAI(
    azure_deployment="name_of_your_deployment",  
    api_version="2024-05-01-preview", 
    temperature=0,
    max_tokens=None,
    # other params...
)

Details here: https://python.langchain.com/v0.2/docs/integrations/chat/azure_chat_openai/

6

According to this document the latest preview and GA versions are 2024-07-01-preview and 2024-06-01.

And whatever you given the api version is not supported, so you are getting no resource found error.

Try any one of the above api version.

I tried you code with sample pdf file give one of the above api version.

Output:

You can also find sample code in your ai studio chat playground, where you will the api version supported.

and the code.

It is also recommended to use azure_ad_token_provider instead of using api key.

Recognized by Microsoft Azure Collective

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật