Spring boot + Azure OpenAI 服务 1.使用 GPT-35-Turbo

Azure OpenAI 服务使用 GPT-35-Turbo

Spring boot + Azure OpenAI 服务 1.使用 GPT-35-Turbo_第1张图片

先决条件

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

你可能感兴趣的:(spring,boot,azure,gpt-3)