您现在的位置是:首页 > 文章详情

对标 LangChain,一个使用 Java 开发的 LLM(大语言模型)应用开发框架

日期:2024-01-19点击:382

Agents-Flex: 一个优雅的 LLM(大语言模型) 应用开发框架

基础能力

  • LLM 的访问能力
  • Prompt、Prompt Template 定义加载的能力
  • Function Calling 定义、调用和执行等能力
  • Embedding
  • Vector Storage
  • 丰富的内容加载器
  • 丰富的文本分割器
  • LLM Chain
  • Agents Chain

简单对话

使用 OpenAi 大语言模型:

 public static void main(String[] args) throws InterruptedException {

    OpenAiConfig config = new OpenAiConfig();
    config.setApiKey("sk-rts5NF6n*******");

    Llm llm = new OpenAiLlm(config);

    Prompt  prompt = new SimplePrompt("请写一个关于小兔子战胜大灰狼的故事。");
    llm.chat(prompt, (llmInstance, message) -> {
        System.out.println("--->" + message.getContent());
    });

    Thread.sleep(10000);
}
 

使用 “通义千问” 大语言模型:

 public static void main(String[] args) throws InterruptedException {

    QwenLlmConfig config = new QwenLlmConfig();
    config.setApiKey("sk-28a6be3236****");
    config.setModel("qwen-turbo");

    Llm llm = new QwenLlm(config);

    Prompt  prompt = new SimplePrompt("请写一个关于小兔子战胜大灰狼的故事。");
    llm.chat(prompt, (llmInstance, message) -> {
        System.out.println("--->" + message.getContent());
    });

    Thread.sleep(10000);
}
 

使用 “讯飞星火” 大语言模型:

 public static void main(String[] args) throws InterruptedException {

    SparkLlmConfig config = new SparkLlmConfig();
    config.setAppId("****");
    config.setApiKey("****");
    config.setApiSecret("****");

    Llm llm = new SparkLlm(config);

    Prompt  prompt = new SimplePrompt("请写一个关于小兔子战胜大灰狼的故事。");
    llm.chat(prompt, (llmInstance, message) -> {
        System.out.println("--->" + message.getContent());
    });

    Thread.sleep(10000);
}
 

历史对话示例

 public static void main(String[] args) throws InterruptedException {

    SparkLlmConfig config = new SparkLlmConfig();
    config.setAppId("****");
    config.setApiKey("****");
    config.setApiSecret("****");

    // 创建一个大模型
    Llm llm = new SparkLlm(config);

    //创建一个历史对话的 prompt
    HistoriesPrompt prompt = new HistoriesPrompt();

    System.out.println("您想问什么?");
    Scanner scanner = new Scanner(System.in);

    //等待用户从控制台输入问题
    String userInput = scanner.nextLine();

    while (userInput != null){

        prompt.addMessage(new HumanMessage(userInput));

        //向大模型提问
        llm.chat(prompt, (instance, message) -> {
            System.out.println(">>>> " + message.getContent());
        });

        //继续等待用户从控制台输入内容
        userInput = scanner.nextLine();
    }
}
 

Function Calling

  • 第一步: 通过注解定义本地方法
public class WeatherUtil {

    @FunctionDef(name = "get_the_weather_info", description = "get the weather info")
    public static String getWeatherInfo(
        @FunctionParam(name = "city", description = "the city name") String name
    ) {
        //这里应该是通过接口去调用获得天气信息
        return name + "的天气是阴转多云。 ";
    }
}
 
  • 第二步: 通过 Prompt、Functions 传入给大模型,然后得到结果
 public static void main(String[] args) throws InterruptedException {

    OpenAiLlmConfig config = new OpenAiLlmConfig();
    config.setApiKey("sk-rts5NF6n*******");

    OpenAiLlm llm = new OpenAiLlm(config);

    Functions<String> functions = Functions.from(WeatherUtil.class, String.class);
    String result = llm.call(new SimplePrompt("今天的天气如何"), functions);

    System.out.println(result);
    // "北京的天气是阴转多云。 ";

    Thread.sleep(10000);
}

 

注意:当前版本为 v1.0.0-alpha.1 ,请暂时勿使用于与正式的商业产品中。

原文链接:https://www.oschina.net/news/276149/agents-flex
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章