Initial commit
This commit is contained in:
42
langchain4j-ai-low-high-api/pom.xml
Normal file
42
langchain4j-ai-low-high-api/pom.xml
Normal file
@@ -0,0 +1,42 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.iwe3</groupId>
|
||||
<artifactId>langchain4j-ai-java</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>langchain4j-ai-low-high-api</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<!--导入低阶依赖-->
|
||||
<dependency>
|
||||
<groupId>dev.langchain4j</groupId>
|
||||
<artifactId>langchain4j-open-ai-spring-boot-starter</artifactId>
|
||||
<version>1.9.1-beta17</version>
|
||||
</dependency>
|
||||
<!--导入高阶依赖-->
|
||||
<dependency>
|
||||
<groupId>dev.langchain4j</groupId>
|
||||
<artifactId>langchain4j-spring-boot-starter</artifactId>
|
||||
<version>1.9.1-beta17</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.iwe3.langchain4j;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class LowHighApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(LowHighApplication.class,args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
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 {
|
||||
|
||||
@Bean(name = "qwen")
|
||||
public ChatModel chatModelQwen(){
|
||||
/*大模型3件套:apikey ,model-name,base-url */
|
||||
return OpenAiChatModel.builder()
|
||||
.apiKey(System.getenv("DASH_SCOPE_API_KEY"))
|
||||
.modelName("qwen-plus")
|
||||
.baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* @Description:
|
||||
* 知识出处,https://api-docs.deepseek.com/zh-cn/
|
||||
*/
|
||||
@Bean(name = "deepseek")
|
||||
public ChatModel chatModelDeepSeek()
|
||||
{
|
||||
return
|
||||
OpenAiChatModel.builder()
|
||||
.apiKey(System.getenv("DEEP_SEEK_API_KEY"))
|
||||
.modelName("deepseek-chat")
|
||||
//.modelName("deepseek-reasoner")
|
||||
.baseUrl("https://api.deepseek.com/v1")
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.iwe3.langchain4j.controller;
|
||||
|
||||
import com.iwe3.langchain4j.service.ChatAssistant;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class HighLevelController {
|
||||
|
||||
@Resource
|
||||
private ChatAssistant chatAssistant;
|
||||
|
||||
// http://localhost:9003/lc4j/boot/declarative?prompt=我是谁
|
||||
@GetMapping(value = "/lc4j/boot/declarative")
|
||||
public String declarative(@RequestParam(value = "prompt", defaultValue = "你是谁") String prompt)
|
||||
{
|
||||
return chatAssistant.chat(prompt);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.iwe3.langchain4j.controller;
|
||||
|
||||
import dev.langchain4j.data.message.UserMessage;
|
||||
import dev.langchain4j.model.chat.ChatModel;
|
||||
import dev.langchain4j.model.chat.response.ChatResponse;
|
||||
import dev.langchain4j.model.output.TokenUsage;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class LowLevelController {
|
||||
|
||||
@Resource(name = "qwen")
|
||||
private ChatModel chatModel;
|
||||
|
||||
// http://localhost:9003/lc4j/lowapi/api01?prompt=我是谁
|
||||
@GetMapping(value = "/lc4j/lowapi/api01")
|
||||
public String api01(@RequestParam(value = "prompt", defaultValue = "你是谁") String prompt)
|
||||
{
|
||||
return chatModel.chat(prompt);
|
||||
}
|
||||
|
||||
// http://localhost:9003/lc4j/lowapi/api02?prompt=我是谁
|
||||
@GetMapping(value = "/lc4j/lowapi/api02")
|
||||
public String api02(@RequestParam(value = "prompt", defaultValue = "你是谁") String prompt)
|
||||
{
|
||||
ChatResponse chatResponse = chatModel.chat(UserMessage.from(prompt));
|
||||
|
||||
String result = chatResponse.aiMessage().text();
|
||||
System.out.println("通过调用大模型返回结果:"+result);
|
||||
|
||||
// Token 用量计算的底层api
|
||||
TokenUsage tokenUsage = chatResponse.tokenUsage();
|
||||
System.out.println("本次调用消耗的token:"+tokenUsage);
|
||||
result = result +"\t\n"+tokenUsage;
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.iwe3.langchain4j.service;
|
||||
|
||||
import dev.langchain4j.service.spring.AiService;
|
||||
import dev.langchain4j.service.spring.AiServiceWiringMode;
|
||||
|
||||
/**
|
||||
* 知识出处:
|
||||
* https://docs.langchain4j.dev/tutorials/spring-boot-integration/#spring-boot-starter-for-declarative-ai-services
|
||||
*/
|
||||
@AiService(wiringMode = AiServiceWiringMode.EXPLICIT
|
||||
,chatModel = "qwen")
|
||||
public interface ChatAssistant {
|
||||
String chat(String prompt);
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
server:
|
||||
port: 9003
|
||||
spring:
|
||||
application:
|
||||
name: langchain4j-ai-low-level
|
||||
Reference in New Issue
Block a user