当前位置: 首页 > news >正文

汕头企业网站软件开发需要的软件

汕头企业网站,软件开发需要的软件,网站的扁平化设计理念,旅游网站建设导航栏需求: 使用java封装kimi接口为http接口,并把调用kimi时的传参和返回数据,保存到mysql数据库中 自己记录一下,以做备忘。 具体步骤如下: 1.申请apiKey 访问:Moonshot AI - 开放平台使用手机号手机号验证…

需求:

使用java封装kimi接口为http接口,并把调用kimi时的传参和返回数据,保存到mysql数据库中

自己记录一下,以做备忘。

具体步骤如下:

1.申请apiKey

访问:Moonshot AI - 开放平台使用手机号+手机号验证码登录

登录后,在如下界面申请:

最多可以保留 5 个 API 密钥,密钥只会在新建后显示一次,请妥善保存。不要与他人共享 API Key,或将其暴露在客户端代码中。为了账户安全,一旦 API 密钥被发现泄露,Moonshot AI 可能会将其禁用。

默认token数量如下:

2.编写Java代码对接kimi

2.1对接kimi日志表

CREATE TABLE `ai_api_log` (`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',`api_key` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'app_key',`api_ip` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'API调用外网IP',`api_method` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'API方法',`api_para` text COLLATE utf8mb4_unicode_ci COMMENT 'API参数',`api_result` text COLLATE utf8mb4_unicode_ci COMMENT 'API返回消息',`create_time` datetime DEFAULT NULL COMMENT '创建时间 ',PRIMARY KEY (`id`),KEY `open_api_log_creat_time` (`create_time`),KEY `open_api_log_method` (`api_method`),KEY `open_api_log_app_key` (`api_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='api日志'

表对应的实体类

package com.example.ai.domain;import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.EqualsAndHashCode;import java.util.Date;
@Data
@EqualsAndHashCode(callSuper = true)
@TableName("ai_api_log")
public class AIApiLog extends BaseEntity {private static final long serialVersionUID=1L;public static String METHOD_KIMI_CHAT="kimi_chat";private Integer id;private String apiKey;private String apiIp;private String apiMethod;private String apiPara;private String apiResult;private Date createTime;}

2.2对接kimi核心代码

package com.example.ai.service;import cn.hutool.core.util.StrUtil;
import cn.hutool.http.ContentType;
import cn.hutool.json.JSONObject;
import com.example.ai.config.AiConfig;
import com.example.ai.constant.PathConstant;
import com.example.ai.network.ChatMessage;
import com.example.ai.util.JsonUtil;
import com.example.ai.network.ChoicesInfo;
import com.example.ai.network.HttpResponse;
import com.example.ai.network.ResponseType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import okhttp3.*;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;import javax.annotation.PostConstruct;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;/*** @date 2023/2/13**/
@Service
public class AiKimiService {@AutowiredAiConfig aiConfig;@AutowiredAIApiLogService aIApiLogService;//webflux的clientprivate WebClient webClient;//用于读取第三方的返回结果private ObjectMapper objectMapper = new ObjectMapper();public String completions( String content)throws IOException{HttpResponse response = this.getJsonResponse(content);ChoicesInfo choicesInfo = response.getChoices().get(0);return StringEscapeUtils.unescapeJava(choicesInfo.getMessage().getContent());}public HttpResponse getJsonResponse(String message) throws IOException {List<ChatMessage> messages=new ArrayList<>();ChatMessage chatMessage=new ChatMessage();chatMessage.setRole("user");chatMessage.setContent(message);messages.add(chatMessage);String requestBody = new JSONObject().putOpt("model", "moonshot-v1-8k").putOpt("messages", messages).putOpt("response_format",new ResponseType()).putOpt("frequency_penalty",0.5).putOpt("stream", false).toString();Request okhttpRequest = new Request.Builder().url(aiConfig.getBaseUrl()+ PathConstant.COMPLETIONS.CREATE_CHAT_COMPLETION).post(RequestBody.create(requestBody, okhttp3.MediaType.get(ContentType.JSON.getValue()))).addHeader("Authorization", "Bearer " + aiConfig.getApiKey()).build();OkHttpClient okHttpClient  = new OkHttpClient.Builder().connectTimeout(20, TimeUnit.SECONDS).readTimeout(60,TimeUnit.SECONDS).writeTimeout(60,TimeUnit.SECONDS).build();Call call = okHttpClient.newCall(okhttpRequest);Response okhttpResponse = call.execute();String json = okhttpResponse.body().string();aIApiLogService.saveOrUpdate(requestBody,json);System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"----"+json);return JsonUtil.jsonToClass(json,HttpResponse.class);}@PostConstructpublic void postConstruct() {this.webClient = WebClient.builder()//创建webflux的client.baseUrl(aiConfig.getBaseUrl())//填写对应的api地址.defaultHeader("Content-Type", "application/json")//设置默认请求类型.build();}private Flux<HttpResponse> handleWebClientResponse(String resp) {if (StrUtil.equals("[DONE]",resp)){//[DONE]是消息结束标识return Flux.empty();}try {JsonNode jsonNode = objectMapper.readTree(resp);HttpResponse result = objectMapper.treeToValue(jsonNode, HttpResponse.class);//将获得的结果转成对象return Flux.just(result);//返回获得的结果} catch (Exception e) {throw new RuntimeException(e.getMessage());}}
}

2.3运行效果

3.完整项目源码

下载地址: Java对接kimi完整项目源码

http://www.yayakq.cn/news/609416/

相关文章:

  • 购物网站 建站服务纯静态网站怎样
  • 长宁手机网站建设成都设计电商网站
  • 百度网站两两学一做心得体会免费 wordpress主题
  • 潍坊 网站免费软件下载平台
  • 网站建设及推广培训班简述建设iis网站的基本过程
  • 各大网站网址网站开发 合同范本
  • 还有人用asp做网站吗网站开发_运行及维护
  • 北京鑫旺路桥建设有限公司网站建设通属于什么网站
  • 上海建设官方网站视频拍摄剪辑岗位职责及要求
  • 网站的安全维护重庆网站建设找珊瑚云
  • 网站怎么被黑wordpress 缩略图作用
  • 建外贸网站哪个好广告设计公司需要什么设备
  • 学企业网站开发昆明app制作公司在哪里
  • 网站建设服务商 需要什么主机ionic做网站
  • 家居企业网站建设如何合肥网站排名提升
  • 大气红色网站wordpress上传到服务器如何解压缩
  • 网站开发技术是上海市网站设计
  • 乐昌门户网站中国网站建设公司有哪些内容
  • 哪个网站可以做鞋鉴定做技术类网站赚钱吗
  • 网站建设开场白怎么说中铁建设集团有限公司领导名单
  • 英语翻译网站开发网站最新程序策划书
  • 小视频解析网站怎么做中国哪家网站做仿古做的好
  • 网站顶部有空白网站开发包含网站维护吗
  • 百度的网站名实验一 电子商务网站建设与维护
  • 网站建设硬件方案建设公司网站计入哪个科目
  • 做的好看的统一登录网站小程序上线需要多少钱
  • 天津市城乡建设部网站首页国外wordpress空间
  • 网站视频与服务器的关系怎样做视频网站的外链
  • 沙井做网站公司seo技术教程
  • pc网站开发语言wordpress 彩色标签云插件