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

中国电信备案网站做网站好还是小程序好

中国电信备案网站,做网站好还是小程序好,小红书 wordpress,华阳路街道网站建设LangChain官网、LangChain官方文档 、langchain Github、langchain API文档、llm-universe 文章目录 一、Chat models1.1 Chat models简介1.2 Chat models的调用方式1.2.1 环境配置1.2.2 使用LCEL方式调用Chat models1.2.3 使用内置Chain调用Chat models 1.3 缓存1.3.1 内存缓存… LangChain官网、LangChain官方文档 、langchain Github、langchain API文档、llm-universe 文章目录 一、Chat models1.1 Chat models简介1.2 Chat models的调用方式1.2.1 环境配置1.2.2 使用LCEL方式调用Chat models1.2.3 使用内置Chain调用Chat models 1.3 缓存1.3.1 内存缓存1.3.2 SQLite 缓存​ 1.4 Prompts1.4.1 使用(role, content)创建1.4.2 使用MessagePromptTemplate创建 1.5 跟踪令牌使用情况1.5.1 跟踪单个 Chat model1.5.2 跟踪chain或agent 二、LLMs2.1 LLMs的调用2.2 异步API2.3 自定义 LLM2.3.1 自定义 LLM的简单实现2.3.2 自定义zhipuai百度文心 LLM 2.4 缓存2.4.1 内存缓存2.4.2 SQLite 缓存​2.4.3 关闭特定LLM的 缓存2.4.4 可选链式缓存​ 2.5 序列化2.5.1 存储2.5.2 加载 2.6 跟踪令牌使用情况略 一、Chat models 参考《Chat models》 1.1 Chat models简介 LCEL提供了声明式的方式来组合Runnables成为链它是一个标准接口可以轻松定义自定义链条并以标准方式调用它们还可以批处理、流处理等。该标准接口包括以下几个方法前缀a表示为对应的异步处理方法 invoke/ainvoke处理单个输入batch/abatch批处理多个输入列表stream/astream流式处理单个输入并产生输出astream_log流式返回中间步骤的数据以及最终响应数据 所有 ChatModel 都实现Runnable接口该接口附带以上所有方法的默认实现 这为所有 ChatModel 提供了对异步、流式传输和批处理的基本支持。另外Stream方法还需要 ChatModel厂商本身也支持最终各种 ChatModel的集成情况详见《Chat models》。 有关LCEL及Runnables的更多信息详见《LangChain0.0.339官方文档二LCEL》有关LangChain内置集成的各种Chat models文档可查看Integrations部分。 Chat models底层实现是LLMs但它不使用“文本输入、文本输出”API而是使用“聊天消息”作为输入和输出的界面即chat model基于消息List[BaseMessage] 而不是原始文本。在langchain中消息接口由 BaseMessage 定义它有两个必需属性 content 消息的内容通常为字符串。role 消息来源BaseMessage的实体类别比如 HumanMessage来自人类/用户的BaseMessage。AIMessage来自AI/助手的BaseMessage。SystemMessage来自系统的BaseMessage。FunctionMessage / ToolMessage包含函数或工具调用输出的BaseMessage。ChatMessage如果上述角色都不合适可以自定义角色详见《Types of MessagePromptTemplate》。消息也可以是 str 将自动转换为 HumanMessage 和 PromptValuePromptTemplate的值 。 对应的LangChain提供了不同类型的 MessagePromptTemplate 。最常用的是 AIMessagePromptTemplate 、 SystemMessagePromptTemplate 和 HumanMessagePromptTemplate 它们分别创建 AI 消息、系统消息和用户消息。 1.2 Chat models的调用方式 在简单的应用中单独使用LLM是可以的但在更复杂的应用中可能需要将多个大型语言模型进行链式组合或与其他组件进行链式调用以对多个输入同时进行处理。组合成Chain的方式有两种 使用内置的 Chain比如LLMChain基本的链类型、SequentialChain处理单个输入且单个输出的情况、Router Chain同一输入router到不同的输出。使用最新的LCELLangChain Expression Language框架来实现chaining调用Runnables的各种实现方法。 1.2.1 环境配置 本章以百度文心一言为例进行演示。要调用文心一言 API需要先获取文心一言调用秘钥。首先我们需要进入文心千帆服务平台注册登录之后选择“应用接入”——“创建应用”。然后简单输入基本信息选择默认配置创建应用即可。 创建完成后点击应用的“详情”即可看到此应用的 AppID,API Key,Secret Key。然后在百度智能云在线调试平台-示例代码中心快速调试接口获取AccessToken不解之处详见API文档。最后在项目文件夹下使用vim .envLinux或type nul .envWindows cmd创建.env文件并在其中写入 QIANFAN_AKxxx QIANFAN_SKxxx access_tokenxxx下面将这些变量配置到环境中后续就可以自动使用了。 # 使用openai、智谱ChatGLM、百度文心需要分别安装openai,zhipuai,qianfan import os import openai,zhipuai,qianfan from langchain.llms import ChatGLM from langchain.chat_models import ChatOpenAI,QianfanChatEndpointfrom dotenv import load_dotenv, find_dotenv _ load_dotenv(find_dotenv()) # read local .env file openai.api_key os.environ[OPENAI_API_KEY] zhipuai.api_key os.environ[ZHIPUAI_API_KEY] qianfan.qianfan_akos.environ[QIANFAN_AK] qianfan.qianfan_skos.environ[QIANFAN_SK]from langchain.schema.messages import HumanMessage,SystemMessagemessages [SystemMessage(contentYoure a helpful assistant),HumanMessage(contentWhat is the purpose of model regularization?), ] chat QianfanChatEndpoint()1.2.2 使用LCEL方式调用Chat models 常规调用 chat.invoke(messages) # 处理单个输入 chat.batch([messages]) # 批处理输入 # 流式处理输入 for chunk in chat.stream(messages):print(chunk.content, end, flushTrue)AIMessage(content模型正则化的目的是为了防止模型过拟合提高模型的泛化能力。模型正则化通过引入额外的项使得模型在训练过程中需要最小化这些项的损失从而约束模型的复杂度避免模型对训练数据中的噪声和异常值过度拟合。这样可以提高模型的泛化能力使得模型在未见过的数据上也能表现良好。常见的模型正则化方法包括L1正则化、L2正则化、dropout等。, additional_kwargs{id: as-2y2heq69su, object: chat.completion, created: 1701506605, result: 模型正则化的目的是为了防止模型过拟合提高模型的泛化能力。模型正则化通过引入额外的项使得模型在训练过程中需要最小化这些项的损失从而约束模型的复杂度避免模型对训练数据中的噪声和异常值过度拟合。这样可以提高模型的泛化能力使得模型在未见过的数据上也能表现良好。常见的模型正则化方法包括L1正则化、L2正则化、dropout等。, is_truncated: False, need_clear_history: False, usage: {prompt_tokens: 9, completion_tokens: 101, total_tokens: 110}})再举一个简单的例子 answerchat.invoke(使用‘白小林’写一首三行诗) answerAIMessage(content白小林游历天涯\n心灵轻舞映朝霞。\n笑看人生四季花。, additional_kwargs{id: as-q59v21q44t, object: chat.completion, created: 1701510940, result: 白小林游历天涯\n心灵轻舞映朝霞。\n笑看人生四季花。, is_truncated: False, need_clear_history: False, usage: {prompt_tokens: 11, completion_tokens: 22, total_tokens: 33}})返回的结果是一个AIMessage可以使用dict将其转为字典格式进行后续处理 print(answer.dict()[content])白小林游历天涯 心灵轻舞映朝霞。 笑看人生四季花。print(answer.dict()[additional_kwargs][usage]){prompt_tokens: 11, completion_tokens: 22, total_tokens: 33}异步调用 # 异步处理输入 await chat.ainvoke(messages) async for chunk in chat.astream(messages):print(chunk.content, end, flushTrue)async for chunk in chat.astream_log(messages):print(chunk)1.2.3 使用内置Chain调用Chat models 在旧版的LangChain中可以将一条或多条消息传递给chat model.来完成聊天输出是一条消息。 chat(messages) # 输出和chat.invoke(messages)一样也可以使用 generate来进行多条消息的批处理 batch_messages [[SystemMessage(contentYou are a helpful assistant that translates English to French.),HumanMessage(contentI love programming.),],[SystemMessage(contentYou are a helpful assistant that translates English to French.),HumanMessage(contentI love artificial intelligence.),], ] result chat.generate(batch_messages) resultLLMResult(generations[[ChatGeneration(text非常好编程是一项非常有趣和有挑战性的工作。你更喜欢哪种类型的编程, generation_info{finish_reason: stop, id: as-eczv9t6wdu, object: chat.completion, created: 1701507897, result: 非常好编程是一项非常有趣和有挑战性的工作。你更喜欢哪种类型的编程, is_truncated: False, need_clear_history: False, usage: {prompt_tokens: 4, completion_tokens: 19, total_tokens: 23}}, messageAIMessage(content非常好编程是一项非常有趣和有挑战性的工作。你更喜欢哪种类型的编程, additional_kwargs{id: as-eczv9t6wdu, object: chat.completion, created: 1701507897, result: 非常好编程是一项非常有趣和有挑战性的工作。你更喜欢哪种类型的编程, is_truncated: False, need_clear_history: False, usage: {prompt_tokens: 4, completion_tokens: 19, total_tokens: 23}}))], [ChatGeneration(text很好你对人工智能的热爱很令人赞赏。人工智能技术已经变得越来越重要它正在改变我们的生活和工作方式。, generation_info{finish_reason: stop, id: as-7gc409h5d1, object: chat.completion, created: 1701507898, result: 很好你对人工智能的热爱很令人赞赏。人工智能技术已经变得越来越重要它正在改变我们的生活和工作方式。, is_truncated: False, need_clear_history: False, usage: {prompt_tokens: 5, completion_tokens: 24, total_tokens: 29}}, messageAIMessage(content很好你对人工智能的热爱很令人赞赏。人工智能技术已经变得越来越重要它正在改变我们的生活和工作方式。, additional_kwargs{id: as-7gc409h5d1, object: chat.completion, created: 1701507898, result: 很好你对人工智能的热爱很令人赞赏。人工智能技术已经变得越来越重要它正在改变我们的生活和工作方式。, is_truncated: False, need_clear_history: False, usage: {prompt_tokens: 5, completion_tokens: 24, total_tokens: 29}}))]], llm_output{}, run[RunInfo(run_idUUID(a81202f3-8210-44f9-959c-0e6af4686ae7)), RunInfo(run_idUUID(564b8ec4-3531-4b6e-b50c-1baafd16e8ff))])1.3 缓存 参考《Caching》 LangChain为chat models提供了可选的缓存层caching layer如果您经常多次进行相同的请求那么它可以减少API调用次数从而在一定程度上降低费用也加速了应用的响应速度。 from langchain.globals import set_llm_cache from langchain.chat_models import ChatOpenAIllm QianfanChatEndpoint()1.3.1 内存缓存 from langchain.cache import InMemoryCache set_llm_cache(InMemoryCache())# 第一次生成时间较长 llm.predict(Tell me a joke)CPU times: user 35.9 ms, sys: 28.6 ms, total: 64.6 ms Wall time: 4.83 s\n\nWhy couldnt the bicycle stand up by itself? It was...two tired!# 第二次从内存加载时间更短 llm.predict(Tell me a joke)CPU times: user 238 µs, sys: 143 µs, total: 381 µs Wall time: 1.76 ms\n\nWhy did the chicken cross the road?\n\nTo get to the other side.1.3.2 SQLite 缓存​ SQLite 是一种嵌入式关系型数据库引擎它是一个零配置的、无服务器的、自包含的数据库系统。与传统的数据库管理系统DBMS不同SQLite 不需要单独的服务器进程来运行而是直接嵌入到应用程序中。这使得 SQLite 成为一种轻量级、无服务器的、自包含的数据库系统适用于小型应用和嵌入式设备。 rm .langchain.db下面设置LangChain 的 LLM 缓存设置为 SQLiteCache并指定 SQLite 数据库的文件路径为 .langchain.db。 from langchain.cache import SQLiteCache set_llm_cache(SQLiteCache(database_path.langchain.db))llm.predict(Tell me a joke)CPU times: user 17 ms, sys: 9.76 ms, total: 26.7 ms Wall time: 825 ms\n\nWhy did the chicken cross the road?\n\nTo get to the other side.# The second time it is, so it goes faster llm.predict(Tell me a joke)CPU times: user 2.46 ms, sys: 1.23 ms, total: 3.7 ms Wall time: 2.67 ms\n\nWhy did the chicken cross the road?\n\nTo get to the other side.1.4 Prompts 1.4.1 使用(role, content)创建 ChatPromptTemplate是 chat models 的聊天消息列表。 每个聊天消息都与内容相关联并具有一个称为role角色的额外参数。例如在 OpenAI 聊天补全 API 中聊天消息可以与 AI 助手、人类或系统角色相关联。创建一个聊天提示模板就像这样 from langchain.prompts import ChatPromptTemplatechat_template ChatPromptTemplate.from_messages([(system, You are a helpful AI bot. Your name is {name}.),(human, Hello, how are you doing?),(ai, Im doing well, thanks!),(human, {user_input}),] )messages chat_template.format_messages(nameBob, user_inputWhat is your name?) messages[SystemMessage(contentYou are a helpful AI bot. Your name is Bob.),HumanMessage(contentHello, how are you doing?),AIMessage(contentIm doing well, thanks!),HumanMessage(contentWhat is your name?)]1.4.2 使用MessagePromptTemplate创建 ChatPromptTemplate.from_messages接受多种消息表示方式。比如除了使用上面提到的(type, content)的2元组表示法之外你还可以传入MessagePromptTemplate或BaseMessage的实例这为你在构建聊天提示时提供了很大的灵活性下面用百度千帆进行演示。 import os import openai,qianfanfrom dotenv import load_dotenv, find_dotenv _ load_dotenv(find_dotenv()) # read local .env file openai.api_key os.environ[OPENAI_API_KEY] qianfan.qianfan_akos.environ[QIANFAN_AK] qianfan.qianfan_skos.environ[QIANFAN_SK]使用MessagePromptTemplate from langchain.chat_models import QianfanChatEndpoint from langchain.prompts import HumanMessagePromptTemplate,SystemMessagePromptTemplatetemplateYou are a helpful assistant that translates {input_language} to {output_language}. system_message_prompt SystemMessagePromptTemplate.from_template(template) human_template{text} human_message_prompt HumanMessagePromptTemplate.from_template(human_template) chat_prompt ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])chat QianfanChatEndpoint() chat(chat_prompt.format_prompt(input_languageEnglish, output_languageFrench, textI love programming.).to_messages())AIMessage(content编程是一项非常有趣和有挑战性的工作我很羡慕你能够享受其中的乐趣。, additional_kwargs{id: as-cxezsmtfga, object: chat.completion, created: 1701520678, result: 编程是一项非常有趣和有挑战性的工作我很羡慕你能够享受其中的乐趣。, is_truncated: False, need_clear_history: False, usage: {prompt_tokens: 4, completion_tokens: 18, total_tokens: 22}})使用BaseMessage的实例 from langchain.schema.messages import SystemMessagechat_template ChatPromptTemplate.from_messages([SystemMessage(content(You are a helpful assistant that re-writes the users text to sound more upbeat.)),HumanMessagePromptTemplate.from_template({text}),] )chat(chat_template.format_messages(texti dont like eating tasty things.))AIMessage(content很抱歉听到您不喜欢吃美味的食物。您有其他喜欢的食物类型吗或许我们可以找到一些其他您喜欢吃的食物您试试看是否能够喜欢呢, additional_kwargs{id: as-sdcbpxad11, object: chat.completion, created: 1701520841, result: 很抱歉听到您不喜欢吃美味的食物。您有其他喜欢的食物类型吗或许我们可以找到一些其他您喜欢吃的食物您试试看是否能够喜欢呢, is_truncated: False, need_clear_history: False, usage: {prompt_tokens: 8, completion_tokens: 34, total_tokens: 42}})1.5 跟踪令牌使用情况 本节介绍如何跟踪调用的令牌使用情况目前仅针对 OpenAI API 实现。 1.5.1 跟踪单个 Chat model from langchain.callbacks import get_openai_callback from langchain.chat_models import ChatOpenAIllm ChatOpenAI(model_namegpt-4)with get_openai_callback() as cb:result llm.invoke(Tell me a joke)print(cb)Tokens Used: 24Prompt Tokens: 11Completion Tokens: 13 Successful Requests: 1 Total Cost (USD): $0.0011099999999999999按顺序跟踪多个请求 with get_openai_callback() as cb:result llm.invoke(Tell me a joke)result2 llm.invoke(Tell me a joke)print(cb.total_tokens)481.5.2 跟踪chain或agent from langchain.agents import AgentType, initialize_agent, load_tools from langchain.llms import OpenAItools load_tools([serpapi, llm-math], llmllm) agent initialize_agent(tools, llm, agentAgentType.OPENAI_FUNCTIONS, verboseTrue)with get_openai_callback() as cb:response agent.run(Who is Olivia Wildes boyfriend? What is his current age raised to the 0.23 power?)print(fTotal Tokens: {cb.total_tokens})print(fPrompt Tokens: {cb.prompt_tokens})print(fCompletion Tokens: {cb.completion_tokens})print(fTotal Cost (USD): ${cb.total_cost})Entering new AgentExecutor chain...Invoking: Search with Olivia Wildes current boyfriend[Things are looking golden for Olivia Wilde, as the actress has jumped back into the dating pool following her split from Harry Styles — read ..., “I did not want service to take place at the home of Olivias current partner because Otis and Daisy might be present,” Sudeikis wrote in his ..., February 2021: Olivia Wilde praises Harry Styles modesty. One month after the duo made headlines with their budding romance, Wilde gave her new beau major ..., An insider revealed to People that the new couple had been dating for some time. They were in Montecito, California this weekend for a wedding, ..., A source told People last year that Wilde and Styles were still friends despite deciding to take a break. He\s still touring and is now going ..., ... love life. “Hes your typical average Joe.” The source adds, “Shes not giving too much away right now and wants to keep the relationship ..., Multiple sources said the two were “taking a break” from dating because of distance and different priorities. “Hes still touring and is now ..., Comments. Filed under. celebrity couples · celebrity dating · harry styles · jason sudeikis · olivia wilde ... Now Holds A Darker MeaningNYPost., ... dating during filming. The 39-year-old did however look very cosy with the comedian, although his relationship status is unknown. Olivia ...] Invoking: Search with Harry Styles current age responded: Olivia Wildes current boyfriend is Harry Styles. Let me find out his age for you.29 years Invoking: Calculator with 29 ^ 0.23Answer: 2.169459462491557Harry Styles current age (29 years) raised to the 0.23 power is approximately 2.17. Finished chain. Total Tokens: 1929 Prompt Tokens: 1799 Completion Tokens: 130 Total Cost (USD): $0.06176999999999999二、LLMs 参考《》 2.1 LLMs的调用 同Chat models一样LLM也实现了 Runnable 接口所以你同样可以使用LCEL语法进行调用比如invoke方法 from langchain.llms import QianfanLLMEndpointllm QianfanLLMEndpoint() print(llm.invoke(使用‘白小林’写一首三行诗))白小林在林间漫步 阳光洒落笑语连连。 林中鸟鸣声声醉 白小林心中乐无边。其它iainvoke、batch/abatch、stream/astream、astream_log方法和Chat models一样就不一一演示了。另外也可以使用内置的chain方法进行调用 llm(Tell me a joke)\n\nQ: What did the fish say when it hit the wall?\nA: Dam!llm_result llm.generate([Tell me a joke, Tell me a poem] * 3) len(llm_result.generations)6另外有关各LLMs是否支持异步调用、流式调用详见LLMs中的Model表格。 2.2 异步API 参考《Async API》 LLM通过实现Runnable接口提供了异步调用的基本支持如果LLM提供商有原生的异步实现会优先使用这些实现否则默认使用LLM的异步支持方式将调用移到后台线程以便让应用中的其他异步函数继续执行。 import asyncio import time from langchain.llms import OpenAIllm OpenAI(modelgpt-3.5-turbo-instruct, temperature0.9)def invoke_serially():for _ in range(10):resp llm.invoke(Hello, how are you?)async def async_invoke(llm):resp await llm.ainvoke(Hello, how are you?)async def invoke_concurrently():tasks [async_invoke(llm) for _ in range(10)]await asyncio.gather(*tasks)s time.perf_counter() # If running this outside of Jupyter, use asyncio.run(generate_concurrently()) await invoke_concurrently() elapsed time.perf_counter() - s print(\033[1m fConcurrent executed in {elapsed:0.2f} seconds. \033[0m)s time.perf_counter() invoke_serially() elapsed time.perf_counter() - s print(\033[1m fSerial executed in {elapsed:0.2f} seconds. \033[0m)Concurrent executed in 1.03 seconds. Serial executed in 6.80 seconds.为了简化代码我们也可以使用 abatch 进行异步批处理 s time.perf_counter() # If running this outside of Jupyter, use asyncio.run(generate_concurrently()) await llm.abatch([Hello, how are you?] * 10) elapsed time.perf_counter() - s print(\033[1m fBatch executed in {elapsed:0.2f} seconds. \033[0m)Batch executed in 1.31 seconds.方法说明执行时间invoke_serially()使用循环方式执行LLM的操作10次约6.8秒invoke_concurrently()使用ainvoke在循环中以异步方式执行操作10次约1.03秒展示了并行执行的优势abatch批处理请求异步约1.31秒展示了批处理的高效性原生异步实现如果LLM提供商支持原生异步操作可能比提供的默认异步功能更有效率 2.3 自定义 LLM 2.3.1 自定义 LLM的简单实现 参考《Custom LLM》 对于你自己的LLM或者LangChain尚没有封装支持的LLM可以创建自定义的LLM封装器只需要实现 _call方法此方法接收一个字符串和一些可选的停用词然后返回一个字符串_identifying_params属性可选用于帮助打印这个类时的显示信息字典类型 下面实现一个非常简单的自定义LLM其功能是返回输入文本的前n个字符。 from typing import Any, List, Mapping, Optionalfrom langchain.callbacks.manager import CallbackManagerForLLMRun from langchain.llms.base import LLMclass CustomLLM(LLM):n: intpropertydef _llm_type(self) - str:return customdef _call(self,prompt: str,stop: Optional[List[str]] None,run_manager: Optional[CallbackManagerForLLMRun] None,**kwargs: Any,) - str:if stop is not None:raise ValueError(stop kwargs are not permitted.)return prompt[: self.n]propertydef _identifying_params(self) - Mapping[str, Any]:Get the identifying parameters.return {n: self.n}这段代码定义了一个名为CustomLLM的类它继承自LLM基类并实现了自定义的LLM功能。 n: int这是一个类属性用于存储一个整数值。它表示后面在_call方法中会用到的字符数量。 _llm_type方法这是一个私有方法返回一个描述此LLM类型的字符串。在这里返回的是custom用于标识这是一个自定义的LLM类型。 _call方法这个方法是必须实现的它接收一个字符串prompt作为输入还可以接收一个可选的stop参数一个字符串列表以及其他任意的关键字参数。这个方法的功能是根据给定的prompt字符串返回其前面n个字符。如果传入了stop参数会引发一个ValueError异常因为这里不允许使用stop参数。 _identifying_params方法这是一个属性方法返回一个包含LLM标识参数的字典。在这个例子中它返回一个字典包含了一个键为n的参数其值为self.n即当前的字符数量。 然后我们就可以像别的LLM一样来调用了 llm CustomLLM(n10) llm(This is a foobar thing)This is a 打印此LLM以查看信息 print(llm)CustomLLM Params: {n: 10}2.3.2 自定义zhipuai百度文心 LLM llm-universe项目中有自定义zhipuai LLM的实现zhipuai_llm.py和自定义百度文心LLM实现wenxin_llm.py可供参考之前版本的LangChain有很多模型每月集成。 2.4 缓存 参考《Caching》、《LLM Caching integrations》 同Chat model一样LLM也可以使用缓存来获取重复的请求结果。 #### 2.4.1 内存缓存 from langchain.globals import set_llm_cache from langchain.llms import OpenAI# To make the caching really obvious, lets use a slower model. llm OpenAI(model_nametext-davinci-002, n2, best_of2)2.4.1 内存缓存 from langchain.cache import InMemoryCache set_llm_cache(InMemoryCache())# The first time, it is not yet in cache, so it should take longer llm.predict(Tell me a joke)# The second time it is, so it goes faster llm.predict(Tell me a joke)2.4.2 SQLite 缓存​ rm .langchain.db# We can do the same thing with a SQLite cache from langchain.cache import SQLiteCache set_llm_cache(SQLiteCache(database_path.langchain.db))# The first time, it is not yet in cache, so it should take longer llm.predict(Tell me a joke)# The second time it is, so it goes faster llm.predict(Tell me a joke)2.4.3 关闭特定LLM的 缓存 您还可以选择在启用了全局缓存后关闭特定的 LLM的 缓存。 from langchain.llms import OpenAIllm OpenAI(model_nametext-davinci-002, n2, best_of2, cacheFalse) llm(Tell me a joke)CPU times: user 5.8 ms, sys: 2.71 ms, total: 8.51 ms Wall time: 745 ms\n\nWhy did the chicken cross the road?\n\nTo get to the other side!llm(Tell me a joke) # 两次时间差不多表示没有启用缓存的结果CPU times: user 4.91 ms, sys: 2.64 ms, total: 7.55 ms Wall time: 623 ms\n\nTwo guys stole a calendar. They got six months each.2.4.4 可选链式缓存​ 您还可以在链中只缓存部分节点。下面示例中我们将加载一个总结器的Map-Reduce链。我们会对Map步骤的结果进行缓存但对Combine步骤的结果不缓存中间结果缓存加载最终结果直接生成。 首先创建两个OpenAI的LLM实例其中一个将缓存关闭。然后使用CharacterTextSplitter读取state_of_the_union.txt的文本内容并将其分成多个文档创建对应的Document对象。 llm OpenAI(model_nametext-davinci-002) no_cache_llm OpenAI(model_nametext-davinci-002, cacheFalse)from langchain.text_splitter import CharacterTextSplitter from langchain.chains.mapreduce import MapReduceChain from langchain.docstore.document import Documenttext_splitter CharacterTextSplitter() with open(../../../state_of_the_union.txt) as f:state_of_the_union f.read() texts text_splitter.split_text(state_of_the_union) docs [Document(page_contentt) for t in texts[:3]]接下来使用load_summarize_chain方法加载总结器的Map-Reduce链并指定了对应的LLM和链类型。在这里为了Map步骤使用了允许缓存的LLM而对Reduce步骤使用了禁用缓存的LLM。 from langchain.chains.summarize import load_summarize_chainchain load_summarize_chain(llm, chain_typemap_reduce, reduce_llmno_cache_llm) chain.run(docs)CPU times: user 452 ms, sys: 60.3 ms, total: 512 ms Wall time: 5.09 s\n\nPresident Biden is discussing the American Rescue Plan and the Bipartisan Infrastructure Law, which will create jobs and help Americans. He also talks about his vision for America, which includes investing in education and infrastructure. In response to Russian aggression in Ukraine, the United States is joining with European allies to impose sanctions and isolate Russia. American forces are being mobilized to protect NATO countries in the event that Putin decides to keep moving west. The Ukrainians are bravely fighting back, but the next few weeks will be hard for them. Putin will pay a high price for his actions in the long run. Americans should not be alarmed, as the United States is taking action to protect its interests and allies.再次当运行该链后执行速度明显提高但最终的结果与之前运行的结果不同。这是因为在Map步骤进行了缓存但在Reduce步骤没有进行缓存而是直接生成的。 chain.run(docs)CPU times: user 11.5 ms, sys: 4.33 ms, total: 15.8 ms Wall time: 1.04 s\n\nPresident Biden is discussing the American Rescue Plan and the Bipartisan Infrastructure Law, which will create jobs and help Americans. He also talks about his vision for America, which includes investing in education and infrastructure.此外还有Redis Cache、GPTCache、Momento Cache等详见《LLM Caching integrations》。 2.5 序列化 序列化是指将对象的状态转换为字节流或JSON和XML等格式使其能够在不同系统、编程语言或存储介质之间进行传输或持久化存储。反序列化则是将序列化后的数据重新转换为对象的过程。 LangChain的Python和LangChain的JavaScript共享一个序列化方案即无论是使用Python还是JavaScript编写的代码在处理LangChain对象的序列化和反序列化时可以采用相同的机制和格式提供了跨语言交互和数据共享的能力。 通过运行类方法is_lc_serializable你可以检查一个LangChain类是否可序列化。 from langchain.llms import OpenAI from langchain.llms.loading import load_llmOpenAI.is_lc_serializable()True2.5.1 存储 任何可序列化的对象都可以序列化为 dict 或 json 字符串下面演示dumpd和dumps两种方法。 from langchain.load import dumpd, dumpsllm OpenAI(modelgpt-3.5-turbo-instruct) dumpd(llm){lc: 1,type: constructor,id: [langchain, llms, openai, OpenAI],kwargs: {model: gpt-3.5-turbo-instruct,openai_api_key: {lc: 1, type: secret, id: [OPENAI_API_KEY]}}}dumps(llm){lc: 1, type: constructor, id: [langchain, llms, openai, OpenAI], kwargs: {model: gpt-3.5-turbo-instruct, openai_api_key: {lc: 1, type: secret, id: [OPENAI_API_KEY]}}}2.5.2 加载 from langchain.load import loads from langchain.load.load import loadloaded_1 load(dumpd(llm)) loaded_2 loads(dumps(llm)) print(loaded_1.invoke(How are you doing?))I am an AI and do not have the capability to experience emotions. But thank you for asking. Is there anything I can assist you with?2.6 跟踪令牌使用情况略 跟踪LLM令牌使用情况同Chat models差不多目前仅也针对 OpenAI API 实现直接查看文档《Tracking token usage》就行这里就不写了。
http://www.huolong8.cn/news/250808/

