SpringAI简单使用(本地模型+自定义知识库)

news2024/9/19 15:19:41

Ollama

简介

Ollama是一个开源的大型语言模型服务工具,它允许用户在本地机器上构建和运行语言模型,提供了一个简单易用的API来创建、运行和管理模型,同时还提供了丰富的预构建模型库,这些模型可以轻松地应用在多种应用场景中。Ollama支持多种操作系统,包括macOS、Windows、Linux,并提供Docker镜像,方便用户在不同环境中部署使用 。

Ollama的特点包括轻量级和可扩展性,它允许用户通过命令行界面(CLI)或REST API与语言模型进行交互。用户可以下载并运行预训练的模型,如Llama 2、Mistral、Dolphin Phi等,这些模型具有不同的参数量和大小,适用于不同的使用场景和需求 。

此外,Ollama还支持模型的自定义,用户可以根据自己的需求调整模型参数,或者导入自有的模型进行使用。例如,用户可以通过创建Modelfile来定制模型,Modelfile是一个配置文件,用于定义和管理Ollama平台上的模型,通过模型文件可以创建新模型或修改现有模型,以适应特定的应用场景 。

安装

官网:https://ollama.com/
Github:https://github.com/ollama/ollama

进入官网之后,点击download下载对应系统版本进行安装。
ollama下载

模型使用llama3
官网:https://ollama.com/library/llama3

ollama下载完成之后,打开命令行,运行命令ollama run llama3,自动下载模型,在命令行可进行简单的聊天
llama3命令行
llama3有8B和70B,上面的命令运行之后,默认选择的是8B
在这里插入图片描述

客户端

python客户端:https://github.com/ollama/ollama-python

import ollama
response = ollama.chat(model='llama3', messages=[
  {
    'role': 'user',
    'content': 'Why is the sky blue?',
  },
])
print(response['message']['content'])

流式响应:

import ollama

stream = ollama.chat(
    model='llama3',
    messages=[{'role': 'user', 'content': '用中文讲一个笑话'}],
    stream=True,
)

for chunk in stream:
  print(chunk['message']['content'], end='', flush=True)

Web UI

Ollama的Github中推荐的UI项目:
在这里插入图片描述
这里我们使用hollama:https://github.com/fmaclen/hollama

先克隆hollama的源代码,进入目录之后运行npm i --registry=https://registry.npmmirror.com安装依赖,然后运行npm run dev启动项目

进入setting中设置ServerModel
在这里插入图片描述
然后再sessions里面可以进行聊天

在这里插入图片描述

Spring AI

官网:https://docs.spring.io/spring-ai/reference/index.html

ollama文档:https://docs.spring.io/spring-ai/reference/api/chat/ollama-chat.html

1、通过https://start.spring.io/创建项目,并引入Ollama AI
在这里插入图片描述
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.3.1</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>pers.fengxu</groupId>
	<artifactId>springaidemo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>springaidemo</name>
	<description>Demo project for Spring Boot</description>
	<url/>
	<licenses>
		<license/>
	</licenses>
	<developers>
		<developer/>
	</developers>
	<scm>
		<connection/>
		<developerConnection/>
		<tag/>
		<url/>
	</scm>
	<properties>
		<java.version>22</java.version>
		<spring-ai.version>1.0.0-M1</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-ollama-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>
	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>

</project>

配置文件application.properties

spring.application.name=springaidemo
spring.ai.ollama.base-url=http://localhost:11434
spring.ai.ollama.chat.options.model=llama3

新建controller

package pers.fengxu.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.ollama.OllamaChatModel;
import org.springframework.ai.ollama.api.OllamaApi;
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 OllamaChatModel chatModel;

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

    @GetMapping("/ai/generate")
    public Map generate(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        return Map.of("generation", 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 chatModel.stream(prompt);
    }

}

新建启动类

package pers.fengxu.springaidemo;

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

@SpringBootApplication
public class SpringaidemoApplication {

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

}

启动项目之后,访问:http://localhost:8080/ai/generate

在这里插入图片描述

AnyThingLLM

简介

AnythingLLM 是一款强大的人工智能商业智能工具,用于商业智能和文档处理,具有以下主要特点:

  1. 多平台支持:适用于 MacOS、Linux 和 Windows 系统。
  2. 隐私保护:可以在本地运行,无需互联网连接。
  3. 自定义模型:支持使用闭源模型如 GPT-4 或自定义微调模型如 Llama2。
  4. 多文档处理:不仅支持 PDF,还能处理 Word 文档等多种格式。
  5. 工作区管理:通过“工作区”管理文档,保持上下文清晰。
  6. 成本效益高:管理大型文档时,成本比其他解决方案节省高达 90%。
  7. 开发者友好:提供完整的开发者 API,支持自定义集成。
  8. 多用户支持:支持多用户实例和权限管理。
  9. 遥测功能:可选的匿名使用信息收集,帮助改进产品。

