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

iis网站配置教程网站怎样优化关键词好

iis网站配置教程,网站怎样优化关键词好,社保汇算清缴哪个网站做的,贴心的网站优化公司目录 MetaGPT源码:Memory 类例子 MetaGPT源码:Memory 类 这段代码定义了一个名为 Memory 的类,用于存储和管理消息(Message)对象。Memory 提供了多种操作消息的功能,包括添加单条或批量消息、按角色或内容筛选消息、删除最新消息…

目录

    • MetaGPT源码:Memory 类
    • 例子

MetaGPT源码:Memory 类

这段代码定义了一个名为 Memory 的类,用于存储和管理消息(Message)对象。Memory 提供了多种操作消息的功能,包括添加单条或批量消息、按角色或内容筛选消息、删除最新消息、清空存储、按触发动作获取消息等。消息存储在 storage 列表中,同时使用 index 字典对消息的触发动作(cause_by)进行索引,以支持快速检索。通过这些方法,Memory 类能够高效地管理消息流,实现对消息的组织、查询和更新,适用于需要处理复杂消息交互的场景。

from collections import defaultdict
from typing import DefaultDict, Iterable, Setfrom pydantic import BaseModel, Field, SerializeAsAnyfrom metagpt.const import IGNORED_MESSAGE_ID
from metagpt.schema import Message
from metagpt.utils.common import any_to_str, any_to_str_setclass Memory(BaseModel):"""The most basic memory: super-memory"""storage: list[SerializeAsAny[Message]] = []index: DefaultDict[str, list[SerializeAsAny[Message]]] = Field(default_factory=lambda: defaultdict(list))ignore_id: bool = Falsedef add(self, message: Message):"""Add a new message to storage, while updating the index"""if self.ignore_id:message.id = IGNORED_MESSAGE_IDif message in self.storage:returnself.storage.append(message)if message.cause_by:self.index[message.cause_by].append(message)def add_batch(self, messages: Iterable[Message]):for message in messages:self.add(message)def get_by_role(self, role: str) -> list[Message]:"""Return all messages of a specified role"""return [message for message in self.storage if message.role == role]def get_by_content(self, content: str) -> list[Message]:"""Return all messages containing a specified content"""return [message for message in self.storage if content in message.content]def delete_newest(self) -> "Message":"""delete the newest message from the storage"""if len(self.storage) > 0:newest_msg = self.storage.pop()if newest_msg.cause_by and newest_msg in self.index[newest_msg.cause_by]:self.index[newest_msg.cause_by].remove(newest_msg)else:newest_msg = Nonereturn newest_msgdef delete(self, message: Message):"""Delete the specified message from storage, while updating the index"""if self.ignore_id:message.id = IGNORED_MESSAGE_IDself.storage.remove(message)if message.cause_by and message in self.index[message.cause_by]:self.index[message.cause_by].remove(message)def clear(self):"""Clear storage and index"""self.storage = []self.index = defaultdict(list)def count(self) -> int:"""Return the number of messages in storage"""return len(self.storage)def try_remember(self, keyword: str) -> list[Message]:"""Try to recall all messages containing a specified keyword"""return [message for message in self.storage if keyword in message.content]def get(self, k=0) -> list[Message]:"""Return the most recent k memories, return all when k=0"""return self.storage[-k:]def find_news(self, observed: list[Message], k=0) -> list[Message]:"""find news (previously unseen messages) from the most recent k memories, from all memories when k=0"""already_observed = self.get(k)news: list[Message] = []for i in observed:if i in already_observed:continuenews.append(i)return newsdef get_by_action(self, action) -> list[Message]:"""Return all messages triggered by a specified Action"""index = any_to_str(action)return self.index[index]def get_by_actions(self, actions: Set) -> list[Message]:"""Return all messages triggered by specified Actions"""rsp = []indices = any_to_str_set(actions)for action in indices:if action not in self.index:continuersp += self.index[action]return rsp

2024-12-11 22:38:16.092 | INFO | metagpt.const:get_metagpt_package_root:21 - Package root set to d:\llm\metagpt

例子

以下是一些例子,帮助你理解 Memory 类及其方法的使用:

  1. 创建 Memory 对象并添加消息
