2026年helloworld使用openAI通用模型的完整实现示例

helloworld使用openAI通用模型的完整实现示例Spring AI Alibaba HelloWorld 示例 本示例展示如何使用 Spring AI Alibaba 集成 GitCode API 的通义千问模型 你也可以改成适配 openai 接口的任意其他模型 快速开始 环境准备 Java 17 Maven 3 6 GitCode API Key 配置 在 application yml 中配置 server port

大家好,我是讯享网,很高兴认识大家。这里提供最前沿的Ai技术和互联网信息。

# Spring AI Alibaba HelloWorld 示例

本示例展示如何使用 Spring AI Alibaba 集成 GitCode API 的通义千问模型,你也可以改成适配openai接口的任意其他模型。

快速开始

环境准备

  • Java 17+
  • Maven 3.6+
  • GitCode API Key

配置

application.yml 中配置:

server: port: 18080 spring: application: name: spring-ai-alibaba-helloworld ai: openai: base-url: https://api-ai.gitcode.com api-key: your_api_key_here chat: model: Qwen/Qwen3.5-35B-A3B options: temperature: 0.7 max-tokens: 1024 

配置说明:

  • base-url: GitCode API 基础地址
  • api-key: GitCode API 密钥
  • model: 使用的模型名称
  • temperature: 控制输出随机性(0-1)
  • max-tokens: 最大生成 token 数

启动

mvn spring-boot:run 

应用将启动在 http://localhost:18080

接口文档

HelloworldController

接口 方法 描述
/helloworld/simple/chat GET 简单对话
/helloworld/stream/chat GET 流式对话
/helloworld/advisor/chat/{conversationId} GET 带记忆的对话

EnhancedController

接口 方法 描述
/api/health GET 健康检查
/api/chat/simple GET 简单聊天(RestClient)
/api/chat/system-prompt GET 带系统提示词的聊天
/api/chat/multi-turn POST 多轮对话

测试示例

# 健康检查 curl http://localhost:18080/api/health # 简单对话 curl "http://localhost:18080/helloworld/simple/chat?query=你好" # 流式对话 curl -N "http://localhost:18080/helloworld/stream/chat?query=Hello" # 带记忆的对话 curl "http://localhost:18080/helloworld/advisor/chat/session123?query=你好" 

技术实现

配置管理

项目采用统一的配置管理方式,所有 API 相关参数均从 application.yml 读取:

  • HelloworldController: 使用 Spring AI ChatClient,自动从配置文件读取
  • EnhancedController: 使用 RestClient,通过 @Value 注解注入配置参数

核心组件

  • Spring Boot: 应用框架
  • Spring AI OpenAI: AI 功能集成(兼容 OpenAI API)
  • GitCode API: 通义千问模型服务
  • ChatClient: Spring AI 核心对话接口
  • RestClient: 直接 HTTP 调用实现
  • Advisor: 功能增强组件
  • ChatMemory: 对话记忆管理

技术栈

  • Spring Boot 3.2.5
  • Spring AI 1.0.0-M6
  • Java 17
  • GitCode API (Qwen3.5-35B-A3B)

部署

# 构建 mvn clean package # 运行 java -jar target/spring-ai-alibaba-helloworld-1.0.0.jar 

注意事项

  • 确保网络能访问大模型服务
  • 建议在环境添加超时配置和重试机制
小讯
上一篇 2026-04-20 08:22
下一篇 2026-04-20 08:20

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/271023.html