springai系列(二)从0开始搭建和接入azure-openai实现智能问答

文章目录

前言

之前使用openai的官网的api需要科学上网,但是我们可以使用其他的代理间接实现使用chatgpt的相关模型,解决这个问题。比如:本文使用azure openai来实现这个功能。开发框架是java的springai。

1.从0开始搭建项目

生成项目和相关的pom依赖。

生成的pom文件如下:

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>3.4.3</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo</name>
	<description>Demo project for Spring Boot</description>
	<url/>
	<licenses>
		<license/>
	</licenses>
	<developers>
		<developer/>
	</developers>
	<scm>
		<connection/>
		<developerConnection/>
		<tag/>
		<url/>
	</scm>
	<properties>
		<java.version>21</java.version>
		<spring-ai.version>1.0.0-M6</spring-ai.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.ai</groupId>
			<artifactId>spring-ai-azure-openai-spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.ai</groupId>
				<artifactId>spring-ai-bom</artifactId>
				<version>${spring-ai.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

2.进入微软openai申请key

申请入口
https://portal.azure.com/#home

可以根据下面的文章创建服务,选择模型部署。
https://www.zhihu.com/question/624318530/answer/3291008787

创建完成后,会得到下面的一个部署名称,apil-key,和节点

3.配置application.yaml

yaml 复制代码
spring:
  ai:
    azure:
      openai:
        api-key: xxxsxxx
        endpoint: https://xxx.openai.azure.com/
        chat:
          options:
            maxTokens: 4096
            temperature: 0.7
            deployment-name: 上面的部署名称

4.编写controller

java 复制代码
package com.example.demo.controller;

import org.springframework.ai.azure.openai.AzureOpenAiChatModel;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import java.util.Map;

@RestController
public class ChatController {

    private final AzureOpenAiChatModel chatModel;

    @Autowired
    public ChatController(AzureOpenAiChatModel chatModel) {
        this.chatModel = chatModel;
    }

    @GetMapping("/ai/generate")
    public Map generate(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        return Map.of("generation", this.chatModel.call(message));
    }

    @GetMapping("/ai/generateStream")
	public Flux<ChatResponse> generateStream(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        Prompt prompt = new Prompt(new UserMessage(message));
        return this.chatModel.stream(prompt);
    }

}

5.测试

显示可以正常问答了。

源码下载地址

https://download.csdn.net/download/baidu_21349635/90436706

总结

这篇文章介绍了如何使用Azure OpenAI API在Java Spring Boot项目中实现ChatGPT功能。文章的主要步骤包括:

  1. 项目搭建
    生成一个Spring Boot项目并配置相关的pom.xml依赖,使用spring-boot-starter-web和spring-ai-azure-openai-spring-boot-starter作为核心依赖。项目还使用了spring-ai-bom来管理版本。
  2. 获取Azure OpenAI API Key
    通过微软Azure门户申请API密钥,并创建OpenAI服务实例。完成后,将获得一个部署名称和API节点,用于配置API请求。
  3. 配置application.yaml
    在application.yaml文件中配置API密钥、API端点、聊天选项(如最大token数和温度),以及部署名称。
  4. 编写Controller
    创建一个ChatController类,使用AzureOpenAiChatModel与OpenAI API进行交互。提供两个接口:一个是同步生成消息的接口(/ai/generate),另一个是流式生成消息的接口(/ai/generateStream)。
  5. 测试
    运行应用程序并测试接口,确认可以正常通过API生成聊天回复。
    总体来说,文章通过详细的步骤指导开发者在Spring Boot框架下集成Azure OpenAI,实现与ChatGPT模型的交互。

本文完

相关推荐
嗷嗷哦润橘_7 小时前
AI Agent学习:MetaGPT之我的工作
人工智能·学习·flask
武藤一雄13 小时前
.NET 中常见计时器大全
microsoft·微软·c#·.net·wpf·.netcore
董世昌4114 小时前
你对面向对象编程的理解,面向过程和面向对象有什么区别?
microsoft
小小代码团1 天前
2026 Office Online Server (全网最新/最详细/含问题修复) 终极部署教程
windows·microsoft·c#
老赵聊算法、大模型备案1 天前
2025 年 12 月北京市生成式人工智能服务备案分析:政务场景再扩容,合规生态更聚焦
人工智能·算法·microsoft·aigc·政务
Jack___Xue1 天前
LangChain实战快速入门笔记(五)--LangChain使用之Tools
笔记·microsoft·langchain
Leinwin1 天前
Microsoft 365 Copilot:更“懂你”的AI助手
人工智能·microsoft·copilot
专注VB编程开发20年1 天前
C#内存加载dll和EXE是不是差不多,主要是EXE有入口点
数据库·windows·microsoft·c#
CNRio2 天前
智能赋能全球化:AI Agent驱动中国科技企业出海的政技融合新范式
人工智能·科技·microsoft
2501_925317132 天前
[鸿蒙2025领航者闯关] 把小智AI装进「第二大脑」:从开箱到MCP智能体的全链路实战
人工智能·microsoft·harmonyos·鸿蒙2025领航者闯关·小智ai智能音箱·mcp开发