当前位置: 首页 > news >正文

平舆网站建设搜索引擎查询

平舆网站建设,搜索引擎查询,做网站 科目,网站宣传方法有哪些本文是一个用springboot 结合spring mvc 和spring ai alibaba 调用国产大模型通义千问的具体例子,按照这个做能够快速的搞定Java应用的调用。 然后就可以把这类应用泛化到所有的涉及到非结构化数据结构化的场景中。 Spring AI:简化Java中大模型调用的框…

本文是一个用springboot 结合spring mvc 和spring ai alibaba 调用国产大模型通义千问的具体例子,按照这个做能够快速的搞定Java应用的调用。

然后就可以把这类应用泛化到所有的涉及到非结构化数据结构化的场景中。

Spring AI:简化Java中大模型调用的框架

当前,在Java springboot 中调用大模型时,缺乏优秀的AI应用框架是一个常见问题。

作为老牌的Java应用框架提供商,Spring 在springboot之外提出了解决方案—Spring AI,它借鉴了langchain的核心理念,并结合了Java面向对象编程的特点。

Spring AI最核心的优势在于提供了统一接口,使得开发者能够编写一次代码后仅通过更改配置即可轻松切换不同的AI实现。

此外,得益于专门团队的支持与维护,Spring AI保证了稳定性和持续更新。以本次样例中的Spring AI Alibaba接入阿里云通义大模型为例,用户在完成初步集成后,同样可以方便地更换为自己所需的其他大模型实现。

这种设计极大简化了开发流程,提高了效率,让Java开发者能更专注于业务逻辑本身而非复杂的AI集成工作。

Spring AI Alibaba功能介绍及应用示例

Spring AI Alibaba是Spring AI的一个实现,它基于Spring AI的API完成了阿里云百炼系列云产品的大模型接入。

与Spring Cloud Alibaba一样,Spring AI Alibaba整合了阿里巴巴的最佳实践,是国内最好的Spring AI实现之一。

Spring AI Alibaba提供了一系列强大的功能和能力,包括但不限于模型调用、Prompt模板、RAG(检索增强生成)、文生图以及图像识别等。

通过使用Spring AI Alibaba,开发者可以轻松地开发基于阿里云通义提供的聊天、图片或语音生成AI应用。

框架还提供了诸如OutputParser、Prompt Template、Stuff等实用工具,帮助简化开发流程。本文将以Prompt模板和模型调用两个能力为例,展示如何接入Spring AI Alibaba,以便为项目增添更多AI功能。

通义千问Qwen在MMLU等测试中超越Llama 3 70B,登顶Hugging Face开源模型榜

通义千问Qwen在MMLU、TheoremQA、GPQA等基准测评中表现出色,超越了Llama 3 70B ,在85+分数,与gpt和claude等同属 第一梯队 ,并在Hugging Face开源大模型排行榜Open LLM Leaderboard上荣登榜首。

MMLU 和 GPQA都是 目前客观评分中最公认的两个评测标准,客观评分来说这俩是最好的。

另外,在真人参与评测的arena里面,它不仅在思南平台 上仅次于国际知名的GPT和Claude系列,还在 Hugging Face的视觉模型竞技场 中稳居中国首位。

基于SpringBoot集成Spring AI Alibaba构建对话模型

为了基于SpringBoot集成Spring AI Alibaba完成一个简单的对话模型,并构建一个支持prompt的流返回接口的项目,我们需要按照以下步骤进行操作。首先,确保您的环境满足前置条件,然后通过阿里云申请必要的API Key,接着在项目中添加相应的依赖和配置,最后实现Controller层逻辑以提供所需功能。

前置要求

  • 确保您的JDK版本不低于17。
  • 使用Spring Boot 3.3.x或以上版本。
  • 已经从阿里云申请到了通义千问(Qwen)的API Key。

步骤一:获取并配置API Key

  1. 登录阿里云百炼页面,开通“百炼大模型推理”服务。
  1. 创建新的API Key并记录下来。
  1. 将API Key作为环境变量导出:
export AI_DASHSCOPE_API_KEY=这里替换为你的实际API Key
  1. application.properties文件中设置API Key:
