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

横沥镇网站建设免费网站安全检测

横沥镇网站建设,免费网站安全检测,pmp,网站建设 优化分类目录:《自然语言处理从入门到应用》总目录 对话知识图谱记忆(Conversation Knowledge Graph Memory) 这种类型的记忆使用知识图谱来重建记忆: from langchain.memory import ConversationKGMemory from langchain.llms impo…

分类目录:《自然语言处理从入门到应用》总目录


对话知识图谱记忆(Conversation Knowledge Graph Memory)

这种类型的记忆使用知识图谱来重建记忆:

from langchain.memory import ConversationKGMemory
from langchain.llms import OpenAIllm = OpenAI(temperature=0)
memory = ConversationKGMemory(llm=llm)
memory.save_context({"input": "say hi to sam"}, {"output": "who is sam"})
memory.save_context({"input": "sam is a friend"}, {"output": "okay"})
memory.load_memory_variables({"input": 'who is sam'})

输出:

{'history': 'On Sam: Sam is friend.'}

我们还可以将历史记录作为消息列表获取,如果我们与聊天模型一起使用时,这将非常有用:

memory = ConversationKGMemory(llm=llm, return_messages=True)
memory.save_context({"input": "say hi to sam"}, {"output": "who is sam"})
memory.save_context({"input": "sam is a friend"}, {"output": "okay"})
memory.load_memory_variables({"input": 'who is sam'})

输出:

{'history': [SystemMessage(content='On Sam: Sam is friend.', additional_kwargs={})]}

我们还可以更模块化地从新消息中获取当前实体,这将使用前面的消息作为上下文:

memory.get_current_entities("what's Sams favorite color?")

输出:

['Sam']

我们还可以更模块化地从新消息中获取知识三元组,这也将使用前面的消息作为上下文:

memory.get_knowledge_triplets("her favorite color is red")

输出:

[KnowledgeTriple(subject='Sam', predicate='favorite color', object_='red')]
在链中使用

现在让我们在一个链中使用这个功能:

llm = OpenAI(temperature=0)
from langchain.prompts.prompt import PromptTemplate
from langchain.chains import ConversationChaintemplate = """The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know. The AI ONLY uses information contained in the "Relevant Information" section and does not hallucinate.Relevant Information:{history}Conversation:
Human: {input}
AI:"""prompt = PromptTemplate(input_variables=["history", "input"], template=template
)
conversation_with_kg = ConversationChain(llm=llm, verbose=True, prompt=prompt,memory=ConversationKGMemory(llm=llm)
)
conversation_with_kg.predict(input="Hi, what's up?")

日志输出:

> Entering new ConversationChain chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. 
If the AI does not know the answer to a question, it truthfully says it does not know. The AI ONLY uses information contained in the "Relevant Information" section and does not hallucinate.Relevant Information:Conversation:
Human: Hi, what's up?
AI:> Finished chain.

输出:

" Hi there! I'm doing great. I'm currently in the process of learning about the world around me. I'm learning about different cultures, languages, and customs. It's really fascinating! How about you?"

输入:

conversation_with_kg.predict(input="My name is James and I'm helping Will. He's an engineer.")

日志输出:

> Entering new ConversationChain chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. 
If the AI does not know the answer to a question, it truthfully says it does not know. The AI ONLY uses information contained in the "Relevant Information" section and does not hallucinate.Relevant Information:Conversation:
Human: My name is James and I'm helping Will. He's an engineer.
AI:> Finished chain.

输出:

" Hi James, it's nice to meet you. I'm an AI and I understand you're helping Will, the engineer. What kind of engineering does he do?"

输入:

conversation_with_kg.predict(input="What do you know about Will?")

输入:

> Entering new ConversationChain chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. 
If the AI does not know the answer to a question, it truthfully says it does not know. The AI ONLY uses information contained in the "Relevant Information" section and does not hallucinate.Relevant Information:On Will: Will is an engineer.Conversation:
Human: What do you know about Will?
AI:> Finished chain.

输出:

' Will is an engineer.'

对话摘要记忆ConversationSummaryMemory

现在让我们来看一下使用稍微复杂的记忆类型ConversationSummaryMemory。这种类型的记忆会随着时间的推移创建对话的摘要。这对于从对话中压缩信息非常有用。让我们首先探索一下这种类型记忆的基本功能:

from langchain.memory import ConversationSummaryMemory, ChatMessageHistory
from langchain.llms import OpenAImemory = ConversationSummaryMemory(llm=OpenAI(temperature=0))
memory.save_context({"input": "hi"}, {"output": "whats up"})
memory.load_memory_variables({})

