自然语言处理从入门到应用——LangChain:模型(Models)-[大型语言模型(LLMs):基础知识]

分类目录:《自然语言处理从入门到应用》总目录


大型语言模型(LLMs)是LangChain的核心组件。LangChain不提供大型语言模型,而是提供了一个标准接口,通过该接口我们可以与各种LLMs进行交互。LLM类是专为与LLM接口设计的类。有许多LLM提供者(如:OpenAI、Cohere、Hugging Face),此类旨在为所有LLM提供一个标准接口。在《自然语言处理从入门到应用------LangChain:模型(Models)-大型语言模型(LLMs)》系列文章中,我们将专注于通用的LLM功能,而有关使用特定LLM包装器的详细信息,请参见具体的示例。

在本文中,我们将使用OpenAI LLM包装器,其功能对于所有LLM类型都是通用的。

dart 复制代码
from langchain.llms import OpenAI
llm = OpenAI(model_name="text-ada-001", n=2, best_of=2)

生成文本(Generate Text)是LLM最基本的功能,其传入一个字符串并返回一个字符串:

dart 复制代码
llm("Tell me a joke")

输出:

复制代码
'\n\nWhy did the chicken cross the road?\n\nTo get to the other side.'

generate:我们还可以用一个输入列表来调用它,得到是比仅输入文本更完整的响应。这个完整的响应包括多个顶级响应,以及LLM供应商特定的信息。

dart 复制代码
llm_result = llm.generate(["Tell me a joke", "Tell me a poem"]*15)
len(llm_result.generations)

输出:

dart 复制代码
30

输入:

dart 复制代码
llm_result.generations[0]

输出:

dart 复制代码
[Generation(text='\n\nWhy did the chicken cross the road?\n\nTo get to the other side!'), 
Generation(text='\n\nWhy did the chicken cross the road?\n\nTo get to the other side.')]

输入:

dart 复制代码
llm_result.generations[-1]
dart 复制代码
[Generation(text="\n\nWhat if love neverspeech\n\nWhat if love never ended\n\nWhat if love was only a feeling\n\nI'll never know this love\n\nIt's not a feeling\n\nBut it's what we have for each other\n\nWe just know that love is something strong\n\nAnd we can't help but be happy\n\nWe just feel what love is for us\n\nAnd we love each other with all our heart\n\nWe just don't know how\n\nHow it will go\n\nBut we know that love is something strong\n\nAnd we'll always have each other\n\nIn our lives."), 
Generation(text='\n\nOnce upon a time\n\nThere was a love so pure and true\n\nIt lasted for centuries\n\nAnd never became stale or dry\n\nIt was moving and alive\n\nAnd the heart of the love-ick\n\nIs still beating strong and true.')]

我们还可以访问返回的特定于服务提供商的信息,这些信息在不同的服务提供商之间并不标准化:

dart 复制代码
llm_result.llm_output

输出:

dart 复制代码
{'token_usage': {'completion_tokens': 3903, 'total_tokens': 4023, 'prompt_tokens': 120}}

Number of Tokens:我们还可以估算在该模型中一段文本将包含多少tokens。这很有用,因为模型有一个上下文长度,并且对于更多tokens的成本更高,这意味着我们需要知道传入的文本有多长。按默认设置,使用tiktoken估计tokens:

dart 复制代码
llm.get_num_tokens("what a joke")

输出:

dart 复制代码
3

参考文献:

1 LangChain 🦜️🔗 中文网,跟着LangChain一起学LLM/GPT开发:https://www.langchain.com.cn/

2 LangChain中文网 - LangChain 是一个用于开发由语言模型驱动的应用程序的框架:http://www.cnlangchain.com/

相关推荐
无心水16 分钟前
【Harness:设计规范】15、Harness 成熟度模型(H0-H3):你的 AI 智能体在第几层
人工智能·设计规范·openclaw·养龙虾·harness·hermes·honcho
Raink老师6 小时前
【AI面试临阵磨枪-79】实时数据 RAG:订单、商家、物流、天气、动态库存
人工智能·面试·职场和发展
脑极体6 小时前
点亮星河AI+鸿蒙,一座艺术场馆的日神觉醒
人工智能·华为·harmonyos
Cosolar6 小时前
Chroma向量库面试学习指南
数据库·人工智能·面试·职场和发展·数据库架构
BUG指挥官6 小时前
Claude Code的自动化编程
人工智能
意图共鸣7 小时前
意图共鸣科技《认知智能白皮书》——感知与执行分离:认知架构(CA)如何重塑大模型底层结构
人工智能·架构
等一个人的@7 小时前
让数据自己开口:数睿通智库新增智能问数模块
人工智能·自然语言处理
ZGi.ai7 小时前
人工审查节点:让自动化工作流多一步人工把关
运维·人工智能·自动化·人机协同·智能体工作流·人工审查
王莎莎-MinerU7 小时前
MinerU 深度技术解析:从架构原理到生产部署的全面指南
css·人工智能·自然语言处理·架构·ocr·个人开发
盘古信息IMS7 小时前
盘古信息IMS V6 8.0重磅发布:以薪火AI数智平台点燃离散制造数智化引擎
大数据·人工智能·制造