关于阿里AI的完整接入方案(通义千问)

项目中用到了AI接口,阿里提供了很多免费的token,遂写出了下面的调用方案。

申请地址:模型服务灵积-模型广场 (aliyun.com)

涉及模型:c81e9330bbe245c29d7930dfd4a0f603.png

175bf3f8d58c4cd3a72015f6ec2d2729.png

第一个带图,第二个不带图,可按需求修改调用模型

代码部分:

Controller

package com.demo.controller;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.demo.pojo.ChatRequest;
import com.demo.pojo.ChatResponse;
import com.demo.pojo.ImgRequest;
import com.demo.pojo.ImgResponse;
import com.demo.util.ApiResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.HashMap;
import java.util.Map;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@Api(
   value = "",
   tags = {"Ai接口"}
)
@RestController
public class ChatController {
   @ApiOperation("单文本接口")
   @PostMapping({"/chatWord"})
   public ApiResponse chat(@RequestBody Map<String, String> requestData) {
      String url = "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation";
      String apiKey = "填入申请的API";
      String input = (String)requestData.get("input");
      ChatRequest chatRequest = new ChatRequest(input);
      String json = JSONUtil.toJsonStr(chatRequest);
      String result = ((HttpRequest)((HttpRequest)HttpRequest.post(url).header("Authorization", "Bearer " + apiKey)).header("Content-Type", "application/json")).body(json).execute().body();
      ChatResponse chatResponse = (ChatResponse)JSONUtil.toBean(result, ChatResponse.class);
      Map<String, Object> responseData = new HashMap();
      responseData.put("chatResponse", chatResponse);
      return new ApiResponse(200, "ok", responseData);
   }

   @ApiOperation("带图接口(url)")
   @PostMapping({"/chatImage"})
   public ApiResponse imgChat(@RequestBody Map<String, String> requestData) {
      String url = "https://dashscope.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation";
      String apiKey = "填入申请的API";
      String image = (String)requestData.get("image");
      String text = (String)requestData.get("text");
      ImgRequest imgRequest = new ImgRequest(image, text);
      String json = JSONUtil.toJsonStr(imgRequest);
      String result = ((HttpRequest)((HttpRequest)HttpRequest.post(url).header("Authorization", "Bearer " + apiKey)).header("Content-Type", "application/json")).body(json).execute().body();
      ImgResponse imgResponse = (ImgResponse)JSONUtil.toBean(result, ImgResponse.class);
      Map<String, Object> responseData = new HashMap();
      String content = imgResponse.getOutput().getChoices()[0].getMessage().getContent();
      responseData.put("content", content);
      return new ApiResponse(200, "ok", responseData);
   }
}

pojo:Requeset

ChatRequst

package com.demo.pojo;

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

public class ChatRequest {
   String model;
   Input input;
   Parameters parameters;

   public ChatRequest(String q) {
      this.model = "qwen-turbo";
      this.input = new Input(q);
      this.parameters = new Parameters();
   }

   public String getModel() {
      return this.model;
   }

   public Input getInput() {
      return this.input;
   }

   public Parameters getParameters() {
      return this.parameters;
   }

   public void setModel(final String model) {
      this.model = model;
   }

   public void setInput(final Input input) {
      this.input = input;
   }

   public void setParameters(final Parameters parameters) {
      this.parameters = parameters;
   }

   public boolean equals(final Object o) {
      if (o == this) {
         return true;
      } else if (!(o instanceof ChatRequest)) {
         return false;
      } else {
         ChatRequest other = (ChatRequest)o;
         if (!other.canEqual(this)) {
            return false;
         } else {
            label47: {
               Object this$model = this.getModel();
               Object other$model = other.getModel();
               if (this$model == null) {
                  if (other$model == null) {
                     break label47;
                  }
               } else if (this$model.equals(other$model)) {
                  break label47;
               }

               return false;
            }

            Object this$input = this.getInput();
            Object other$input = other.getInput();
            if (this$input == null) {
               if (other$input != null) {
                  return false;
               }
            } else if (!this$input.equals(other$input)) {
               return false;
            }

            Object this$parameters = this.getParameters();
            Object other$parameters = other.getParameters();
            if (this$parameters == null) {
               if (other$parameters != null) {
                  return false;
               }
            } else if (!this$parameters.equals(other$parameters)) {
               return false;
            }

            return true;
         }
      }
   }