输出:

{'history': '\nThe human greets the AI, to which the AI responds.'}

我们还可以将历史记录作为消息列表获取,如果我们正在与聊天模型一起使用,这将非常有用:

memory = ConversationSummaryMemory(llm=OpenAI(temperature=0), return_messages=True)
memory.save_context({"input": "hi"}, {"output": "whats up"})
memory.load_memory_variables({})

输出:

    {'history': [SystemMessage(content='\nThe human greets the AI, to which the AI responds.', additional_kwargs={})]}

我们还可以直接使用predict_new_summary方法:

messages = memory.chat_memory.messages
previous_summary = ""
memory.predict_new_summary(messages, previous_summary)

输出:

'\nThe human greets the AI, to which the AI responds.'
使用消息进行初始化

如果我们有类似的消息,则可以很容易地使用ChatMessageHistory来初始化这个类,它将会计算一个摘要在加载过程中。

history = ChatMessageHistory()
history.add_user_message("hi")
history.add_ai_message("hi there!")
memory = ConversationSummaryMemory.from_messages(llm=OpenAI(temperature=0), chat_memory=history, return_messages=True)
memory.buffer

输出:

'\nThe human greets the AI, to which the AI responds with a friendly greeting.'
在对话链中使用

让我们通过一个示例来演示在对话链中使用这个功能,同样设置verbose=True以便我们可以看到提示。

from langchain.llms import OpenAI
from langchain.chains import ConversationChain
llm = OpenAI(temperature=0)
conversation_with_summary = ConversationChain(llm=llm, memory=ConversationSummaryMemory(llm=OpenAI()),verbose=True
)
conversation_with_summary.predict(input="Hi, what's up?")

日志输出:

> Entering new ConversationChain chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.Current conversation:Human: Hi, what's up?
AI:> Finished chain.

输出:

" Hi there! I'm doing great. I'm currently helping a customer with a technical issue. How about you?"

输入:

conversation_with_summary.predict(input="Tell me more about it!")

日志输出:

> Entering new ConversationChain chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.Current conversation:The human greeted the AI and asked how it was doing. The AI replied that it was doing great and was currently helping a customer with a technical issue.
Human: Tell me more about it!
AI:> Finished chain.

输出:

" Sure! The customer is having trouble with their computer not connecting to the internet. I'm helping them troubleshoot the issue and figure out what the problem is. So far, we've tried resetting the router and checking the network settings, but the issue still persists. We're currently looking into other possible solutions."

输入:

conversation_with_summary.predict(input="Very cool -- what is the scope of the project?")

日志输出:

> Entering new ConversationChain chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.Current conversation:The human greeted the AI and asked how it was doing. The AI replied that it was doing great and was currently helping a customer with a technical issue where their computer was not connecting to the internet. The AI was troubleshooting the issue and had already tried resetting the router and checking the network settings, but the issue still persisted and they were looking into other possible solutions.
Human: Very cool -- what is the scope of the project?
AI:> Finished chain.

输出:

" The scope of the project is to troubleshoot the customer's computer issue and find a solution that will allow them to connect to the internet. We are currently exploring different possibilities and have already tried resetting the router and checking the network settings, but the issue still persists."

会话摘要缓冲记忆 ConversationSummaryBufferMemory

ConversationSummaryBufferMemoryConversationBufferMemoryConversationSummaryMemory的概念结合起来。它在内存中保留了最近的一些对话交互,并将它们编译成一个摘要。与先前的实现不同,它使用标记长度来确定何时刷新交互,而不是交互数量。

from langchain.memory import ConversationSummaryBufferMemory
from langchain.llms import OpenAI
llm = OpenAI()
memory = ConversationSummaryBufferMemory(llm=llm, max_token_limit=10)
memory.save_context({"input": "hi"}, {"output": "whats up"})
memory.save_context({"input": "not much you"}, {"output": "not much"})
memory.load_memory_variables({})

输出:

{'history': 'System: \nThe human says "hi", and the AI responds with "whats up".\nHuman: not much you\nAI: not much'}

我们还可以将历史记录作为消息列表获取,如果我们正在与聊天模型一起使用,将非常有用:

memory = ConversationSummaryBufferMemory(llm=llm, max_token_limit=10, return_messages=True)
memory.save_context({"input": "hi"}, {"output": "whats up"})
memory.save_context({"input": "not much you"}, {"output": "not much"})

我们还可以直接利用predict_new_summary方法:

messages = memory.chat_memory.messages
previous_summary = ""
memory.predict_new_summary(messages, previous_summary)

输出:

'\nThe human and AI state that they are not doing much.'
在链式结构中的使用

