赞
踩
pip install langchain
langsmith官方提示是用来观察大模型复杂调用情况,可选项。
[LangSmith]点击注册然后把秘钥填进去就行,这里我略过了
export LANGCHAIN_TRACING_V2="true"
export LANGCHAIN_API_KEY="..."
安装langchain-openai
pip install langchain-openai
设置秘钥
export OPENAI_API_KEY="..."
使用前初始化
from langchain_openai import ChatOpenAI
llm = ChatOpenAI()
一旦你已经安装并初始化了所选的大型语言模型(LLM),我们就可以尝试使用它了!让我们问它"LangSmith是什么"——这是训练数据中不存在的内容,所以它可能不会有很好的回答。
llm.invoke("how can langsmith help with testing?")
我们还可以使用prompt提示模板来引导它的响应。提示模板将原始用户输入转换为更适合大型语言模型(LLM)的输入。
from langchain_core.prompts import ChatPromptTemplate
prompt = ChatPromptTemplate.from_messages([ ("system", "You are a world class technical documentation writer."), ("user", "{input}")])
我们可以直接传递文档来自己运行这个流程:
from langchain_core.documents import Document
document_chain.invoke({
"input": "how can langsmith help with testing?",
"context": [Document(page_content="langsmith can let you visualize test results")]
})
prompt和llm一起使用
from langchain_core.output_parsers import StrOutputParser
output_parser = StrOutputParser()
// prompt 是提示,llm|output_parser将大语言模型输出的结构化chatmodel 变为字符串输出
chain = prompt | llm | output_parser
chain.invoke({"input": "how can langsmith help with testing?"})
目前整理如下,注意api_key要替换成自己的,并且有余额才行,无余额会报错
from langchain_openai import ChatOpenAI llm = ChatOpenAI() from langchain_core.prompts import ChatPromptTemplate prompt = ChatPromptTemplate.from_messages([ ("system", "You are a world class technical documentation writer."), ("user", "{input}") ]) from langchain_core.output_parsers import StrOutputParser output_parser = StrOutputParser() chain = prompt | llm | output_parser result = chain.invoke({"input": "how can langsmith help with testing?"}) print(result)
我这里打印的结果是这样的(貌似每次输出都不一样啊,这里仅供参考)
Langsmith can help with testing in several ways:
Automated Testing: Langsmith can be used to generate test data for automated testing scripts. By creating realistic and diverse test data, Langsmith can help ensure comprehensive test coverage.
Performance Testing: Langsmith can generate large volumes of data to simulate real-world usage scenarios, allowing for performance testing of systems and applications under load.
Data Validation: Langsmith can be used to validate the accuracy and integrity of data by generating test data sets that cover various edge cases and boundary conditions.
Regression Testing: Langsmith can help streamline the testing process by quickly generating test data for regression testing, ensuring that new code changes do not introduce unexpected bugs or issues.
Overall, Langsmith can be a valuable tool for testing teams to improve the efficiency and effectiveness of their testing processes.
问题
注意chantgpt 需要有付费账号并有余额才行,否则会报错如下
openai.RateLimitError: Error code: 429 - {'error': {'message': 'You exceeded your current quota, please check your plan and billing details. For more information on this error, read the docs: https://platform.openai.com/docs/guides/error-codes/api-errors.', 'type': 'insufficient_quota', 'param': None, 'code': 'insufficient_quota'}}
为了恰当地回答原始问题(“langsmith如何帮助测试?”),我们需要为大型语言模型(LLM)提供额外的上下文。这可以通过检索来实现。当你有太多数据无法直接传递给LLM时,检索就很有用。然后,你可以使用检索器仅获取最相关的部分并将其传递进去。
在这个过程中,我们将从检索器中查找相关文档,然后将它们传递给提示。检索器可以由任何内容支持——SQL表、互联网等——但在这个例子中,我们将填充一个向量存储并使用它作为检索器。有关向量存储的更多信息,请参阅相关文档。
首先,我们需要加载我们想要索引的数据。为此,我们将使用WebBaseLoader。这需要安装BeautifulSoup:
pip install beautifulsoup4
之后我们可以导入并使用它
from langchain_community.document_loaders import WebBaseLoader
loader = WebBaseLoader("https://docs.smith.langchain.com/user_guide")
docs = loader.load()
接下来,我们需要将数据索引到向量存储中。这需要几个组件,即[embedding model]。
对于嵌入模型,我们再次提供通过API访问或运行本地模型的示例。 以openAI 为例
from langchain_openai import OpenAIEmbeddings
embeddings = OpenAIEmbeddings()
安装向量数据库
pip install faiss-cpu
有了向量数据库我们就可以构建索引了
from langchain_community.vectorstores import FAISS
from langchain_text_splitters import RecursiveCharacterTextSplitter
text_splitter = RecursiveCharacterTextSplitter()
documents = text_splitter.split_documents(docs)
vector = FAISS.from_documents(documents, embeddings)
既然我们已经将数据索引到向量存储中,接下来我们将创建一个检索链。这个链将接受一个输入问题,查找相关文档,然后将这些文档与原始问题一起传递给大型语言模型(LLM),并请求它回答原始问题。
首先,让我们设置这个链,该链将接受一个问题以及检索到的文档,并生成一个答案。
from langchain.chains.combine_documents import create_stuff_documents_chain prompt = ChatPromptTemplate.from_template("""Answer the following question based only on the provided context: <context> {context} </context> Question: {input}""") from langchain_openai import ChatOpenAI llm = ChatOpenAI() document_chain = create_stuff_documents_chain(llm, prompt)
然而,我们希望文档首先来自我们刚刚设置的检索器。这样,我们就可以使用检索器动态选择最相关的文档,并将这些文档传递给给定的问题。
from langchain.chains import create_retrieval_chain
retriever = vector.as_retriever()
retrieval_chain = create_retrieval_chain(retriever, document_chain)
我们现在可以调用这个链。这将返回一个字典——大型语言模型(LLM)的响应位于“answer”键中。
response = retrieval_chain.invoke({"input": "how can langsmith help with testing?"})
print(response["answer"])
# LangSmith offers several features that can help with testing:...
最终我这里打印的返回是这样的(貌似每次输出都不一样啊,这里仅供参考):
LangSmith can help with testing in several ways:
Prototyping: LangSmith allows for quick experimentation between prompts, model types, retrieval strategies, and other parameters, enabling rapid understanding of how the model is performing and debugging where it is failing during the prototyping phase.
Debugging: LangSmith tracing provides clear visibility and debugging information at each step of an LLM sequence, making it easier to identify and root-cause issues when things go wrong.
Initial Test Set: Developers can create datasets and use them to run tests on their LLM applications. LangSmith also facilitates running custom evaluations to score test results.
Comparison View: LangSmith offers a user-friendly comparison view for test runs to track and diagnose regressions in test scores across multiple revisions of an application.
Playground: LangSmith provides a playground environment for rapid iteration and experimentation, allowing developers to quickly test out different prompts and models, and log every playground run in the system for future use.
Beta Testing: LangSmith enables the collection of data on how LLM applications are performing in real-world scenarios, aiding in the curation of test cases to track regressions/improvements and the development of automatic evaluations.
Capturing Feedback: Users can gather human feedback on the responses produced by their applications and attach feedback scores to logged traces, then filter on traces that have a specific feedback tag and score.
Adding Runs to a Dataset: LangSmith enables the addition of runs as examples to datasets, expanding test coverage on real-world scenarios as the application progresses through the beta testing phase.
Overall, LangSmith supports testing by providing visibility, debugging tools, test creation and execution capabilities, comparison views, and environments for rapid iteration and experimentation.
到目前为止,我们创建的链只能回答单个问题。人们正在构建的LLM应用程序的主要类型之一是聊天机器人。那么我们如何将这个链变成一个可以回答后续问题的链呢? 我们仍然可以使用 create_retrieval_chain 函数,但我们需要更改两件事:
为了更新检索,我们将创建一个新的链。这个链将接收最新的输入(input)和对话历史(chat_history),并使用LLM生成搜索查询。
from langchain.chains import create_history_aware_retriever
from langchain_core.prompts import MessagesPlaceholder
# First we need a prompt that we can pass into an LLM to generate this search query
prompt = ChatPromptTemplate.from_messages([
MessagesPlaceholder(variable_name="chat_history"),
("user", "{input}"),
("user", "Given the above conversation, generate a search query to look up to get information relevant to the conversation")
])
retriever_chain = create_history_aware_retriever(llm, retriever, prompt)
试用一下更新后检索器的效果
from langchain_core.messages import HumanMessage, AIMessage
chat_history = [HumanMessage(content="Can LangSmith help test my LLM applications?"), AIMessage(content="Yes!")]
retriever_chain.invoke({
"chat_history": chat_history,
"input": "Tell me how"
})
如果输出没问题,那么我们生成一个新的检索链
prompt = ChatPromptTemplate.from_messages([
("system", "Answer the user's questions based on the below context:\n\n{context}"),
MessagesPlaceholder(variable_name="chat_history"),
("user", "{input}"),
])
document_chain = create_stuff_documents_chain(llm, prompt)
retrieval_chain = create_retrieval_chain(retriever_chain, document_chain)
调用新的检索链
chat_history = [HumanMessage(content="Can LangSmith help test my LLM applications?"), AIMessage(content="Yes!")]
retrieval_chain.invoke({
"chat_history": chat_history,
"input": "Tell me how"
})
输出如下(貌似每次输出都不一样啊,这里仅供参考):
LangSmith can help test your LLM applications by providing features like creating datasets for test cases, running custom evaluations, offering comparison views for different configurations, providing a playground environment for rapid iteration and experimentation, supporting beta testing with feedback collection and annotation queues, enabling feedback scoring on logged traces, allowing annotation of traces with different criteria, adding runs to datasets for real-world scenario coverage, and offering monitoring, A/B testing, automations, and thread views for multi-turn interactions.
我在一线互联网企业工作十余年里,指导过不少同行后辈。帮助很多人得到了学习和成长。
我意识到有很多经验和知识值得分享给大家,也可以通过我们的能力和经验解答大家在人工智能学习中的很多困惑,所以在工作繁忙的情况下还是坚持各种整理和分享。但苦于知识传播途径有限,很多互联网行业朋友无法获得正确的资料得到学习提升,故此将并将重要的AI大模型资料包括AI大模型入门学习思维导图、精品AI大模型学习书籍手册、视频教程、实战学习等录播视频免费分享出来。
第一阶段: 从大模型系统设计入手,讲解大模型的主要方法;
第二阶段: 在通过大模型提示词工程从Prompts角度入手更好发挥模型的作用;
第三阶段: 大模型平台应用开发借助阿里云PAI平台构建电商领域虚拟试衣系统;
第四阶段: 大模型知识库应用开发以LangChain框架为例,构建物流行业咨询智能问答系统;
第五阶段: 大模型微调开发借助以大健康、新零售、新媒体领域构建适合当前领域大模型;
第六阶段: 以SD多模态大模型为主,搭建了文生图小程序案例;
第七阶段: 以大模型平台应用与开发为主,通过星火大模型,文心大模型等成熟大模型构建大模型行业应用。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。