Langchain入门1

Langchain的基本思想

将大语言模型的输入到执行联合起来以实现一个快速的应用搭建

简单的demo

实验均在谷歌的Colaboratory平台上进行
免费的Openai_ai_key来源于github的GPT_API_free仓库

demo_1:展示如何调用openai的大模型

from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
	api_key = "sk_" ,					#这里需要填上申请的apikey
	base_url = "https://api.chatanywhere.tech/v1"  #GPT_API_free的代理仓库
)

res = llm.invoke("how can langsmith help with teseting?")

输出的结果如下,包含了很多的信息,不仅有GPT的回复信息,还有GPT的很多相关信息。

AIMessage(content='Langsmith can help with testing by providing automated testing tools that can quickly and efficiently test code for bugs and errors. It can also help with generating test cases, analyzing test results, and identifying areas of improvement in the code. Additionally, Langsmith can assist with performance testing, security testing, and integration testing to ensure that the code is robust and reliable.', response_metadata={'token_usage': {'completion_tokens': 71, 'prompt_tokens': 15, 'total_tokens': 86}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': 'fp_b28b39ffa8', 'finish_reason': 'stop', 'logprobs': None}, id='run-24ad6708-64f4-4912-b056-0f00ebf0d3bc-0')

demo_2:如何定义输入模板并进行链式连接

from langchain_core.prompts import ChatPromptTemplate

prompt = ChatTemplate.from_message([
	('system','You are world class technical documentation writer.')
	('user','{input}')
])

chain1 = prompt | llm #链式连接,类似于管道,将prompt输入到llm中
res1 = chain1.invoke({"input":'what can langsmith help with writer?'})

下面的输出结果和上面的一致,在有回复的结果的同时,还包含了各种信息。

AIMessage(content='Langsmith can help with testing in various ways, including:\n\n1. Test automation: Langsmith can automate the testing process by generating test scripts based on natural language test cases. This can help speed up the testing process and ensure consistent and thorough test coverage.\n\n2. Test case generation: Langsmith can assist in generating test cases based on user stories or requirements written in natural language. This can help ensure that all possible scenarios are considered during testing.\n\n3. Test data generation: Langsmith can help in generating test data for different test scenarios, which can be used to validate the functionality of the software under test.\n\n4. Test execution: Langsmith can also be used to execute test scripts and provide feedback on test results, helping to identify issues and bugs in the software.\n\nOverall, Langsmith can improve the efficiency and effectiveness of the testing process by automating tasks, generating test cases, and providing valuable insights during testing.', response_metadata={'token_usage': {'completion_tokens': 184, 'prompt_tokens': 27, 'total_tokens': 211}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': 'fp_b28b39ffa8', 'finish_reason': 'stop', 'logprobs': None}, id='run-71d275bf-dc1e-4c82-bb45-d5d25cc9d347-0')

demo_3:Str解析器实现多链

在上面的输出中,其实我们并不关心输出的各种信息,作为对话模型,我们最关心的还是回复的结果。如果我们只需要提取出回复的结果,直接使用res.content是一个很好的方法。在langchain中,还提出了输出解析器,用于提取回复的结果。

from langchain_core.output_parsers import StrOutputParser

output_parser = StrOutputParser()

chain1 = prompt | llm | output_parser

res2 = chain1.invoke({"input":"how can langsmith help with testing?"})

结果如下,只包含回复信息,没有其他额外的信息。

Langsmith can help with testing in several ways:\n\n1. Automated Testing: Langsmith can generate test code in various languages to automate the testing process. This can save time and effort by eliminating the need to write test scripts manually.\n\n2. Test Data Generation: Langsmith can create realistic test data to ensure comprehensive test coverage. This can help in identifying edge cases and potential issues early in the testing phase.\n\n3. Test Case Generation: Langsmith can generate test cases based on requirements and specifications, ensuring that all scenarios are covered during testing.\n\n4. Regression Testing: Langsmith can assist in automating regression testing to quickly identify any regressions or defects introduced in the codebase.\n\n5. Integration Testing: Langsmith can help in generating test code for integration testing to ensure that different components of the software work seamlessly together.\n\nOverall, Langsmith can streamline the testing process, improve tes

上面这样做的好处是,可以直接利用llm的输出,而不用进行信息处理。举一个简单的例子。

prompt1 = ChatPromptTemplate.from_template("tell me a story about {what1} and {what2}")
prompt2 = ChatPromptTemplate.from_template("This is a {story}.please tell me Who does this story happen to?")

结果如下:

This story happens to a young woman named Sarah and her two dogs, Buddy and Max.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值