安装配置

官网:https://useanything.com/download

下载之后,双击安装,之后打开进行初始设置:
在这里插入图片描述
选择Ollama
在这里插入图片描述
继续
在这里插入图片描述
设置工作区名称:
在这里插入图片描述
可以在设置里面进行语言和其他相关属性的配置:

在这里插入图片描述

在这里插入图片描述

知识库导入

现在先问ai一个它可能不知道的问题,例如“高启强是谁?”,它的回答显然有些驴头不对马嘴。

在这里插入图片描述

点击左边的上传按钮

在这里插入图片描述

左边支持网址和文本

在这里插入图片描述
所以可以直接讲百度百科的链接提供给ai学习:

地址为:https://baike.baidu.com/item/%E9%AB%98%E5%90%AF%E5%BC%BA/59990049

在这里插入图片描述
解析网页完成之后,将该知识库移动至当前空间
在这里插入图片描述
点击保存
在这里插入图片描述

然后再次输入问题,便可以得到我们想要的答案。

在这里插入图片描述
备注:如果电脑性能不够可以选择阿里的qwen2:0.5b模型,只需要几百兆,运行ollama run qwen2:0.5b即可安装运行,并且对中文的支持更好,对应网址:https://ollama.com/library/qwen2:0.5b

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1937858.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

使用shedlock实现分布式互斥执行

前言 前序章节&#xff1a;springboot基础(82):分布式定时任务解决方案shedlock 如果你不清楚shedlock&#xff0c;建议先阅读前序章节&#xff0c;再来查看本文。 如果我们不在spring环境下&#xff0c;如何使用shedlock实现分布式互斥执行&#xff1f; 我们可以使用shedl…

【Java数据结构】线性表之栈和队列

栈&#xff08;Stack&#xff09; 简单描述 栈&#xff1a;一种特殊的线性表&#xff0c;其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶&#xff0c;另一端称为栈底。栈中的数据元素遵守后进先出LIFO&#xff08;Last In First Out&…

DETR算法解读——Transformer在目标检测任务的首次应用

论文&#xff1a;End-to-End Object Detection with Transformers 作者&#xff1a;Nicolas Carion, Francisco Massa, Gabriel Synnaeve, Nicolas Usunier, Alexander Kirillov, Sergey Zagoruyko 机构&#xff1a;Facebook AI 链接&#xff1a;https://arxiv.org/abs/2005.12…

【网络】网络基础(框架)

网络基础 序言&#xff1a;什么是网络&#xff1f;一、协议的概念二、网络位置三、网络协议1、协议分层2、OSI七层模型与TCP/IP五层(或四层)模型 四、网络传输基本流程1、网络传输流程图2、以太网通信&#xff08;1&#xff09;一个小故事&#xff08;2&#xff09;原理i、问题…

解答word图标变白

把WPS卸载了之后就变成白色了&#xff0c;然后在注册表中把word的地址改成office word的地址之后图标变成这样了&#xff0c;怎么办 1.winR打开命令提示符 2.输入regedit&#xff0c;打开注册表编辑器 3.找到下面这个路径 计算机\HKEY_CLASSES_ROOT\Word.Document.8\Defaul…

从零开始学量化~Ptrade使用教程(七)——期权相关操作

期权交易 可点击证券代码右侧的选&#xff0c;进入期权选择菜单。通过选择标的商品&#xff0c;认购期权和认沽期权中间的选项&#xff08;包括代码、成交价、幅度%、隐波%、内在价值、时间价值等&#xff09;&#xff0c;以及认购期权或认沽期权&#xff0c;选择所需的期权标的…

2024年第二季度 DDoS 威胁趋势报告

2024 年上半年&#xff0c;Cloudflare 缓解了 850 万次 DDoS 攻击&#xff1a;第一季度 450 万次&#xff0c;第二季度 400 万次。总体而言&#xff0c;第二季度 DDoS 攻击数量环比下降了 11%&#xff0c;但同比增长了 20%。 DDoS 攻击分布&#xff08;按类型和手段&#xff09…

pycharm中运行.sh文件

最近在跑一个项目代码&#xff0c;里面要运行.sh文件。于是配置了下如何在pycharm中正常运行.sh文件。 首先安装好git&#xff0c;然后 File—>Settings—>Tools—>Terminal—>Shell path&#xff0c;将cmd.exe改成刚刚下载的git的路径&#xff0c;注意选择的是s…

【MySQL进阶之路 | 高级篇】EXPLAIN的进一步使用

1. EXPLAIN的四种输出格式 EXPLAIN可以输出四种格式&#xff1a;传统格式&#xff0c;JSON格式&#xff0c;TREE格式以及可视化输出。用户可以根据需要选择使用于自己的格式。 1). 传统格式 传统格式简单明了&#xff0c;输出是一个表格形式。 2). JSON格式 第一种格式中介…