   protected boolean canEqual(final Object other) {
      return other instanceof ChatRequest;
   }

   public int hashCode() {
      int result = 1;
      Object $model = this.getModel();
      result = result * 59 + ($model == null ? 43 : $model.hashCode());
      Object $input = this.getInput();
      result = result * 59 + ($input == null ? 43 : $input.hashCode());
      Object $parameters = this.getParameters();
      result = result * 59 + ($parameters == null ? 43 : $parameters.hashCode());
      return result;
   }

   public String toString() {
      String var10000 = this.getModel();
      return "ChatRequest(model=" + var10000 + ", input=" + this.getInput() + ", parameters=" + this.getParameters() + ")";
   }

   public ChatRequest(final String model, final Input input, final Parameters parameters) {
      this.model = model;
      this.input = input;
      this.parameters = parameters;
   }

   public ChatRequest() {
   }

   class Parameters {
      public String result_format = "text";
   }

   class Chat {
      public String role;
      public String content;

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

   class Input {
      public List<Chat> messages;

      Input(String q) {
         ArrayList<Chat> chats = new ArrayList();
         chats.add(ChatRequest.this.new Chat("system", "你叫贾维斯,我是你的主人"));
         chats.add(ChatRequest.this.new Chat("user", q));
         this.messages = chats;
      }
   }
}

ImgRequst

注:因为在测试阶段写了很多费代码没有清理,见谅

package com.demo.pojo;

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

public class ImgRequest {
   String model = "qwen-vl-plus";
   Input input;

   public ImgRequest(String imageUrl, String questionText) {
      this.input = new Input(imageUrl, questionText);
   }

   public String getModel() {
      return this.model;
   }

   public Input getInput() {
      return this.input;
   }

   public void setModel(final String model) {
      this.model = model;
   }

   public void setInput(final Input input) {
      this.input = input;
   }

   public boolean equals(final Object o) {
      if (o == this) {
         return true;
      } else if (!(o instanceof ImgRequest)) {
         return false;
      } else {
         ImgRequest other = (ImgRequest)o;
         if (!other.canEqual(this)) {
            return false;
         } else {
            Object this$model = this.getModel();
            Object other$model = other.getModel();
            if (this$model == null) {
               if (other$model != null) {
                  return false;
               }
            } else if (!this$model.equals(other$model)) {
               return false;
            }

            Object this$input = this.getInput();
            Object other$input = other.getInput();
            if (this$input == null) {
               if (other$input != null) {
                  return false;
               }
            } else if (!this$input.equals(other$input)) {
               return false;
            }

            return true;
         }
      }
   }

   protected boolean canEqual(final Object other) {
      return other instanceof ImgRequest;
   }

   public int hashCode() {
      int result = 1;
      Object $model = this.getModel();
      result = result * 59 + ($model == null ? 43 : $model.hashCode());
      Object $input = this.getInput();
      result = result * 59 + ($input == null ? 43 : $input.hashCode());
      return result;
   }

   public String toString() {
      String var10000 = this.getModel();
      return "ImgRequest(model=" + var10000 + ", input=" + this.getInput() + ")";
   }

   public ImgRequest(final String model, final Input input) {
      this.model = model;
      this.input = input;
   }

   public ImgRequest() {
   }

   static class Content {
      String text;
      String image;

      Content(String text) {
         this.text = text;
      }

      Content(String image, String unused) {
         this.image = image;
      }

      public String getText() {
         return this.text;
      }

      public String getImage() {
         return this.image;
      }

