Azure OpenAI 服务使用 GPT-35-Turbo
先决条件
maven 注意 beta.6 版本
<dependency><groupId>com.azure</groupId><artifactId>azure-ai-openai</artifactId><version>1.0.0-beta.6</version></dependency>
问答工具类
package com.aiit.project.util;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 java.util.ArrayList;
import java.util.List;
/*** azureOpenai 问答工具类*/
@Slf4j
public class GptUtils {public static String getGpt(String str) throws InterruptedException {//读取系统参数String azureOpenaiKey = System.getenv("AZURE_OPENAI_KEY");String endpoint = System.getenv("AZURE_OPENAI_ENDPOINT");String deploymentOrModelId = "gpt";//参数拼接OpenAIClient client = new OpenAIClientBuilder().endpoint(endpoint).credential(new AzureKeyCredential(azureOpenaiKey)).buildClient();List<ChatRequestMessage> chatMessages = new ArrayList<>();//提示词工程chatMessages.add(new ChatRequestSystemMessage("*****"));chatMessages.add(new ChatRequestUserMessage("****"));chatMessages.add(new ChatRequestAssistantMessage("*****"));chatMessages.add(new ChatRequestUserMessage(str));//建立连接ChatCompletions chatCompletions = client.getChatCompletions(deploymentOrModelId, new ChatCompletionsOptions(chatMessages));//返回return chatCompletions.getChoices().get(0).getMessage().getContent();}
}