国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Spring Boot集成Spring AI實(shí)現(xiàn)快速接入openAI

這篇具有很好參考價值的文章主要介紹了Spring Boot集成Spring AI實(shí)現(xiàn)快速接入openAI。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

1.什么是Spring AI?

Spring AI API 涵蓋了廣泛的功能。每個主要功能都在其專門的部分中進(jìn)行了詳細(xì)介紹。為了提供概述,可以使用以下關(guān)鍵功能:

  • 跨 AI 提供商的可移植 API,用于聊天、文本到圖像和嵌入模型。支持同步和流 API 選項(xiàng)。還支持下拉訪問模型特定功能。我們支持 OpenAI、Microsoft、Amazon、Google、Huggingface 等公司的 AI 模型。

  • 跨 Vector Store 提供商的可移植 API,包括同樣可移植的新穎的類似 SQL 的元數(shù)據(jù)過濾器 API。支持 8 個矢量數(shù)據(jù)庫。

  • 函數(shù)調(diào)用。Spring AI 使 AI 模型可以輕松調(diào)用 POJO java.util.Function 對象。

  • AI 模型和向量存儲的 Spring Boot 自動配置和啟動器。

  • 數(shù)據(jù)工程的 ETL 框架。這為將數(shù)據(jù)加載到矢量數(shù)據(jù)庫提供了基礎(chǔ),有助于實(shí)現(xiàn)檢索增強(qiáng)生成模式,使您能夠?qū)?shù)據(jù)引入 AI 模型以納入其響應(yīng)中。

Chat Completion API

  • 聊天 API 使開發(fā)人員能夠?qū)⑷斯ぶ悄苤С值牧奶旃δ芗傻剿麄兊膽?yīng)用程序中。它利用預(yù)先訓(xùn)練的語言模型,例如 GPT(生成式預(yù)訓(xùn)練變壓器),以自然語言對用戶輸入生成類似人類的響應(yīng)。

  • API 通常通過向 AI 模型發(fā)送提示或部分對話來工作,然后 AI 模型根據(jù)其訓(xùn)練數(shù)據(jù)和對自然語言模式的理解生成對話的完成或延續(xù)。然后,完成的響應(yīng)將返回到應(yīng)用程序,應(yīng)用程序可以將其呈現(xiàn)給用戶或?qū)⑵溆糜谶M(jìn)一步處理。

  • Spring AI Chat Completion API 被設(shè)計(jì)為一個簡單且可移植的接口,用于與各種 AI 模型交互,允許開發(fā)人員以最少的代碼更改在不同模型之間切換。這種設(shè)計(jì)符合 Spring 的模塊化和可互換性理念。

  • 此外,在輸入封裝 Prompt 和輸出處理 ChatResponse 等配套類的幫助下,聊天完成 API 統(tǒng)一了與 AI 模型的通信。它管理請求準(zhǔn)備和響應(yīng)解析的復(fù)雜性,提供直接且簡化的 API 交互。

2.openapi相關(guān)環(huán)境準(zhǔn)備

參考鏈接:https://www.rebelmouse.com/openai-account-set-up

免費(fèi)提供api-key

加博主微信,免費(fèi)送apikey嘗試

Spring Boot集成Spring AI實(shí)現(xiàn)快速接入openAI,spring,spring boot,人工智能,后端,java

3.代碼工程

實(shí)驗(yàn)?zāi)康模簩?shí)現(xiàn)聊天功能api

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.1</version>
    </parent>


    <modelVersion>4.0.0</modelVersion>


    <artifactId>ai</artifactId>


    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>
    <dependencies>




        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
            <version>0.8.0-SNAPSHOT</version>
        </dependency>




    </dependencies>
    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <releases>
                <enabled>false</enabled>
            </releases>
        </repository>
    </repositories>


</project>

application.yaml

server:
  port: 8088


spring:
  ai:
    openai:
      base-url: https://api.openai.com/
      api-key: sk-xxx
      embedding:
        options:
          model: text-davinci-003
      chat:
        #指定某一個API配置(覆蓋全局配置)
        api-key: sk-xxx
        base-url: https://api.openai.com/
        options:
          model: gpt-3.5-turbo # 模型配置

controller

package com.et.ai.controller;