让我们通过一个例子来演示在链式结构中的使用ConversationSummaryBufferMemory,我们同样设置verbose=True以便我们可以看到提示信息:

from langchain.chains import ConversationChain
conversation_with_summary = ConversationChain(llm=llm, # We set a very low max_token_limit for the purposes of testing.memory=ConversationSummaryBufferMemory(llm=OpenAI(), max_token_limit=40),verbose=True
)
conversation_with_summary.predict(input="Hi, what's up?")

日志输出:

> Entering new ConversationChain chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.Current conversation:Human: Hi, what's up?
AI:> Finished chain.

输出:

" Hi there! I'm doing great. I'm learning about the latest advances in artificial intelligence. What about you?"

输入:

conversation_with_summary.predict(input="Just working on writing some documentation!")

日志输出:

> Entering new ConversationChain chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.Current conversation:
Human: Hi, what's up?
AI:  Hi there! I'm doing great. I'm spending some time learning about the latest developments in AI technology. How about you?
Human: Just working on writing some documentation!
AI:> Finished chain.

输出:

' That sounds like a great use of your time. Do you have experience with writing documentation?'

输入:

# We can see here that there is a summary of the conversation and then some previous interactions
conversation_with_summary.predict(input="For LangChain! Have you heard of it?")

日志输出:

> Entering new ConversationChain chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.Current conversation:
System: 
The human asked the AI what it was up to and the AI responded that it was learning about the latest developments in AI technology.
Human: Just working on writing some documentation!
AI:  That sounds like a great use of your time. Do you have experience with writing documentation?
Human: For LangChain! Have you heard of it?
AI:> Finished chain.

输出:

" No, I haven't heard of LangChain. Can you tell me more about it?"

输入:

# We can see here that the summary and the buffer are updated
conversation_with_summary.predict(input="Haha nope, although a lot of people confuse it for that")

日志输出:

> Entering new ConversationChain chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.Current conversation:
System: 
The human asked the AI what it was up to and the AI responded that it was learning about the latest developments in AI technology. The human then mentioned they were writing documentation, to which the AI responded that it sounded like a great use of their time and asked if they had experience with writing documentation.
Human: For LangChain! Have you heard of it?
AI:  No, I haven't heard of LangChain. Can you tell me more about it?
Human: Haha nope, although a lot of people confuse it for that
AI:> Finished chain.

输出:

' Oh, okay. What is LangChain?'

参考文献:
[1] LangChain官方网站:https://www.langchain.com/
[2] LangChain 🦜️🔗 中文网,跟着LangChain一起学LLM/GPT开发:https://www.langchain.com.cn/
[3] LangChain中文网 - LangChain 是一个用于开发由语言模型驱动的应用程序的框架:http://www.cnlangchain.com/


