Initial commit
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
package com.iwe3.langchain4j;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class PromptApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(PromptApplication.class,args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.iwe3.langchain4j.config;
|
||||
|
||||
import dev.langchain4j.model.chat.ChatModel;
|
||||
import dev.langchain4j.model.openai.OpenAiChatModel;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class LLMConfig {
|
||||
|
||||
/**
|
||||
* @Description: 普通对话接口 ChatModel
|
||||
*/
|
||||
@Bean(name = "qwen")
|
||||
public ChatModel chatModelQwen()
|
||||
{
|
||||
return OpenAiChatModel.builder()
|
||||
.apiKey(System.getenv("DASH_SCOPE_API_KEY"))
|
||||
.modelName("qwen-plus")
|
||||
.baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
package com.iwe3.langchain4j.controller;
|
||||
|
||||
import com.iwe3.langchain4j.entity.LawPrompt;
|
||||
import com.iwe3.langchain4j.service.LawExplainAssistant;
|
||||
import dev.langchain4j.data.message.UserMessage;
|
||||
import dev.langchain4j.model.chat.ChatModel;
|
||||
import dev.langchain4j.model.chat.response.ChatResponse;
|
||||
import dev.langchain4j.model.input.Prompt;
|
||||
import dev.langchain4j.model.input.PromptTemplate;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.assertj.core.util.DateUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@Slf4j
|
||||
public class ChatPromptController {
|
||||
|
||||
@Resource
|
||||
private LawExplainAssistant lawExplainAssistant;
|
||||
/**
|
||||
* http://localhost:9008/lc4j/chatprompt/test2
|
||||
* TRIPS协议(与贸易有关的知识产权协议):
|
||||
* 世界贸易组织(WTO)成员间的一个重要协议,
|
||||
* 它规定了最低标准的知识产权保护要求,并适用于所有WTO成员。
|
||||
* @return
|
||||
*/
|
||||
@GetMapping(value = "/lc4j/chatprompt/test2")
|
||||
public String test2()
|
||||
{
|
||||
var prompt = new LawPrompt();
|
||||
prompt.setLegal("知识产权");
|
||||
prompt.setQuestion("TRIPS协议?");
|
||||
|
||||
var chat = lawExplainAssistant.chat(prompt);
|
||||
System.out.println(chat);
|
||||
|
||||
return "success : "+ DateUtil.now()+"<br> \n\n chat: "+chat;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 高阶API调用
|
||||
* http://localhost:9008/lc4j/chatprompt/test1
|
||||
* @return
|
||||
*/
|
||||
@GetMapping(value = "/lc4j/chatprompt/test1")
|
||||
public String test1()
|
||||
{
|
||||
String chat = lawExplainAssistant.chat("什么是知识产权?",2000);
|
||||
System.out.println(chat);
|
||||
|
||||
String chat2 = lawExplainAssistant.chat("什么是java?",2000);
|
||||
System.out.println(chat2);
|
||||
|
||||
return "success : "+ DateUtil.now()+"<br> \n\n chat: "+chat+"<br> \n\n chat2: "+chat2;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@Resource(name = "qwen")
|
||||
private ChatModel chatModel;
|
||||
|
||||
/**
|
||||
* 低阶API调用
|
||||
* http://localhost:9008/lc4j/chatprompt/test
|
||||
* @return
|
||||
*/
|
||||
@GetMapping(value = "/lc4j/chatprompt/test")
|
||||
public String test(){
|
||||
// 看看源码,默认 PromptTemplate 构造使用 it 属性作为默认占位符
|
||||
|
||||
/*String role = "外科医生";
|
||||
String question = "牙疼";*/
|
||||
|
||||
String role = "财务会计";
|
||||
String question = "人民币大写";
|
||||
|
||||
//1 构造PromptTemplate模板
|
||||
PromptTemplate template = PromptTemplate.from("你是一个{{it}}助手,{{question}}怎么办");
|
||||
//2 由PromptTemplate生成Prompt
|
||||
Prompt prompt = template.apply(Map.of("it",role,"question",question));
|
||||
//3 Prompt提示词变成UserMessage
|
||||
UserMessage userMessage = prompt.toUserMessage();
|
||||
//4 调用大模型
|
||||
ChatResponse chatResponse = chatModel.chat(userMessage);
|
||||
|
||||
//4.1 后台打印
|
||||
System.out.println(chatResponse.aiMessage().text());
|
||||
//4.2 前台返回
|
||||
return "success : "+ DateUtil.now()+"<br> \n\n chat: "+chatResponse.aiMessage().text();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.iwe3.langchain4j.entity;
|
||||
|
||||
import dev.langchain4j.model.input.structured.StructuredPrompt;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@StructuredPrompt("根据中国{{legal}}法律,解答以下问题:{{question}}")
|
||||
public class LawPrompt
|
||||
{
|
||||
private String legal;//法律
|
||||
private String question;//问题
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.iwe3.langchain4j.service;
|
||||
|
||||
import com.iwe3.langchain4j.entity.LawPrompt;
|
||||
import dev.langchain4j.service.SystemMessage;
|
||||
import dev.langchain4j.service.UserMessage;
|
||||
import dev.langchain4j.service.V;
|
||||
import dev.langchain4j.service.spring.AiService;
|
||||
|
||||
/**
|
||||
* 一个法律解释助手
|
||||
*/
|
||||
@AiService(chatModel = "qwen")
|
||||
public interface LawExplainAssistant {
|
||||
|
||||
//案例3 当提示词过多,也可以将从外部资源加载
|
||||
@SystemMessage(fromResource = "prompt-template.txt")
|
||||
String chat(LawPrompt lawPrompt);
|
||||
|
||||
//案例2 新建带着@StructuredPrompt的业务实体类,比如LawPrompt
|
||||
// @SystemMessage("你是一位专业的中国法律顾问,只回答与中国法律相关的问题。" +
|
||||
// "输出限制:对于其他领域的问题禁止回答,直接返回'抱歉,我只能回答中国法律相关的问题。'")
|
||||
// String chat(LawPrompt lawPrompt);
|
||||
|
||||
|
||||
// @SystemMessage : 定AI-LLM 的角色,任务,风格,格式
|
||||
// @UserMessage : 用户提示词
|
||||
// @V : 超过1个以上参数,需要起别名
|
||||
//案例1 @SystemMessage + @UserMessage + @V
|
||||
@SystemMessage("你是一位专业的中国法律顾问,只回答与中国法律相关的问题。" +
|
||||
"输出限制:对于其他领域的问题禁止回答,直接返回'抱歉,我只能回答中国法律相关的问题。'")
|
||||
|
||||
@UserMessage("请回答以下法律问题:{{question}},字数控制在{{length}}以内")
|
||||
String chat(@V("question") String question, @V("length") int length);
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
5
langchain4j-ai-prompt/src/main/resources/application.yml
Normal file
5
langchain4j-ai-prompt/src/main/resources/application.yml
Normal file
@@ -0,0 +1,5 @@
|
||||
server:
|
||||
port: 9008
|
||||
spring:
|
||||
application:
|
||||
name: langchain4j-ai-prompt
|
||||
@@ -0,0 +1,2 @@
|
||||
你是一位专业的中国法律顾问,只回答与中国法律相关的问题。
|
||||
输出限制:对于其他领域的问题禁止回答,直接返回'抱歉,我只能回答中国法律相关的问题。
|
||||
Reference in New Issue
Block a user