resource not found with Azure OpenAI service

题意:使用 Azure OpenAI 服务时,系统未能找到所请求的资源

问题背景:

when I am using this demo code to use the Azure OpenAI service in Java 11:

当我在Java 11环境中使用这段示例代码来调用Azure OpenAI服务时:

cs 复制代码
package com.dolphin.soa.post.config.ai.azure;

import com.azure.ai.openai.OpenAIClient;
import com.azure.ai.openai.OpenAIClientBuilder;
import com.azure.ai.openai.models.*;
import com.azure.core.credential.AzureKeyCredential;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class AzureOpenAIClient {

    public static void getChatCompletion(){
        String azureOpenaiKey = "the-key";
        String endpoint = "https://reddwarfcv.openai.azure.com/";
        String deploymentOrModelId = "gpt-35-turbo";

        OpenAIClient client = new OpenAIClientBuilder()
                .endpoint(endpoint)
                .credential(new AzureKeyCredential(azureOpenaiKey))
                .buildClient();

        List<ChatMessage> chatMessages = new ArrayList<>();
        chatMessages.add(new ChatMessage(ChatRole.SYSTEM).setContent("You are a helpful assistant."));
        chatMessages.add(new ChatMessage(ChatRole.USER).setContent("Does Azure OpenAI support customer managed keys?"));
        chatMessages.add(new ChatMessage(ChatRole.ASSISTANT).setContent("Yes, customer managed keys are supported by Azure OpenAI?"));
        chatMessages.add(new ChatMessage(ChatRole.USER).setContent("Do other Azure Cognitive Services support this too?"));

        ChatCompletions chatCompletions = client.getChatCompletions(deploymentOrModelId, new ChatCompletionsOptions(chatMessages));

        System.out.printf("Model ID=%s is created at %d.%n", chatCompletions.getId(), chatCompletions.getCreated());
        for (ChatChoice choice : chatCompletions.getChoices()) {
            ChatMessage message = choice.getMessage();
            System.out.printf("Index: %d, Chat Role: %s.%n", choice.getIndex(), message.getRole());
            System.out.println("Message:");
            System.out.println(message.getContent());
        }

        System.out.println();
        CompletionsUsage usage = chatCompletions.getUsage();
        System.out.printf("Usage: number of prompt token is %d, "
                        + "number of completion token is %d, and number of total tokens in request and response is %d.%n",
                usage.getPromptTokens(), usage.getCompletionTokens(), usage.getTotalTokens());
    }

    public static void main(String[] args) {
        getChatCompletion();
    }

}

show error: 显示错误:

cs 复制代码
com.azure.core.exception.ResourceNotFoundException: Status code 404, "{"error":{"code":"DeploymentNotFound", "message":"The API deployment for this resource does not exist. If you created the deployment within the last 5 minutes, please wait a moment and try again."}}"
Exception in thread "main"  at com.azure.core.implementation.http.rest.RestProxyBase.instantiateUnexpectedException(RestProxyBase.java:347)
    at com.azure.core.implementation.http.rest.SyncRestProxy.ensureExpectedStatus(SyncRestProxy.java:130)
    at com.azure.core.implementation.http.rest.SyncRestProxy.handleRestReturnType(SyncRestProxy.java:213)
com.azure.core.exception.ResourceNotFoundException: Status code 404, "{"error":{"code":"DeploymentNotFound", "message":"The API deployment for this resource does not exist. If you created the deployment within the last 5 minutes, please wait a moment and try again."}}"
    at com.azure.core.implementation.http.rest.RestProxyBase.instantiateUnexpectedException(RestProxyBase.java:347)
    at com.azure.core.implementation.http.rest.SyncRestProxy.ensureExpectedStatus(SyncRestProxy.java:130)
    at com.azure.core.implementation.http.rest.SyncRestProxy.handleRestReturnType(SyncRestProxy.java:213)
    at com.azure.core.implementation.http.rest.SyncRestProxy.invoke(SyncRestProxy.java:81)
    at com.azure.core.implementation.http.rest.SyncRestProxy.invoke(SyncRestProxy.java:81)
    at com.azure.core.implementation.http.rest.RestProxyBase.invoke(RestProxyBase.java:109)
    at com.azure.core.http.rest.RestProxy.invoke(RestProxy.java:91)
    at jdk.proxy2/jdk.proxy2.$Proxy3.getChatCompletionsSync(Unknown Source)
    at com.azure.core.implementation.http.rest.RestProxyBase.invoke(RestProxyBase.java:109)
    at com.azure.ai.openai.implementation.OpenAIClientImpl.getChatCompletionsWithResponse(OpenAIClientImpl.java:757)
    at com.azure.core.http.rest.RestProxy.invoke(RestProxy.java:91)
    at jdk.proxy2/jdk.proxy2.$Proxy3.getChatCompletionsSync(Unknown Source)
    at com.azure.ai.openai.implementation.OpenAIClientImpl.getChatCompletionsWithResponse(OpenAIClientImpl.java:757)
    at com.azure.ai.openai.OpenAIClient.getChatCompletionsWithResponse(OpenAIClient.java:255)
    at com.azure.ai.openai.OpenAIClient.getChatCompletionsWithResponse(OpenAIClient.java:255)
    at com.azure.ai.openai.OpenAIClient.getChatCompletions(OpenAIClient.java:381)
    at com.dolphin.soa.post.config.ai.azure.AzureOpenAIClient.getChatCompletion(AzureOpenAIClient.java:37)
    at com.azure.ai.openai.OpenAIClient.getChatCompletions(OpenAIClient.java:381)
    at com.dolphin.soa.post.config.ai.azure.AzureOpenAIClient.main(AzureOpenAIClient.java:55)
    at com.dolphin.soa.post.config.ai.azure.AzureOpenAIClient.getChatCompletion(AzureOpenAIClient.java:37)
    at com.dolphin.soa.post.config.ai.azure.AzureOpenAIClient.main(AzureOpenAIClient.java:55)
