当前位置:   article > 正文

【尝鲜】SpringCloudAlibaba AI 配置&使用教程_spring-ai-core

spring-ai-core

1、环境配置

<dependencies>

		<!--Base-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<!--AI-->
		<dependency>
			<groupId>com.alibaba.cloud</groupId>
			<artifactId>spring-cloud-starter-alibaba-ai</artifactId>
			<version>2023.0.1.0</version>
			<exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-simple</artifactId>
                </exclusion>
            </exclusions>
		</dependency>

	</dependencies>

	<repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • application.yaml
# Tomcat
server:
  port: 9310

# Spring
spring:
  cloud:
    ai:
      tongyi:
          api-key: api密钥
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

api-key申请地址:申请地址
在这里插入图片描述

2、服务实现

controller就不详细展示了,主要展示service中如何调用

@Service
@AllArgsConstructor
public class TongyiServiceImpl implements ITongyiService {

    private final ChatClient chatClient;
    private final ImageClient imageClient;
    private final SpeechClient speechClient;


	// 对话
    @Override
    public String chat(String question) {
        return chatClient.call(question);
    }
	
	// 文生图
    @Override
    public String image(String promptWord) {
        ImagePrompt imagePrompt = new ImagePrompt(promptWord);
        return imageClient.call(imagePrompt).getResult().getOutput().getUrl();
    }

	// 语音
    @Override
    public ByteBuffer speech(String promptWord) {
        SpeechPrompt question = new SpeechPrompt(promptWord);
        return speechClient.call(question).getResult().getOutput();
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

效果图
java聊天gpt
java文生图ai

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/873743
推荐阅读
相关标签
  

闽ICP备14008679号