      public void setText(final String text) {
         this.text = text;
      }

      public void setImage(final String image) {
         this.image = image;
      }

      public boolean equals(final Object o) {
         if (o == this) {
            return true;
         } else if (!(o instanceof Content)) {
            return false;
         } else {
            Content other = (Content)o;
            if (!other.canEqual(this)) {
               return false;
            } else {
               Object this$text = this.getText();
               Object other$text = other.getText();
               if (this$text == null) {
                  if (other$text != null) {
                     return false;
                  }
               } else if (!this$text.equals(other$text)) {
                  return false;
               }

               Object this$image = this.getImage();
               Object other$image = other.getImage();
               if (this$image == null) {
                  if (other$image != null) {
                     return false;
                  }
               } else if (!this$image.equals(other$image)) {
                  return false;
               }

               return true;
            }
         }
      }

      protected boolean canEqual(final Object other) {
         return other instanceof Content;
      }

      public int hashCode() {
         int result = 1;
         Object $text = this.getText();
         result = result * 59 + ($text == null ? 43 : $text.hashCode());
         Object $image = this.getImage();
         result = result * 59 + ($image == null ? 43 : $image.hashCode());
         return result;
      }

      public String toString() {
         String var10000 = this.getText();
         return "ImgRequest.Content(text=" + var10000 + ", image=" + this.getImage() + ")";
      }
   }

   static class Message {
      String role;
      List<Content> content;

      public Message() {
      }

      public String getRole() {
         return this.role;
      }

      public List<Content> getContent() {
         return this.content;
      }

      public void setRole(final String role) {
         this.role = role;
      }

      public void setContent(final List<Content> content) {
         this.content = content;
      }

      public boolean equals(final Object o) {
         if (o == this) {
            return true;
         } else if (!(o instanceof Message)) {
            return false;
         } else {
            Message other = (Message)o;
            if (!other.canEqual(this)) {
               return false;
            } else {
               Object this$role = this.getRole();
               Object other$role = other.getRole();
               if (this$role == null) {
                  if (other$role != null) {
                     return false;
                  }
               } else if (!this$role.equals(other$role)) {
                  return false;
               }

               Object this$content = this.getContent();
               Object other$content = other.getContent();
               if (this$content == null) {
                  if (other$content != null) {
                     return false;
                  }
               } else if (!this$content.equals(other$content)) {
                  return false;
               }

               return true;
            }
         }
      }

      protected boolean canEqual(final Object other) {
         return other instanceof Message;
      }

      public int hashCode() {
         int result = 1;
         Object $role = this.getRole();
         result = result * 59 + ($role == null ? 43 : $role.hashCode());
         Object $content = this.getContent();
         result = result * 59 + ($content == null ? 43 : $content.hashCode());
         return result;
      }

      public String toString() {
         String var10000 = this.getRole();
         return "ImgRequest.Message(role=" + var10000 + ", content=" + this.getContent() + ")";
      }
   }

   static class Input {
      List<Message> messages = new ArrayList();

      Input(String imageUrl, String questionText) {
         Message systemMessage = new Message();
         systemMessage.setRole("system");
         List<Content> systemContent = new ArrayList();
         systemContent.add(new Content(""));
         systemMessage.setContent(systemContent);
         Message userMessage = new Message();
         userMessage.setRole("user");
         List<Content> userContent = new ArrayList();
         userContent.add(new Content(questionText));
         userContent.add(new Content(imageUrl, ""));
         userMessage.setContent(userContent);
         this.messages.add(systemMessage);
         this.messages.add(userMessage);
      }

      public List<Message> getMessages() {
         return this.messages;
      }

      public void setMessages(final List<Message> messages) {
         this.messages = messages;
      }

