当前位置: 首页 > ds >正文

如何基于langchain基类LLM自定义大模型

在langchain调用外部大模型时,往往会因为sk、ak参数不匹配犯难。

如果能自定义一个LLM,对这些外部模型进行封装,就可以采用熟悉方式使用这些外部模型了。

1 创建自定义模型

langchain环境,遵循标准的LLM接口,实现一个自定义LLM类主要需实现以下两个方法:

  • _call: 接受一个字符串(以及一些可选的停止词),返回生成的字符串。
  • _llm_type: 这是一个用于日志记录的属性,返回模型类型的字符串。

可选实现

  • _identifying_params: 返回一个用于帮助识别模型及其输出的字典。
  • _acall: 异步版本的_call方法。
  • _stream: 实现模型输出的逐token流。
  • _astream: 异步版本的_stream方法。

示例如下

from typing import Any, Dict, Iterator, List, Mapping, Optionalfrom langchain_core.callbacks.manager import CallbackManagerForLLMRun
from langchain_core.language_models.llms import LLM
from langchain_core.outputs import GenerationChunkclass CustomLLM(LLM):"""A custom chat model that echoes the first `n` characters of the input.When contributing an implementation to LangChain, carefully documentthe model including the initialization parameters, includean example of how to initialize the model and include any relevantlinks to the underlying models documentation or API.Example:.. code-block:: pythonmodel = CustomChatModel(n=2)result = model.invoke([HumanMessage(content="hello")])result = model.batch([[HumanMessage(content="hello")],[HumanMessage(content="world")]])"""n: int"""The number of characters from the last message of the prompt to be echoed."""def _call(self,prompt: str,stop: Optional[List[str]] = None,run_manager: Optional[CallbackManagerForLLMRun] = None,**kwargs: Any,) -> str:"""Run the LLM on the given input.Override this method to implement the LLM logic.Args:prompt: The prompt to generate from.stop: Stop words to use when generating. Model output is cut off at thefirst occurrence of any of the stop substrings.If stop tokens are not supported consider raising NotImplementedError.run_manager: Callback manager for the run.**kwargs: Arbitrary additional keyword arguments. These are usually passedto the model provider API call.Returns:The model output as a string. Actual completions SHOULD NOT include the prompt."""if stop is not None:raise ValueError("stop kwargs are not permitted.")return prompt[: self.n]def _stream(self,prompt: str,stop: Optional[List[str]] = None,run_manager: Optional[CallbackManagerForLLMRun] = None,**kwargs: Any,) -> Iterator[GenerationChunk]:"""Stream the LLM on the given prompt.This method should be overridden by subclasses that support streaming.If not implemented, the default behavior of calls to stream will be tofallback to the non-streaming version of the model and returnthe output as a single chunk.Args:prompt: The prompt to generate from.stop: Stop words to use when generating. Model output is cut off at thefirst occurrence of any of these substrings.run_manager: Callback manager for the run.**kwargs: Arbitrary additional keyword arguments. These are usually passedto the model provider API call.Returns:An iterator of GenerationChunks."""for char in prompt[: self.n]:chunk = GenerationChunk(text=char)if run_manager:run_manager.on_llm_new_token(chunk.text, chunk=chunk)yield chunk@propertydef _identifying_params(self) -> Dict[str, Any]:"""Return a dictionary of identifying parameters."""return {# The model name allows users to specify custom token counting# rules in LLM monitoring applications (e.g., in LangSmith users# can provide per token pricing for their model and monitor# costs for the given LLM.)"model_name": "CustomChatModel",}@propertydef _llm_type(self) -> str:"""Get the type of language model used by this chat model. Used for logging purposes only."""return "custom"

2 测试自定义模型

测试示例如下

llm = CustomLLM(n=5)
llm.invoke("This is a foobar thing")

await llm.ainvoke("world")

llm.batch(["woof woof woof", "meow meow meow"])

await llm.abatch(["woof woof woof", "meow meow meow"])

async for token in llm.astream("hello"):
    print(token, end="|", flush=True)

3 langchain集成

自定义LLM集成到langchain的示例如下

# <!--IMPORTS:[{"imported": "ChatPromptTemplate", "source": "langchain_core.prompts", "docs": "https://python.langchain.com/api_reference/core/prompts/langchain_core.prompts.chat.ChatPromptTemplate.html", "title": "How to create a custom LLM class"}]-->
from langchain_core.prompts import ChatPromptTemplateprompt = ChatPromptTemplate.from_messages([("system", "you are a bot"), ("human", "{input}")]
)llm = CustomLLM(n=7)
chain = prompt | llmidx = 0
async for event in chain.astream_events({"input": "hello there!"}, version="v1"):print(event)idx += 1if idx > 7:# Truncatebreak

返回

{'event': 'on_chain_start', 'run_id': 'a79babe4-fb33-44d6-b85e-55e09420ae0a', 'name': 'RunnableSequence', 'tags': [], 'metadata': {}, 'data': {'input': {'input': 'hello there!'}}, 'parent_ids': []}
{'event': 'on_prompt_start', 'name': 'ChatPromptTemplate', 'run_id': 'cbddb0e8-c0f8-4fb4-a276-d16dec173b1a', 'tags': ['seq:step:1'], 'metadata': {}, 'data': {'input': {'input': 'hello there!'}}, 'parent_ids': []}
{'event': 'on_prompt_end', 'name': 'ChatPromptTemplate', 'run_id': 'cbddb0e8-c0f8-4fb4-a276-d16dec173b1a', 'tags': ['seq:step:1'], 'metadata': {}, 'data': {'input': {'input': 'hello there!'}, 'output': ChatPromptValue(messages=[SystemMessage(content='you are a bot', additional_kwargs={}, response_metadata={}), HumanMessage(content='hello there!', additional_kwargs={}, response_metadata={})])}, 'parent_ids': []}
{'event': 'on_llm_start', 'name': 'CustomLLM', 'run_id': 'd9254fee-074b-45e7-b671-063a1abafb14', 'tags': ['seq:step:2'], 'metadata': {'ls_provider': 'custom', 'ls_model_type': 'llm'}, 'data': {'input': {'prompts': ['System: you are a bot\nHuman: hello there!']}}, 'parent_ids': []}
{'event': 'on_llm_stream', 'name': 'CustomLLM', 'run_id': 'd9254fee-074b-45e7-b671-063a1abafb14', 'tags': ['seq:step:2'], 'metadata': {'ls_provider': 'custom', 'ls_model_type': 'llm'}, 'data': {'chunk': 'S'}, 'parent_ids': []}
{'event': 'on_chain_stream', 'run_id': 'a79babe4-fb33-44d6-b85e-55e09420ae0a', 'tags': [], 'metadata': {}, 'name': 'RunnableSequence', 'data': {'chunk': 'S'}, 'parent_ids': []}
{'event': 'on_llm_stream', 'name': 'CustomLLM', 'run_id': 'd9254fee-074b-45e7-b671-063a1abafb14', 'tags': ['seq:step:2'], 'metadata': {'ls_provider': 'custom', 'ls_model_type': 'llm'}, 'data': {'chunk': 'y'}, 'parent_ids': []}
{'event': 'on_chain_stream', 'run_id': 'a79babe4-fb33-44d6-b85e-55e09420ae0a', 'tags': [], 'metadata': {}, 'name': 'RunnableSequence', 'data': {'chunk': 'y'}, 'parent_ids': []}

reference

---

如何创建自定义 LLM 类

https://www.langchain.com.cn/docs/how_to/custom_llm/

http://www.xdnf.cn/news/17988.html

相关文章:

  • 飞算JavaAI开发全流程解析:从自然语言到可运行工程的智能进化
  • 从零开始学Python之数据结构(字符串以及数字)
  • 深入解析 Chrome UI 布局配置的设计思想与实现机制
  • STL算法【常用的算数生成算法】
  • vue3+leaflet案例:告警系统GIS一张图(附源码下载)
  • 基于大语言模型的爬虫数据清洗与结构化
  • 手机实时提取SIM卡打电话的信令声音-整体解决方案规划
  • 《WebGL中FBO的底层运行逻辑》
  • Day57--图论--53. 寻宝(卡码网)
  • 【前端工具】使用 Node.js 脚本实现项目打包后自动压缩
  • 计算机视觉(opencv)实战三——图像运算、cv2.add()、cv2.addWeighted()
  • Docker + Cronicle + Traefik 搭建服务器计划任务工具
  • nginx入门需知(含安装教程)
  • QT+Yolov8 推理部署,ONNX模型 ,实例分割+目标检测
  • 14、Docker Compose 安装 Redis 集群(三主三从)
  • linux 软硬链接详解
  • vscode的wsl环境,ESP32驱动0.96寸oled屏幕
  • 前端包管理工具
  • 基于wireshark的USB 全速硬件抓包工具USB Sniffer Lite的使用
  • 【lucene】DocumentsWriterFlushControl
  • 负载因子(Load Factor) :哈希表(Hash Table)中的一个关键性能指标
  • C++ 滑动窗口、二分查找
  • Ubuntu 22.04 远程桌面设置固定密码的方法
  • 快手入局外卖?上桌了,又没上
  • 第4节课:多模态大模型的核心能力(多模态大模型基础教程)
  • 18.13 《3倍效率提升!Hugging Face datasets.map高级技巧实战指南》
  • 顺序表插入删除
  • list模拟实现
  • 2025 年电赛 C 题 发挥部分 1:多正方形 / 重叠正方形高精度识别与最小边长测量
  • 36 C++ STL模板库5-string