Spring AI教程(二)Chat API之Prompts模板语法

Prompts 模板语法

 上节我们介绍了提示词工程,并通过设定SystemMessage获得了一个会骂人的AI。而本节介绍的内容仍然与提示词有关。

 Spring AI为我们提供了提示词模板,允许我们通过一些模板,快速地动态生成提示词并发起提问。除此之外,我们还能使用Spring AI为我们提供的输出解析器将AI回复的内容解析为Bean对象。

5.1 PromptTemplate

PromptTemplate能够帮助我们创建结构化提示词,是Spring AI提示词工程中的关键组件,该类实现了三个接口:PromptTemplateStringActionsPromptTemplateActionsPromptTemplateMessageActions,这些接口的主要功能也有所不同:

  • PromptTemplateStringActions: 主要用于创建和渲染提示词字符串,接口的返回值类型均是String类型,这是提示词的基本形式。
  • PromptTemplateActions: 主要用于创建Prompt对象,该对象可直接传递给ChatClient以生成响应。
  • PromptTemplateMessageActions:主要用于创建Message对象,这允许我们针对Message对象进行其他的相关操作。

 例如,我们想定义一个这样的提示词:提供作者姓名,返回该作者最受欢迎的书,出版时间和书的内容概述

    @GetMapping("/template")
    public String promptTemplate(String author){
   
        // 提示词
        final String template = "请问{author}最受欢迎的书是哪本书?什么时候发布的?书的内容是什么?";
        PromptTemplate promptTemplate = new PromptTemplate(template);
        // 动态地将author填充进去
        Prompt prompt = promptTemplate.create(Map.of("author", author));
        
        ChatResponse chatResponse = chatClient.call(prompt);
        
        AssistantMessage assistantMessage = chatResponse.getResult().getOutput();
        return assistantMessage.getContent();
    }

 我们除了可以通过定义字符串加载Template以外,我们还可以以Resource的形式加载Template,例如,我们在resouces下创建prompt.st(文件后缀名合理即可),将刚刚的提示词模板写入到该文件中。

package com.ningning0111.controller;

import org.springframework.ai.chat.ChatClient;
import org.springframework.ai.chat.ChatResponse;
import org.springframework.ai.chat.messages.AssistantMessage;
### LangChain on Alibaba Cloud BaiLian Usage Guide Integrating LangChain with Alibaba Cloud BaiLian allows developers to leverage advanced language models and chain-based workflows within the robust cloud infrastructure provided by Alibaba Cloud. This combination enhances application development capabilities, especially when building complex AI-driven applications that require both powerful model support and scalable deployment environments. #### Integration Methods To integrate LangChain into an Alibaba Cloud BaiLian environment, one can utilize LCEL (LangChain Expression Language), which offers a flexible way of organizing components such as prompts, large language models (LLMs), and output parsers[^2]. By embedding these elements through LCEL expressions directly inside Python scripts running on Alibaba Cloud's platform, users gain greater control over how different parts interact while benefiting from optimized performance tuning features specific to this ecosystem. For instance, consider setting up a simple chatbot service using LangChain alongside Alibaba Cloud services: ```python from langchain import PromptTemplate, LLMChain from alibabacloud_bailian import initialize_sdk # Hypothetical function based on [^1] initialize_sdk() # Initialize connection to Alibaba Cloud resources template = """You are a helpful assistant who always provides answers in {style}.""" prompt = PromptTemplate(template=template, input_variables=["style"]) llm_chain = LLMChain(prompt=prompt) response = llm_chain.run({"style": "a friendly tone"}) print(response) ``` This code snippet demonstrates initializing the SDK for connecting with Alibaba Cloud BaiLian services before defining a prompt template and creating an `LLMChain` object to process inputs according to specified styles. #### Case Studies One potential use case involves integrating Spring Cloud Alibaba AI functionalities with LangChain constructs to create intelligent microservices architectures capable of handling natural language processing tasks efficiently across distributed systems[^3]. Such setups could involve deploying multiple instances of specialized AI modules—each powered by distinct configurations of LangChain chains—that communicate via RESTful APIs or message queues managed under the umbrella of Spring Cloud. Another example might focus on utilizing AliCloud's Go SDK for implementing backend logic behind web applications where frontend interactions trigger calls to pre-defined LangChain pipelines hosted remotely on Alibaba Cloud servers[^4].
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

PG Thinker

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值