
传统上,许多人工智能驱动的应用程序依赖基于云的 api 或集中式服务来进行模型托管和执行。虽然这种方法有其优点,例如可扩展性和易用性,但它也带来了有关延迟、数据隐私和对第三方提供商的依赖的挑战。
这就是本地人工智能模型的闪光点。通过直接在应用程序的基础架构中运行模型,您可以更好地控制性能、数据安全性和部署灵活性。然而,构建这样的系统需要正确的工具和框架来弥合传统软件开发和人工智能模型集成之间的差距。
在本文中,我们将探讨如何将 quarkus(一种针对云原生应用程序优化的现代 java 框架)与 ollama(一种用于本地运行 ai 模型的平台)结合起来。我们还将演示 testcontainers 和 quarkus dev services 等工具如何简化开发和测试工作流程。使用 pingpong-ai 项目作为实际示例,您将学习如何构建和测试利用本地模型的力量的 ai 驱动的应用程序。
pingpong-ai 项目演示了使用 quarkus 作为后端框架和 ollama 来处理 ai 模型的 ai 驱动功能的简单实现。让我们分解架构并浏览一下代码的关键组件。
在 pingpong-ai 中,ollama 用于模拟一个简单的对话模型,其中好奇的服务围绕某个主题生成问题,明智的服务以信息丰富的答案进行响应,这将产生更多关于好奇的服务的问题。
该项目将 quarkus 与 ollama 集成以创建 ai 模型驱动的应用程序。它利用 quarkus 的轻量级快速开发模型作为调用和管理 ai 交互的后端。以下是我们将要介绍的内容的概述:
ollama 简化了应用程序中 ai 模型的部署和使用。它为 ai 模型执行提供运行时,并且可以轻松集成到现有应用程序中。
与 quarkus 的集成是使用 rest 端点与 ollama 交互来处理的。 quarkus 应用程序充当中间件来处理客户端请求并与 ollama 运行时通信。
这是 pingpongresource.java 中的一个片段,它定义了 pingpong 交互的 rest 端点:
@path("/chat")
public class curiouschatresource {
@inject
curiousservice curiousservice;
@inject
wiseservice wiseservice;
@post
@produces(mediatype.text_plain)
@consumes(mediatype.text_plain)
@path("/{numberofquestions}")
public string chat(@pathparam("numberofquestions") integer numberofquestions, string topic) {
stringbuilder sb = new stringbuilder();
sb.append("> topic: ").append(topic).append("\n");
while (numberofquestions-- > 0) {
string question = curiousservice.chat(topic);
sb.append("> question: ").append(question).append("\n");
topic = wiseservice.chat(question);
sb.append("> answer: ").append(topic).append("\n");
}
return sb.tostring();
}
}
在此代码中:
这些 ollama 服务负责调用 ollama 运行时。这是 curiousservice.java 的片段:
@registeraiservice
@systemmessage("you are a curious person that creates a short question for every message you receive.")
public interface curiousservice {
public string chat(@usermessage string message);
}
我们甚至可以为每个服务使用不同的模型,指定标识模型的配置属性。这个例子来自curiousservice.java:
每个应用程序都要使用数据,Android应用程序也不例外,Android使用开源的、与操作系统无关的SQL数据库--SQLite,本文介绍的就是如何为你的Android应用程序创建和操作SQLite数据库。 数据库支持每个应用程序无论大小的生命线,除非你的应用程序只处理简单的数据,那么就需要一个数据库系统存储你的结构化数据,Android使用SQLite数据库,它是一个开源的、支持多操作系统的SQL数据库,在许多领域广泛使用,如Mozilla FireFox就是使用SQLite来存储配置数据的,iPhon
0
@registeraiservice(modelname = "curiousmodel")
我们在 application.properties 中识别模型:
quarkus.langchain4j.ollama.wisemodel.chat-model.model-id=tinydolphin quarkus.langchain4j.ollama.curiousmodel.chat-model.model-id=tinyllama
testcontainers 是一个 java 库,用于在测试期间运行轻量级、一次性容器。在 pingpong-ai 中,测试容器用于设置具有 ollama 运行时的环境以进行集成测试。
测试类演示了如何配置和使用测试容器:
@quarkustest
class curiouschatresourcetest {
@inject
curiousservice curiousservice;
@inject
wiseservice wiseservice;
@test
@activaterequestcontext
void testfxmaincontrollerinteraction() {
// perform interaction and assertions
var curiousanswer = curiousservice.chat("barcelona");
var response = wiseservice.chat(curiousanswer);
log.infof("wise service response: %s", response);
// using llama2 model we can check if the response contains 'barcelona', but not
// with tinyllama
assertfalse(response.isempty(), "response should not be empty");
}
@test
@activaterequestcontext
void testchatendpoint() {
given()
.when()
.body("barcelona")
.contenttype(contenttype.text)
.post("/chat/3")
.then()
.statuscode(200)
.contenttype(contenttype.text)
.body(not(empty()))
.body(org.hamcrest.matchers.stringcontainsinorder("question:", "answer:", "question:", "answer:", "question:", "answer:"));
}
}
quarkus dev services 简化了开发过程中所需服务的设置。对于这个项目,quarkus dev services 可以自动启动 ollama 运行时容器。
application.properties 文件包含启用开发服务的配置:
quarkus.langchain4j.ollama.chat-model.model-id=tinyllama quarkus.langchain4j.ollama.devservices.model=tinyllama quarkus.langchain4j.log-requests=true
通过这些配置,当应用程序在开发模式或测试中运行时,quarkus dev services 会自动启动 ollama 容器,无需手动设置。
您可以使用以下命令在开发模式下启动应用程序:
./mvnw quarkus:dev
此命令:
pingpong-ai 项目演示了 quarkus 与 ollama 的无缝集成,可以轻松构建人工智能驱动的应用程序。通过利用 testcontainers 和 quarkus 开发服务,开发人员可以在容器化和自动化环境中高效地测试和开发他们的应用程序。
要进一步探索这个项目,请查看 pingpong-ai github 存储库。
以上就是使用 Quarkus、Ollama 和 Testcontainers 构建本地 LLM AI 支持的应用程序的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号