获取客户端临时Key
This commit is contained in:
1
.idea/compiler.xml
generated
1
.idea/compiler.xml
generated
@@ -11,6 +11,7 @@
|
|||||||
<module name="vetti-generator" />
|
<module name="vetti-generator" />
|
||||||
<module name="vetti-quartz" />
|
<module name="vetti-quartz" />
|
||||||
<module name="vetti-system" />
|
<module name="vetti-system" />
|
||||||
|
<module name="vetti-hotake" />
|
||||||
<module name="vetti-common" />
|
<module name="vetti-common" />
|
||||||
<module name="vetti-framework" />
|
<module name="vetti-framework" />
|
||||||
<module name="vetti-ai" />
|
<module name="vetti-ai" />
|
||||||
|
|||||||
2
.idea/encodings.xml
generated
2
.idea/encodings.xml
generated
@@ -13,6 +13,8 @@
|
|||||||
<file url="file://$PROJECT_DIR$/vetti-framework/src/main/resources" charset="UTF-8" />
|
<file url="file://$PROJECT_DIR$/vetti-framework/src/main/resources" charset="UTF-8" />
|
||||||
<file url="file://$PROJECT_DIR$/vetti-generator/src/main/java" charset="UTF-8" />
|
<file url="file://$PROJECT_DIR$/vetti-generator/src/main/java" charset="UTF-8" />
|
||||||
<file url="file://$PROJECT_DIR$/vetti-generator/src/main/resources" charset="UTF-8" />
|
<file url="file://$PROJECT_DIR$/vetti-generator/src/main/resources" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/vetti-hotake/src/main/java" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/vetti-hotake/src/main/resources" charset="UTF-8" />
|
||||||
<file url="file://$PROJECT_DIR$/vetti-quartz/src/main/java" charset="UTF-8" />
|
<file url="file://$PROJECT_DIR$/vetti-quartz/src/main/java" charset="UTF-8" />
|
||||||
<file url="file://$PROJECT_DIR$/vetti-quartz/src/main/resources" charset="UTF-8" />
|
<file url="file://$PROJECT_DIR$/vetti-quartz/src/main/resources" charset="UTF-8" />
|
||||||
<file url="file://$PROJECT_DIR$/vetti-system/src/main/java" charset="UTF-8" />
|
<file url="file://$PROJECT_DIR$/vetti-system/src/main/java" charset="UTF-8" />
|
||||||
|
|||||||
@@ -5,6 +5,9 @@ import com.vetti.common.ai.gpt.ChatGPTClient;
|
|||||||
import com.vetti.common.ai.whisper.WhisperClient;
|
import com.vetti.common.ai.whisper.WhisperClient;
|
||||||
import com.vetti.common.core.controller.BaseController;
|
import com.vetti.common.core.controller.BaseController;
|
||||||
import com.vetti.common.core.domain.AjaxResult;
|
import com.vetti.common.core.domain.AjaxResult;
|
||||||
|
import com.vetti.common.core.domain.R;
|
||||||
|
import com.vetti.common.core.domain.dto.RealtimeClientSecretDto;
|
||||||
|
import com.vetti.web.service.ICommonService;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -32,6 +35,9 @@ public class AiCommonController extends BaseController
|
|||||||
@Autowired
|
@Autowired
|
||||||
private WhisperClient whisperClient;
|
private WhisperClient whisperClient;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ICommonService commonService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 进行文本转语音处理
|
* 进行文本转语音处理
|
||||||
*/
|
*/
|
||||||
@@ -70,4 +76,16 @@ public class AiCommonController extends BaseController
|
|||||||
return success();
|
return success();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取客户端临时Key
|
||||||
|
*/
|
||||||
|
@ApiOperation("获取客户端临时Key")
|
||||||
|
@GetMapping("/getRealtimeClientSecret")
|
||||||
|
public R<RealtimeClientSecretDto> handleRealtimeClientSecret()
|
||||||
|
{
|
||||||
|
RealtimeClientSecretDto dto = commonService.getRealtimeClientSecret();
|
||||||
|
return R.ok(dto);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package com.vetti.web.service;
|
||||||
|
|
||||||
|
import com.vetti.common.core.domain.dto.RealtimeClientSecretDto;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 共同 信息 服务类
|
||||||
|
*
|
||||||
|
* @author WangXiangShun
|
||||||
|
* @since 2025-08-27
|
||||||
|
*/
|
||||||
|
public interface ICommonService {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取openAi客户端临时Key
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public RealtimeClientSecretDto getRealtimeClientSecret();
|
||||||
|
}
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
package com.vetti.web.service.impl;
|
||||||
|
|
||||||
|
import cn.hutool.json.JSONUtil;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.vetti.common.core.domain.dto.RealtimeClientSecretDto;
|
||||||
|
import com.vetti.web.service.ICommonService;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.http.HttpClient;
|
||||||
|
import java.net.http.HttpRequest;
|
||||||
|
import java.net.http.HttpResponse;
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.util.Map;
|
||||||
|
/**
|
||||||
|
* 共同 信息 服务实现类
|
||||||
|
*
|
||||||
|
* @author WangXiangShun
|
||||||
|
* @since 2025-08-27
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Service
|
||||||
|
public class CommonServiceImpl implements ICommonService {
|
||||||
|
|
||||||
|
// OpenAI API密钥,需要替换为你自己的密钥
|
||||||
|
@Value("${chatGpt.apiKey}")
|
||||||
|
private String apiKey;
|
||||||
|
// API端点URL
|
||||||
|
@Value("${chatGpt.apiClientTokenUrl}")
|
||||||
|
private String apiClientTokenUrl;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取openAi客户端临时Key
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public RealtimeClientSecretDto getRealtimeClientSecret() {
|
||||||
|
RealtimeClientSecretDto dto = new RealtimeClientSecretDto();
|
||||||
|
try {
|
||||||
|
// 1. 创建 HTTP 客户端
|
||||||
|
HttpClient client = HttpClient.newBuilder()
|
||||||
|
.version(HttpClient.Version.HTTP_2)
|
||||||
|
.connectTimeout(Duration.ofSeconds(10))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
// 2. 构建请求体(空 JSON 对象)
|
||||||
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
String requestBody = "";
|
||||||
|
// 3. 构建 HTTP 请求
|
||||||
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
|
.uri(URI.create(apiClientTokenUrl))
|
||||||
|
.header("Content-Type", "application/json")
|
||||||
|
.header("Authorization", "Bearer " + apiKey) // 核心认证头
|
||||||
|
.POST(HttpRequest.BodyPublishers.ofString(requestBody))
|
||||||
|
.build();
|
||||||
|
// 4. 发送请求并获取响应
|
||||||
|
HttpResponse<String> response = client.send(
|
||||||
|
request,
|
||||||
|
HttpResponse.BodyHandlers.ofString()
|
||||||
|
);
|
||||||
|
// 5. 处理响应
|
||||||
|
if (response.statusCode() == 200) {
|
||||||
|
// 解析响应为实体类
|
||||||
|
System.out.println(JSONUtil.toJsonStr(response.body()));
|
||||||
|
Map entity = JSONUtil.toBean(response.body(), Map.class);
|
||||||
|
dto.setClientSecret(entity.get("value").toString());
|
||||||
|
dto.setExpiresAt(Long.valueOf(entity.get("expires_at").toString()));
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return dto;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -162,6 +162,7 @@ chatGpt:
|
|||||||
apiUrl: https://api.openai.com/v1/chat/completions
|
apiUrl: https://api.openai.com/v1/chat/completions
|
||||||
model: gpt-3.5-turbo
|
model: gpt-3.5-turbo
|
||||||
role: user
|
role: user
|
||||||
|
apiClientTokenUrl: https://api.openai.com/v1/realtime/client_secrets
|
||||||
|
|
||||||
|
|
||||||
http:
|
http:
|
||||||
|
|||||||
@@ -169,6 +169,7 @@ chatGpt:
|
|||||||
apiUrl: https://api.openai.com/v1/chat/completions
|
apiUrl: https://api.openai.com/v1/chat/completions
|
||||||
model: ft:gpt-3.5-turbo-0125:vetti:construction-labourer-test:CWKBNvE2
|
model: ft:gpt-3.5-turbo-0125:vetti:construction-labourer-test:CWKBNvE2
|
||||||
role: system
|
role: system
|
||||||
|
apiClientTokenUrl: https://api.openai.com/v1/realtime/client_secrets
|
||||||
|
|
||||||
|
|
||||||
http:
|
http:
|
||||||
|
|||||||
@@ -167,7 +167,7 @@ whisper:
|
|||||||
chatGpt:
|
chatGpt:
|
||||||
apiKey: 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
|
apiKey: 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
|
||||||
apiUrl: https://api.openai.com/v1/chat/completions
|
apiUrl: https://api.openai.com/v1/chat/completions
|
||||||
model: ft:gpt-3.5-turbo-0125:vetti:construction-labourer-test:CTIvLD5n
|
model: ft:gpt-3.5-turbo-0125:vetti:construction-labourer-test:CWKBNvE2
|
||||||
role: system
|
role: system
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package com.vetti.common.core.domain.dto;
|
||||||
|
|
||||||
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.experimental.Accessors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* openAi 客户端密钥返回结构
|
||||||
|
*
|
||||||
|
* @author wangxiangshun
|
||||||
|
* @date 2025-10-29
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@Accessors(chain = true)
|
||||||
|
public class RealtimeClientSecretDto {
|
||||||
|
|
||||||
|
|
||||||
|
@ApiModelProperty("生成的客户端密钥")
|
||||||
|
private String clientSecret;
|
||||||
|
|
||||||
|
@ApiModelProperty("过期时间(Unix 时间戳,秒")
|
||||||
|
private long expiresAt;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -35,6 +35,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||||||
<result property="relocate" column="relocate" />
|
<result property="relocate" column="relocate" />
|
||||||
<result property="bestSideJson" column="best_side_json" />
|
<result property="bestSideJson" column="best_side_json" />
|
||||||
|
|
||||||
|
|
||||||
<association property="dept" javaType="SysDept" resultMap="deptResult" />
|
<association property="dept" javaType="SysDept" resultMap="deptResult" />
|
||||||
<collection property="roles" javaType="java.util.List" resultMap="RoleResult" />
|
<collection property="roles" javaType="java.util.List" resultMap="RoleResult" />
|
||||||
</resultMap>
|
</resultMap>
|
||||||
|
|||||||
Reference in New Issue
Block a user