Hutool Hutool
(opens new window)
🏡首页
📖指南
  • 核心(Hutool-core)
  • 配置文件(Hutool-setting)
  • 日志(Hutool-log)
  • 缓存(Hutool-cache)
  • JSON(Hutool-json)
  • 加密解密(Hutool-crypto)
  • DFA查找(Hutool-dfa)
  • 数据库(Hutool-db)
  • HTTP客户端(Hutool-http)
  • 定时任务(Hutool-cron)
  • 扩展(Hutool-extra)
  • 布隆过滤(Hutool-bloomFilter)
  • 切面(Hutool-aop)
  • 脚本(Hutool-script)
  • Office文档操作(Hutool-poi)
  • 系统调用(Hutool-system)
  • 图形验证码(Hutool-captcha)
  • 网络Socket(Hutool-socket)
  • JWT(Hutool-jwt)
💡javaDoc (opens new window)
⏳更新记录 (opens new window)
  • 🍎gitee (opens new window)
  • 🍏github (opens new window)
(opens new window)
🏡首页
📖指南
  • 核心(Hutool-core)
  • 配置文件(Hutool-setting)
  • 日志(Hutool-log)
  • 缓存(Hutool-cache)
  • JSON(Hutool-json)
  • 加密解密(Hutool-crypto)
  • DFA查找(Hutool-dfa)
  • 数据库(Hutool-db)
  • HTTP客户端(Hutool-http)
  • 定时任务(Hutool-cron)
  • 扩展(Hutool-extra)
  • 布隆过滤(Hutool-bloomFilter)
  • 切面(Hutool-aop)
  • 脚本(Hutool-script)
  • Office文档操作(Hutool-poi)
  • 系统调用(Hutool-system)
  • 图形验证码(Hutool-captcha)
  • 网络Socket(Hutool-socket)
  • JWT(Hutool-jwt)
💡javaDoc (opens new window)
⏳更新记录 (opens new window)
  • 🍎gitee (opens new window)
  • 🍏github (opens new window)
  • 快速入门

  • 核心(Hutool-core)

    • AI(Hutool-ai)

      • 概述
      • AI封装-AIUtil
      • AI配置Builder-AIConfigBuilder
      • AI核心工厂-AIServiceFactory
      • AI核心服务-AIService
      • DeepSeek服务-DeepSeekService
      • 豆包服务-DoubaoService
      • Grok服务-GrokService
      • Openai服务-OpenaiService
        • 介绍
        • 特有功能
        • 使用
          • 视觉相关
          • 语音相关
          • 其他
    • 克隆

    • 类型转换

    • 日期时间

    • IO流相关

    • 工具类

    • 语言特性

    • JavaBean

    • 集合类

    • Map

    • Codec编码

    • 文本操作

    • 注解

    • 比较器

    • 异常

    • 数学

    • 线程和并发

    • 图片

    • 网络

    • 源码编译

  • 配置文件(Hutool-setting)

  • 日志(Hutool-log)

  • 缓存(Hutool-cache)

  • JSON(Hutool-json)

  • 加密解密(Hutool-crypto)

  • DFA查找(Hutool-dfa)

  • 数据库(Hutool-db)

  • HTTP客户端(Hutool-http)

  • 定时任务(Hutool-cron)

  • 扩展(Hutool-extra)

  • 布隆过滤(Hutool-bloomFilter)

  • 切面(Hutool-aop)

  • 脚本(Hutool-script)

  • Office文档操作(Hutool-poi)

  • 系统调用(Hutool-system)

  • 图形验证码(Hutool-captcha)

  • 网络Socket(Hutool-socket)

  • JWT(Hutool-jwt)

  • 指南
  • 核心(Hutool-core)
  • AI(Hutool-ai)
Hutool
2025-05-13
目录

Openai服务-OpenaiService

特别赞助 by:

# 介绍

OpenaiService 是 Hutool AI 模块中专门对接 OpenAI 服务的接口,提供了对 OpenAI 各种功能的完整支持,包括聊天、图像生成与处理、语音转换、文本向量化等功能。

# 特有功能

  • 视觉相关
    • 图像理解 (chatVision)
    • 文生图 (imagesGenerations)
    • 图片编辑 (imagesEdits)
    • 图片变形 (imagesVariations)
  • 语音相关
    • 文本转语音 (textToSpeech)
    • 语音转文本 (speechToText)
  • 其他
    • 文本向量化 (embeddingText)
    • 内容审核 (moderations)
    • 推理聊天 (chatReasoning)

# 使用

# 视觉相关

  • 视觉理解