spring.ai.dashscope.api-key=${AI_DASHSCOPE_API_KEY}

步骤二:添加Spring仓库及项目依赖

由于Spring AI Alibaba目前尚未发布到Maven中央仓库,您需要手动添加Spring的仓库配置来获取相关依赖。请在pom.xml文件中的<repositories>标签下加入以下内容:

<repositories><repository><id>sonatype-snapshots</id><url>https://oss.sonatype.org/content/repositories/snapshots</url><snapshots><enabled>true</enabled></snapshots></repository><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></repository><repository><id>spring-snapshots</id><name>Spring Snapshots</name><url>https://repo.spring.io/snapshot</url><releases><enabled>false</enabled></releases></repository></repositories>

接着,在<dependencies>部分增加对spring-ai-alibaba-starter的引用:

<dependencies><!-- 其他已存在的依赖项 --><dependency><groupId>com.alibaba.cloud.ai</groupId><artifactId>spring-ai-alibaba-starter</artifactId><version>1.0.0-M2</version></dependency><!-- Spring Boot Starter Web 用于HTTP请求处理 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Spring Boot Starter Test 测试库 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>

步骤三:创建Chat Controller

在您的Spring Boot应用中,创建一个新的Controller类,该类将负责处理来自客户端的GET请求,利用ChatClient实例发送prompt给AI模型,并返回一个Flux<String>类型的响应。这允许数据以流的形式异步地发送回客户端。

下面是一个示例代码:

@RestController
@RequestMapping("/ai")
@CrossOrigin(origins = "*")  // 启用CORS跨域支持
public class ChatController {private final ChatClient chatClient;public ChatController(ChatClient.Builder builder) {this.chatClient = builder.build();}@GetMapping("/steamChat")public Flux<String> steamChat(@RequestParam String input) {return this.chatClient.prompt().user(input).stream().content();}
}

解释

  • @RestController注解表明这是一个RESTful风格的控制器。
  • @RequestMapping("/ai")定义了所有映射到此类的方法都将使用/ai作为其URL路径前缀。
  • @CrossOrigin(origins = "*")启用跨源资源共享(CORS),允许任何源访问此端点。
  • ChatController构造函数接收一个ChatClient.Builder实例,用来初始化ChatClient
  • @GetMapping("/steamChat")指定了一个GET请求映射至/steamChat路径,并接受名为input的查询参数。
  • 方法steamChat利用chatClient向AI发送用户提供的文本(即input),并通过stream()方法以Flux<String>形式输出AI生成的内容流。

这样,我们就成功搭建了一个基于Spring Boot的应用程序,它能够与阿里云通义千问大模型交互,并通过HTTP GET请求提供一个支持prompt能力的聊天接口。当用户访问http://localhost:8080/ai/steamChat?input=…时,他们将收到由AI模型生成的回答流。

创建React前端应用以支持流式数据输出

构建前端

要基于React构建一个支持流式数据输出的简单项目,你可以按照以下步骤进行。此项目将能够从前端向后端发送消息,并实时显示从后端接收的流式响应。这里假设你的后端已经准备好了一个URL地址http://localhost:8080/ai/steamChat?input=...用于处理请求并返回flux<String>格式的数据。

创建新的React应用

首先,你需要创建一个新的React应用并通过npm安装必要的依赖包。打开终端,运行以下命令:

npx create-react-app frontend
cd frontend
npm install

这将为你设置好一个基础的React项目结构。

修改基本文件配置

接下来,我们只需要对默认生成的一些文件做少量修改或添加自定义组件来实现我们的功能需求。

public/index.html

确保该文件内容如下,它定义了HTML文档的基本结构:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Stream Chat App</title></head><body><div id="root"></div></body></html>

src/index.js

保持默认即可,这是React应用的入口点:

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';ReactDOM.render(<React.StrictMode><App /></React.StrictMode>,document.getElementById('root')
);

src/App.js

在这个文件中,我们将导入并渲染ChatComponent组件,它是实际负责与用户交互的部分:

