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

烟台提供网站设计制作职高门户网站建设标准

烟台提供网站设计制作,职高门户网站建设标准,wordpress如何设置用户中心,网络公司经营范围互联网金融1. 概述 利用大型语言模型(LLM),我们可以检索大量有用的信息。我们可以学习关于任何事物的许多新知识,并基于互联网上已有的数据获得答案。我们可以让它们处理输入数据并执行各种操作。但如果我们让模型调用API来准备输出呢? 为此&#xff…

1. 概述

利用大型语言模型(LLM),我们可以检索大量有用的信息。我们可以学习关于任何事物的许多新知识,并基于互联网上已有的数据获得答案。我们可以让它们处理输入数据并执行各种操作。但如果我们让模型调用API来准备输出呢?

为此,我们可以使用函数调用(Function Calling)。函数调用使大型语言模型能够交互并操作数据,执行计算,或获取超出其固有文本能力的信息。

本文将探讨函数调用是什么,以及如何利用它将大型语言模型与我们内部的业务逻辑集成。作为模型提供方,我们将使用 Mistral AI 的 API。


2. Mistral AI API

Mistral AI 致力于为开发者和企业提供开放且可移植的生成式 AI 模型。我们既可以用它来处理简单的提示,也可以用来实现函数调用集成。

2.1 获取 API 密钥

要开始使用 Mistral API,首先需要获取 API 密钥。这一步读者自行去网上搜索。

2.2 使用示例

我们先从一个简单的提示开始。我们将请求 Mistral API 返回一个患者状态列表。下面来实现这样一个调用:

@Test
void givenHttpClient_whenSendTheRequestToChatAPI_thenShouldBeExpectedWordInResponse() throws IOException, InterruptedException {String apiKey = System.getenv("MISTRAL_API_KEY");String apiUrl = "https://api.mistral.ai/v1/chat/completions";String requestBody = "{"+ "\"model\": \"mistral-large-latest\","+ "\"messages\": [{\"role\": \"user\", "+ "\"content\": \"What the patient health statuses can be?\"}]"+ "}";HttpClient client = HttpClient.newHttpClient();HttpRequest request = HttpRequest.newBuilder().uri(URI.create(apiUrl)).header("Content-Type", "application/json").header("Accept", "application/json").header("Authorization", "Bearer " + apiKey).POST(HttpRequest.BodyPublishers.ofString(requestBody)).build();HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());String responseBody = response.body();logger.info("Model response: " + responseBody);Assertions.assertThat(responseBody).containsIgnoringCase("healthy");
}

我们创建了一个 HTTP 请求并发送到/chat/completions端点。然后,我们使用API密钥作为授权头的值。正如预期的那样,响应中包含了元数据和内容本身:

Model response: {"id":"585e3599275545c588cb0a502d1ab9e0","object":"chat.completion",
"created":1718308692,"model":"mistral-large-latest",
"choices":[{"index":0,"message":{"role":"assistant","content":"Patient health statuses can be
categorized in various ways, depending on the specific context or medical system being used.
However, some common health statuses include:
1.Healthy: The patient is in good health with no known medical issues.
...
10.Palliative: The patient is receiving care that is focused on relieving symptoms and improving quality of life, rather than curing the underlying disease.",
"tool_calls":null},"finish_reason":"stop","logprobs":null}],
"usage":{"prompt_tokens":12,"total_tokens":291,"completion_tokens":279}}

函数调用的示例为复杂,且在调用之前需要做大量准备工作。我们将在后面部分中详细了解。

3. Spring AI 集成

下面来看几个使用 Mistral API 进行函数调用的示例。借助 Spring AI,我们可以省去很多准备工作,让框架帮我们完成。

3.1 依赖项

所需的依赖项位于 Spring 里程碑版本仓库中。我们将其添加到 pom.xml 文件中:

<repositories><repository><id>spring-milestones</id><name>Spring milestones</name><url>https://repo.spring.io/milestone</url></repository>
</repositories>

添加Mistral API的集成依赖

<dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-mistral-ai-spring-boot-starter</artifactId><version>0.8.1</version>
</dependency>

3.2 配置

把上面获取到的key配置到属性文件

spring:ai:mistralai:api-key: ${MISTRAL_AI_API_KEY}chat:options:model: mistral-small-latest

3.3 仅使用一个调用函数的用例

在我们的演示示例中,我们将创建一个函数,根据患者的 ID 返回患者的健康状态。

让我们先创建患者记录:

public record Patient(String patientId) {
}

