252 lines
29 KiB
Plaintext
252 lines
29 KiB
Plaintext
|
|
{
|
|||
|
|
"cells": [
|
|||
|
|
{
|
|||
|
|
"cell_type": "code",
|
|||
|
|
"id": "initial_id",
|
|||
|
|
"metadata": {
|
|||
|
|
"collapsed": true,
|
|||
|
|
"ExecuteTime": {
|
|||
|
|
"end_time": "2025-09-14T00:10:52.305914Z",
|
|||
|
|
"start_time": "2025-09-14T00:10:51.828275Z"
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
"source": [
|
|||
|
|
"import os\n",
|
|||
|
|
"\n",
|
|||
|
|
"os.environ['DASHSCOPE_API_KEY'] = 'sk-e2a05bbcfac84e53b73f98acef15a009'\n",
|
|||
|
|
"\n",
|
|||
|
|
"# Step 0: Define tools and model\n",
|
|||
|
|
"\n",
|
|||
|
|
"from langchain_core.tools import tool\n",
|
|||
|
|
"from langchain_community.chat_models.tongyi import ChatTongyi\n",
|
|||
|
|
"\n",
|
|||
|
|
"llm = ChatTongyi(\n",
|
|||
|
|
" model=\"qwen-max\", # 此处以qwen-max为例,您可按需更换模型名称。模型列表:https://help.aliyun.com/zh/model-studio/getting-started/models\n",
|
|||
|
|
" streaming=True,\n",
|
|||
|
|
" # other params...\n",
|
|||
|
|
")"
|
|||
|
|
],
|
|||
|
|
"outputs": [],
|
|||
|
|
"execution_count": 1
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
"metadata": {
|
|||
|
|
"ExecuteTime": {
|
|||
|
|
"end_time": "2025-09-14T00:13:15.270547Z",
|
|||
|
|
"start_time": "2025-09-14T00:13:15.236537Z"
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
"cell_type": "code",
|
|||
|
|
"source": [
|
|||
|
|
"from typing_extensions import TypedDict\n",
|
|||
|
|
"from langgraph.graph import StateGraph, START, END\n",
|
|||
|
|
"from IPython.display import Image, display\n",
|
|||
|
|
"\n",
|
|||
|
|
"\n",
|
|||
|
|
"# Graph state\n",
|
|||
|
|
"class State(TypedDict):\n",
|
|||
|
|
" topic: str\n",
|
|||
|
|
" joke: str\n",
|
|||
|
|
" story: str\n",
|
|||
|
|
" poem: str\n",
|
|||
|
|
" combined_output: str\n",
|
|||
|
|
"\n",
|
|||
|
|
"\n",
|
|||
|
|
"# Nodes\n",
|
|||
|
|
"def call_llm_1(state: State):\n",
|
|||
|
|
" \"\"\"First LLM call to generate initial joke\"\"\"\n",
|
|||
|
|
"\n",
|
|||
|
|
" msg = llm.invoke(f\"Write a joke about {state['topic']}\")\n",
|
|||
|
|
" return {\"joke\": msg.content}\n",
|
|||
|
|
"\n",
|
|||
|
|
"\n",
|
|||
|
|
"def call_llm_2(state: State):\n",
|
|||
|
|
" \"\"\"Second LLM call to generate story\"\"\"\n",
|
|||
|
|
"\n",
|
|||
|
|
" msg = llm.invoke(f\"Write a story about {state['topic']}\")\n",
|
|||
|
|
" return {\"story\": msg.content}\n",
|
|||
|
|
"\n",
|
|||
|
|
"\n",
|
|||
|
|
"def call_llm_3(state: State):\n",
|
|||
|
|
" \"\"\"Third LLM call to generate poem\"\"\"\n",
|
|||
|
|
"\n",
|
|||
|
|
" msg = llm.invoke(f\"Write a poem about {state['topic']}\")\n",
|
|||
|
|
" return {\"poem\": msg.content}\n",
|
|||
|
|
"\n",
|
|||
|
|
"\n",
|
|||
|
|
"def aggregator(state: State):\n",
|
|||
|
|
" \"\"\"Combine the joke and story into a single output\"\"\"\n",
|
|||
|
|
"\n",
|
|||
|
|
" combined = f\"Here's a story, joke, and poem about {state['topic']}!\\n\\n\"\n",
|
|||
|
|
" combined += f\"STORY:\\n{state['story']}\\n\\n\"\n",
|
|||
|
|
" combined += f\"JOKE:\\n{state['joke']}\\n\\n\"\n",
|
|||
|
|
" combined += f\"POEM:\\n{state['poem']}\"\n",
|
|||
|
|
" return {\"combined_output\": combined}\n",
|
|||
|
|
"\n",
|
|||
|
|
"\n",
|
|||
|
|
"# Build workflow\n",
|
|||
|
|
"parallel_builder = StateGraph(State)\n",
|
|||
|
|
"\n",
|
|||
|
|
"# Add nodes\n",
|
|||
|
|
"parallel_builder.add_node(\"call_llm_1\", call_llm_1)\n",
|
|||
|
|
"parallel_builder.add_node(\"call_llm_2\", call_llm_2)\n",
|
|||
|
|
"parallel_builder.add_node(\"call_llm_3\", call_llm_3)\n",
|
|||
|
|
"parallel_builder.add_node(\"aggregator\", aggregator)\n",
|
|||
|
|
"\n",
|
|||
|
|
"# Add edges to connect nodes\n",
|
|||
|
|
"parallel_builder.add_edge(START, \"call_llm_1\")\n",
|
|||
|
|
"parallel_builder.add_edge(START, \"call_llm_2\")\n",
|
|||
|
|
"parallel_builder.add_edge(START, \"call_llm_3\")\n",
|
|||
|
|
"parallel_builder.add_edge(\"call_llm_1\", \"aggregator\")\n",
|
|||
|
|
"parallel_builder.add_edge(\"call_llm_2\", \"aggregator\")\n",
|
|||
|
|
"parallel_builder.add_edge(\"call_llm_3\", \"aggregator\")\n",
|
|||
|
|
"parallel_builder.add_edge(\"aggregator\", END)\n",
|
|||
|
|
"parallel_workflow = parallel_builder.compile()"
|
|||
|
|
],
|
|||
|
|
"id": "ca97abef33e37605",
|
|||
|
|
"outputs": [],
|
|||
|
|
"execution_count": 2
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
"metadata": {
|
|||
|
|
"ExecuteTime": {
|
|||
|
|
"end_time": "2025-09-14T00:13:17.609433Z",
|
|||
|
|
"start_time": "2025-09-14T00:13:16.738137Z"
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
"cell_type": "code",
|
|||
|
|
"source": [
|
|||
|
|
"\n",
|
|||
|
|
"# Show workflow\n",
|
|||
|
|
"display(Image(parallel_workflow.get_graph().draw_mermaid_png()))"
|
|||
|
|
],
|
|||
|
|
"id": "b0f300553c496de0",
|
|||
|
|
"outputs": [
|
|||
|
|
{
|
|||
|
|
"data": {
|
|||
|
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAaMAAAFNCAIAAAAiuZdRAAAQAElEQVR4nOzdB3wTZR8H8OeSNOmedDI62W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P4WadOHWE0Tp9q7969t27dMtzpVajgG4tt3kZERLC1PPhpfHF5xCVMOq6ZbJzCT2dni9zIxgrt2rUrMjJy9uzZRBzy8/PZti38lMvlhuatjY0NQVzBpOOIYEZOn922bdsSEhJmzJhBxCcmJsZwmF6LFi3Y1GvUqBFBFoZJZ0HibJw+1ZYtW9LS0qZOnUrE7fLly+xQBmwnhoperVq1CLIATLpqBo1Tw3Eh4mycPtVPP/2kVCoNd31FmZmZhoqeq6sre8wKbDMEVR9Muuph3Dg1HBcizsbpU23YsAG2ug8++ICgMmD4gq3ohYWFGSp6MIxL0LPBpKs6bJxWzffff69QKMaMGUNQ+WDHNFT0srOzDaO32DioGkw682Dj9Nl9/fXXLi4uo0aNIqhyUlJSDKO38J3Kpl7r1q0JqjRMukrBxmk1Wr16tbe394gRIwgyH2yKbOrdunXL0LytW1eA1xasXph05WIbp2zAYeO0Gi1fvjwwMHDYsGEEPYPCwkJD85amaTb1gK2tLUFlYNKVULZxygYcNk6r0bJlyxo3bjx48GCCqklCQgKbeqBRo0ZsRS80NJSgYph0Otg45dKiRYtatWo1YMAAgiwgPDyc/aqOjo5mrysFm7SPT3VesI+PxJt02DitKQsWLIDvkj59+hBkSbm5ubBtnz17FrZzOzs7Q6eeVCol4iOupNNoNIauDWyc1pTZs2d
|
|||
|
|
"text/plain": [
|
|||
|
|
"<IPython.core.display.Image object>"
|
|||
|
|
]
|
|||
|
|
},
|
|||
|
|
"metadata": {},
|
|||
|
|
"output_type": "display_data",
|
|||
|
|
"jetTransient": {
|
|||
|
|
"display_id": null
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
],
|
|||
|
|
"execution_count": 3
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
"metadata": {
|
|||
|
|
"ExecuteTime": {
|
|||
|
|
"end_time": "2025-09-14T00:13:36.700225Z",
|
|||
|
|
"start_time": "2025-09-14T00:13:25.108178Z"
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
"cell_type": "code",
|
|||
|
|
"source": [
|
|||
|
|
"\n",
|
|||
|
|
"# Invoke\n",
|
|||
|
|
"state = parallel_workflow.invoke({\"topic\": \"cats\"})\n",
|
|||
|
|
"print(state[\"combined_output\"])"
|
|||
|
|
],
|
|||
|
|
"id": "4526cc9bbb346e0b",
|
|||
|
|
"outputs": [
|
|||
|
|
{
|
|||
|
|
"name": "stdout",
|
|||
|
|
"output_type": "stream",
|
|||
|
|
"text": [
|
|||
|
|
"Here's a story, joke, and poem about cats!\n",
|
|||
|
|
"\n",
|
|||
|
|
"STORY:\n",
|
|||
|
|
"In the heart of a bustling city, nestled between towering skyscrapers and busy streets, lay a hidden gem: a small, enchanting garden. This garden was no ordinary place; it was a sanctuary for a group of cats, each with their own unique story and personality.\n",
|
|||
|
|
"\n",
|
|||
|
|
"At the center of the garden stood an ancient, gnarled tree, its branches reaching out like welcoming arms. Beneath this tree, a wise old cat named Whisker lived. Whisker had been in the garden longer than any of the other cats could remember, and he was the unofficial leader of the feline community. His fur was a soft, silvery gray, and his eyes held the wisdom of countless sunrises and moonlit nights.\n",
|
|||
|
|
"\n",
|
|||
|
|
"One sunny morning, a tiny, scruffy kitten stumbled into the garden. Her fur was matted, and her eyes were wide with fear and wonder. The other cats, curious but cautious, watched from a distance as the little one explored her new surroundings. Whisker, sensing the kitten's distress, approached her gently.\n",
|
|||
|
|
"\n",
|
|||
|
|
"\"Hello, little one,\" Whisker said in a soothing voice. \"What is your name?\"\n",
|
|||
|
|
"\n",
|
|||
|
|
"The kitten, still trembling, looked up at Whisker with big, round eyes. \"I... I don't have a name,\" she stammered. \"I've been on my own for as long as I can remember.\"\n",
|
|||
|
|
"\n",
|
|||
|
|
"Whisker's heart ached for the young cat. \"Well, then, we shall give you a name,\" he declared. \"How about Luna? It means 'moon,' and you have such bright, shining eyes.\"\n",
|
|||
|
|
"\n",
|
|||
|
|
"Luna's ears perked up, and a small smile crept across her face. \"Luna... I like that,\" she said, her voice growing stronger.\n",
|
|||
|
|
"\n",
|
|||
|
|
"Over the next few weeks, Luna settled into the garden, making friends with the other cats. There was Shadow, a sleek black cat who loved to play hide-and-seek among the shadows of the garden. Then there was Paws, a plump, orange tabby who was always eager to share a meal or a nap in the sun. And there was Bella, a graceful, white-furred cat with a gentle spirit and a love for storytelling.\n",
|
|||
|
|
"\n",
|
|||
|
|
"As the days turned into weeks, Luna grew more confident and happy. She learned the secrets of the garden, from the best spots to catch the warmest rays of sunlight to the most comfortable places to curl up for a nap. Whisker took her under his wing, teaching her the ways of the garden and the importance of looking out for one another.\n",
|
|||
|
|
"\n",
|
|||
|
|
"One evening, as the sun set and the sky painted itself in hues of pink and gold, the cats gathered around the ancient tree. Whisker, sitting on a high branch, began to tell a story. It was a tale of bravery, friendship, and the unbreakable bond between the cats of the garden.\n",
|
|||
|
|
"\n",
|
|||
|
|
"Luna listened intently, her heart swelling with a sense of belonging. She knew that she had found her true home, not just in the garden, but in the hearts of her new family.\n",
|
|||
|
|
"\n",
|
|||
|
|
"And so, life in the garden continued, filled with laughter, adventures, and the quiet, contented purrs of cats who had found their place in the world.\n",
|
|||
|
|
"\n",
|
|||
|
|
"JOKE:\n",
|
|||
|
|
"Why did the cat join a band? Because it already had its own drumsticks!\n",
|
|||
|
|
"\n",
|
|||
|
|
"POEM:\n",
|
|||
|
|
"In the quiet of night, when the world is asleep,\n",
|
|||
|
|
"Soft paws tiptoe, in shadows they creep.\n",
|
|||
|
|
"Eyes like lanterns, aglow with a gleam,\n",
|
|||
|
|
"Silent whispers, in moonlight, they dream.\n",
|
|||
|
|
"\n",
|
|||
|
|
"Fur as soft as the clouds in the sky,\n",
|
|||
|
|
"Whiskers twitching, beneath the moon's eye.\n",
|
|||
|
|
"Graceful dancers, on invisible strings,\n",
|
|||
|
|
"Leaping, bounding, their joy it brings.\n",
|
|||
|
|
"\n",
|
|||
|
|
"Purr of contentment, a soothing sound,\n",
|
|||
|
|
"In the heart of the night, all around.\n",
|
|||
|
|
"Kittens mewling, in cozy retreat,\n",
|
|||
|
|
"Nestled warmly, where love and dreams meet.\n",
|
|||
|
|
"\n",
|
|||
|
|
"Guardians of secrets, keepers of lore,\n",
|
|||
|
|
"In every alley, by every door.\n",
|
|||
|
|
"Mysteries wrapped in a velvet cloak,\n",
|
|||
|
|
"Wise beyond years, in silence they spoke.\n",
|
|||
|
|
"\n",
|
|||
|
|
"Cats, oh cats, with your enigmatic grace,\n",
|
|||
|
|
"In sunbeams you bask, in life's gentle pace.\n",
|
|||
|
|
"Forever weaving, through time's endless thread,\n",
|
|||
|
|
"A tapestry rich, in which we are led.\n"
|
|||
|
|
]
|
|||
|
|
}
|
|||
|
|
],
|
|||
|
|
"execution_count": 4
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
"metadata": {},
|
|||
|
|
"cell_type": "code",
|
|||
|
|
"outputs": [],
|
|||
|
|
"execution_count": null,
|
|||
|
|
"source": "",
|
|||
|
|
"id": "faff8da474589197"
|
|||
|
|
}
|
|||
|
|
],
|
|||
|
|
"metadata": {
|
|||
|
|
"kernelspec": {
|
|||
|
|
"display_name": "Python 3",
|
|||
|
|
"language": "python",
|
|||
|
|
"name": "python3"
|
|||
|
|
},
|
|||
|
|
"language_info": {
|
|||
|
|
"codemirror_mode": {
|
|||
|
|
"name": "ipython",
|
|||
|
|
"version": 2
|
|||
|
|
},
|
|||
|
|
"file_extension": ".py",
|
|||
|
|
"mimetype": "text/x-python",
|
|||
|
|
"name": "python",
|
|||
|
|
"nbconvert_exporter": "python",
|
|||
|
|
"pygments_lexer": "ipython2",
|
|||
|
|
"version": "2.7.6"
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
"nbformat": 4,
|
|||
|
|
"nbformat_minor": 5
|
|||
|
|
}
|