相关文章:

  • 引流软件下载站邯郸优企网络
  • 滨州建设网站潍坊网站排名推广
  • wordpress建站工具十大免费模板网站
  • 网站建设顾问汉中市建设局网站
  • 文山建设5G网站网络舆情监测与研判
  • 网站被取消备案html登录注册页面
  • 网站设计与开发实例深圳平湖做网站
  • 浮山网站建设会用框架做网站能找到工作吗
  • 403.14网站网站吸流量
  • 免费推广网站教程动易网站后台修改栏目的字
  • 网站建设一般需要几个步骤全flash 电子商务网站如何推广
  • 0基础多久学会网站架构怎样建立自己的视频号
  • 还有什么类似建设通的网站百度首页清爽版
  • 资阳网站建设资阳wordpress网站底部版权代码
  • 视频图站主题 wordpress凉山州建设厅官方网站
  • 合肥做网站开发多少钱搬瓦工怎么做网站
  • 如何做企业网站推广产品网站空间和域名价格
  • 做网站许昌营销案例网站推荐
  • 在线app开发网站建设网站服务器可以做家用电脑
  • 网站服务器停止响应是什么意思东圃手机网站开发
  • 做ppt图片用的网站容城轻松seo优化排名
  • 一个合格的网站设计小说排行榜百度
  • 为一个网站设计一个推广方案公众号文章存储wordpress
  • 做公司网站要收费吗广告网站建设网
  • 网站制作的软件做网站页面对PS切图
  • 网站设计形式做网站每年要交不费用吗
  • 网站还在建设中英文买好域名和云主机后怎么做网站
  • 电子商务网站建设摘要旅游营销技巧美剧
  • 有没有网站开发软件北京营销型网站推广
  • wordpress做的网站吗网站互动功能