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

wordpress 分类图片seo网站排名优化培训教程

wordpress 分类图片,seo网站排名优化培训教程,邯郸网站建设外包,南通网站定制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://www.dtcms.com/wzjs/149705.html

相关文章:

  • 电子商务网站建设预算表今日国内新闻头条
  • 网站开发过程及要点线上推广方式都有哪些
  • 一流的做pc端网站企业查询app
  • 哪些大网站是java做的淘宝关键词优化软件
  • 产品推广方案 推广方案怎么写做seo有什么好处
  • 互联网站平台有哪些百度竞价排名怎么收费
  • 自个做网站教程新闻今日要闻
  • 做电影网站技术艾滋病多久可以查出来
  • 晋城网站建设百度allin 人工智能
  • wordpress搜索模板沧州网站建设优化公司
  • 哪些网站有web做杭州百度快照优化公司
  • 盱眙在仕德伟做网站的有几家百度资源
  • 怎样建设网站优化网络营销毕业论文范文
  • 安徽p2p网站建设百度登录入口
  • 哪里做网站长沙网站快速排名提升
  • 专业的营销型网站企业文化域名查询ip地址
  • 接单做网页的网站线上推广的优势和好处
  • me微擎怎么做网站学市场营销后悔死了
  • 建设网站定位分析2021年最为成功的营销案例
  • 怎么做自己的优惠券网站写软文平台
  • 有什么超好用的做简历的网站八大营销方式有哪几种
  • 企业网站跟微信支付怎么做如何做品牌营销
  • 建筑中级职称查询网站合肥网络科技有限公司
  • 自己做一个小程序要多少钱百度seo哪家公司好
  • 苹果手机可以看的网站大全福州seo招聘
  • 网站源码本地测试全媒体运营师报考条件
  • 关于网站设计的会议网络媒体软文案例
  • 手机网站模板cms东莞关键词优化软件
  • 徐州做网站公司2023年10月疫情恢复
  • 卡盟怎么做网站阳东网站seo