13:35:44.075 [main] ERROR com.azure.core.implementation.http.rest.RestProxyBase - Status code 404, "{"error":{"code":"DeploymentNotFound", "message":"The API deployment for this resource does not exist. If you created the deployment within the last 5 minutes, please wait a moment and try again."}}"
com.azure.core.exception.ResourceNotFoundException: Status code 404, "{"error":{"code":"DeploymentNotFound", "message":"The API deployment for this resource does not exist. If you created the deployment within the last 5 minutes, please wait a moment and try again."}}"
    at com.azure.core.implementation.http.rest.RestProxyBase.instantiateUnexpectedException(RestProxyBase.java:347)
    at com.azure.core.implementation.http.rest.SyncRestProxy.ensureExpectedStatus(SyncRestProxy.java:130)
    at com.azure.core.implementation.http.rest.SyncRestProxy.handleRestReturnType(SyncRestProxy.java:213)
    at com.azure.core.implementation.http.rest.SyncRestProxy.invoke(SyncRestProxy.java:81)
    at com.azure.core.implementation.http.rest.RestProxyBase.invoke(RestProxyBase.java:109)
    at com.azure.core.http.rest.RestProxy.invoke(RestProxy.java:91)
    at jdk.proxy2/jdk.proxy2.$Proxy3.getChatCompletionsSync(Unknown Source)
    at com.azure.ai.openai.implementation.OpenAIClientImpl.getChatCompletionsWithResponse(OpenAIClientImpl.java:757)
    at com.azure.ai.openai.OpenAIClient.getChatCompletionsWithResponse(OpenAIClient.java:255)
    at com.azure.ai.openai.OpenAIClient.getChatCompletions(OpenAIClient.java:381)
    at com.dolphin.soa.post.config.ai.azure.AzureOpenAIClient.getChatCompletion(AzureOpenAIClient.java:37)
    at com.dolphin.soa.post.config.ai.azure.AzureOpenAIClient.main(AzureOpenAIClient.java:55)

I have checked all the configuration and make sure the endpoint was correct, the deployment was created more than 10 hours. Am I missing something? This is the dependencies:

我已经检查了所有配置,并确保端点地址是正确的,部署已经超过10个小时了。我是不是遗漏了什么?这是依赖项:"

cs 复制代码
implementation'com.azure:azure-ai-openai:1.0.0-beta.1'

I have already tried to upgrade the azure openai dependencies version to 1.0.0-beta.2 but still could not fixed this issue.

我已经尝试将Azure OpenAI的依赖项版本升级到1.0.0-beta.2,但仍然无法解决这个问题。

问题解决:

I tried in my environment and got the below results:

"我在我的环境中尝试了,并得到了以下结果:"

Initially, I got the same error in my environment.

"最初,我在我的环境中遇到了同样的错误。"

[main] ERROR com.azure.core.implementation.http.rest.RestProxyBase Status code 404, "{"error":{"code":"DeploymentNotFound", "message":"The API deployment for this resource does not exist. If you created the deployment within the last 5 minutes, please wait a moment and try again."}}"

In your code, at deploymentOrModelId , you have given gpt-35-turbo but you need to mention your deployment name. You can get the deployment name using the portal below.

在你的代码中,deploymentOrModelId 这个位置你填写了 gpt-35-turbo,但你需要提供你的部署名称。你可以使用下面的门户来获取你的部署名称。