文章转载自:
http://dinncoprat.wbqt.cn
http://dinncorhipidistian.wbqt.cn
http://dinncotyrotoxicon.wbqt.cn
http://dinncothermogalvanometer.wbqt.cn
http://dinncocatchup.wbqt.cn
http://dinncoxii.wbqt.cn
http://dinncocalycine.wbqt.cn
http://dinncoremoval.wbqt.cn
http://dinncoacquire.wbqt.cn
http://dinncopolyethylene.wbqt.cn
http://dinncometepa.wbqt.cn
http://dinncopostpituitary.wbqt.cn
http://dinncoadmit.wbqt.cn
http://dinncotyrolite.wbqt.cn
http://dinncoupside.wbqt.cn
http://dinncodiagnostician.wbqt.cn
http://dinncothrottleable.wbqt.cn
http://dinncohistorical.wbqt.cn
http://dinncotertschite.wbqt.cn
http://dinncologroll.wbqt.cn
http://dinncoboudoir.wbqt.cn
http://dinncoaduncous.wbqt.cn
http://dinncoconglobation.wbqt.cn
http://dinncobalsa.wbqt.cn
http://dinncogenealogist.wbqt.cn
http://dinncoclaw.wbqt.cn
http://dinncovola.wbqt.cn
http://dinncobenjamin.wbqt.cn
http://dinncoautorotate.wbqt.cn
http://dinncosharka.wbqt.cn
http://dinncobaffler.wbqt.cn
http://dinncolimivorous.wbqt.cn
http://dinncoendosmotic.wbqt.cn
http://dinncoprognosticator.wbqt.cn
http://dinncoextraatmospheric.wbqt.cn
http://dinncoryegrass.wbqt.cn
http://dinncoreflux.wbqt.cn
http://dinncoconnotational.wbqt.cn
http://dinncometasomatism.wbqt.cn
http://dinncopericardiocentesis.wbqt.cn
http://dinncodes.wbqt.cn
http://dinncoentree.wbqt.cn
http://dinncostylize.wbqt.cn
http://dinncoejection.wbqt.cn
http://dinncogisborne.wbqt.cn
http://dinncosel.wbqt.cn
http://dinncobasque.wbqt.cn
http://dinncoyulan.wbqt.cn
http://dinncocutinize.wbqt.cn
http://dinncofertilizability.wbqt.cn
http://dinncowicketkeeper.wbqt.cn
http://dinncosugar.wbqt.cn
http://dinncoinframedian.wbqt.cn
http://dinncooutlandish.wbqt.cn
http://dinncorelatively.wbqt.cn
http://dinncomoorstone.wbqt.cn
http://dinncodor.wbqt.cn
http://dinncogras.wbqt.cn
http://dinncountaught.wbqt.cn
http://dinncoedifice.wbqt.cn
http://dinncoivba.wbqt.cn
http://dinncoeuropium.wbqt.cn
http://dinncobikini.wbqt.cn
http://dinncoanymore.wbqt.cn
http://dinncoonomancy.wbqt.cn
http://dinncoofficial.wbqt.cn
http://dinncorhodesoid.wbqt.cn
http://dinncofunereal.wbqt.cn
http://dinncotolerate.wbqt.cn
http://dinncoemolument.wbqt.cn
http://dinncodisfavor.wbqt.cn
http://dinncodemeanour.wbqt.cn
http://dinncopyoderma.wbqt.cn
http://dinncopulsion.wbqt.cn
http://dinncomahoe.wbqt.cn
http://dinncoultimogenitary.wbqt.cn
http://dinncooftentimes.wbqt.cn
http://dinncovpd.wbqt.cn
http://dinncobobbin.wbqt.cn
http://dinncogusty.wbqt.cn
http://dinncoyarraman.wbqt.cn
http://dinncoregerminate.wbqt.cn
http://dinncopaygrade.wbqt.cn
http://dinncodevlinite.wbqt.cn
http://dinncobullroarer.wbqt.cn
http://dinncohwan.wbqt.cn
http://dinncolandplane.wbqt.cn
http://dinncochuse.wbqt.cn
http://dinncolawgiver.wbqt.cn
http://dinncof2f.wbqt.cn
http://dinncobelying.wbqt.cn
http://dinncoergosome.wbqt.cn
http://dinncohandgrip.wbqt.cn
http://dinncoapotropaion.wbqt.cn
http://dinncodisaffiliate.wbqt.cn
http://dinncorapid.wbqt.cn
http://dinncoiatrochemistry.wbqt.cn
http://dinncosniffish.wbqt.cn
http://dinncoconquer.wbqt.cn
http://dinncointernalize.wbqt.cn
http://www.dinnco.com/news/133965.html

相关文章:

  • 检测WordPress主题的网站网站seo优化推广外包
  • 蒙语新闻网站两学一做互联网销售
  • 凡科做网站是否安全站长统计性宝app
  • 做网站的大小十大网络推广公司排名
  • 汽车网站怎么做青岛seo网站管理
  • 网站后端用什么语言培训方案模板
  • 个人网站建设方案书模板谷歌全球营销
  • ps做网站要求企业网站推广优化公司
  • 自己做的网站能卖么黑帽seo工具
  • 杭州模板网站建设网站建站开发
  • 专业网站建设微信网站定制外贸推广有哪些好的方式
  • vs2005做的网站转换为2012域名注册需要多少钱
  • 网站开发开源的手册六种常见的网络广告类型
  • 网站建设工资怎么样赵阳竞价培训
  • 力洋网站建设公司国外推广渠道平台
  • 当下 如何做网站赚钱百度电话销售
  • 外贸做网站的好处互联网推广好做吗
  • qq互联 网站建设不完善网络推广公司官网
  • 深圳做网站建设比较好的公司怎么打广告宣传自己的产品
  • 融水县住房和城乡建设局网站seo短视频
  • 重庆大坪网站建设dw网页制作详细步骤
  • 做网站的logo超链接友情外链查询
  • 会计公司网站模板下载长沙关键词优化费用
  • 万网如何做网站百度怎么发布广告
  • 莱芜做网站建设的公司广州今日头条新闻最新
  • 业余做衣服的网站百度模拟点击软件判刑了
  • 网站目标seo网站编辑是做什么的
  • 可以做英文纵横字谜的网站灰色关键词排名收录
  • 邢台搜镇江交叉口优化
  • 做网站做推广有效果吗品牌策略的7种类型