{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "bc1b70ed", "metadata": {}, "outputs": [], "source": [ "from agno.agent import Agent, RunResponse\n", "from agno.models.openai.like import OpenAILike\n", "from agno.memory.v2.db.sqlite import SqliteMemoryDb\n", "from agno.memory.v2.memory import Memory\n", "from agno.memory.v2.schema import UserMemory\n", "from agno.memory.v2.db.schema import MemoryRow\n", "from agno.memory.v2.db.sqlite import SqliteMemoryDb\n", "from agno.memory.v2.memory import Memory\n", "from agno.memory.v2.schema import UserMemory\n", "from agno.storage.sqlite import SqliteStorage\n", "import uuid\n", "import os\n", "\n", "memory_db = SqliteMemoryDb(db_file=\"tmp/bank_memory.db\", table_name=\"memory\")\n", "storge_db = SqliteStorage(\n", " table_name=\"agent_sessions\", db_file=\"tmp/persistent_memory.db\"\n", " )\n", "\n", "memory = Memory(\n", " \n", " model = OpenAILike(\n", " id=\"qwen3-4b\",\n", " api_key=os.getenv(\"BAILIAN_API_KEY\"),\n", " base_url=\"https://dashscope.aliyuncs.com/compatible-mode/v1\",\n", " request_params={\"extra_body\": {\"enable_thinking\": False}},\n", " ),\n", " db=memory_db\n", " )\n", "\n", "user_id = str(uuid.uuid4())\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "93039b6a", "metadata": {}, "outputs": [], "source": [ "memory.add_user_memory(\n", " memory=UserMemory(\n", " memory=\"我叫hhh,我喜欢冰淇淋和看电影。\",\n", " topics=[\"姓名\", \"兴趣\"]\n", " ),\n", " user_id=user_id,\n", " \n", ")\n" ] }, { "cell_type": "code", "execution_count": null, "id": "b0f6fa9c", "metadata": {}, "outputs": [], "source": [ "memory.clear()" ] }, { "cell_type": "code", "execution_count": null, "id": "99e6b8be", "metadata": {}, "outputs": [], "source": [ "agent = Agent(\n", " model=OpenAILike(\n", " id=\"qwen3-4b\",\n", " api_key=os.getenv(\"BAILIAN_API_KEY\"),\n", " base_url=\"https://dashscope.aliyuncs.com/compatible-mode/v1\",\n", " request_params={\"extra_body\": {\"enable_thinking\": False}},\n", " ),\n", " add_history_to_messages=True,\n", " #存用户记忆,可指定模型按topic检索?\n", " memory=memory,\n", " #存对话历史,工具调用历史\n", " storage=storge_db,\n", " #将用户对话加入记忆\n", " enable_user_memories=True,\n", " #自动将过去5轮对话加入promt ,搭配storge使用\n", " num_history_responses=5,\n", " #将会话摘要加入上下文\n", " add_session_summary_references=True,\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "34d7cd93", "metadata": {}, "outputs": [], "source": [ "from typing import List\n", "memories: List[MemoryRow] = memory_db.read_memories()\n", "print(\"All the memory_db memories:\")\n", "for i, m in enumerate(memories):\n", " print(f\"{i}: {m.memory['memory']} ({m.last_updated})\")" ] }, { "cell_type": "code", "execution_count": null, "id": "8099f79a", "metadata": {}, "outputs": [], "source": [ "def ask_agent(question: str):\n", " response: RunResponse = agent.run(\n", " messages=[{\"role\": \"user\", \"content\": f\"{question}\"}],\n", " user_id=user_id,\n", " )\n", " print(\"== AI 回复 ==\")\n", " print(response.content)" ] }, { "cell_type": "code", "execution_count": null, "id": "f8316605", "metadata": {}, "outputs": [], "source": [ "# 示例对话\n", "ask_agent(\"你好\")\n", "ask_agent(\"你还记得我叫什么吗?\")\n", "ask_agent(\"我最喜欢什么?\")" ] }, { "cell_type": "code", "execution_count": null, "id": "cd90f220", "metadata": {}, "outputs": [], "source": [ "ask_agent(\"我还喜欢摄影和吃火锅\")\n", "ask_agent(\"最近我想去旅游,请帮我根据我的爱好推荐城市\")" ] } ], "metadata": { "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 5 }