再创建一个健康状态记录

public record HealthStatus(String status) {
}

然后,创建一个配置类

@Configuration
public class MistralAIFunctionConfiguration {public static final Map<Patient, HealthStatus> HEALTH_DATA = Map.of(new Patient("P001"), new HealthStatus("Healthy"),new Patient("P002"), new HealthStatus("Has cough"),new Patient("P003"), new HealthStatus("Healthy"),new Patient("P004"), new HealthStatus("Has increased blood pressure"),new Patient("P005"), new HealthStatus("Healthy"));@Bean@Description("Get patient health status")public Function<Patient, HealthStatus> retrievePatientHealthStatus() {return (patient) -> new HealthStatus(HEALTH_DATA.get(patient).status());}
}

此处,我们已定义了包含患者健康数据的数据集。此外,我们还创建了

retrievePatientHealthStatus()函数,该函数可根据给定的患者 ID 返回其健康状态。

现在,让我们通过在集成环境中调用该函数来进行测试:

@Import(MistralAIFunctionConfiguration.class)
@ExtendWith(SpringExtension.class)
@SpringBootTest
public class MistralAIFunctionCallingManualTest {@Autowiredprivate MistralAiChatModel chatClient;@Testvoid givenMistralAiChatClient_whenAskChatAPIAboutPatientHealthStatus_thenExpectedHealthStatusIsPresentInResponse() {var options = MistralAiChatOptions.builder().withFunction("retrievePatientHealthStatus").build();ChatResponse paymentStatusResponse = chatClient.call(new Prompt("What's the health status of the patient with id P004?",  options));String responseContent = paymentStatusResponse.getResult().getOutput().getContent();logger.info(responseContent);Assertions.assertThat(responseContent).containsIgnoringCase("has increased blood pressure");}
}

我们导入了MistralAIFunctionConfiguration类,将retrievePatientHealthStatus()函数添加到测试的Spring上下文中。同时,我们注入了MistralAiChatClient,该客户端会由 Spring AI Starter 自动实例化。

在请求聊天 API 时,我们指定了包含某个患者 ID 的提示文本,以及用于获取健康状态的函数名称。随后调用了 API,并验证响应中包含了预期的健康状态。

此外,我们还记录了整个响应文本,内容如下:

The patient with id P004 has increased blood pressure.

3.4 多函数用例

我们还可以指定多个函数,AI 会根据我们发送的提示决定使用哪个函数。

为了演示这一点,让我们扩展一下 HealthStatus 记录:

public record HealthStatus(String status, LocalDate changeDate) {
}

我们添加了状态上次变更的日期。

现在,让我们修改配置类:

@Configuration
public class MistralAIFunctionConfiguration {public static final Map<Patient, HealthStatus> HEALTH_DATA = Map.of(new Patient("P001"), new HealthStatus("Healthy",LocalDate.of(2024,1, 20)),new Patient("P002"), new HealthStatus("Has cough",LocalDate.of(2024,3, 15)),new Patient("P003"), new HealthStatus("Healthy",LocalDate.of(2024,4, 12)),new Patient("P004"), new HealthStatus("Has increased blood pressure",LocalDate.of(2024,5, 19)),new Patient("P005"), new HealthStatus("Healthy",LocalDate.of(2024,6, 1)));@Bean@Description("Get patient health status")public Function<Patient, String> retrievePatientHealthStatus() {return (patient) -> HEALTH_DATA.get(patient).status();}@Bean@Description("Get when patient health status was updated")public Function<Patient, LocalDate> retrievePatientHealthStatusChangeDate() {return (patient) -> HEALTH_DATA.get(patient).changeDate();}
}

我们为每个状态项填写了变更日期。同时,我们还创建了 retrievePatientHealthStatusChangeDate() 函数,用于返回状态变更日期的信息。

下面来看如何使用这两个新函数与 Mistral API 进行交互:

@Test
void givenMistralAiChatClient_whenAskChatAPIAboutPatientHealthStatusAndWhenThisStatusWasChanged_thenExpectedInformationInResponse() {var options = MistralAiChatOptions.builder().withFunctions(Set.of("retrievePatientHealthStatus","retrievePatientHealthStatusChangeDate")).build();ChatResponse paymentStatusResponse = chatClient.call(new Prompt("What's the health status of the patient with id P005",options));String paymentStatusResponseContent = paymentStatusResponse.getResult().getOutput().getContent();logger.info(paymentStatusResponseContent);Assertions.assertThat(paymentStatusResponseContent).containsIgnoringCase("healthy");ChatResponse changeDateResponse = chatClient.call(new Prompt("When health status of the patient with id P005 was changed?",options));String changeDateResponseContent = changeDateResponse.getResult().getOutput().getContent();logger.info(changeDateResponseContent);Assertions.assertThat(paymentStatusResponseContent).containsIgnoringCase("June 1, 2024");
}

