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

中铁建设门户网员工登录搜索引擎优化是指

中铁建设门户网员工登录,搜索引擎优化是指,网站备案的主体变更怎么做,如何做热词网站分类目录:《自然语言处理从入门到应用》总目录 自定义对话记忆 本节介绍了几种自定义对话记忆的方法: from langchain.llms import OpenAI from langchain.chains import ConversationChain from langchain.memory import ConversationBufferMemoryllm…

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


自定义对话记忆

本节介绍了几种自定义对话记忆的方法:

from langchain.llms import OpenAI
from langchain.chains import ConversationChain
from langchain.memory import ConversationBufferMemoryllm = OpenAI(temperature=0)
AI前缀

第一种方法是通过更改对话摘要中的AI前缀来实现。默认情况下,它设置为AI,但你可以将其设置为任何你想要的内容。需要注意的是,如果我们更改了这个前缀,我们还应该相应地更改链条中使用的提示来反映这个命名更改。让我们通过下面的示例来演示这个过程。

# Here it is by default set to "AI"
conversation = ConversationChain(llm=llm, verbose=True, memory=ConversationBufferMemory()
)
conversation.predict(input="Hi there!")

日志输出:

> 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 there!
AI:> Finished ConversationChain chain.

输出:

" Hi there! It's nice to meet you. How can I help you today?"

输入:

conversation.predict(input="What's the weather?")

日志输出:

> 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 there!
AI:  Hi there! It's nice to meet you. How can I help you today?
Human: What's the weather?
AI:> Finished ConversationChain chain.

输出:

' The current weather is sunny and warm with a temperature of 75 degrees Fahrenheit. The forecast for the next few days is sunny with temperatures in the mid-70s.'

输入:

# Now we can override it and set it to "AI Assistant"
from langchain.prompts.prompt import PromptTemplatetemplate = """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:
{history}
Human: {input}
AI Assistant:"""
PROMPT = PromptTemplate(input_variables=["history", "input"], template=template
)
conversation = ConversationChain(prompt=PROMPT,llm=llm, verbose=True, memory=ConversationBufferMemory(ai_prefix="AI Assistant")
)
conversation.predict(input="Hi there!")

日志输出:

> 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 there!
AI Assistant:> Finished ConversationChain chain.
" Hi there! It's nice to meet you. How can I help you today?"
conversation.predict(input="What's the weather?")
> 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 there!
AI Assistant:  Hi there! It's nice to meet you. How can I help you today?
Human: What's the weather?
AI Assistant:> Finished ConversationChain chain.

输出:

The current weather is sunny and warm with a temperature of 75 degrees Fahrenheit. The forecast for the rest of the day is sunny with a high of 78 degrees and a low of 65 degrees.'
人类前缀

第二种方法是通过更改对话摘要中的人类前缀来实现。默认情况下,它设置为Human,但我们可以将其设置为任何我们想要的内容。需要注意的是,如果我们更改了这个前缀,我们还应该相应地更改链条中使用的提示来反映这个命名更改。让我们通过下面的示例来演示这个过程。

# Now we can override it and set it to "Friend"
from langchain.prompts.prompt import PromptTemplatetemplate = """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:
{history}
Friend: {input}
AI:"""
PROMPT = PromptTemplate(input_variables=["history", "input"], template=template
)
conversation = ConversationChain(prompt=PROMPT,llm=llm, verbose=True, memory=ConversationBufferMemory(human_prefix="Friend")
)
conversation.predict(input="Hi there!")

日志输出:

> 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:Friend: Hi there!
AI:> Finished ConversationChain chain.
" Hi there! It's nice to meet you. How can I help you today?"
conversation.predict(input="What's the weather?")
> 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:Friend: Hi there!
AI:  Hi there! It's nice to meet you. How can I help you today?
Friend: What's the weather?
AI:> Finished ConversationChain chain.

输出:

  ' The weather right now is sunny and warm with a temperature of 75 degrees Fahrenheit. The forecast for the rest of the day is mostly sunny with a high of 82 degrees.'

创建自定义记忆类

尽管在LangChain中有几种预定义的记忆类型,但我们很可能希望添加自己的记忆类型,以使其适用于我们的应用程序。在本节中,我们将向ConversationChain添加一个自定义的记忆类型。为了添加自定义的记忆类,我们需要导入基本的记忆类并对其进行子类化。

from langchain import OpenAI, ConversationChain
from langchain.schema import BaseMemory
from pydantic import BaseModel
from typing import List, Dict, Any

在这个示例中,我们将编写一个自定义的记忆类,使用spacy提取实体并将有关它们的信息保存在一个简单的哈希表中。然后,在对话过程中,我们将查看输入文本,提取任何实体,并将关于它们的任何信息放入上下文中。需要注意的是,这种实现相当简单且脆弱,可能在生产环境中不太有用。它的目的是展示我们可以添加自定义的记忆实现。为此,我们需要首先安装spacy

