Using Java to call the Qianfan large model interface

Foreword

Originally, I wanted to call the ChatGPT interface, but my ability was limited and I couldn’t get the apiKey of ChatGPT. So I thought about implementing the interface of Wen Xinyiyan (from Baidu’s Qianfan large model). If you need to implement the dialogue function, you will need to pay for calling this interface. The specific payment rules are as follows

The account you just registered has a 20 yuan voucher, and you can use the voucher to practice.

If you don’t have a voucher and want to test the function of calling the interface without activating it, there is a free trial interface. Prompt template.

Next, I will implement the function of calling the Prompt template and the function of calling the dialogue interface respectively.

Get AccessToken

Whether you are calling a free interface or a paid interface, you must first obtain the AccessToken for authentication.

View the corresponding document to obtain access_token – Qianfan Large Model Platform | Baidu Intelligent Cloud Document (baidu.com)

According to the documentation we know that we make a POST request to the interface. A request header and three Query parameters.

We can Obtain part of the Java code through the online debugging platform. We refer to its documentation to write.

First we imported the following jar package in the pom file

 <dependencies>
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>4.8.1</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>2.0.19</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.36</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.8</version>
        </dependency>
    </dependencies>

Write a test method to obtain AccessToken

@Slf4j
public class Chat {
    private final String ACCESS_TOKEN_URI = "https://aip.baidubce.com/oauth/2.0/token";

    private String apiKey = "yourApikey";
    private String secretKey = "yourSecetkey";
    private String accessToken;

    private OkHttpClient client = new OkHttpClient();

    public boolean getAccessToken(){
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, "");
        //Create a request
        Request request = new Request.Builder()
                .url(ACCESS_TOKEN_URI + "?client_id=" + apiKey + " & amp;client_secret=" + secretKey + " & amp;grant_type=client_credentials")
                .method("POST",body)
                .addHeader("Content-Type", "application/json")
                .build();
        try {
            //Use the browser object to initiate a request
            Response response = client.newCall(request).execute();
            //response.body().string() can only be executed once. The next execution will throw a stream closing exception, so an object is needed to store the return result.
            String responseMessage = response.body().string();
            log.debug("Get accessToken successfully");
            JSONObject jsonObject = JSON.parseObject(responseMessage);
            accessToken = (String) jsonObject.get("access_token");
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
}

After we convert the information obtained by calling the AccessToken interface into JSON format, we obtain the access_token attribute value and copy it to the class attribute.

Test

public class Main {
    public static void main(String[] args) throws InterruptedException {
        Chat chat = new Chat();
        boolean result = chat.getAccessToken();
        System.out.println(result);
    }
}

The running result is:

19:46:13,387 DEBUG Chat:48 – Obtain accessToken successfully
true

Implement Prompt template interface call

View the corresponding document Prompt template – Qianfan Large Model Platform | Baidu Intelligent Cloud Document (baidu.com).

To call this interface, we need to create a Promet template in advance. The process of creating a template is as follows

After creating the template, we need to call the template ID later.

The specific implementation code is as follows