在这种情况下,我们指定了两个函数名称并发送了两个提示。首先,我们询问了患者的健康状态;然后,我们询问了该状态的变更时间。我们确认返回的结果包含了预期的信息。除此之外,我们还记录了所有响应,内容如下:

The patient with id P005 is currently healthy.
The health status of the patient with id P005 was changed on June 1, 2024.

4. 结论

函数调用是扩展大型语言模型功能的绝佳工具。我们还可以利用它将大型语言模型与我们的业务逻辑进行集成。

在本教程中,我们探讨了如何通过调用一个或多个自定义函数来实现基于大型语言模型的流程。通过这种方法,我们能够开发与AI API深度集成的现代应用程

 关注我不迷路,系列化的给您提供当代程序员需要掌握的现代AI工具和框架


文章转载自:

http://HWKQqZqI.mqfhy.cn
http://GdxQYYBJ.mqfhy.cn
http://LsDfKTAA.mqfhy.cn
http://UaMyd7Js.mqfhy.cn
http://yBkT2jDx.mqfhy.cn
http://w2tcdqcN.mqfhy.cn
http://gE8xWYkv.mqfhy.cn
http://p8ZL2w6I.mqfhy.cn
http://zvEzPhTY.mqfhy.cn
http://gEdpYuEu.mqfhy.cn
http://Qp0ec91I.mqfhy.cn
http://YtBUZdQE.mqfhy.cn
http://mmyq4dig.mqfhy.cn
http://CZk4ABDw.mqfhy.cn
http://RoQDDG35.mqfhy.cn
http://QZW3tzOk.mqfhy.cn
http://foGM4AM6.mqfhy.cn
http://hT22G6mb.mqfhy.cn
http://RWjywBkP.mqfhy.cn
http://rxlViUoj.mqfhy.cn
http://YAOhZfsf.mqfhy.cn
http://A3UAJ5L6.mqfhy.cn
http://RkpDdIKM.mqfhy.cn
http://RDOmzHnc.mqfhy.cn
http://3mY91TP1.mqfhy.cn
http://9Q0NHGNK.mqfhy.cn
http://zNDCjAn8.mqfhy.cn
http://BEbdtCQw.mqfhy.cn
http://pZNGLOK8.mqfhy.cn
http://DnIPT8vx.mqfhy.cn
http://www.dtcms.com/wzjs/665218.html

相关文章:

  • 查找企业信息的网站网站建设维护费 会计科目
  • 境外企业网站推广生活中实用的产品设计
  • 网站集约化建设报告做电影网站需要多打了服务器
  • 做淘客网站要多大的服务器永久免费做网站
  • 麒麟网站建设集团网站网页模板
  • 网站后台排版布局呼和浩特市做网站公司好的
  • 佛山网站制作哪家北京中小企业建站价格
  • 哪里可以免费注册网站网站活动专题页面
  • 什么网站可以做会计题目百度竞价排名收费
  • 做网站有名的公司湖南it网站建设mxtia
  • 会展网站建设大余网站建设
  • 做普通网站价格wordpress 配置邮件
  • 网站开发专业主修课程最新网络营销方式
  • 网站规划说明书net网站建设
  • 深圳论坛网站设计哪家公司好网站专项审批查询
  • 网站制作苏州企业通过网络营销学到了什么
  • 手机网站制作哪家好国外创意设计网站
  • 济南专业的设计网站温州网站关键词
  • 手机网站转app开发教程网站制作电话多少
  • 大兴网站设计揭阳高端品牌网站建设
  • 网站怎么seo关键词排名优化推广有名的网页游戏
  • 怎么做frontpage网站网页制作素材图片是什么格式
  • 网站建设销售工作内容女主网站和男主做
  • 网站不能风格哪个微信公众号有a
  • 高要网站制作保安服定制公司
  • 保定酒店网站制作wordpress 展示微博
  • 泉州网站设计找哪家vue如何网站开发
  • 网站免费建站众享星球人物网页设计模板
  • 个人网站广告投放玩具外贸网站模板
  • 济宁华园建设有限公司网站akm建站系统