wireshark抓包语法

主要功能 Wireshark 的主要功能包括&#xff1a; 实时数据捕获&#xff1a;能够捕获实时流量并对其进行分析。数据包解码&#xff1a;支持数百种网络协议&#xff0c;能够解码并显示各种协议的详细信息。过滤功能&#xff1a;提供强大的过滤功能&#xff0c;可以根据特定条件…

esp32c2-ble-2mb-at,蓝牙AT固件获取方法

文章目录 1. 背景2. 本地编译3. 环境安装依赖包下载1. 背景 最近在做一个数据透传项目,买了一块ESP32-C2 2MB的开发板,WIFI透传串口数据功能调通了(感兴趣的小伙伴可以了解下ESP32-C2模组数据透传模式配置详细教程),想试试蓝牙透传,官方给出的说法是BLE固件未发布,可以从…

项目收获总结--大数据量存储架构设计方案

项目收获总结--大数据量存储架构设计方案 一、背景二、数据存储层技术选型2.1 MySQL2.2 MongoDB2.3 HBase2.4 HBaseElasticSearch 三、HBaseElasticSearch基本原理3.1 前置考虑3.2 HBaseElasticSearch优点3.3 HBaseElasticSearch缺点 四、HBaseElasticSearch数据一致性架构4.1 …

A Survey on Multimodal Large Language Models综述

论文题目:A Survey on Multimodal Large Language Models 论文地址:https://arxiv.org/pdf/2306.13549 话题:多模态LLMs综述 MLLMs Paper: https://github.com/BradyFU/Awesome-Multimodal-Large-Language-Models 1. 摘要 近期,以GPT-4V为代表的跨模态大型语言模型(MLLM…

vue3前端开发-小兔鲜项目-人气推荐栏目的前端渲染

vue3前端开发-小兔鲜项目-人气推荐栏目的前端渲染&#xff01;今天和大家分享一下&#xff0c;人气推荐栏目的前端页面如何渲染内容。 经历过上一次的&#xff0c;新鲜好物的栏目渲染之后&#xff0c;我们已经熟练了&#xff0c;vue3的接口调用&#xff0c;数据渲染到页面中的整…

Zabbix监控介绍与部署

目 录 一、zabbix介绍和架构 1.1 zabbix介绍 1.2 为什么需要监控 1.3 需要监控什么 二、zabbix使用场景与系统概述 2.1 zabbix的功能 2.2 zabbix架构 2.3 Zabbix术语 三、编译安装zabbix 3.1 安装依赖环境 3.2 建立管理用户 3.3 准备源码包&#xff0c;解压包 3.…

数据结构初阶-单链表

链表的结构非常多样&#xff0c;以下情况组合起来就有8种&#xff08;2 x 2 x 2&#xff09;链表结构&#xff1a; 而我们主要要熟悉的单链表与双向链表的全称分别为&#xff1a;不带头单向不循环链表&#xff0c;带头双向循环链表&#xff0c;当我们对这两种链表熟悉后&#x…

基于python深度学习遥感影像地物分类与目标识别、分割实践技术应用

目录 专题一、深度学习发展与机器学习 专题二、深度卷积网络基本原理 专题三、TensorFlow与Keras介绍与入门 专题四、PyTorch介绍与入门 专题五、卷积神经网络实践与遥感图像场景分类 专题六、深度学习与遥感图像检测 专题七、遥感图像检测案例 专题八、深度学习与遥感…

【教学类-68-01】20240720裙子涂色(女孩篇)

背景需求&#xff1a; 通义万相下载了简笔画裙子&#xff0c;制作成涂色卡给幼儿涂色、剪纸用。 代码展示 裙子简笔画图 6张 星火讯飞、通义万相、阿夏 2024年7月20日import os,time import shutil from docx import Document from docx.shared import Cm from PIL import Ima…

ApolloAndroid 使用笔记

由于业务需求的变化&#xff0c;需要使用 Graphql 作为客户端与服务端的交互查询框架&#xff0c;特此记录使用。 测试代码下载链接 一、导入引用以及规则示例 1、首先需要在我们需要使用的模块下面创建对应的src/main/graphql文件夹 2、将后台对应的服务生成的 xxx.graphql…

1个Xpath定位可以在Web页面查找到多个元素Selenium

1个Xpath定位可以在Web页面查找到多个元素Selenium//input[id\"transactionId\"] 打开Web页面&#xff0c; 点击F12可以看到压面 点击Ctrl F 可以点图如下图的输入框&#xff0c;输入xpath&#xff0c;看右侧可以找到3个对应的元素 点击Ctrl F 点击Ctrl F 点…