gpt4 book ai didi

azure - Azure OpenAI 服务找不到资源

转载 作者:行者123 更新时间:2023-12-03 06:13:07 34 4
gpt4 key购买 nike

当我使用此演示代码在 Java 11 中使用 Azure OpenAI 服务时:

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();
}

}

显示错误:

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)

我已检查所有配置并确保端点正确,部署已创建超过 10 小时。我错过了什么吗?这是依赖项:

implementation'com.azure:azure-ai-openai:1.0.0-beta.1'

我已经尝试将 azure openai 依赖项版本升级到 1.0.0-beta.2 但仍然无法解决此问题。

最佳答案

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

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

[main] ERROR com.azure.core.implementation.http.rest.RestProxyBaseStatus 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."}}"

enter image description here

在您的代码中,在 deploymentOrModelId 处,您已给出 gpt-35-turbo 但您需要提及您的部署名称。您可以使用下面的门户获取部署名称。

门户:

enter image description here

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

代码:

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();
}

}

输出:

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.

enter image description here

引用:

Azure OpenAI client library for Java | Microsoft Learn

关于azure - Azure OpenAI 服务找不到资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76549064/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com