首页 > 基础资料 博客日记

LangChain-java 学习篇(一)

2024-03-31 15:00:05基础资料围观321

这篇文章介绍了LangChain-java 学习篇(一),分享给大家做个参考,收藏Java资料网收获更多编程知识

1. 这是什么?

       这是langchain的Java语言实现。大型语言模型(LLM)正成为一种变革性的技术,使开发人员能够构建以前无法构建的应用程序。但是,单个的使用LLM往往不足以创建真正强大的应用程序,当我们能够将它们与其他计算或知识来源相结合时,才会展现出真正的威力。这个库旨在协助开发这类应用程序。

2. 快速入门指南

2.1 Maven存储库

先决条件:

             1.Java 17或更高版本

             2.类Unix环境(我们使用Linux,Mac OS X)

             3.Maven(我们推荐版本3.8.6,至少需要3.5.4)
         

 <dependency>
        <groupId>io.github.hamawhitegg</groupId>
        <artifactId>langchain-core</artifactId>
        <version>0.1.12</version>
 </dependency> 

2.2扩展 lang-chain框架支持

使用 LangChain 通常需要与一个或多个模型提供程序、数据存储、API 等集成。在本例中,我们将扩展lang-chain框架支持文心一言的API

2.2.1继承BaseLLM

@SuperBuilder
public class WenXinQianFan extends BaseLLM {
    private static final Logger LOG = LoggerFactory.getLogger(WenXinQianFan.class);

    /**
     * Endpoint URL to use.
     */
    @Builder.Default
    private String endpointUrl = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions?access_token=";

    /**
     * Max token allowed to pass to the model.
     */
    @Builder.Default
    private int maxToken = 4096;

    /**
     * LLM model temperature from 0 to 10.
     */
    @Builder.Default
    private float temperature = 0.95f;

    /**
     * History of the conversation
     */
    @Builder.Default
    private List<JSONObject> messages = new ArrayList<>();

    /**
     * Top P for nucleus sampling from 0 to 1
     */
    @Builder.Default
    private float topP = 1.0f;

    @Builder.Default
    private float penaltyScore=1.5f;

    @Builder.Default
    private boolean stream = false;

    @Builder.Default
    private String accessToken="";




    /**
     * Whether to use history or not
     */
    private boolean withHistory;

    private TextRequestsWrapper requestsWrapper;

    public WenXinQianFan init() {
        Map<String, String> headers = Map.of("Content-Type", "application/json");
        this.requestsWrapper = new TextRequestsWrapper(headers);
        return this;
    }


    @Override
    public String llmType() {
        return "wenxin";
    }


    public List<String> createStream(String prompt, List<String> stop) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("content",prompt);
        jsonObject.put("role","user");
        messages.add(jsonObject);
        Map<String, Object> payload = Map.of(
                "temperature", temperature,
                "messages", messages,
                "max_length", maxToken,
                "stream",stream,
                "top_p", topP);
        LOG.debug("WenXin payload: {}", payload);
        String response = requestsWrapper.post(endpointUrl+this.accessToken, payload);
        LOG.debug("WenXin response: {}", response);
        return response.lines().toList();
    }
    @Override
    protected LLMResult innerGenerate(List<String> prompts, List<String> stop) {
        List<List<GenerationChunk>> generations = new ArrayList<>();

        for (String prompt : prompts) {
            GenerationChunk finalChunk = null;

            for (String streamResp : createStream(prompt, stop)) {
                if (StringUtils.isNotEmpty(streamResp)) {
                    GenerationChunk chunk = streamResponseToGenerationChunk(streamResp);
                    if (finalChunk == null) {
                        finalChunk = chunk;
                    } else {
                        finalChunk = finalChunk.add(chunk);
                    }
                }
            }
            generations.add(List.of(requireNonNull(finalChunk)));
        }
        return new LLMResult(generations);
    }

    public static GenerationChunk streamResponseToGenerationChunk(String streamResponse) {
        String replace = streamResponse.replace("data:", "");
        Map<String, Object> parsedResponse = JsonUtils.convertFromJsonStr(replace, new TypeReference<>() {});

        String text = (String) parsedResponse.getOrDefault("result", "");
        return new GenerationChunk(text, null);
    }
}
2.2.2 测试使用

2.2.3 请求结果


文章来源:https://blog.csdn.net/qq_38410391/article/details/132849746
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!

标签:

相关文章

本站推荐

标签云