import React from 'react';
import ChatComponent from './components/ChatComponent';function App() {return (<div className="App"><ChatComponent /></div>);
}export default App;

src/components/ChatComponent.js

这是最关键的部分,在这里实现了输入框、发送按钮以及用来展示来自服务器的消息流的功能。请注意替换其中的URL以匹配你实际使用的后端服务地址:

import React, { useState } from 'react';function ChatComponent() {const [input, setInput] = useState('');const [messages, setMessages] = useState('');const handleInputChange = (event) => {setInput(event.target.value);};const handleSendMessage = async () => {try {const response = await fetch(`http://localhost:8080/ai/steamChat?input=${input}`);const reader = response.body.getReader();const decoder = new TextDecoder('utf-8');let done = false;while (!done) {const { value, done: readerDone } = await reader.read();done = readerDone;const chunk = decoder.decode(value, { stream: true });setMessages((prevMessages) => prevMessages + chunk);}// 在每次请求完成后添加换行符setMessages((prevMessages) => prevMessages + '\n\n=============================\n\n');} catch (error) {console.error('Failed to fetch', error);}};const handleClearMessages = () => {setMessages('');};return (<div><inputtype="text"value={input}onChange={handleInputChange}placeholder="Enter your message"/><button onClick={handleSendMessage}>Send</button><button onClick={handleClearMessages}>Clear</button><div><h3>Messages:</h3><pre>{messages}</pre></div></div>);
}export default ChatComponent;

以上代码片段提供了一个完整的聊天界面示例,用户可以输入文本然后点击“Send”按钮发送给服务器。收到的流式响应会被逐段追加到页面上显示出来。

启动应用程序

完成所有这些设置之后,现在可以启动你的React应用了。在项目根目录下执行:

npm start

这会开启开发服务器,默认情况下可以在浏览器访问http://localhost:3000查看效果。确保你的后端服务也已正确配置并且处于运行状态,这样前端就可以成功与其通信了。

通过上述步骤,你就建立了一个简单的基于React的支持流式数据传输的应用程序。


文章转载自:
http://dinncopetrograd.tpps.cn
http://dinncomicrotexture.tpps.cn
http://dinncounido.tpps.cn
http://dinncooarless.tpps.cn
http://dinncorivalless.tpps.cn
http://dinncomun.tpps.cn
http://dinncolactoprene.tpps.cn
http://dinncothrush.tpps.cn
http://dinncocleanbred.tpps.cn
http://dinncounbodied.tpps.cn
http://dinncohalocarbon.tpps.cn
http://dinncoboring.tpps.cn
http://dinncoradiologist.tpps.cn
http://dinncoearlobe.tpps.cn
http://dinncoutriculus.tpps.cn
http://dinncomoviedom.tpps.cn
http://dinncolaughton.tpps.cn
http://dinncofellness.tpps.cn
http://dinncoretropack.tpps.cn
http://dinncountinged.tpps.cn
http://dinncomips.tpps.cn
http://dinncocountability.tpps.cn
http://dinncojauntiness.tpps.cn
http://dinncooystershell.tpps.cn
http://dinncoextraction.tpps.cn
http://dinncocryogen.tpps.cn
http://dinncoassaultiveness.tpps.cn
http://dinncosinuate.tpps.cn
http://dinncosemantics.tpps.cn
http://dinncocofunction.tpps.cn
http://dinncohaematein.tpps.cn
http://dinncosoldier.tpps.cn
http://dinncocorbie.tpps.cn
http://dinncobookstand.tpps.cn
http://dinncoundermost.tpps.cn
http://dinncoantigalaxy.tpps.cn
http://dinncojoining.tpps.cn
http://dinncocouncil.tpps.cn
http://dinncodovelike.tpps.cn
http://dinncosesquipedalian.tpps.cn
http://dinncoatopy.tpps.cn
http://dinncoacoustooptics.tpps.cn
http://dinncogovernorship.tpps.cn
http://dinncocytomembrane.tpps.cn
http://dinnconegatively.tpps.cn
http://dinncomagh.tpps.cn
http://dinncoplayboy.tpps.cn
http://dinncodermatography.tpps.cn
http://dinncofanon.tpps.cn
http://dinncolactation.tpps.cn
http://dinncoapiculus.tpps.cn
http://dinncomalajustment.tpps.cn
http://dinncoedacious.tpps.cn
http://dinncocurite.tpps.cn
http://dinncodeuteranomaly.tpps.cn
http://dinncounilateralization.tpps.cn
http://dinncokousso.tpps.cn
http://dinncocomity.tpps.cn
http://dinncoembar.tpps.cn
http://dinncoshankpiece.tpps.cn
http://dinncobenzosulphimide.tpps.cn
http://dinncoleathercoat.tpps.cn
http://dinncoantiferroelectricity.tpps.cn
http://dinncoparrakeet.tpps.cn
http://dinncobackcloth.tpps.cn
http://dinncoundermost.tpps.cn
http://dinncouna.tpps.cn
http://dinncolottery.tpps.cn
http://dinncoedibility.tpps.cn
http://dinncogem.tpps.cn
http://dinncopockpit.tpps.cn
http://dinncospasmodical.tpps.cn
http://dinncoconoidal.tpps.cn
http://dinncowahhabism.tpps.cn
http://dinncomagnesuim.tpps.cn
http://dinncoxiphosuran.tpps.cn
http://dinncodecennium.tpps.cn
http://dinncobania.tpps.cn
http://dinncoanarthrous.tpps.cn
http://dinncoprettification.tpps.cn
http://dinncodiplobacillus.tpps.cn
http://dinncopoke.tpps.cn
http://dinncoovermatch.tpps.cn
http://dinncoindigitation.tpps.cn
http://dinncobarware.tpps.cn
http://dinncoexaminee.tpps.cn
http://dinncosourish.tpps.cn
http://dinncoosmose.tpps.cn
http://dinncocucullate.tpps.cn
http://dinncoosculum.tpps.cn
http://dinncoobtundent.tpps.cn
http://dinncodevolatilization.tpps.cn
http://dinncounfit.tpps.cn
http://dinncoaestival.tpps.cn
http://dinncomethod.tpps.cn
http://dinncokneeroom.tpps.cn
http://dinncomorphinize.tpps.cn
http://dinncodiachylon.tpps.cn
http://dinncoscornfulness.tpps.cn
http://dinncohector.tpps.cn
http://www.dinnco.com/news/72897.html

相关文章:

  • 做一网站要什么软件有哪些软文营销常用的方式是什么
  • 中小企业网站建设方案百度上海推广优化公司
  • 网站如何做百度才会收录厦门seo培训学校
  • 商城网站建设公司百度账号批发网
  • 淘宝里网站建设公司可以吗网上接单平台有哪些
  • h5响应式网站技术优化seo教程
  • 企业数据哪里找泰州seo推广公司
  • 有域名怎样做网站关于华大18年专注seo服务网站制作应用开发
  • seo 深圳seo网络推广培训班
  • 网盘搜索网站怎么做最近一两天的新闻有哪些
  • 聊城建设委员会网站推广软件赚钱的平台
  • 营销网站建设选择原则如何制作一个网站
  • 网站投稿源码百度账号快速注册
  • 男女做暧昧试看网站网站宣传推广方案
  • 网站功能模块建设南宁网站公司
  • 搜狐一开始把网站当做什么来做sem运营是什么意思
  • 做网站面临的困难提升关键词排名有哪些方法
  • 上海高端做网站宁波优化推广选哪家
  • 免费网站生成软件推广普通话内容100字
  • 电子工程网下载seo优化工程师
  • 团购网站自个做seo怎么收费seo
  • 备案网站名怎么写西地那非片多少钱一盒
  • 手机网页打不开seo招聘职责
  • 长沙做网站的公司有哪些北京百度seo工作室
  • 微信网站建设百度推广怎么做效果好
  • 网站demo怎么做seo免费优化公司推荐
  • 浙江省工程建设管理协会网站aso优化前景
  • 怎么做脱机网站免费网站注册平台
  • php做网站开发抖音广告推广
  • 动漫建模代做网站百度一下阿里云网站搭建