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

mem0跟Memgraph交互

目录

      • 1. 安装和设置
      • 2. 配置连接
      • 3. 使用 `mem0` 进行交互
      • 4. 添加和查询数据
      • 5. 代码运行结果

1. 安装和设置

首先,确保你已经安装了 Memgraph 和 mem0 库。你可以使用 pip 来安装 mem0

uv pip install "mem0ai[graph]"
uv pip install langchain-memgraph

确保 Memgraph 数据库正在运行,并且你可以通过 Bolt 协议连接到它。

在这里插入图片描述

2. 配置连接

在你的 Python 脚本中,你需要配置连接到 Memgraph 的参数,并配置相关的大模型key。以下是一个示例配置,用的是阿里大模型:

openai_client = OpenAI(api_key="",base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
)config = {"embedder": {"provider": "openai","config": {"model": "text-embedding-v2","embedding_dims": 1536,"api_key": "","openai_base_url": "https://dashscope.aliyuncs.com/compatible-mode/v1"}
},"graph_store": {"provider": "memgraph","config": {"url": "bolt://localhost:7687","username": "memgraph","password": "mem0graph",},},"llm": {"provider":"openai","config":{"model": "qwen-turbo","api_key": "","openai_base_url": "https://dashscope.aliyuncs.com/compatible-mode/v1"},},
}

3. 使用 mem0 进行交互

mem0 提供了一个 Memory 类,用于与 Memgraph 进行交互。你可以通过配置字典来初始化这个类:

from mem0 import Memorym = Memory.from_config(config_dict=config)

4. 添加和查询数据

你可以使用 add 方法将数据添加到 Memgraph 中,并使用 search 方法进行查询。例如:


messages = [{"role": "user","content": "I'm planning to watch a movie tonight. Any recommendations?",},{"role": "assistant","content": "How about a thriller movies? They can be quite engaging.",},{"role": "user","content": "I'm not a big fan of thriller movies but I love sci-fi movies.",},{"role": "assistant","content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future.",},
]result = m.add(messages, user_id="alice", metadata={"category": "movie_recommendations"}
)for result in m.search("what does alice love?", user_id="alice")["results"]:print(result["memory"], result["score"])

5. 代码运行结果

在这里插入图片描述

在这里插入图片描述

参考链接:https://github.com/mem0ai/mem0/blob/main/examples/graph-db-demo/memgraph-example.ipynb

http://www.xdnf.cn/news/385939.html

相关文章:

  • spring cloud loadbalancer实现机房感知的负载均衡
  • ESP32-S3 学习笔记(1)
  • mac环境配置(homebrew版)
  • [案例四] 智能填写属性工具(支持装配组件还有建模实体属性的批量创建、编辑)
  • ST表(稀疏表)
  • 理解反向Shell:隐藏在合法流量中的威胁
  • Python并发编程:开启性能优化的大门(7/10)
  • MySQL 索引设计宝典:原理、原则与实战案例深度解析
  • 【C++】模板初阶
  • 从零开始开发纯血鸿蒙应用之XML解析
  • 《AI大模型应知应会100篇》第58篇:Semantic Kernel:微软的大模型应用框架
  • 计算机网络|| 常用网络命令的作用及工作原理
  • 张量并行优质博客
  • 【东枫科技】使用LabVIEW进行深度学习开发
  • 面试中常问的设计模式及其简洁定义
  • 【React】Craco 简介
  • JavaScript 循环语句全解析:选择最适合的遍历方式
  • 客服系统重构详细计划
  • 如何选择 RabbitMQ、Redis 队列等消息中间件?—— 深度解析与实战评估
  • 御网杯2025 Web,Msic,密码 WP
  • Docker、ECS 与 K8s 网段冲突:解决跨服务通信中的路由问题
  • [思维模式-30]:《本质思考力》-10-产品研发的两种模式:①自顶向下的规划、分解、牵引;②自底向上的堆叠、聚合。
  • Win全兼容!五五 Excel Word 转 PDF 工具解决多场景转换难题
  • MyBatis快速入门——实操
  • spark运行架构及核心组件介绍
  • spark-Schema 定义字段强类型和弱类型
  • 06.three官方示例+编辑器+AI快速学习webgl_animation_skinning_additive_blending
  • openharmony系统移植之gpu mesa3d适配
  • [Java][Leetcode middle] 80. 删除有序数组中的重复项 II
  • 【MySQL】页结构详解:页的大小、分类、头尾信息、数据行、查询、记录及数据页的完整结构