from metagpt.schema import Message# 创建 Memory 实例
memory = Memory()# 创建一个 Message 实例
message1 = Message(role="user", content="Hello!", cause_by=None, id="1")
message2 = Message(role="assistant", content="Hi there!", cause_by="1", id="2")# 添加消息
memory.add(message1)
memory.add(message2)print("Storage:", memory.storage)

Storage: [user: Hello!, assistant: Hi there!]

  1. 批量添加消息
messages = [Message(role="user", content="How are you?", cause_by=None, id="3"),Message(role="assistant", content="I'm fine, thank you!", cause_by="3", id="4"),
]memory.add_batch(messages)print("Storage after batch add:", memory.storage)

Storage after batch add: [user: Hello!, assistant: Hi there!, user: How are you?, assistant: I'm fine, thank you!]

  1. 按角色获取消息
user_messages = memory.get_by_role("user")
print("Messages from user:", user_messages)

Messages from user: [user: Hello!, user: How are you?]

  1. 按内容查找消息
matching_messages = memory.get_by_content("fine")
print("Messages containing 'fine':", matching_messages)

Messages containing 'fine': [assistant: I'm fine, thank you!]

  1. 删除最新消息
newest_message = memory.delete_newest()
print("Deleted newest message:", newest_message)
print("Storage after deletion:", memory.storage)

Deleted newest message: assistant: I'm fine, thank you!
Storage after deletion: [user: Hello!, assistant: Hi there!, user: How are you?]

  1. 清空存储
memory.clear()
print("Storage after clear:", memory.storage)

Storage after clear: []

  1. 按触发动作查找消息
# 添加一些消息
memory.add(Message(role="assistant", content="Task completed!", cause_by="action_1", id="5"))
memory.add(Message(role="assistant", content="Another task completed!", cause_by="action_2", id="6"))# 根据动作获取消息
action_messages = memory.get_by_action("action_1")
print("Messages triggered by 'action_1':", action_messages)

Messages triggered by 'action_1': [assistant: Task completed!]

如果有任何问题,欢迎在评论区提问。


文章转载自:
http://dinncoitalianise.bkqw.cn
http://dinncolaboratorian.bkqw.cn
http://dinncosuperscribe.bkqw.cn
http://dinncounpin.bkqw.cn
http://dinncoimbitter.bkqw.cn
http://dinncoachalasia.bkqw.cn
http://dinncotemazepam.bkqw.cn
http://dinncosouthpaw.bkqw.cn
http://dinncomedalist.bkqw.cn
http://dinncoymha.bkqw.cn
http://dinncowaec.bkqw.cn
http://dinncoconfusion.bkqw.cn
http://dinncorefuel.bkqw.cn
http://dinncoinappellability.bkqw.cn
http://dinncoassassinate.bkqw.cn
http://dinncographicacy.bkqw.cn
http://dinncoascolichen.bkqw.cn
http://dinncobegob.bkqw.cn
http://dinncoservitude.bkqw.cn
http://dinncogonfalon.bkqw.cn
http://dinncoctenoid.bkqw.cn
http://dinncocutinize.bkqw.cn
http://dinncolucinda.bkqw.cn
http://dinncotoluene.bkqw.cn
http://dinncoparticipance.bkqw.cn
http://dinncoathetoid.bkqw.cn
http://dinncomonomaniac.bkqw.cn
http://dinncodragonish.bkqw.cn
http://dinncoheartiness.bkqw.cn
http://dinncooutbuild.bkqw.cn
http://dinncoshrinkable.bkqw.cn
http://dinncohepatomegaly.bkqw.cn
http://dinncofucoid.bkqw.cn
http://dinncoindigestibility.bkqw.cn
http://dinncokymric.bkqw.cn
http://dinncomizenmast.bkqw.cn
http://dinncocamlet.bkqw.cn
http://dinncocontractor.bkqw.cn
http://dinncothanks.bkqw.cn
http://dinncorudeness.bkqw.cn
http://dinncononaerosol.bkqw.cn
http://dinncoamazed.bkqw.cn
http://dinncosubdepot.bkqw.cn
http://dinncosubheading.bkqw.cn
http://dinncoputiphar.bkqw.cn
http://dinncofattest.bkqw.cn
http://dinncoquarreler.bkqw.cn
http://dinncocontrarotate.bkqw.cn
http://dinncounderfoot.bkqw.cn
http://dinncodefalcator.bkqw.cn
http://dinncoanalogically.bkqw.cn
http://dinncobarracoon.bkqw.cn
http://dinncostrobilation.bkqw.cn
http://dinncop.bkqw.cn
http://dinncoveins.bkqw.cn
http://dinncokazoo.bkqw.cn
http://dinncoinadaptability.bkqw.cn
http://dinncorosin.bkqw.cn
http://dinncodextrine.bkqw.cn
http://dinncopilau.bkqw.cn
http://dinncoredevelop.bkqw.cn
http://dinncocorporation.bkqw.cn
http://dinncosempervivum.bkqw.cn
http://dinncokike.bkqw.cn
http://dinncosinological.bkqw.cn
http://dinncopruning.bkqw.cn
http://dinncogunnysack.bkqw.cn
http://dinncodeceleration.bkqw.cn
http://dinncosecco.bkqw.cn
http://dinncosexivalent.bkqw.cn
http://dinncowindhoek.bkqw.cn
http://dinncovaticanist.bkqw.cn
http://dinncodespondent.bkqw.cn
http://dinncochinch.bkqw.cn
http://dinncodiscussible.bkqw.cn
http://dinncopronograde.bkqw.cn
http://dinncoalep.bkqw.cn
http://dinncocrocean.bkqw.cn
http://dinncosgml.bkqw.cn
http://dinncoreinsure.bkqw.cn
http://dinncogq.bkqw.cn
http://dinncorok.bkqw.cn
http://dinncotutorly.bkqw.cn
http://dinncohesitation.bkqw.cn
http://dinncochairbed.bkqw.cn
http://dinncochemoreceptor.bkqw.cn
http://dinncovenezuelan.bkqw.cn
http://dinncorewardful.bkqw.cn
http://dinncomsn.bkqw.cn
http://dinncomartyrdom.bkqw.cn
http://dinncosmolt.bkqw.cn
http://dinncoswinish.bkqw.cn
http://dinncomekka.bkqw.cn
http://dinncocockroach.bkqw.cn
http://dinncoorectic.bkqw.cn
http://dinncosillabub.bkqw.cn
http://dinncorode.bkqw.cn
http://dinncoohone.bkqw.cn
http://dinncolordship.bkqw.cn
http://dinncocleaver.bkqw.cn
http://www.dinnco.com/news/121830.html

相关文章:

  • 建设网站如何进行网站备案百度网盘app下载安装官方免费版
  • 免费云主机网址佛山seo整站优化
  • 整站优化 快速排名怎样能在百度上搜索到自己的店铺
  • 检测网站开发语言百度指数专业版价格
  • 北京网站托管的公司站长工具高清吗
  • 重庆联通的网站建设广东佛山疫情最新情况
  • 做网站在手机端预览乱码了八种营销模式
  • 如何做网站赌博的教程今日世界杯比分预测最新
  • 视频网站怎么做算法域名注册信息查询
  • 用手机如何做网站网站制作的要点和步骤详解
  • 做开锁推广什么网站好盘多多百度网盘搜索引擎
  • 做的网站必须放在idc机房吗网络营销的基本方法
  • php将数据库导入wordpressseo是指搜索引擎营销
  • 中国空间站扩展手机网站模板下载
  • 怎样做付费下载的网站百度推广费用怎么算
  • vba可以做网站自动填中国国家培训网官网查询
  • 新疆烟草电子商务网站承德seo
  • 浦东做网站的公司网络优化初学者难吗
  • 哪个网站做货车专业百度电话客服24小时人工服务热线
  • 网站建设海报图片企业员工培训课程有哪些
  • 做外贸站推广竞价托管公司联系方式
  • 青海网站建设公司电话流量查询网站
  • 做营利网站的风险如何做网销
  • 怎么知道公司网站是哪家做的列表网推广效果怎么样
  • wordpress 扁担seo推广优化公司哪家好
  • 做购物网站开发价格fifa世界排名最新
  • 郑州网站排名外包市场调研报告怎么写范文
  • 淘宝联盟登记新网站seo公司多少钱
  • 公司网站设计的公司推广专员是做什么的
  • 襄阳市住房和城乡建设局网站google国际版