# !pip install spacy
# !python -m spacy download en_core_web_lg
import spacy
nlp = spacy.load('en_core_web_lg')
class SpacyEntityMemory(BaseMemory, BaseModel):"""Memory class for storing information about entities."""# Define dictionary to store information about entities.entities: dict = {}# Define key to pass information about entities into prompt.memory_key: str = "entities"def clear(self):self.entities = {}@propertydef memory_variables(self) -> List[str]:"""Define the variables we are providing to the prompt."""return [self.memory_key]def load_memory_variables(self, inputs: Dict[str, Any]) -> Dict[str, str]:"""Load the memory variables, in this case the entity key."""# Get the input text and run through spacydoc = nlp(inputs[list(inputs.keys())[0]])# Extract known information about entities, if they exist.entities = [self.entities[str(ent)] for ent in doc.ents if str(ent) in self.entities]# Return combined information about entities to put into context.return {self.memory_key: "\n".join(entities)}def save_context(self, inputs: Dict[str, Any], outputs: Dict[str, str]) -> None:"""Save context from this conversation to buffer."""# Get the input text and run through spacytext = inputs[list(inputs.keys())[0]]doc = nlp(text)# For each entity that was mentioned, save this information to the dictionary.for ent in doc.ents:ent_str = str(ent)if ent_str in self.entities:self.entities[ent_str] += f"\n{text}"else:self.entities[ent_str] = text

我们现在定义一个提示,其中包含有关实体的信息以及用户的输入:

from langchain.prompts.prompt import PromptTemplatetemplate = """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. You are provided with information about entities the Human mentions, if relevant.Relevant entity information:
{entities}Conversation:
Human: {input}
AI:"""
prompt = PromptTemplate(input_variables=["entities", "input"], template=template
)

现在,我们把它们整合起来:

llm = OpenAI(temperature=0)
conversation = ConversationChain(llm=llm, prompt=prompt, verbose=True, memory=SpacyEntityMemory())

在第一个例子中,由于对Harrison没有先前的了解,"Relevant entity information"部分是空的:

conversation.predict(input="Harrison likes machine learning")

日志输出:

> 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. You are provided with information about entities the Human mentions, if relevant.Relevant entity information:Conversation:
Human: Harrison likes machine learning
AI:> Finished ConversationChain chain.

输出:

" That's great to hear! Machine learning is a fascinating field of study. It involves using algorithms to analyze data and make predictions. Have you ever studied machine learning, Harrison?"

现在在第二个例子中,我们可以看到它提取了关于Harrison的信息。

conversation.predict(input="What do you think Harrison's favorite subject in college was?")

日志输出:

> 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. You are provided with information about entities the Human mentions, if relevant.Relevant entity information:
Harrison likes machine learningConversation:
Human: What do you think Harrison's favorite subject in college was?
AI:> Finished ConversationChain chain.

输出:

' From what I know about Harrison, I believe his favorite subject in college was machine learning. He has expressed a strong interest in the subject and has mentioned it often.'

这个实现方式相对简单且容易出错,可能在实际生产环境中没有太大的用途,但它展示了我们可以添加自定义的内存实现方式。

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

http://www.ds6.com.cn/news/32532.html

相关文章:

  • wordpress登录页面显示ip武汉网络推广seo
  • 网站建设需要哪些技术人员自助建站系统个人网站
  • 招聘去建设赌博类网站seo关键字排名优化
  • 南海网站建设多少钱域名注册免费
  • 做网站高亮seo评测论坛
  • 网站建设需求指引免费行情网站app大全
  • 商业中心 网站建设域名注册服务网站哪个好
  • 怎么做网站地图seo技巧分享
  • 做电商网站需要做什么准备网络竞价推广开户
  • 哪里有网站建设中心武汉seo管理
  • 做金融的网站有哪些查图百度识图
  • 做网站外包是什么意思推广赚钱的平台
  • 盐城市建设工程网站滨州网站seo
  • 手机上怎么创建wordpress网站推广和优化系统
  • 男女一起做暖暖网站百度网站排名优化
  • 怎么用PS做网站广告图营销团队找产品合作
  • 网站建设bz3399分发平台
  • 竹子建站加盟咨询谷歌广告优化师
  • 龙口网站建设哪家好武汉百度开户电话
  • 企业名录搜索网站怎么在百度上发布信息广告
  • 做网站免费百度热门搜索排行榜
  • 徐州市专业做网站的公司批量关键词排名查询工具
  • 咋做网站代码背景图线上平台推广方式
  • 天津网站建设公司企业建站流程
  • wordpress合并百度信息流优化
  • 64m vps 安装wordpress百度seo推广计划类型包括
  • 南京网站制作费用seo课程排行榜
  • 网上赚钱论坛重庆seo顾问
  • 企业网站被转做非法用途杭州百度推广代理商
  • 公司网站邮箱怎么看接收服务器类型搜索引擎免费登录入口