Agent - Codename: Test_01

15 May 2024 Deep-Learning and Note

This is how I build my first LLM agent.

Agent.. Do you mean someone plays something pretty cool for a certain mysterious orgnization

Agents are systems that use LLMs as reasoning engines to determine which actions to take and the inputs to pass them. Beacuse by themselves, language models can’t take actions - they just output text. After executing actions, the results can be fed back into the LLM to determine whether more actions are needed, or whether it is okay to finish.[1]

The core idea of agents is to use a language model to choose a sequence of actions to take. In chains, a sequence of actions is hardcoded (in code). In agents, a language model is used as a reasoning engine to determine which actions to take and in which order.[2]

Component

Agent Consider:

Ingredients

Considering Langchain agent building demo request a certain LLM API, I will implement my own here, instead of using a service which you unwilling to use (and pay).

Okay then What to Do

Bulletpoints: Refer to the langchain documentation[2] and consider the following related content:

Code

Summary

What On Earth Dose Agents Do

Why Choosing LangChain

Weibo User @真阿当 评价LangChain -

用langchain,API友好到想哭。langchain对function calling的封装如下:

1、function calling定义那里,直接通过baseModel的类,封装成一个对象,function calling里的参数,就是对象的属性。

2、function的调用,就是将baseModel类对象传到一个函数里,函数返回值直接在函数里写个return就完了。函数名字与function calling的name参数不用对应。

3、函数返回值再传回给LLM,只需要通过管道语法,直接在chain后面再加一个chain,对返回值的格式有要求,再后面追加一个parser类型的chain。把函数式编程的威力发挥到极致。之前我一直是面向对象的忠实信徒,一直不明白函数式编程适合的场景。langchain向我演示了这个场景,彻底服气了。

4、原生open api的assistant、thread、run这些概念,以及多轮对话的记录,在longchain里做了重新的设计。assitant变成了agent,对agent传参数checkpointer,agent就有了memory的能力,可以记住多轮对话的上下文。在调invoke方法时,传参config就可以区分不同thread。不管是invoke,还是stream模式,返回值都直接做了封装。是否stream模式,只需要将chain的方法由invoke改为stream,就完成了模式切换,各种run.status、thread_id细节都透明了,应用层只需要关注应用层的代码逻辑即可。

先不说通过community将大量的三方tools引入进来,也不说屏蔽不同LLM大模型的原生接口的差异,单是对LLM原生接口层的语法糖封装,langchain就已经让人无法放弃了,就看得出来langchain做了非常深入的思考,拥有强大的抽象能力和架构能力。

Reference

[1] Build an Agent - https://python.langchain.com/v0.2/docs/tutorials/agents/

[2] Agents - https://python.langchain.com/v0.1/docs/modules/agents/

[3] JSONFormer - https://python.langchain.com/v0.2/docs/integrations/llms/jsonformer_experimental/

Comments