 /**
     * Call Prompt interface
     * @param param
     */
    public void getPrompt(int id,String param){
        Request request = new Request.Builder()
// https://aip.baidubce.com/rest/2.0/wenxinworkshop/api/v1/template/info?access_token=xxx & amp;id=7964 & amp;name=value
                .url(CHAT_URI + "?access_token=" + accessToken + " & amp;id=" + id + " & amp;name=" + param)
                .addHeader("Content-Type", "application/json")
                .method("GET",null)
                .build();
        try {
            Response responseMessage = client.newCall(request).execute();
            JSONObject jsonObject = JSON.parseObject(responseMessage.body().string());
            log.debug(jsonObject.toString());
            Object result = jsonObject.get("result");
            log.debug("{}",result.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Test

public class Main {
    public static void main(String[] args) throws InterruptedException {
        Chat chat = new Chat();
        boolean result = chat.getAccessToken();
        if (result){
            chat.getPrompt(7964,"zmbwcx");
        }
    }
}

The running result is

19:48:04,906 DEBUG Chat:48 – Obtain accessToken successfully
19:48:05,144 DEBUG Chat:99 – {“result”:{“templateName”:”Test interface call”,”templateContent”:”Article content: {name} test interface call successful “,”templateId”:7964,”content”:”Article content: The zmbwcx test interface was called successfully”,”templateVariables”:”name”},”log_id”:\ “0hkyy9izb4azvsgp”,”success”:true,”status”:200}
19:48:05,145 DEBUG Chat:101 – {“templateName”:”Test interface call”,”templateContent”:”Article content: {name} test interface call successfully”,”templateId\ “:7964,”content”:”Article content: The zmbwcx test interface was called successfully”,”templateVariables”:”name”}

Implement conversation interface call

It is also an observation document, but implementing the conversation interface call is slightly more complicated than implementing the Prompt interface call. Some classes need to be created to set our parameters.

Create request parameter class

@Data
public class RequestMessage {
    /**
     * Chat context
     */
    List<Message> messages = new ArrayList<>();

    /**
     * Range (0~1.0]
     * Higher values will make the output more random
     */
    float temperature = Float.parseFloat("0.95");

    /**
     * Affects the diversity of text. The larger the value, the stronger the diversity of text generated.
     * It is recommended to set only one parameter and temperature. It is recommended not to change top_p and temperature at the same time
     */
    float top_p = Float.parseFloat("0.8");

    /**
     * Reduce the phenomenon of repeated generation by adding penalties to already generated tokens
     * The larger the value, the greater the penalty
     * Value range [1,2]
     */
    float penalty_score = Float.parseFloat("1.0");

    /**
     * Whether to return data in the form of streaming interface
     */
    boolean stream = false;

    /**
     * Model character design
     */
    String system = null;

    /**
     * Represents a unique user identifier, used for monitoring and detecting abuse. Prevent malicious calls to interfaces.
     */
    String user_id = "";

    public void addMessage(Message message){
        this.messages.add(message);
    }
}

Create Message class

@Data
public class Message {
    /**
     * User role
     * Currently supports:
     * user user
     * assistant conversation assistant
     */
    String role;

    /**
     *Conversation content.
     */
    String content;

    public Message(String role, String content) {
        this.role = role;
        this.content = content;
    }
}

Set the receiving response information class

@Data
public class ResponseMessage {
    //Conversation id of this round
    String id;

    //Return packet type. chat.completion: Multiple rounds of dialogue return
    String object;

    // timestamp
    int created;

    //Indicates the sequence number of the current clause. This field will only be returned in streaming interface mode
    int sentence_id;

    //Indicates whether the current clause is the last sentence. This field is returned only in streaming interface mode.
    boolean is_end;

    //Conversation returns results.
    String result;

    /**
     * Indicates whether user input is safe, whether to close the current session, and clear historical reply information.
     * true: Yes, it means that user input has security risks. It is recommended to close the current session and clear historical session information.
     * false: No, indicating that user input has no security risks.
     */
    boolean need_clear_history;

    //Token statistical information, number of tokens = number of Chinese characters + number of words*1.3 (only estimation logic).
    Usage usage;
}

Set Usage class

public class Usage {
    //Number of question tokens
    int prompt_tokens;
    //Number of answer tokens
    int completion_tokens;
    //Total number of tokens
    int total_tokens;
}

If the documentation is unclear, we can use the online debugging platform to observe the required parameters required for the request and the response parameter structure.

Next we write the Chat class

@Slf4j
public class Chat {
    private final String ACCESS_TOKEN_URI = "https://aip.baidubce.com/oauth/2.0/token";
    private final String CHAT_URI = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/eb-instant";

    private String apiKey = "yourApikey";
    private String secretKey = "yourSecretkey";
    private String accessToken = "";

    private OkHttpClient client;

    //Request parameters
    private RequestMessage requestBody = new RequestMessage();
    //Response timeout
    private int responseTimeOut = 5000;

    public Chat(){
        this.client = new OkHttpClient.Builder().readTimeout(responseTimeOut,TimeUnit.SECONDS).build();
    }

    public Chat(int responseTimeOut){
        this.client = new OkHttpClient.Builder().readTimeout(responseTimeOut,TimeUnit.SECONDS).build();
    }

     public boolean getAccessToken(){
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, "");
        //Create a request
        Request request = new Request.Builder()
                .url(ACCESS_TOKEN_URI + "?client_id=" + apiKey + " & amp;client_secret=" + secretKey + " & amp;grant_type=client_credentials")
                .method("POST",body)
                .addHeader("Content-Type", "application/json")
                .build();
        try {
            //Use the browser object to initiate a request
            Response response = client.newCall(request).execute();
            //response.body().string() can only be executed once. The next execution will throw a stream closing exception, so an object is needed to store the return result.
            String responseMessage = response.body().string();
            log.debug("Get accessToken successfully");
            JSONObject jsonObject = JSON.parseObject(responseMessage);
            accessToken = (String) jsonObject.get("access_token");
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     *
     * Get the question parameters and return the answer
     * @param question
     */
    public void getAnswer(String question){
        //Get a Message through parameters
        Message message = new Message("user",question);
        //Add new question to message context
        requestBody.addMessage(message);
        String jsonStr = JSON.toJSONString(requestBody);
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, jsonStr);
        Request request = new Request.Builder()
                .url(CHAT_URI + "?access_token=" + accessToken)
                .addHeader("Content-Type", "application/json")
                .method("POST",body)
                .build();
        try {
            Response response = client.newCall(request).execute();
            log.debug("Send a request and ask a question: {}",question);
            String responseJsonStr = response.body().string();
            ResponseMessage responseMessage = JSON.parseObject(responseJsonStr, ResponseMessage.class);
            System.out.println("The response result returned is:" + responseJsonStr);
            String result = responseMessage.getResult();
            String answer = result.replaceAll("\\
 + ", "\\
");
            log.debug("{}",answer);
            Message assistant = new Message("assistant", answer);
            requestBody.addMessage(assistant);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void getRequestBody(){
        System.out.println(JSON.toJSONString(requestBody));
    }
}

Test

public class Main {
    public static void main(String[] args) throws InterruptedException {
        Chat chat = new Chat();
        boolean result = chat.getAccessToken();
        if (result){
            Scanner scanner = new Scanner(System.in);
            String question = scanner.nextLine();
            while(!"q".equals(question)){
                chat.getAnswer(question);
                chat.getRequestBody();
                question = scanner.nextLine();
            }
        }
    }
}

Run tests

Convert the returned results and requestBody into JSON format for observation

The format conversion is correct. Test whether contextual contact is possible

Convert the format of the last line to observe

Found to respond normally. At this point, we have implemented the conversation interface call for calling the Qianfan large model.

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Java skill treeClasses and interfacesInterfaces 137588 people are learning the system