南昌建设网站,科技太空讲座观后感,软件开发培训机构学费,wordpress耗资源升级程序原文地址#xff1a;【LangChain系列 15】语言模型——LLMs(一)
本文速读#xff1a; 异步API 自定义LLM Fake LLM HumanInput LLM 本文将介绍LLMs在LangChain中的一些用法#xff0c;帮助我们更好地了解LLM模块。 01 异步API LangChain通过异步库实现了对异步的支持【LangChain系列 15】语言模型——LLMs(一)
本文速读 异步API 自定义LLM Fake LLM HumanInput LLM 本文将介绍LLMs在LangChain中的一些用法帮助我们更好地了解LLM模块。 01 异步API LangChain通过异步库实现了对异步的支持异步对于多LLM的并发调用是非常有用的。目前OpenAI、PromptLayerOpenAI、ChatOpenAI、Anthropic、Cohere都是支持异步的其它LLM的异步支持已经在规划中。 在LangChain中可以通过agenerate方法去异步地调用OpenAI LLM。 import time
import asynciofrom langchain.llms import OpenAIdef generate_serially():llm OpenAI(temperature0.9)for _ in range(10):resp llm.generate([Hello, how are you?])print(resp.generations[0][0].text)async def async_generate(llm):resp await llm.agenerate([Hello, how are you?])print(resp.generations[0][0].text)async def generate_concurrently():llm OpenAI(temperature0.9)tasks [async_generate(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 generate_concurrently()
elapsed time.perf_counter() - s
print(fConcurrent executed in {elapsed:0.2f} seconds.)s time.perf_counter()
generate_serially()
elapsed time.perf_counter() - s
print(fSerial executed in {elapsed:0.2f} seconds.)
执行代码输出结果
Im doing well, thank you. How about you?Im doing well, thank you. How about you?Im doing well, how about you?Im doing well, thank you. How about you?Im doing well, thank you. How about you?Im doing well, thank you. How about yourself?Im doing well, thank you! How about you?Im doing well, thank you. How about you?Im doing well, thank you! How about you?Im doing well, thank you. How about you?Concurrent executed in 1.39 seconds.Im doing well, thank you. How about you?Im doing well, thank you. How about you?Im doing well, thank you. How about you?Im doing well, thank you. How about you?Im doing well, thank you. How about yourself?Im doing well, thanks for asking. How about you?Im doing well, thanks! How about you?Im doing well, thank you. How about you?Im doing well, thank you. How about yourself?Im doing well, thanks for asking. How about you?Serial executed in 5.77 seconds. 02 自定义LLM 通过自定义LLM你可以定义一个自己的LLM也可以基于LangChain已有的LLM封装成一个新的LLM。
封装一个LLM你唯一要实现的方法是_call这个方法接收一个字符串和一些可选的停止词然后返回一个字符串另外还有一个可选的_identifying_params属性你可以根据需要决定是否实现它主要作用是辅助这个类信息的打印输出。 下面我们动手实现一个自定义的LLM。
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}
这样就定义好了一个LLM接下来就可以使用它了。
llm CustomLLM(n10)
llm(This is a foobar thing
我们可以输出这个类的对象查看它的打印输出
print(llm)CustomLLMParams: {n: 10}因为我们实现了_identifying_params所以在打印输出这个对象时输出了相应的属性。 03 Fake LLM LangChain提供了一个伪造的LLM类可以用来调试通过模拟调用LLM当LLM以某种方式返回时模拟后续会发生什么。
下面我们通过agent的方式使用FakeLLM。 from langchain.llms.fake import FakeListLLM
from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.agents import AgentTypetools load_tools([python_repl])
responses [Action: Python REPL\nAction Input: print(2 2), Final Answer: 4]
llm FakeListLLM(responsesresponses)
agent initialize_agent(tools, llm, agentAgentType.ZERO_SHOT_REACT_DESCRIPTION, verboseTrue
)
agent.run(whats 2 2)
执行代码输出结果 Entering new AgentExecutor chain...Action: Python REPLAction Input: print(2 2)Observation: Python REPL is not a valid tool, try one of [Python_REPL].Thought:Final Answer: 4 Finished chain. 04 HumanInput LLM 和FakeLLM类似HumanInput LLM也是一个伪LLM类可以用来测试、调试等。也是通过模拟调用LLM然后模拟人类会如何响应。 同样我们通过agent的方式使用HumanInputLLM。
由于需要用到wikipedia首先安装一下 pip install wikipedia 然后创建HumanInputLLM。 from langchain.llms.human import HumanInputLLM
from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.agents import AgentTypetools load_tools([wikipedia])
llm HumanInputLLM(prompt_funclambda prompt: print(f\nPROMPT\n{prompt}\nEND OF PROMPT)
)
agent initialize_agent(tools, llm, agentAgentType.ZERO_SHOT_REACT_DESCRIPTION, verboseTrue
)
agent.run(What is Bocchi the Rock!?)
运行代码输出结果 Entering new AgentExecutor chain...PROMPTAnswer the following questions as best you can. You have access to the following tools:Wikipedia: A wrapper around Wikipedia. Useful for when you need to answer general questions about people, places, companies, historical events, or other subjects. Input should be a search query.Use the following format:Question: the input question you must answerThought: you should always think about what to doAction: the action to take, should be one of [Wikipedia]Action Input: the input to the actionObservation: the result of the action... (this Thought/Action/Action Input/Observation can repeat N times)Thought: I now know the final answerFinal Answer: the final answer to the original input questionBegin!Question: What is Bocchi the Rock!?Thought:END OF PROMPTI need to use a tool.Action: WikipediaAction Input: Bocchi the Rock!, Japanese four-panel manga and anime series.Observation: Page: Bocchi the Rock!Summary: Bocchi the Rock! (ぼっち・ざ・ろっく!, Bocchi Za Rokku!) is a Japanese four-panel manga series written and illustrated by Aki Hamaji. It has been serialized in Houbunshas seinen manga magazine Manga Time Kirara Max since December 2017. Its chapters have been collected in five tankōbon volumes as of November 2022.An anime television series adaptation produced by CloverWorks aired from October to December 2022. The series has been praised for its writing, comedy, characters, and depiction of social anxiety, with the animes visual creativity receiving acclaim.Page: Manga Time KiraraSummary: Manga Time Kirara (まんがタイムきらら, Manga Taimu Kirara) is a Japanese seinen manga magazine published by Houbunsha which mainly serializes four-panel manga. The magazine is sold on the ninth of each month and was first published as a special edition of Manga Time, another Houbunsha magazine, on May 17, 2002. Characters from this magazine have appeared in a crossover role-playing game called Kirara Fantasia.Page: Manga Time Kirara MaxSummary: Manga Time Kirara Max (まんがタイムきららMAX) is a Japanese four-panel seinen manga magazine published by Houbunsha. It is the third magazine of the Kirara series, after Manga Time Kirara and Manga Time Kirara Carat. The first issue was released on September 29, 2004. Currently the magazine is released on the 19th of each month.Thought:PROMPTAnswer the following questions as best you can. You have access to the following tools:Wikipedia: A wrapper around Wikipedia. Useful for when you need to answer general questions about people, places, companies, historical events, or other subjects. Input should be a search query.Use the following format:Question: the input question you must answerThought: you should always think about what to doAction: the action to take, should be one of [Wikipedia]Action Input: the input to the actionObservation: the result of the action... (this Thought/Action/Action Input/Observation can repeat N times)Thought: I now know the final answerFinal Answer: the final answer to the original input questionBegin!Question: What is Bocchi the Rock!?Thought:I need to use a tool.Action: WikipediaAction Input: Bocchi the Rock!, Japanese four-panel manga and anime series.Observation: Page: Bocchi the Rock!Summary: Bocchi the Rock! (ぼっち・ざ・ろっく!, Bocchi Za Rokku!) is a Japanese four-panel manga series written and illustrated by Aki Hamaji. It has been serialized in Houbunshas seinen manga magazine Manga Time Kirara Max since December 2017. Its chapters have been collected in five tankōbon volumes as of November 2022.An anime television series adaptation produced by CloverWorks aired from October to December 2022. The series has been praised for its writing, comedy, characters, and depiction of social anxiety, with the animes visual creativity receiving acclaim.Page: Manga Time KiraraSummary: Manga Time Kirara (まんがタイムきらら, Manga Taimu Kirara) is a Japanese seinen manga magazine published by Houbunsha which mainly serializes four-panel manga. The magazine is sold on the ninth of each month and was first published as a special edition of Manga Time, another Houbunsha magazine, on May 17, 2002. Characters from this magazine have appeared in a crossover role-playing game called Kirara Fantasia.Page: Manga Time Kirara MaxSummary: Manga Time Kirara Max (まんがタイムきららMAX) is a Japanese four-panel seinen manga magazine published by Houbunsha. It is the third magazine of the Kirara series, after Manga Time Kirara and Manga Time Kirara Carat. The first issue was released on September 29, 2004. Currently the magazine is released on the 19th of each month.Thought:END OF PROMPTThese are not relevant articles.Action: WikipediaAction Input: Bocchi the Rock!, Japanese four-panel manga series written and illustrated by Aki Hamaji.Observation: Page: Bocchi the Rock!Summary: Bocchi the Rock! (ぼっち・ざ・ろっく!, Bocchi Za Rokku!) is a Japanese four-panel manga series written and illustrated by Aki Hamaji. It has been serialized in Houbunshas seinen manga magazine Manga Time Kirara Max since December 2017. Its chapters have been collected in five tankōbon volumes as of November 2022.An anime television series adaptation produced by CloverWorks aired from October to December 2022. The series has been praised for its writing, comedy, characters, and depiction of social anxiety, with the animes visual creativity receiving acclaim.Thought:PROMPTAnswer the following questions as best you can. You have access to the following tools:Wikipedia: A wrapper around Wikipedia. Useful for when you need to answer general questions about people, places, companies, historical events, or other subjects. Input should be a search query.Use the following format:Question: the input question you must answerThought: you should always think about what to doAction: the action to take, should be one of [Wikipedia]Action Input: the input to the actionObservation: the result of the action... (this Thought/Action/Action Input/Observation can repeat N times)Thought: I now know the final answerFinal Answer: the final answer to the original input questionBegin!Question: What is Bocchi the Rock!?Thought:I need to use a tool.Action: WikipediaAction Input: Bocchi the Rock!, Japanese four-panel manga and anime series.Observation: Page: Bocchi the Rock!Summary: Bocchi the Rock! (ぼっち・ざ・ろっく!, Bocchi Za Rokku!) is a Japanese four-panel manga series written and illustrated by Aki Hamaji. It has been serialized in Houbunshas seinen manga magazine Manga Time Kirara Max since December 2017. Its chapters have been collected in five tankōbon volumes as of November 2022.An anime television series adaptation produced by CloverWorks aired from October to December 2022. The series has been praised for its writing, comedy, characters, and depiction of social anxiety, with the animes visual creativity receiving acclaim.Page: Manga Time KiraraSummary: Manga Time Kirara (まんがタイムきらら, Manga Taimu Kirara) is a Japanese seinen manga magazine published by Houbunsha which mainly serializes four-panel manga. The magazine is sold on the ninth of each month and was first published as a special edition of Manga Time, another Houbunsha magazine, on May 17, 2002. Characters from this magazine have appeared in a crossover role-playing game called Kirara Fantasia.Page: Manga Time Kirara MaxSummary: Manga Time Kirara Max (まんがタイムきららMAX) is a Japanese four-panel seinen manga magazine published by Houbunsha. It is the third magazine of the Kirara series, after Manga Time Kirara and Manga Time Kirara Carat. The first issue was released on September 29, 2004. Currently the magazine is released on the 19th of each month.Thought:These are not relevant articles.Action: WikipediaAction Input: Bocchi the Rock!, Japanese four-panel manga series written and illustrated by Aki Hamaji.Observation: Page: Bocchi the Rock!Summary: Bocchi the Rock! (ぼっち・ざ・ろっく!, Bocchi Za Rokku!) is a Japanese four-panel manga series written and illustrated by Aki Hamaji. It has been serialized in Houbunshas seinen manga magazine Manga Time Kirara Max since December 2017. Its chapters have been collected in five tankōbon volumes as of November 2022.An anime television series adaptation produced by CloverWorks aired from October to December 2022. The series has been praised for its writing, comedy, characters, and depiction of social anxiety, with the animes visual creativity receiving acclaim.Thought:END OF PROMPTIt worked.Final Answer: Bocchi the Rock! is a four-panel manga series and anime television series. The series has been praised for its writing, comedy, characters, and depiction of social anxiety, with the animes visual creativity receiving acclaim. Finished chain.Bocchi the Rock! is a four-panel manga series and anime television series. The series has been praised for its writing, comedy, characters, and depiction of social anxiety, with the animes visual creativity receiving acclaim. 本文小结
本文是LLMs的第一部分主要介绍了异步API、自定义LLM、FakeLLM和HumanInputLLM。 更多最新文章请关注公众号大白爱爬山