{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# StoryGeneratorAgent - 智能故事生成助手\n",
    "\n",
    "本项目演示如何使用HelloAgents框架构建一个智能故事生成助手。\n",
    "\n",
    "## 📖 使用说明\n",
    "\n",
    "- **快速体验**: 运行「第0部分」的快速演示\n",
    "- **完整功能**: 依次运行第1-4部分，体验完整的故事生成流程\n",
    "- **参数调整**: 在第4部分修改 **generate_story()** 函数的参数，尝试不同故事类型\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "## 第0部分：快速演示 ⚡\n",
    "\n",
    "如果你想快速了解项目功能,可以运行这个简化版本。"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "故事类型选择"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 选择故事类型（小说/剧本/诗歌）\n",
    "story_type = input(\"请输入故事类型（小说/剧本/诗歌）: \")\n",
    "# 输入故事主题\n",
    "theme = input(\"请输入故事主题: \")\n",
    "# 选择故事风格\n",
    "style = input(\"请输入故事风格: \")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "生成故事"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# 快速演示 - 导入库和配置\n",
    "from hello_agents import SimpleAgent, HelloAgentsLLM\n",
    "from hello_agents.tools import Tool, ToolParameter\n",
    "from typing import Dict, Any, List\n",
    "import os\n",
    "\n",
    "# 配置LLM参数\n",
    "os.environ[\"LLM_MODEL_ID\"] = \"Qwen/Qwen2.5-72B-Instruct\"\n",
    "os.environ[\"LLM_API_KEY\"] = \"ms-fb406c2e-4246-4bf6-b0ad-7a686cccc270\"\n",
    "os.environ[\"LLM_BASE_URL\"] = \"https://api-inference.modelscope.cn/v1/\"\n",
    "os.environ[\"LLM_TIMEOUT\"] = \"60\"\n",
    "\n",
    "print(\"✅ 库导入和配置完成\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# 快速演示 - 定义故事生成工具\n",
    "class StoryGeneratorTool(Tool):\n",
    "    def __init__(self):\n",
    "        super().__init__(name=\"story_generator\",\n",
    "                            description=\"根据用户输入生成不同类型的故事内容\")\n",
    "\n",
    "    def run(self, parameters: Dict[str, Any]) -> str:\n",
    "        # 获取用户输入参数\n",
    "        story_type = parameters.get(\"type\", \"小说\")\n",
    "        theme = parameters.get(\"theme\", \"奇幻冒险\")\n",
    "        style = parameters.get(\"style\", \"轻松幽默\")\n",
    "\n",
    "        # 构造提示词\n",
    "        prompt = f\"生成一个{story_type}，主题是{theme}，风格为{style}。\"\n",
    "\n",
    "        # 调用LLM生成故事\n",
    "        llm = HelloAgentsLLM()\n",
    "        response = llm.generate_text(prompt, max_tokens=1024)\n",
    "\n",
    "        return response\n",
    "\n",
    "    def get_parameters(self) -> List[ToolParameter]:\n",
    "        return [\n",
    "            ToolParameter(name=\"type\", type=\"string\", description=\"故事类型（小说/剧本/诗歌）\", required=True),\n",
    "            ToolParameter(name=\"theme\", type=\"string\", description=\"故事主题\", required=True),\n",
    "            ToolParameter(name=\"style\", type=\"string\", description=\"故事风格\", required=True)\n",
    "        ]\n",
    "\n",
    "print(\"✅ 工具定义完成\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# 快速演示 - 创建工具注册表和智能体\n",
    "from hello_agents import ToolRegistry\n",
    "\n",
    "quick_registry  = ToolRegistry()\n",
    "quick_registry.register_tool(StoryGeneratorTool())\n",
    "\n",
    "# 创建智能体\n",
    "agent = SimpleAgent(\n",
    "    name=\"故事生成助手\",\n",
    "    llm=HelloAgentsLLM(),\n",
    "    system_prompt=\"你是经验丰富的故事创作者，能够根据用户提供的参数生成不同类型的故事内容。请确保故事符合要求的类型、主题和风格，并保持内容的连贯性和创意性。\",\n",
    "    tool_registry=quick_registry\n",
    ")\n",
    "\n",
    "print(\"✅ 智能体创建完成\")\n",
    "print(f\"✅ 可用工具: {list(quick_registry._tools.keys())}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# 生成并打印故事\n",
    "print(\"=== 开始生成故事 ===\")\n",
    "story = agent.run(f\"请根据以下参数生成一个故事：\\n- 类型：{story_type}\\n- 主题：{theme}\\n- 风格：{style}\")\n",
    "print(story)\n",
    "print(\"\\n✅ 故事生成完成！\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "# 完整版代码审查系统\n",
    "\n",
    "下面是完整的代码审查系统,包含更强大的分析功能。"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第1部分：环境配置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入库\n",
    "from hello_agents import SimpleAgent, HelloAgentsLLM\n",
    "from hello_agents.tools import Tool, ToolParameter\n",
    "from typing import Dict, Any, List\n",
    "import os\n",
    "\n",
    "# 配置LLM参数\n",
    "os.environ[\"LLM_MODEL_ID\"] = \"Qwen/Qwen2.5-72B-Instruct\"\n",
    "os.environ[\"LLM_API_KEY\"] = \"ms-fb406c2e-4246-4bf6-b0ad-7a686cccc270\"\n",
    "os.environ[\"LLM_BASE_URL\"] = \"https://api-inference.modelscope.cn/v1/\"\n",
    "os.environ[\"LLM_TIMEOUT\"] = \"60\"\n",
    "\n",
    "print(\"✅ 库导入和配置完成\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第2部分：定义故事生成工具"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义故事生成工具\n",
    "class StoryGeneratorTool(Tool):\n",
    "    def __init__(self):\n",
    "        super().__init__(name=\"story_generator\",\n",
    "                            description=\"根据用户输入生成不同类型的故事内容\")\n",
    "\n",
    "    def run(self, parameters: Dict[str, Any]) -> str:\n",
    "        # 获取用户输入参数\n",
    "        story_type = parameters.get(\"type\", \"小说\")\n",
    "        theme = parameters.get(\"theme\", \"奇幻冒险\")\n",
    "        style = parameters.get(\"style\", \"轻松幽默\")\n",
    "\n",
    "        # 构造提示词\n",
    "        prompt = f\"生成一个{story_type}，主题是{theme}，风格为{style}。\"\n",
    "\n",
    "        # 添加类型特定的指导\n",
    "        if story_type == \"小说\":\n",
    "            prompt += \"请使用小说格式，以第三人称叙述，包含完整的情节发展和人物描写。\"\n",
    "        elif story_type == \"剧本\":\n",
    "            prompt += \"请使用剧本格式，包含场景描述、角色对话和动作指示。\"\n",
    "        elif story_type == \"诗歌\":\n",
    "            prompt += \"请使用诗歌格式，注意押韵和节奏感。\"\n",
    "\n",
    "        # 调用LLM生成故事\n",
    "        llm = HelloAgentsLLM()\n",
    "        response = llm.generate_text(prompt, max_tokens=1024)\n",
    "\n",
    "        return response\n",
    "\n",
    "    def get_parameters(self) -> List[ToolParameter]:\n",
    "        return [\n",
    "            ToolParameter(name=\"type\", type=\"string\", description=\"故事类型（小说/剧本/诗歌）\", required=True),\n",
    "            ToolParameter(name=\"theme\", type=\"string\", description=\"故事主题\", required=True),\n",
    "            ToolParameter(name=\"style\", type=\"string\", description=\"故事风格\", required=True)\n",
    "        ]\n",
    "\n",
    "print(\"✅ 故事生成工具定义完成\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第3部分：创建智能体"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义系统提示词\n",
    "system_prompt = \"\"\"你是一位经验丰富的故事创作者，能够根据用户提供的参数生成不同类型的故事内容。\n",
    "\n",
    "你的任务是：\n",
    "1. 根据用户指定的故事类型（小说/剧本/诗歌）生成相应格式的内容\n",
    "2. 确保故事符合用户指定的主题和风格\n",
    "3. 保持故事的连贯性和创意性\n",
    "4. 根据需要使用预设的故事元素库\n",
    "\n",
    "生成故事时，请注意：\n",
    "- 小说：以第三人称叙述，包含完整的情节发展和人物描写\n",
    "- 剧本：使用标准剧本格式，包含场景描述、角色对话和动作指示\n",
    "- 诗歌：注意押韵和节奏感，使用生动的意象和比喻\n",
    "\n",
    "请直接输出生成的故事内容，无需添加任何额外说明或解释。\n",
    "\"\"\"\n",
    "\n",
    "# 导入工具注册表\n",
    "from hello_agents import ToolRegistry\n",
    "\n",
    "# 创建工具注册表\n",
    "tool_registry = ToolRegistry()\n",
    "tool_registry.register_tool(StoryGeneratorTool())\n",
    "\n",
    "# 创建智能体\n",
    "agent = SimpleAgent(\n",
    "    name=\"智能故事生成器\",\n",
    "    llm=HelloAgentsLLM(),\n",
    "    system_prompt=system_prompt,\n",
    "    tool_registry=tool_registry\n",
    ")\n",
    "\n",
    "print(\"✅ 智能体创建完成\")\n",
    "print(f\"智能体名称: {agent.name}\")\n",
    "print(f\"可用工具: {list(tool_registry._tools.keys())}\")\n",
    "\n",
    "# 示例输入参数\n",
    "sample_parameters = {\n",
    "    \"type\": \"小说\",\n",
    "    \"theme\": \"魔法森林中的冒险\",\n",
    "    \"style\": \"轻松幽默\"\n",
    "}"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第4部分：生成并展示故事"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def generate_story(agent, parameters):\n",
    "    # 生成故事\n",
    "    story = agent.run(f\"请根据以下参数生成一个故事：\\n{', '.join([f'- {k}: {v}' for k, v in parameters.items()])}\")\n",
    "\n",
    "    # 返回故事内容\n",
    "    return story"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "小说示例："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 小说参数\n",
    "novel_parameters = {\n",
    "    \"type\": \"小说\",\n",
    "    \"theme\": \"魔法森林中的冒险\",\n",
    "    \"style\": \"轻松幽默\"\n",
    "}\n",
    "\n",
    "# 生成小说\n",
    "print(\"=== 生成小说示例 ===\")\n",
    "novelStory = generate_story(agent, novel_parameters)\n",
    "print(novelStory)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "剧本示例："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 剧本参数\n",
    "script_parameters = {\n",
    "    \"type\": \"剧本\",\n",
    "    \"theme\": \"拯救被困的朋友\",\n",
    "    \"style\": \"悬疑紧张\"\n",
    "}\n",
    "\n",
    "# 生成剧本\n",
    "print(\"=== 生成剧本示例 ===\")\n",
    "scriptStory = generate_story(agent, script_parameters)\n",
    "print(scriptStory)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "诗歌示例："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 诗歌参数\n",
    "poem_parameters = {\n",
    "    \"type\": \"诗歌\",\n",
    "    \"theme\": \"彩虹山的美景\",\n",
    "    \"style\": \"浪漫温馨\"\n",
    "}\n",
    "\n",
    "# 生成诗歌\n",
    "print(\"=== 生成诗歌示例 ===\")\n",
    "poemStory = generate_story(agent, poem_parameters)\n",
    "print(poemStory)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第5部分：总结与展望\n",
    "\n",
    "\n",
    "### 实现的功能\n",
    "- ✅ 环境配置与LLM模型设置\n",
    "- ✅ 小说/剧本/诗歌三种故事类型生成\n",
    "- ✅ 根据用户指定主题和风格定制故事内容\n",
    "- ✅ 交互式参数输入和故事生成\n",
    "### 遇到的挑战\n",
    "- 确保不同故事类型格式的准确性和合适的文本内容量（字数）\n",
    "- 处理不同风格故事的语言特点\n",
    "### 未来改进方向\n",
    "- 扩展故事元素库，增加更多角色、地点和情节\n",
    "- 支持用户自定义故事元素\n",
    "- 添加故事优化功能，如语法检查和风格统一\n",
    "- 实现多语言故事生成支持\n",
    "- 添加故事可视化功能，如场景图或角色关系图"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ha",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.19"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
