mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-12-06 17:18:54 +08:00
fix: ai配置支持进行代理配置
This commit is contained in:
parent
7d84d1a81c
commit
afc1036fb6
@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
package cn.hutool.ai.core;
|
package cn.hutool.ai.core;
|
||||||
|
|
||||||
|
import java.net.Proxy;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -142,4 +143,36 @@ public interface AIConfig {
|
|||||||
*/
|
*/
|
||||||
int getReadTimeout();
|
int getReadTimeout();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取是否使用代理
|
||||||
|
*
|
||||||
|
* @return hasProxy
|
||||||
|
* @since 5.8.42
|
||||||
|
*/
|
||||||
|
boolean getHasProxy();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置是否使用代理
|
||||||
|
*
|
||||||
|
* @param hasProxy 是否使用代理
|
||||||
|
* @since 5.8.42
|
||||||
|
*/
|
||||||
|
void setHasProxy(boolean hasProxy);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取代理配置
|
||||||
|
*
|
||||||
|
* @return proxy
|
||||||
|
* @since 5.8.42
|
||||||
|
*/
|
||||||
|
Proxy getProxy();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置代理配置
|
||||||
|
*
|
||||||
|
* @param proxy 连接超时时间
|
||||||
|
* @since 5.8.42
|
||||||
|
*/
|
||||||
|
void setProxy(Proxy proxy);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -17,6 +17,7 @@
|
|||||||
package cn.hutool.ai.core;
|
package cn.hutool.ai.core;
|
||||||
|
|
||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
|
import java.net.Proxy;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用于AIConfig的创建,创建同时支持链式设置参数
|
* 用于AIConfig的创建,创建同时支持链式设置参数
|
||||||
@ -160,6 +161,21 @@ public class AIConfigBuilder {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置代理
|
||||||
|
*
|
||||||
|
* @param proxy 取超时时间
|
||||||
|
* @return config
|
||||||
|
* @since 5.8.42
|
||||||
|
*/
|
||||||
|
public synchronized AIConfigBuilder setProxy(final Proxy proxy) {
|
||||||
|
if (null != proxy) {
|
||||||
|
config.setHasProxy(true);
|
||||||
|
config.setProxy(proxy);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 返回config实例
|
* 返回config实例
|
||||||
*
|
*
|
||||||
|
|||||||
@ -28,6 +28,8 @@ import java.net.URL;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
import static cn.hutool.core.thread.GlobalThreadPool.execute;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 基础AIService,包含基公共参数和公共方法
|
* 基础AIService,包含基公共参数和公共方法
|
||||||
*
|
*
|
||||||
@ -56,11 +58,14 @@ public class BaseAIService {
|
|||||||
//链式构建请求
|
//链式构建请求
|
||||||
try {
|
try {
|
||||||
//设置超时3分钟
|
//设置超时3分钟
|
||||||
return HttpRequest.get(config.getApiUrl() + endpoint)
|
HttpRequest httpRequest = HttpRequest.get(config.getApiUrl() + endpoint)
|
||||||
.header(Header.ACCEPT, "application/json")
|
.header(Header.ACCEPT, "application/json")
|
||||||
.header(Header.AUTHORIZATION, "Bearer " + config.getApiKey())
|
.header(Header.AUTHORIZATION, "Bearer " + config.getApiKey())
|
||||||
.timeout(config.getTimeout())
|
.timeout(config.getTimeout());
|
||||||
.execute();
|
if (config.getHasProxy()) {
|
||||||
|
httpRequest.setProxy(config.getProxy());
|
||||||
|
}
|
||||||
|
return httpRequest.execute();
|
||||||
} catch (final AIException e) {
|
} catch (final AIException e) {
|
||||||
throw new AIException("Failed to send GET request: " + e.getMessage(), e);
|
throw new AIException("Failed to send GET request: " + e.getMessage(), e);
|
||||||
}
|
}
|
||||||
@ -75,13 +80,16 @@ public class BaseAIService {
|
|||||||
protected HttpResponse sendPost(final String endpoint, final String paramJson) {
|
protected HttpResponse sendPost(final String endpoint, final String paramJson) {
|
||||||
//链式构建请求
|
//链式构建请求
|
||||||
try {
|
try {
|
||||||
return HttpRequest.post(config.getApiUrl() + endpoint)
|
HttpRequest httpRequest = HttpRequest.post(config.getApiUrl() + endpoint)
|
||||||
.header(Header.CONTENT_TYPE, "application/json")
|
.header(Header.CONTENT_TYPE, "application/json")
|
||||||
.header(Header.ACCEPT, "application/json")
|
.header(Header.ACCEPT, "application/json")
|
||||||
.header(Header.AUTHORIZATION, "Bearer " + config.getApiKey())
|
.header(Header.AUTHORIZATION, "Bearer " + config.getApiKey())
|
||||||
.body(paramJson)
|
.body(paramJson)
|
||||||
.timeout(config.getTimeout())
|
.timeout(config.getTimeout());
|
||||||
.execute();
|
if (config.getHasProxy()) {
|
||||||
|
httpRequest.setProxy(config.getProxy());
|
||||||
|
}
|
||||||
|
return httpRequest.execute();
|
||||||
} catch (final AIException e) {
|
} catch (final AIException e) {
|
||||||
throw new AIException("Failed to send POST request:" + e.getMessage(), e);
|
throw new AIException("Failed to send POST request:" + e.getMessage(), e);
|
||||||
}
|
}
|
||||||
@ -98,13 +106,16 @@ public class BaseAIService {
|
|||||||
//链式构建请求
|
//链式构建请求
|
||||||
try {
|
try {
|
||||||
//设置超时3分钟
|
//设置超时3分钟
|
||||||
return HttpRequest.post(config.getApiUrl() + endpoint)
|
HttpRequest httpRequest = HttpRequest.post(config.getApiUrl() + endpoint)
|
||||||
.header(Header.CONTENT_TYPE, "multipart/form-data")
|
.header(Header.CONTENT_TYPE, "multipart/form-data")
|
||||||
.header(Header.ACCEPT, "application/json")
|
.header(Header.ACCEPT, "application/json")
|
||||||
.header(Header.AUTHORIZATION, "Bearer " + config.getApiKey())
|
.header(Header.AUTHORIZATION, "Bearer " + config.getApiKey())
|
||||||
.form(paramMap)
|
.form(paramMap)
|
||||||
.timeout(config.getTimeout())
|
.timeout(config.getTimeout());
|
||||||
.execute();
|
if (config.getHasProxy()) {
|
||||||
|
httpRequest.setProxy(config.getProxy());
|
||||||
|
}
|
||||||
|
return httpRequest.execute();
|
||||||
} catch (final AIException e) {
|
} catch (final AIException e) {
|
||||||
throw new AIException("Failed to send POST request:" + e.getMessage(), e);
|
throw new AIException("Failed to send POST request:" + e.getMessage(), e);
|
||||||
}
|
}
|
||||||
@ -123,6 +134,9 @@ public class BaseAIService {
|
|||||||
// 创建连接
|
// 创建连接
|
||||||
URL apiUrl = new URL(config.getApiUrl() + endpoint);
|
URL apiUrl = new URL(config.getApiUrl() + endpoint);
|
||||||
connection = (HttpURLConnection) apiUrl.openConnection();
|
connection = (HttpURLConnection) apiUrl.openConnection();
|
||||||
|
if (config.getHasProxy()) {
|
||||||
|
connection = (HttpURLConnection) apiUrl.openConnection(config.getProxy());
|
||||||
|
}
|
||||||
connection.setRequestMethod(Method.POST.name());
|
connection.setRequestMethod(Method.POST.name());
|
||||||
connection.setRequestProperty(Header.CONTENT_TYPE.getValue(), "application/json");
|
connection.setRequestProperty(Header.CONTENT_TYPE.getValue(), "application/json");
|
||||||
connection.setRequestProperty(Header.AUTHORIZATION.getValue(), "Bearer " + config.getApiKey());
|
connection.setRequestProperty(Header.AUTHORIZATION.getValue(), "Bearer " + config.getApiKey());
|
||||||
|
|||||||
@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
package cn.hutool.ai.core;
|
package cn.hutool.ai.core;
|
||||||
|
|
||||||
|
import java.net.Proxy;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
@ -39,6 +40,10 @@ public class BaseConfig implements AIConfig {
|
|||||||
protected volatile int timeout = 180000;
|
protected volatile int timeout = 180000;
|
||||||
//读取超时时间
|
//读取超时时间
|
||||||
protected volatile int readTimeout = 300000;
|
protected volatile int readTimeout = 300000;
|
||||||
|
//是否设置代理
|
||||||
|
protected volatile boolean hasProxy = false;
|
||||||
|
//代理设置
|
||||||
|
protected volatile Proxy proxy;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setApiKey(final String apiKey) {
|
public void setApiKey(final String apiKey) {
|
||||||
@ -104,4 +109,24 @@ public class BaseConfig implements AIConfig {
|
|||||||
public void setReadTimeout(final int readTimeout) {
|
public void setReadTimeout(final int readTimeout) {
|
||||||
this.readTimeout = readTimeout;
|
this.readTimeout = readTimeout;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean getHasProxy() {
|
||||||
|
return hasProxy;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setHasProxy(boolean hasProxy) {
|
||||||
|
this.hasProxy = hasProxy;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Proxy getProxy() {
|
||||||
|
return proxy;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setProxy(Proxy proxy) {
|
||||||
|
this.proxy = proxy;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,252 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2025 Hutool Team and hutool.cn
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package cn.hutool.ai.model.openai;
|
||||||
|
|
||||||
|
import cn.hutool.ai.AIServiceFactory;
|
||||||
|
import cn.hutool.ai.ModelName;
|
||||||
|
import cn.hutool.ai.Models;
|
||||||
|
import cn.hutool.ai.core.AIConfigBuilder;
|
||||||
|
import cn.hutool.ai.core.Message;
|
||||||
|
import cn.hutool.core.io.FileUtil;
|
||||||
|
import cn.hutool.core.thread.ThreadUtil;
|
||||||
|
import org.junit.jupiter.api.Disabled;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
import java.net.Proxy;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
|
||||||
|
class OpenaiProxyServiceTest {
|
||||||
|
|
||||||
|
String key = "your key";
|
||||||
|
//you proxy hostname
|
||||||
|
String hostname = "you proxy hostname";
|
||||||
|
//you proxy port
|
||||||
|
int port = 7890;
|
||||||
|
OpenaiService openaiService = AIServiceFactory.getAIService(new AIConfigBuilder(ModelName.OPENAI.getValue()).setApiKey(key).setProxy(new Proxy(Proxy.Type.HTTP,
|
||||||
|
new InetSocketAddress(hostname, port))).build(), OpenaiService.class);
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Disabled
|
||||||
|
void chat(){
|
||||||
|
final String chat = openaiService.chat("写一个疯狂星期四广告词");
|
||||||
|
assertNotNull(chat);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Disabled
|
||||||
|
void chatStream() {
|
||||||
|
String prompt = "写一个疯狂星期四广告词";
|
||||||
|
// 使用AtomicBoolean作为结束标志
|
||||||
|
AtomicBoolean isDone = new AtomicBoolean(false);
|
||||||
|
|
||||||
|
openaiService.chat(prompt, data -> {
|
||||||
|
assertNotNull(data);
|
||||||
|
if (data.contains("[DONE]")) {
|
||||||
|
// 设置结束标志
|
||||||
|
isDone.set(true);
|
||||||
|
} else if (data.contains("\"error\"")) {
|
||||||
|
isDone.set(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
// 轮询检查结束标志
|
||||||
|
while (!isDone.get()) {
|
||||||
|
ThreadUtil.sleep(100);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Disabled
|
||||||
|
void testChat(){
|
||||||
|
final List<Message> messages = new ArrayList<>();
|
||||||
|
messages.add(new Message("system","你是个抽象大师,会说很抽象的话,最擅长说抽象的笑话"));
|
||||||
|
messages.add(new Message("user","给我说一个笑话"));
|
||||||
|
final String chat = openaiService.chat(messages);
|
||||||
|
assertNotNull(chat);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Disabled
|
||||||
|
void chatVision() {
|
||||||
|
final OpenaiService openaiService = AIServiceFactory.getAIService(new AIConfigBuilder(ModelName.OPENAI.getValue())
|
||||||
|
.setApiKey(key).setModel(Models.Openai.GPT_4O_MINI.getModel()).build(), OpenaiService.class);
|
||||||
|
final String chatVision = openaiService.chatVision("图片上有些什么?", Arrays.asList("https://img2.baidu.com/it/u=862000265,4064861820&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=1544","https://img2.baidu.com/it/u=1682510685,1244554634&fm=253&fmt=auto&app=138&f=JPEG?w=803&h=800"));
|
||||||
|
assertNotNull(chatVision);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Disabled
|
||||||
|
void testChatVisionStream() {
|
||||||
|
final OpenaiService openaiService = AIServiceFactory.getAIService(new AIConfigBuilder(ModelName.OPENAI.getValue())
|
||||||
|
.setApiKey(key).setModel(Models.Openai.GPT_4O_MINI.getModel()).build(), OpenaiService.class);
|
||||||
|
String prompt = "图片上有些什么?";
|
||||||
|
List<String> images = Arrays.asList("https://img2.baidu.com/it/u=862000265,4064861820&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=1544\",\"https://img2.baidu.com/it/u=1682510685,1244554634&fm=253&fmt=auto&app=138&f=JPEG?w=803&h=800");
|
||||||
|
|
||||||
|
// 使用AtomicBoolean作为结束标志
|
||||||
|
AtomicBoolean isDone = new AtomicBoolean(false);
|
||||||
|
openaiService.chatVision(prompt,images, data -> {
|
||||||
|
assertNotNull(data);
|
||||||
|
if (data.contains("[DONE]")) {
|
||||||
|
// 设置结束标志
|
||||||
|
isDone.set(true);
|
||||||
|
} else if (data.contains("\"error\"")) {
|
||||||
|
isDone.set(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
// 轮询检查结束标志
|
||||||
|
while (!isDone.get()) {
|
||||||
|
ThreadUtil.sleep(100);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Disabled
|
||||||
|
void imagesGenerations() {
|
||||||
|
final OpenaiService openaiService = AIServiceFactory.getAIService(new AIConfigBuilder(ModelName.OPENAI.getValue())
|
||||||
|
.setApiKey(key).setModel(Models.Openai.DALL_E_3.getModel()).build(), OpenaiService.class);
|
||||||
|
final String imagesGenerations = openaiService.imagesGenerations("一位年轻的宇航员站在未来感十足的太空站内,透过巨大的弧形落地窗凝望浩瀚宇宙。窗外,璀璨的星河与五彩斑斓的星云交织,远处隐约可见未知星球的轮廓,仿佛在召唤着探索的脚步。宇航服上的呼吸灯与透明显示屏上的星图交相辉映,象征着人类科技与宇宙奥秘的碰撞。画面深邃而神秘,充满对未知的渴望与无限可能的想象。");
|
||||||
|
assertNotNull(imagesGenerations);
|
||||||
|
//https://oaidalleapiprodscus.blob.core.windows.net/private/org-l99H6T0zCZejctB2TqdYrXFB/user-LilDVU1V8cUxJYwVAGRkUwYd/img-yA9kNatHnBiUHU5lZGim1hP2.png?st=2025-03-07T01%3A04%3A18Z&se=2025-03-07T03%3A04%3A18Z&sp=r&sv=2024-08-04&sr=b&rscd=inline&rsct=image/png&skoid=d505667d-d6c1-4a0a-bac7-5c84a87759f8&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2025-03-06T15%3A04%3A42Z&ske=2025-03-07T15%3A04%3A42Z&sks=b&skv=2024-08-04&sig=rjcRzC5U7Y3pEDZ4ME0CiviAPdIpoGO2rRTXw3m8rHw%3D
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Disabled
|
||||||
|
void imagesEdits() {
|
||||||
|
final OpenaiService openaiService = AIServiceFactory.getAIService(new AIConfigBuilder(ModelName.OPENAI.getValue())
|
||||||
|
.setApiKey(key).setModel(Models.Openai.DALL_E_2.getModel()).build(), OpenaiService.class);
|
||||||
|
final File file = FileUtil.file("your imgUrl");
|
||||||
|
final String imagesEdits = openaiService.imagesEdits("茂密的森林中,有一只九色鹿若隐若现",file);
|
||||||
|
assertNotNull(imagesEdits);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Disabled
|
||||||
|
void imagesVariations() {
|
||||||
|
final OpenaiService openaiService = AIServiceFactory.getAIService(new AIConfigBuilder(ModelName.OPENAI.getValue())
|
||||||
|
.setApiKey(key).setModel(Models.Openai.DALL_E_2.getModel()).build(), OpenaiService.class);
|
||||||
|
final File file = FileUtil.file("your imgUrl");
|
||||||
|
final String imagesVariations = openaiService.imagesVariations(file);
|
||||||
|
assertNotNull(imagesVariations);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Disabled
|
||||||
|
void textToSpeech() {
|
||||||
|
final OpenaiService openaiService = AIServiceFactory.getAIService(new AIConfigBuilder(ModelName.OPENAI.getValue())
|
||||||
|
.setApiKey(key).setModel(Models.Openai.TTS_1_HD.getModel()).build(), OpenaiService.class);
|
||||||
|
final InputStream inputStream = openaiService.textToSpeech("万里山河一夜白,\n" +
|
||||||
|
"千峰尽染玉龙哀。\n" +
|
||||||
|
"长风卷起琼花碎,\n" +
|
||||||
|
"直上九霄揽月来。", OpenaiCommon.OpenaiSpeech.NOVA);
|
||||||
|
|
||||||
|
final String filePath = "your filePath";
|
||||||
|
final Path path = Paths.get(filePath);
|
||||||
|
try (final FileOutputStream outputStream = new FileOutputStream(filePath)) {
|
||||||
|
Files.createDirectories(path.getParent());
|
||||||
|
final byte[] buffer = new byte[1024];
|
||||||
|
int bytesRead;
|
||||||
|
while ((bytesRead = inputStream.read(buffer)) != -1) {
|
||||||
|
outputStream.write(buffer, 0, bytesRead);
|
||||||
|
}
|
||||||
|
} catch (final IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Disabled
|
||||||
|
void speechToText() {
|
||||||
|
final OpenaiService openaiService = AIServiceFactory.getAIService(new AIConfigBuilder(ModelName.OPENAI.getValue())
|
||||||
|
.setApiKey(key).setModel(Models.Openai.WHISPER_1.getModel()).build(), OpenaiService.class);
|
||||||
|
final File file = FileUtil.file("your filePath");
|
||||||
|
final String speechToText = openaiService.speechToText(file);
|
||||||
|
assertNotNull(speechToText);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Disabled
|
||||||
|
void embeddingText() {
|
||||||
|
final OpenaiService openaiService = AIServiceFactory.getAIService(new AIConfigBuilder(ModelName.OPENAI.getValue())
|
||||||
|
.setApiKey(key).setModel(Models.Openai.TEXT_EMBEDDING_3_SMALL.getModel()).build(), OpenaiService.class);
|
||||||
|
final String embeddingText = openaiService.embeddingText("萬里山河一夜白,千峰盡染玉龍哀,長風捲起瓊花碎,直上九霄闌月來");
|
||||||
|
assertNotNull(embeddingText);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Disabled
|
||||||
|
void moderations() {
|
||||||
|
final OpenaiService openaiService = AIServiceFactory.getAIService(new AIConfigBuilder(ModelName.OPENAI.getValue())
|
||||||
|
.setApiKey(key).setModel(Models.Openai.OMNI_MODERATION_LATEST.getModel()).build(), OpenaiService.class);
|
||||||
|
final String moderations = openaiService.moderations("你要杀人", "https://img2.baidu.com/it/u=862000265,4064861820&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=1544");
|
||||||
|
assertNotNull(moderations);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Disabled
|
||||||
|
void chatReasoning() {
|
||||||
|
final OpenaiService openaiService = AIServiceFactory.getAIService(new AIConfigBuilder(ModelName.OPENAI.getValue())
|
||||||
|
.setApiKey(key).setModel(Models.Openai.O3_MINI.getModel()).build(), OpenaiService.class);
|
||||||
|
final List<Message> messages = new ArrayList<>();
|
||||||
|
messages.add(new Message("system","你是现代抽象家"));
|
||||||
|
messages.add(new Message("user","给我一个KFC疯狂星期四的文案"));
|
||||||
|
final String chatReasoning = openaiService.chatReasoning(messages, OpenaiCommon.OpenaiReasoning.HIGH.getEffort());
|
||||||
|
assertNotNull(chatReasoning);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Disabled
|
||||||
|
void chatReasoningStream() {
|
||||||
|
final OpenaiService openaiService = AIServiceFactory.getAIService(new AIConfigBuilder(ModelName.OPENAI.getValue())
|
||||||
|
.setApiKey(key).setModel(Models.Openai.O3_MINI.getModel()).build(), OpenaiService.class);
|
||||||
|
final List<Message> messages = new ArrayList<>();
|
||||||
|
messages.add(new Message("system","你是现代抽象家"));
|
||||||
|
messages.add(new Message("user","给我一个KFC疯狂星期四的文案"));
|
||||||
|
|
||||||
|
// 使用AtomicBoolean作为结束标志
|
||||||
|
AtomicBoolean isDone = new AtomicBoolean(false);
|
||||||
|
openaiService.chatReasoning(messages,OpenaiCommon.OpenaiReasoning.HIGH.getEffort(), data -> {
|
||||||
|
assertNotNull(data);
|
||||||
|
if (data.contains("[DONE]")) {
|
||||||
|
// 设置结束标志
|
||||||
|
isDone.set(true);
|
||||||
|
} else if (data.contains("\"error\"")) {
|
||||||
|
isDone.set(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
// 轮询检查结束标志
|
||||||
|
while (!isDone.get()) {
|
||||||
|
ThreadUtil.sleep(100);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user