当前位置: 首页 > 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

相关文章:

  • 当 Manus AI 遇上 OpenAI Operator,谁能更胜一筹?
  • 大模型应用中常说的Rerank是什么技术?
  • 大模型核心基础简介
  • GIF图像技术介绍
  • MacOS Python3安装
  • Java在人工智能中的应用:机器学习与深度学习技术探讨
  • 剪板机结构设计:关键技术分析与优化策略
  • python 实现文件批量重命名
  • 网络IP分片
  • JVM——Java字节码基础
  • Web 实时通信技术:WebSocket 与 Server-Sent Events (SSE) 深入解析
  • SpringCloud Gateway知识点整理和全局过滤器实现
  • 梯形路径规划详解
  • Linux共享内存深度解析:从内核机制到云原生应用
  • 接口继承与扩展的使用技巧
  • 泰勒展开式
  • C#游戏开发中的注意事项
  • 22.第二阶段x64游戏实战-分析周围对象类型
  • SpringBoot主入口类分析
  • PXE安装Ubuntu系统
  • 多元史料下的“西狩”叙事——《“庚子西狩”中外资料六种》解题
  • 为证明我爸是我爸,我将奶奶告上法庭
  • 郑州通报“夜市摊贩收取香烟交给城管”:涉事人员停职调查
  • 会计江湖|年报披露关注什么:独董给出的“信号”
  • 蔡达峰:推动食品安全法全面有效实施,为维护人民群众身体健康提供有力法治保障
  • 71岁导演詹姆斯・弗雷病逝,曾执导《纸牌屋》、麦当娜MV