      public boolean equals(final Object o) {
         if (o == this) {
            return true;
         } else if (!(o instanceof Input)) {
            return false;
         } else {
            Input other = (Input)o;
            if (!other.canEqual(this)) {
               return false;
            } else {
               Object this$messages = this.getMessages();
               Object other$messages = other.getMessages();
               if (this$messages == null) {
                  if (other$messages != null) {
                     return false;
                  }
               } else if (!this$messages.equals(other$messages)) {
                  return false;
               }

               return true;
            }
         }
      }

      protected boolean canEqual(final Object other) {
         return other instanceof Input;
      }

      public int hashCode() {
         int result = 1;
         Object $messages = this.getMessages();
         result = result * 59 + ($messages == null ? 43 : $messages.hashCode());
         return result;
      }

      public String toString() {
         return "ImgRequest.Input(messages=" + this.getMessages() + ")";
      }
   }
}

pojo:Response

ChatResponse

注:写项目时前端要求较高,剔除了很多返回内容,可自行修改加入

package com.demo.pojo;

public class ChatResponse {
   private Output output;
   private Usage usage;
   private String request_id;

   public Output getOutput() {
      return this.output;
   }

   public Usage getUsage() {
      return this.usage;
   }

   public String getRequest_id() {
      return this.request_id;
   }

   public void setOutput(final Output output) {
      this.output = output;
   }

   public void setUsage(final Usage usage) {
      this.usage = usage;
   }

   public void setRequest_id(final String request_id) {
      this.request_id = request_id;
   }

   public boolean equals(final Object o) {
      if (o == this) {
         return true;
      } else if (!(o instanceof ChatResponse)) {
         return false;
      } else {
         ChatResponse other = (ChatResponse)o;
         if (!other.canEqual(this)) {
            return false;
         } else {
            label47: {
               Object this$output = this.getOutput();
               Object other$output = other.getOutput();
               if (this$output == null) {
                  if (other$output == null) {
                     break label47;
                  }
               } else if (this$output.equals(other$output)) {
                  break label47;
               }

               return false;
            }

            Object this$usage = this.getUsage();
            Object other$usage = other.getUsage();
            if (this$usage == null) {
               if (other$usage != null) {
                  return false;
               }
            } else if (!this$usage.equals(other$usage)) {
               return false;
            }

            Object this$request_id = this.getRequest_id();
            Object other$request_id = other.getRequest_id();
            if (this$request_id == null) {
               if (other$request_id != null) {
                  return false;
               }
            } else if (!this$request_id.equals(other$request_id)) {
               return false;
            }

            return true;
         }
      }
   }

   protected boolean canEqual(final Object other) {
      return other instanceof ChatResponse;
   }

   public int hashCode() {
      int result = 1;
      Object $output = this.getOutput();
      result = result * 59 + ($output == null ? 43 : $output.hashCode());
      Object $usage = this.getUsage();
      result = result * 59 + ($usage == null ? 43 : $usage.hashCode());
      Object $request_id = this.getRequest_id();
      result = result * 59 + ($request_id == null ? 43 : $request_id.hashCode());
      return result;
   }

   public String toString() {
      Output var10000 = this.getOutput();
      return "ChatResponse(output=" + var10000 + ", usage=" + this.getUsage() + ", request_id=" + this.getRequest_id() + ")";
   }

   public static class Usage {
      public String output_tokens;
      public String input_tokens;

      public String getOutput_tokens() {
         return this.output_tokens;
      }

      public String getInput_tokens() {
         return this.input_tokens;
      }

      public void setOutput_tokens(final String output_tokens) {
         this.output_tokens = output_tokens;
      }

      public void setInput_tokens(final String input_tokens) {
         this.input_tokens = input_tokens;
      }