OpenaiService openaiService = AIServiceFactory.getAIService(new AIConfigBuilder(ModelName.OPENAI.getValue()).setApiKey(key).setModel(Models.Openai.GPT_4O_MINI.getModel()).build(), OpenaiService.class);
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"));
  • 文生图
OpenaiService openaiService = AIServiceFactory.getAIService(new AIConfigBuilder(ModelName.OPENAI.getValue()).setApiKey(key).setModel(Models.Openai.DALL_E_3.getModel()).build(), OpenaiService.class);
String imagesGenerations = openaiService.imagesGenerations("一位年轻的宇航员站在未来感十足的太空站内,透过巨大的弧形落地窗凝望浩瀚宇宙。
        窗外,璀璨的星河与五彩斑斓的星云交织,远处隐约可见未知星球的轮廓,仿佛在召唤着探索的脚步。宇航服上的呼吸灯与透明显示屏上的星图交相辉映,象征着人类科技与宇宙奥秘的碰撞。画面深邃而神秘,充满对未知的渴望与无限可能的想象。");
  • 图片编辑
OpenaiService openaiService = AIServiceFactory.getAIService(new AIConfigBuilder(ModelName.OPENAI.getValue()).setApiKey(key).setModel(Models.Openai.DALL_E_2.getModel()).build(), OpenaiService.class);
File file = FileUtil.file("your imgUrl");
String imagesEdits = openaiService.imagesEdits("茂密的森林中,有一只九色鹿若隐若现",file);
  • 图片变形
OpenaiService openaiService = AIServiceFactory.getAIService(new AIConfigBuilder(ModelName.OPENAI.getValue()).setApiKey(key).setModel(Models.Openai.DALL_E_2.getModel()).build(), OpenaiService.class);
File file = FileUtil.file("your imgUrl");
String imagesVariations = openaiService.imagesVariations(file);

# 语音相关

  • TTS-文字转语音
OpenaiService openaiService = AIServiceFactory.getAIService(new AIConfigBuilder(ModelName.OPENAI.getValue()).setApiKey(key).setModel(Models.Openai.TTS_1_HD.getModel()).build(), OpenaiService.class);
InputStream inputStream = openaiService.textToSpeech("万里山河一夜白,\n" +"千峰尽染玉龙哀。\n" +
			"长风卷起琼花碎,\n" +"直上九霄揽月来。", OpenaiCommon.OpenaiSpeech.NOVA);
String filePath = "your filePath";
Path path = Paths.get(filePath);
    try (final FileOutputStream outputStream = new FileOutputStream(filePath)) {
		Files.createDirectories(path.getParent());
		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);
	}
  • STT-语音转文字
OpenaiService openaiService = AIServiceFactory.getAIService(new AIConfigBuilder(ModelName.OPENAI.getValue()).setApiKey(key).setModel(Models.Openai.WHISPER_1.getModel()).build(), OpenaiService.class);
File file = FileUtil.file("your filePath");
String speechToText = openaiService.speechToText(file);	

# 其他

  • 文本向量化
OpenaiService openaiService = AIServiceFactory.getAIService(new AIConfigBuilder(ModelName.OPENAI.getValue()).setApiKey(key).setModel(Models.Openai.TEXT_EMBEDDING_3_SMALL.getModel()).build(), OpenaiService.class);
String embeddingText = openaiService.embeddingText("万里山河一夜白,千峰尽染玉龙哀。长风卷起琼花碎,直上九霄揽月来。");
  • 内容审核
OpenaiService openaiService = AIServiceFactory.getAIService(new AIConfigBuilder(ModelName.OPENAI.getValue()).setApiKey(key).setModel(Models.Openai.OMNI_MODERATION_LATEST.getModel()).build(), OpenaiService.class);
String moderations = openaiService.moderations("检测文本", "https://img2.baidu.com/it/u=862000265,4064861820&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=1544");
  • 推理聊天
final OpenaiService openaiService = AIServiceFactory.getAIService(new AIConfigBuilder(ModelName.OPENAI.getValue()).setApiKey(key).setModel(Models.Openai.O3_MINI.getModel()).build(), OpenaiService.class);
List<Message> messages = new ArrayList<>();
messages.add(new Message("system","你是现代抽象家"));
messages.add(new Message("user","给我一个KFC疯狂星期四的文案"));
String chatReasoning = openaiService.chatReasoning(messages, OpenaiCommon.OpenaiReasoning.HIGH.getEffort());
上次更新: 2025/05/21, 11:39:56
Grok服务-GrokService
支持泛型的克隆接口和克隆类

← Grok服务-GrokService 支持泛型的克隆接口和克隆类→

Theme by Vdoing | Copyright © 2023-2025 Hutool | MulanPSL-2.0
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×