import jakarta.annotation.Resource;
import org.springframework.ai.chat.ChatClient;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.embedding.EmbeddingClient;
import org.springframework.ai.embedding.EmbeddingResponse;
import org.springframework.ai.openai.OpenAiChatClient;
import org.springframework.ai.openai.api.OpenAiApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
public class HelloWorldController {
    @Autowired
    EmbeddingClient embeddingClient;
    @Autowired
    ChatClient chatClient;
    @GetMapping("/ai/embedding")
    public Map embed(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        EmbeddingResponse embeddingResponse = this.embeddingClient.embedForResponse(List.of(message));
        return Map.of("embedding", embeddingResponse);
    }
    @GetMapping("/ai/chat")
    public String chat(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        Prompt prompt = new Prompt(message);
        return chatClient.call(prompt).getResult().getOutput().getContent();
    }
}

DemoApplication.java

package com.et.ai;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class DemoApplication {


   public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
   }
}

以上只是一些關(guān)鍵代碼,所有代碼請參見下面代碼倉庫

代碼倉庫

  • https://github.com/Harries/springboot-demo

4.測試

  • 啟動Spring Boot應(yīng)用

  • 訪問http://127.0.0.1:8088/ai/chat,返回響應(yīng)消息

Why couldn't the bicycle stand up by itself? Because it was two tired!

5.參考引用

  • https://docs.spring.io/spring-ai/reference/api/embeddings/openai-embeddings.htm

  • http://www.liuhaihua.cn/archives/710471.html

  • https://springboot.io/t/topic/5166文章來源地址http://www.zghlxwxcb.cn/news/detail-860940.html

