欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 高考 > SpringAi整合免费大模型(NVIDIA)

SpringAi整合免费大模型(NVIDIA)

2025/2/26 15:05:43 来源:https://blog.csdn.net/weixin_54925172/article/details/144143523  浏览:    关键词:SpringAi整合免费大模型(NVIDIA)

接上回,发布了springAI整合本地大模型之后,我们来看看怎么去利用别人已经训练好的大模型。

如果对整合本地大模型感兴趣的,请阅读:

SpringAI集成本地AI大模型ollama(调用篇)非常简单!!_spring ai ollama-CSDN博客

同样这里只做一些最基本的调用构造讲解,如果对深入使用比较感兴趣的,请阅读本文最后的Demo讲解。

一,拿到大模型的密钥

        只有拿到对应大模型的密钥才能去调用该模型的API,当然目前springAI支持的大模型有很多除了上次讲过的ollama之外,还有像千帆,通义千问,openai等等,当然本次使用的为英伟达的llama大模型。

        为啥呢,主要是人穷,银行卡比脸还干净,所以只能借用提供免费的API大模型了。

        废话不多说,请访问英伟达官网,拿到我们需要的密钥信息。

llama-3-swallow-70b-instruct-v0.1 Model by Tokyotech-llm | NVIDIA NIM

二, 编写代码,提供对外访问chatAPI

        如果你们有访问SpringAI官网的习惯也能找到简单的构造例子,但是需要注意的有几点。不小心会浪费大量时间在排查问题上。下面进行代码展示,同时会对需要注意的问题进行讲解。

        博主主要是写Java的,这里以java为例

构建简单的SpringBoot的maven项目。

1,pom文件中主要的依赖

		<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-openai-spring-boot-starter</artifactId><version>1.0.0-M4</version></dependency>

可以按照SpringAI官网中提供的依赖管理

2,  坑1依赖无法下载

按照这个配置完后,你会发现我们无法拉取到

spring-ai-openai-spring-boot-starter

的依赖,国内我们大多使用的是阿里云镜像,出现无法下载的情况,可以去实际的仓库地址看看有没有该依赖包。

解决该问题我们需要指定仓库,在pom文件中加入

<!--	使用自定义的仓库地址--><repositories><repository><id>spring-milestones</id><url>https://repo.spring.io/milestone</url><snapshots><enabled>true</enabled></snapshots></repository></repositories>

即可下载,如果不通外网,请自行考虑联通性问题。

当然这里还有一个坑,如果你使用了SpringAI提供的版本依赖管理,没有自己指定版本,也会出现无法拉取的问题。这里可以直接去仓库看看

原因很简单,根本没有

1.0.0-SNAPSHOT

快照版本当然拉取不到了。

2,编写基础的调用demo代码

        默认的启动类

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

        配置信息

spring.application.name=springAiDemo
spring.ai.openai.api-key={这是密钥}
spring.ai.openai.base-url=https://integrate.api.nvidia.comspring.ai.openai.chat.options.model=meta/llama-3.1-70b-instruct# The NVIDIA LLM API doesn't support embeddings, so we need to disable it.
spring.ai.openai.embedding.enabled=false# The NVIDIA LLM API requires this parameter to be set explicitly or server internal error will be thrown.
spring.ai.openai.chat.options.max-tokens=2048

        简单的controller(其中一个为流式调用)

package org.example.springaidemo.controller;import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.openai.OpenAiChatModel;
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 SimpleController {private final OpenAiChatModel chatModel;@Autowiredpublic SimpleController(OpenAiChatModel openAiChatModel) {this.chatModel = openAiChatModel;}@GetMapping("/ai/generate")public Map generate(@RequestParam(value = "message", defaultValue = "讲个笑话") String message) {return Map.of("generation", this.chatModel.call(message));}@GetMapping("/ai/generateStream")public Flux<ChatResponse> generateStream(@RequestParam(value = "message", defaultValue = "讲个笑话") String message) {Prompt prompt = new Prompt(new UserMessage(message));return this.chatModel.stream(prompt);}}

3, 坑2--项目无法启动

        第一请检查JDK版本是不是17,17以下的请更换到17.

        如果JDK版本正确,启动却报

Type must be a Function, BiFunction, Function1 or Function2. Found: org.apache.groovy.internal.util.Function<org.example.springaidemo.SpringAiDemoApplication$MockWeatherService$WeatherRequest, org.example.springaidemo.SpringAiDemoApplication$MockWeatherService$WeatherResponse>

        请删除您pom中的版本管理

三,正确启动,测试

测试1

        

测试2--流式调用需要前端做一定处理进行展示

以上。

如果对进阶使用感兴趣,请看下一篇

SpringAi整合大模型(进阶版)-CSDN博客

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词