Portal:

I tried with the same code change deployment id into deployment1 it executed successfully.

我尝试用相同的代码将部署ID更改为deployment1,它成功执行了。

Code:

cs 复制代码
import com.azure.ai.openai.OpenAIClient;
import com.azure.ai.openai.OpenAIClientBuilder;
import com.azure.ai.openai.models.*;
import com.azure.core.credential.AzureKeyCredential;


import java.util.ArrayList;
import java.util.List;

public class App {

    public static void getChatCompletion(){
        String azureOpenaiKey = "";
        String endpoint = "https://xxxxx.openai.azure.com/";
        String deploymentOrModelId = "deployment1";

        OpenAIClient client = new OpenAIClientBuilder()
                .endpoint(endpoint)
                .credential(new AzureKeyCredential(azureOpenaiKey))
                .buildClient();

        List<ChatMessage> chatMessages = new ArrayList<>();
        chatMessages.add(new ChatMessage(ChatRole.SYSTEM).setContent("You are a helpful assistant."));
        chatMessages.add(new ChatMessage(ChatRole.USER).setContent("Does Azure OpenAI support customer managed keys?"));
        chatMessages.add(new ChatMessage(ChatRole.ASSISTANT).setContent("Yes, customer managed keys are supported by Azure OpenAI?"));
        chatMessages.add(new ChatMessage(ChatRole.USER).setContent("Do other Azure Cognitive Services support this too?"));

        ChatCompletions chatCompletions = client.getChatCompletions(deploymentOrModelId, new ChatCompletionsOptions(chatMessages));

        System.out.printf("Model ID=%s is created at %d.%n", chatCompletions.getId(), chatCompletions.getCreated());
        for (ChatChoice choice : chatCompletions.getChoices()) {
            ChatMessage message = choice.getMessage();
            System.out.printf("Index: %d, Chat Role: %s.%n", choice.getIndex(), message.getRole());
            System.out.println("Message:");
            System.out.println(message.getContent());
        }

        System.out.println();
        CompletionsUsage usage = chatCompletions.getUsage();
        System.out.printf("Usage: number of prompt token is %d, "
                        + "number of completion token is %d, and number of total tokens in request and response is %d.%n",
                usage.getPromptTokens(), usage.getCompletionTokens(), usage.getTotalTokens());
    }

    public static void main(String[] args) {
        getChatCompletion();
    }

}

Output: 输出

cs 复制代码
Model ID=chatcmpl-xxxxx is created at 168776xxx.
Index: 0, Chat Role: assistant.
Message:
Yes, most Azure Cognitive Services support customer managed keys for enhanced security and compliance. Some of the popular services that support customer managed keys include Azure Cognitive Search, Azure Speech Services, Azure Language Understanding, and Azure Computer Vision.

Usage: number of prompt token is 59, number of completion token is 44, and number of total tokens in request and response is 103.

Reference: 引用

Azure OpenAI client library for Java | Microsoft Learn

相关推荐
佛州小李哥6 小时前
Agent群舞,在亚马逊云科技搭建数字营销多代理(Multi-Agent)(下篇)
人工智能·科技·ai·语言模型·云计算·aws·亚马逊云科技
oioihoii14 小时前
【2024 博客之星评选】请继续保持Passion
ai
Damon小智20 小时前
合合信息DocFlow产品解析与体验:人人可搭建的AI自动化单据处理工作流
图像处理·人工智能·深度学习·机器学习·ai·自动化·docflow
健忘的派大星20 小时前
【AI大模型】根据官方案例使用milvus向量数据库打造问答RAG系统
人工智能·ai·语言模型·llm·milvus·agi·rag
孤独且没人爱的纸鹤20 小时前
【机器学习】深入无监督学习分裂型层次聚类的原理、算法结构与数学基础全方位解读,深度揭示其如何在数据空间中构建层次化聚类结构
人工智能·python·深度学习·机器学习·支持向量机·ai·聚类
shelby_loo1 天前
Azure学生订阅上手实操:快速搭建Docker+WordPress环境
microsoft·docker·azure
格砸1 天前
Trae使用体验,未来已至?
人工智能·openai·trae
AI2AGI1 天前
天天AI-20250121:全面解读 AI 实践课程:动手学大模型(含PDF课件)
大数据·人工智能·百度·ai·文心一言
程序员海军2 天前
字节跳动推出AI编程神器Trae,比Cursor更懂中文开发者!
前端·openai·trae
鸭鸭鸭进京赶烤2 天前
OpenAI秘密重塑机器人军团: 实体AGI的崛起!
人工智能·opencv·机器学习·ai·机器人·agi·机器翻译引擎