到了這里,關(guān)于Spring Boot集成Spring AI實(shí)現(xiàn)快速接入openAI的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 實(shí)現(xiàn)Spring Boot集成MyBatis

    在Java開發(fā)中,Spring Boot和MyBatis是非常常用的框架。Spring Boot是一個快速開發(fā)應(yīng)用程序的框架,而MyBatis是一個持久化框架,可以方便地操作數(shù)據(jù)庫。本文將介紹如何使用Idea集成Spring Boot和MyBatis,并創(chuàng)建一個簡單的示例項(xiàng)目。 步驟1:創(chuàng)建Spring Boot項(xiàng)目 1、打開Idea,點(diǎn)擊\\\"Create N

    2024年02月09日
    瀏覽(29)
  • 【Spring Boot】Spring Boot 集成 RocketMQ 實(shí)現(xiàn)簡單的消息發(fā)送和消費(fèi)

    【Spring Boot】Spring Boot 集成 RocketMQ 實(shí)現(xiàn)簡單的消息發(fā)送和消費(fèi)

    本文主要有以下內(nèi)容: 簡單消息的發(fā)送 順序消息的發(fā)送 RocketMQTemplate的API介紹 環(huán)境搭建: RocketMQ的安裝教程:在官網(wǎng)上下載bin文件,解壓到本地,并配置環(huán)境變量,如下圖所示: 在 Spring boot 項(xiàng)目中引入 RocketMQ 依賴: 在application.yml增加相關(guān)配置: 在 Spring Boot 中使用RocketM

    2024年02月14日
    瀏覽(93)
  • Spring Boot集成WebSocket實(shí)現(xiàn)消息推送

    Spring Boot集成WebSocket實(shí)現(xiàn)消息推送

    項(xiàng)目中經(jīng)常會用到消息推送功能,關(guān)于推送技術(shù)的實(shí)現(xiàn),我們通常會聯(lián)想到輪詢、comet長連接技術(shù),雖然這些技術(shù)能夠?qū)崿F(xiàn),但是需要反復(fù)連接,對于服務(wù)資源消耗過大,隨著技術(shù)的發(fā)展,HtML5定義了WebSocket協(xié)議,能更好的節(jié)省服務(wù)器資源和帶寬,并且能夠更實(shí)時地進(jìn)行通訊。

    2023年04月08日
    瀏覽(20)
  • Spring Boot 集成 Redisson 實(shí)現(xiàn)分布式鎖

    Spring Boot 集成 Redisson 實(shí)現(xiàn)分布式鎖

    ????????Redisson 是一種基于 Redis 的 Java 駐留集群的分布式對象和服務(wù)庫,可以為我們提供豐富的分布式鎖和線程安全集合的實(shí)現(xiàn)。在 Spring Boot 應(yīng)用程序中使用 Redisson 可以方便地實(shí)現(xiàn)分布式應(yīng)用程序的某些方面,例如分布式鎖、分布式集合、分布式事件發(fā)布和訂閱等。本篇

    2024年02月08日
    瀏覽(24)
  • Spring Boot集成Debezium實(shí)現(xiàn)postgres增量同步

    Debezium 是一個開源項(xiàng)目,為捕獲數(shù)據(jù)更改(change data capture,CDC)提供了一個低延遲的流式處理平臺。你可以安裝并且配置Debezium去監(jiān)控你的數(shù)據(jù)庫,然后你的應(yīng)用就可以消費(fèi)對數(shù)據(jù)庫的每一個行級別(row-level)的更改。只有已提交的更改才是可見的,所以你的應(yīng)用不用擔(dān)心事務(wù)(tra

    2024年04月11日
    瀏覽(13)
  • 【Spring Boot】集成Kafka實(shí)現(xiàn)消息發(fā)送和訂閱

    【Spring Boot】集成Kafka實(shí)現(xiàn)消息發(fā)送和訂閱

    最近忙著搞低代碼開發(fā),好久沒新建spring項(xiàng)目了,結(jié)果今天心血來潮準(zhǔn)備建個springboot項(xiàng)目 注意Type選Maven,java選8,其他默認(rèn) 點(diǎn)下一步后完成就新建了一個spring boot項(xiàng)目,配置下Maven環(huán)境,主要是settings.xml文件,里面要包含阿里云倉庫,不然可能依賴下載不下來 在maven配置沒問

    2024年02月09日
    瀏覽(31)
  • spring-boot集成spring-brick實(shí)現(xiàn)動態(tài)插件

    spring-boot集成spring-brick實(shí)現(xiàn)動態(tài)插件

    spring-boot集成spring-brick實(shí)現(xiàn)動態(tài)插件 項(xiàng)目結(jié)構(gòu) 需求實(shí)現(xiàn) spring-boot集成spring-brick 環(huán)境說明 1. 主程序集成spring-brick 第一步:引入相關(guān)依賴 第二步:修改程序入口方法 第三步:編寫配置 第四步:設(shè)置maven插件 2. 準(zhǔn)備plugin-api 第一步:引入相關(guān)依賴 第二步:引入相關(guān)依賴 3. 實(shí)現(xiàn)

    2024年02月14日
    瀏覽(23)
  • spring boot集成jasypt 并 實(shí)現(xiàn)自定義加解密

    由于項(xiàng)目中的配置文件 配置的地方過多,現(xiàn)將配置文件統(tǒng)一放到nacos上集中管理 且密碼使用加密的方式放在配置文件中 項(xiàng)目中組件使用的版本環(huán)境如下 spring cloud 2021.0.5 spring cloud alibaba 2021.0.5.0 spring boot 2.6.13 配置文件的加密使用 加密庫 jasypt 引入maven依賴 添加配置 使用jasy

    2024年02月11日
    瀏覽(21)
  • Spring Boot集成EasyExcel實(shí)現(xiàn)excel導(dǎo)入導(dǎo)出操作

    Spring Boot集成EasyExcel實(shí)現(xiàn)excel導(dǎo)入導(dǎo)出操作

    Easy Excel 官網(wǎng) Java解析、生成Excel比較有名的框架有Apache poi、jxl。但他們都存在一個嚴(yán)重的問題就是非常的耗內(nèi)存,poi有一套SAX模式的API可以一定程度的解決一些內(nèi)存溢出的問題,但POI還是有一些缺陷,比如07版Excel解壓縮以及解壓后存儲都是在內(nèi)存中完成的,內(nèi)存消耗依然很

    2024年02月14日
    瀏覽(19)
  • 【Redis系列】Spring Boot 集成 Redis 實(shí)現(xiàn)緩存功能

    【Redis系列】Spring Boot 集成 Redis 實(shí)現(xiàn)緩存功能

    ??????歡迎來到我的博客,很高興能夠在這里和您見面!希望您在這里可以感受到一份輕松愉快的氛圍,不僅可以獲得有趣的內(nèi)容和知識,也可以暢所欲言、分享您的想法和見解。 推薦:kwan 的首頁,持續(xù)學(xué)習(xí),不斷總結(jié),共同進(jìn)步,活到老學(xué)到老 導(dǎo)航 檀越劍指大廠系列:全面總

    2024年04月10日
    瀏覽(95)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包