Co-creation-projects/laoyouf-aistory/main.ipynb
本项目演示如何使用HelloAgents框架构建一个智能故事生成助手。
如果你想快速了解项目功能,可以运行这个简化版本。
故事类型选择
# 选择故事类型(小说/剧本/诗歌)
story_type = input("请输入故事类型(小说/剧本/诗歌): ")
# 输入故事主题
theme = input("请输入故事主题: ")
# 选择故事风格
style = input("请输入故事风格: ")
生成故事
# 快速演示 - 导入库和配置
from hello_agents import SimpleAgent, HelloAgentsLLM
from hello_agents.tools import Tool, ToolParameter
from typing import Dict, Any, List
import os
# 配置LLM参数
os.environ["LLM_MODEL_ID"] = "Qwen/Qwen2.5-72B-Instruct"
os.environ["LLM_API_KEY"] = "ms-fb406c2e-4246-4bf6-b0ad-7a686cccc270"
os.environ["LLM_BASE_URL"] = "https://api-inference.modelscope.cn/v1/"
os.environ["LLM_TIMEOUT"] = "60"
print("✅ 库导入和配置完成")
# 快速演示 - 定义故事生成工具
class StoryGeneratorTool(Tool):
def __init__(self):
super().__init__(name="story_generator",
description="根据用户输入生成不同类型的故事内容")
def run(self, parameters: Dict[str, Any]) -> str:
# 获取用户输入参数
story_type = parameters.get("type", "小说")
theme = parameters.get("theme", "奇幻冒险")
style = parameters.get("style", "轻松幽默")
# 构造提示词
prompt = f"生成一个{story_type},主题是{theme},风格为{style}。"
# 调用LLM生成故事
llm = HelloAgentsLLM()
response = llm.generate_text(prompt, max_tokens=1024)
return response
def get_parameters(self) -> List[ToolParameter]:
return [
ToolParameter(name="type", type="string", description="故事类型(小说/剧本/诗歌)", required=True),
ToolParameter(name="theme", type="string", description="故事主题", required=True),
ToolParameter(name="style", type="string", description="故事风格", required=True)
]
print("✅ 工具定义完成")
# 快速演示 - 创建工具注册表和智能体
from hello_agents import ToolRegistry
quick_registry = ToolRegistry()
quick_registry.register_tool(StoryGeneratorTool())
# 创建智能体
agent = SimpleAgent(
name="故事生成助手",
llm=HelloAgentsLLM(),
system_prompt="你是经验丰富的故事创作者,能够根据用户提供的参数生成不同类型的故事内容。请确保故事符合要求的类型、主题和风格,并保持内容的连贯性和创意性。",
tool_registry=quick_registry
)
print("✅ 智能体创建完成")
print(f"✅ 可用工具: {list(quick_registry._tools.keys())}")
# 生成并打印故事
print("=== 开始生成故事 ===")
story = agent.run(f"请根据以下参数生成一个故事:\n- 类型:{story_type}\n- 主题:{theme}\n- 风格:{style}")
print(story)
print("\n✅ 故事生成完成!")
下面是完整的代码审查系统,包含更强大的分析功能。
# 导入库
from hello_agents import SimpleAgent, HelloAgentsLLM
from hello_agents.tools import Tool, ToolParameter
from typing import Dict, Any, List
import os
# 配置LLM参数
os.environ["LLM_MODEL_ID"] = "Qwen/Qwen2.5-72B-Instruct"
os.environ["LLM_API_KEY"] = "ms-fb406c2e-4246-4bf6-b0ad-7a686cccc270"
os.environ["LLM_BASE_URL"] = "https://api-inference.modelscope.cn/v1/"
os.environ["LLM_TIMEOUT"] = "60"
print("✅ 库导入和配置完成")
# 定义故事生成工具
class StoryGeneratorTool(Tool):
def __init__(self):
super().__init__(name="story_generator",
description="根据用户输入生成不同类型的故事内容")
def run(self, parameters: Dict[str, Any]) -> str:
# 获取用户输入参数
story_type = parameters.get("type", "小说")
theme = parameters.get("theme", "奇幻冒险")
style = parameters.get("style", "轻松幽默")
# 构造提示词
prompt = f"生成一个{story_type},主题是{theme},风格为{style}。"
# 添加类型特定的指导
if story_type == "小说":
prompt += "请使用小说格式,以第三人称叙述,包含完整的情节发展和人物描写。"
elif story_type == "剧本":
prompt += "请使用剧本格式,包含场景描述、角色对话和动作指示。"
elif story_type == "诗歌":
prompt += "请使用诗歌格式,注意押韵和节奏感。"
# 调用LLM生成故事
llm = HelloAgentsLLM()
response = llm.generate_text(prompt, max_tokens=1024)
return response
def get_parameters(self) -> List[ToolParameter]:
return [
ToolParameter(name="type", type="string", description="故事类型(小说/剧本/诗歌)", required=True),
ToolParameter(name="theme", type="string", description="故事主题", required=True),
ToolParameter(name="style", type="string", description="故事风格", required=True)
]
print("✅ 故事生成工具定义完成")
# 定义系统提示词
system_prompt = """你是一位经验丰富的故事创作者,能够根据用户提供的参数生成不同类型的故事内容。
你的任务是:
1. 根据用户指定的故事类型(小说/剧本/诗歌)生成相应格式的内容
2. 确保故事符合用户指定的主题和风格
3. 保持故事的连贯性和创意性
4. 根据需要使用预设的故事元素库
生成故事时,请注意:
- 小说:以第三人称叙述,包含完整的情节发展和人物描写
- 剧本:使用标准剧本格式,包含场景描述、角色对话和动作指示
- 诗歌:注意押韵和节奏感,使用生动的意象和比喻
请直接输出生成的故事内容,无需添加任何额外说明或解释。
"""
# 导入工具注册表
from hello_agents import ToolRegistry
# 创建工具注册表
tool_registry = ToolRegistry()
tool_registry.register_tool(StoryGeneratorTool())
# 创建智能体
agent = SimpleAgent(
name="智能故事生成器",
llm=HelloAgentsLLM(),
system_prompt=system_prompt,
tool_registry=tool_registry
)
print("✅ 智能体创建完成")
print(f"智能体名称: {agent.name}")
print(f"可用工具: {list(tool_registry._tools.keys())}")
# 示例输入参数
sample_parameters = {
"type": "小说",
"theme": "魔法森林中的冒险",
"style": "轻松幽默"
}
def generate_story(agent, parameters):
# 生成故事
story = agent.run(f"请根据以下参数生成一个故事:\n{', '.join([f'- {k}: {v}' for k, v in parameters.items()])}")
# 返回故事内容
return story
小说示例:
# 小说参数
novel_parameters = {
"type": "小说",
"theme": "魔法森林中的冒险",
"style": "轻松幽默"
}
# 生成小说
print("=== 生成小说示例 ===")
novelStory = generate_story(agent, novel_parameters)
print(novelStory)
剧本示例:
# 剧本参数
script_parameters = {
"type": "剧本",
"theme": "拯救被困的朋友",
"style": "悬疑紧张"
}
# 生成剧本
print("=== 生成剧本示例 ===")
scriptStory = generate_story(agent, script_parameters)
print(scriptStory)
诗歌示例:
# 诗歌参数
poem_parameters = {
"type": "诗歌",
"theme": "彩虹山的美景",
"style": "浪漫温馨"
}
# 生成诗歌
print("=== 生成诗歌示例 ===")
poemStory = generate_story(agent, poem_parameters)
print(poemStory)