深圳建站網(wǎng)站模板網(wǎng)絡(luò)營(yíng)銷(xiāo)有哪些手段
模型(Models)
- LLMs
大型語(yǔ)言模型,將文本字符串作為輸入,并返回文本字符串作為輸出。
- 聊天模型
聊天模型通常由語(yǔ)言模型支持,但它們的API更加結(jié)構(gòu)化。這些模型將聊天消息列表作為輸入,并返回聊天消息。
- 文本嵌入模型
文本嵌入模型將文本作為輸入,并返回一個(gè)浮點(diǎn)數(shù)列表,常見(jiàn)的嵌入集成:OpenAI。
LLM從語(yǔ)言模型中獲取預(yù)測(cè),LangChain最基本的構(gòu)建塊是對(duì)某些輸入調(diào)用LLM。
- 首先導(dǎo)入LLM包裝器:
from langchain.llms import OpenAI
- 然后用參數(shù)初始化包裝器,如果希望輸出更加隨機(jī),初始化溫度(temperature)即可:
llm = OpenAI(temperature=0.9)
- 最后可以根據(jù)輸入來(lái)調(diào)用它:
text = "What would be a good company name for a company that makes colorful socks?" print(llm(text))
提示工程(Prompts)
提示模板(PromptTemplate):管理LLM的提示
from langchain.prompts import PromptTemplateprompt = PromptTemplate(input_variables=["product"],template="What is a good name for a company that makes {product}?",
)print(prompt.format(product="colorful socks"))What is a good name for a company that makes colorful socks?
鏈(Chains)
- 在多步驟的工作流中組合LLM和提示
- 在LangChain中,鏈?zhǔn)怯涉溄M成的,可以是LLM這樣的原始鏈,也可以是其他鏈。
- 最核心的鏈類(lèi)型是
LLMChain
,它由PromptTemplate
和LLM
組成。- 接受用戶(hù)輸入,使用 PromptTemplate 對(duì)其進(jìn)行格式化,然后將格式化后的響應(yīng)傳遞給
LLM
。
from langchain.prompts import PromptTemplate
from langchain.llms import OpenAIllm = OpenAI(temperature=0.9)
prompt = PromptTemplate(input_variables=["product"],template="What is a good name for a company that makes {product}?",
)
現(xiàn)在可以創(chuàng)建一個(gè)簡(jiǎn)單的鏈,它接受用戶(hù)輸入,用它格式化提示符,然后將它發(fā)送到 LLM:
from langchain.chains import LLMChain
chain = LLMChain(llm=llm, prompt=prompt)chain.run("colorful socks")
# -> '\n\nSocktastic!'
代理(Agents)
基于用戶(hù)輸入的動(dòng)態(tài)調(diào)用鏈,通常鏈運(yùn)行在一個(gè)預(yù)先確定的順序,但是代理使用LLM來(lái)確定要執(zhí)行哪些操作以及按照什么順序執(zhí)行。操作可以使用工具并觀察其輸出,也可以返回給用戶(hù)。
代理相關(guān)基本概念:
- 工具(tools):執(zhí)行特定任務(wù)的功能??梢允?#xff1a;Google 搜索、數(shù)據(jù)庫(kù)查找、Python REPL、其他鏈等。工具的接口目前是一個(gè)函數(shù),預(yù)計(jì)將有一個(gè)字符串作為輸入,一個(gè)字符串作為輸出。
- 大語(yǔ)言模型(LLM):為代理提供動(dòng)力的語(yǔ)言模型。
- 代理(agents):要使用的代理,是引用支持代理類(lèi)的字符串。
安裝SerpAPI Python包:pip install google-search-results
設(shè)置適當(dāng)?shù)沫h(huán)境變量:import osos.environ["SERPAPI_API_KEY"] = "..."
from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.agents import AgentType
from langchain.llms import OpenAI# First, let's load the language model we're going to use to control the agent.
llm = OpenAI(temperature=0)# Next, let's load some tools to use. Note that the `llm-math` tool uses an LLM, so we need to pass that in.
tools = load_tools(["serpapi", "llm-math"], llm=llm)# Finally, let's initialize an agent with the tools, the language model, and the type of agent we want to use.
agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)# Now let's test it out!
agent.run("What was the high temperature in SF yesterday in Fahrenheit? What is that number raised to the .023 power?")
記憶存儲(chǔ)(Memory)
向鏈和代理添加狀態(tài):
- 通常所有工具和代理都是無(wú)狀態(tài)的。
- 如果鏈或代理具有某種“內(nèi)存”概念,以便它可以記住關(guān)于其以前的交互的信息,這樣它就可以利用這些消息的上下文來(lái)進(jìn)行更好的對(duì)話,這是一種“短期記憶”。
- 如果鏈條/代理隨著時(shí)間的推移記住關(guān)鍵信息,這將是一種形式的“長(zhǎng)期記憶”。
- LangChain提供了鏈(
ConversationChain)
和兩種不同類(lèi)型的內(nèi)存來(lái)完成操作。
默認(rèn)情況下,ConversationChain
有個(gè)簡(jiǎn)單的內(nèi)存類(lèi)型,它記住所有以前的輸入/輸出,并將它們添加到傳遞的上下文中,(設(shè)置verbose=True,
可以看到提示符)。
from langchain import OpenAI, ConversationChain
llm = OpenAI(temperature=0)
conversation = ConversationChain(llm=llm, verbose=True)
output = conversation.predict(input="Hi there!")
print(output)output = conversation.predict(input="I'm doing well! Just having a conversation with an AI.")
print(output)
索引(Indexes)
- 索引是指構(gòu)造文檔的方法,以便LLM可以最好地與它們交互。此模塊包含用于處理文檔的實(shí)用工具函數(shù)、不同類(lèi)型的索引,以及在鏈中使用這些索引的示例。
- 在鏈中使用索引的最常見(jiàn)方式是“檢索”步驟。接受用戶(hù)的查詢(xún)并返回最相關(guān)的文檔。索引可以用于檢索之外的其他事情,檢索可以使用索引之外的其他邏輯來(lái)查找相關(guān)文檔。
- 大多數(shù)時(shí)候,談?wù)撍饕蜋z索時(shí),談?wù)摰氖撬饕蜋z索非結(jié)構(gòu)化數(shù)據(jù),如文本文檔。
- LangChain支持的主要索引和檢索類(lèi)型目前主要集中在向量數(shù)據(jù)庫(kù)上。
- 文檔加載器(Document Loaders),文檔加載程序,如何從各種源加載文檔。
from langchain.document_loaders.csv_loader import CSVLoaderloader = CSVLoader(file_path='./example_data/mlb_teams_2012.csv')
data = loader.load()
- 文本分割器(Text Splitters),文字分割器,關(guān)于分割文本的抽象和實(shí)現(xiàn)的概述。
from langchain.text_splitter import CharacterTextSplitter
text_splitter = CharacterTextSplitter( separator = " ",chunk_size = 1000,chunk_overlap = 200,length_function = len,
)texts = text_splitter.create_documents([state_of_the_union])
- 向量存儲(chǔ)(Vectorstores),概述Vector Stores和LangChain提供的許多集成。
import os
import getpassos.environ['OPENAI_API_KEY'] = getpass.getpass('OpenAI API Key:')from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.text_splitter import CharacterTextSplitter
from langchain.vectorstores import Milvus
from langchain.document_loaders import TextLoaderfrom langchain.document_loaders import TextLoader
loader = TextLoader('../../../state_of_the_union.txt')
documents = loader.load()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
docs = text_splitter.split_documents(documents)embeddings = OpenAIEmbeddings()vector_db = Milvus.from_documents(docs,embeddings,connection_args={"host": "127.0.0.1", "port": "19530"},
)docs = vector_db.similarity_search(query)
- 檢索器(Retrievers),檢索器概述和LangChain提供的實(shí)現(xiàn)。
from langchain.retrievers import ChatGPTPluginRetrieverretriever = ChatGPTPluginRetriever(url="http://0.0.0.0:8000", bearer_token="foo")
retriever.get_relevant_documents("alice's phone number")