      public boolean equals(final Object o) {
         if (o == this) {
            return true;
         } else if (!(o instanceof Usage)) {
            return false;
         } else {
            Usage other = (Usage)o;
            if (!other.canEqual(this)) {
               return false;
            } else {
               Object this$output_tokens = this.getOutput_tokens();
               Object other$output_tokens = other.getOutput_tokens();
               if (this$output_tokens == null) {
                  if (other$output_tokens != null) {
                     return false;
                  }
               } else if (!this$output_tokens.equals(other$output_tokens)) {
                  return false;
               }

               Object this$input_tokens = this.getInput_tokens();
               Object other$input_tokens = other.getInput_tokens();
               if (this$input_tokens == null) {
                  if (other$input_tokens != null) {
                     return false;
                  }
               } else if (!this$input_tokens.equals(other$input_tokens)) {
                  return false;
               }

               return true;
            }
         }
      }

      protected boolean canEqual(final Object other) {
         return other instanceof Usage;
      }

      public int hashCode() {
         int result = 1;
         Object $output_tokens = this.getOutput_tokens();
         result = result * 59 + ($output_tokens == null ? 43 : $output_tokens.hashCode());
         Object $input_tokens = this.getInput_tokens();
         result = result * 59 + ($input_tokens == null ? 43 : $input_tokens.hashCode());
         return result;
      }

      public String toString() {
         String var10000 = this.getOutput_tokens();
         return "ChatResponse.Usage(output_tokens=" + var10000 + ", input_tokens=" + this.getInput_tokens() + ")";
      }
   }

   public static class Output {
      public String text;
      public String finish_reason;

      public String getText() {
         return this.text;
      }

      public String getFinish_reason() {
         return this.finish_reason;
      }

      public void setText(final String text) {
         this.text = text;
      }

      public void setFinish_reason(final String finish_reason) {
         this.finish_reason = finish_reason;
      }

      public boolean equals(final Object o) {
         if (o == this) {
            return true;
         } else if (!(o instanceof Output)) {
            return false;
         } else {
            Output other = (Output)o;
            if (!other.canEqual(this)) {
               return false;
            } else {
               Object this$text = this.getText();
               Object other$text = other.getText();
               if (this$text == null) {
                  if (other$text != null) {
                     return false;
                  }
               } else if (!this$text.equals(other$text)) {
                  return false;
               }

               Object this$finish_reason = this.getFinish_reason();
               Object other$finish_reason = other.getFinish_reason();
               if (this$finish_reason == null) {
                  if (other$finish_reason != null) {
                     return false;
                  }
               } else if (!this$finish_reason.equals(other$finish_reason)) {
                  return false;
               }

               return true;
            }
         }
      }

      protected boolean canEqual(final Object other) {
         return other instanceof Output;
      }

      public int hashCode() {
         int result = 1;
         Object $text = this.getText();
         result = result * 59 + ($text == null ? 43 : $text.hashCode());
         Object $finish_reason = this.getFinish_reason();
         result = result * 59 + ($finish_reason == null ? 43 : $finish_reason.hashCode());
         return result;
      }

      public String toString() {
         String var10000 = this.getText();
         return "ChatResponse.Output(text=" + var10000 + ", finish_reason=" + this.getFinish_reason() + ")";
      }
   }
}

ImgResponse

package com.demo.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ImgResponse {
    Output output;
    Usage usage;
    String request_id;

    @Data
    public static class Output {
        Choice[] choices;
    }

    @Data
    public static class Choice {
        Message message;
    }

    @Data
    public static class Message {
        String role;
        String content;
    }

    @Data
    public static class Usage {
        String output_tokens;
        String input_tokens;
    }
}

Util:ApiResponse

package com.demo.util;

import java.util.Map;

public class ApiResponse {
   private int code;
   private String msg;
   private Map<String, Object> data;

   public ApiResponse(int code, String msg, Map<String, Object> data) {
      this.code = code;
      this.msg = msg;
      this.data = data;
   }

   public int getCode() {
      return this.code;
   }

   public void setCode(int code) {
      this.code = code;
   }

   public String getMsg() {
      return this.msg;
   }

   public void setMsg(String msg) {
      this.msg = msg;
   }

   public Map<String, Object> getData() {
      return this.data;
   }

   public void setData(Map<String, Object> data) {
      this.data = data;
   }
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值