Instant experiment
准备
from datetime import datetime
import Agently
# 创建Agently的Agent工厂
agent_factory = (
Agently.AgentFactory(is_debug = False)
# 实验选用deepseek-chat商用API作为参考
.set_settings("current_model", "OAIClient")
.set_settings("model.OAIClient.auth", { "api_key": "****" })
.set_settings("model.OAIClient.url", "https://api.deepseek.com/v1")
.set_settings("model.OAIClient.options", { "model": "deepseek-chat" })
)
# 构建一个用于计时的tick对象
class Tick:
def __init__(self):
self.time_records = {}
def start(self, name):
now = datetime.now()
print(f">>>[{ name }] start>>>", now)
self.time_records.update({ name: now })
return
def stop(self, name):
now = datetime.now()
time_diff = now - self.time_records[name]
print(f">>>[{ name }] stop>>>", now)
print(f">>>[{ name }] gap>>>", str(time_diff.seconds) + "." + str(time_diff.microseconds))
return
tick = Tick()
串行模式
def create_words():
return (
agent_factory.create_agent()
.input("Generate 3 words, then use those 3 words to make a sentence.")
.output({
"words": [("str", )],
"sentence": ("str", ),
})
.start()
)
def generate_sentence(word):
tick.start(f"Generate Sentence: { word }")
tick.stop("Total")
result = (
agent_factory.create_agent()
.input(f"Generate 1 sentence using word '{ word }'")
.start()
)
tick.stop(f"Generate Sentence: { word }")
tick.stop("Total")
print("sentence:", result)
tick.start("Total")
tick.start("Create Words")
result = create_words()
tick.stop("Create Words")
print("words:", result["words"])
print("sentence:", result["sentence"])
for word in result["words"]:
generate_sentence(word)
tick.stop("Total")
运行结果:
>>>[Total] start>>> 2024-10-17 22:27:34.408858
>>>[Create Words] start>>> 2024-10-17 22:27:34.408965
>>>[Create Words] stop>>> 2024-10-17 22:27:37.683771
>>>[Create Words] gap>>> 3.274806
words: ['sunset', 'ocean', 'breeze']
sentence: The sunset over the ocean was accompanied by a gentle breeze.
>>>[Generate Sentence: sunset] start>>> 2024-10-17 22:27:37.683987
>>>[Total] stop>>> 2024-10-17 22:27:37.684013
>>>[Total] gap>>> 3.275155
>>>[Generate Sentence: sunset] stop>>> 2024-10-17 22:27:40.078293
>>>[Generate Sentence: sunset] gap>>> 2.394306
>>>[Total] stop>>> 2024-10-17 22:27:40.078362
>>>[Total] gap>>> 5.669504
sentence: As the vibrant hues of the sunset painted the sky in a breathtaking display of oranges and pinks, the world seemed to pause in awe of nature's fleeting masterpiece.
>>>[Generate Sentence: ocean] start>>> 2024-10-17 22:27:40.078375
>>>[Total] stop>>> 2024-10-17 22:27:40.078380
>>>[Total] gap>>> 5.669522
>>>[Generate Sentence: ocean] stop>>> 2024-10-17 22:27:42.873992
>>>[Generate Sentence: ocean] gap>>> 2.795617
>>>[Total] stop>>> 2024-10-17 22:27:42.874091
>>>[Total] gap>>> 8.465233
sentence: The vast ocean, with its endless waves and hidden depths, whispers secrets of ancient mysteries to those who dare to listen.
>>>[Generate Sentence: breeze] start>>> 2024-10-17 22:27:42.874109
>>>[Total] stop>>> 2024-10-17 22:27:42.874114
>>>[Total] gap>>> 8.465256
>>>[Generate Sentence: breeze] stop>>> 2024-10-17 22:27:44.377338
>>>[Generate Sentence: breeze] gap>>> 1.503229
>>>[Total] stop>>> 2024-10-17 22:27:44.377515
>>>[Total] gap>>> 9.968657
sentence: The gentle breeze whispered through the trees, carrying the scent of blooming flowers.
>>>[Total] stop>>> 2024-10-17 22:27:44.377710
>>>[Total] gap>>> 9.968852
并发模式
import asyncio
import nest_asyncio
nest_asyncio.apply()
async def create_words():
return (
await agent_factory.create_agent()
.input("Generate 3 other words, then use those 3 words to make a sentence.")
.output({
"words": [("str", )],
"sentence": ("str", ),
})
.start_async()
)
async def generate_sentence(word):
tick.start(f"Generate Sentence: { word }")
tick.stop("Total")
result = (
await agent_factory.create_agent()
.input(f"Generate 1 sentence using word '{ word }'")
.start_async()
)
tick.stop(f"Generate Sentence: { word }")
tick.stop("Total")
print("sentence:", result)
async def run():
tasks = []
tick.start("Total")
tick.start("Create Words")
result = await create_words()
tick.stop("Create Words")
print("words:", result["words"])
print("sentence:", result["sentence"])
for word in result["words"]:
tasks.append(asyncio.create_task(generate_sentence(word)))
await asyncio.gather(*tasks)
tick.stop("Total")
asyncio.run(run())
输出结果:
>>>[Total] start>>> 2024-10-17 22:31:08.466437
>>>[Create Words] start>>> 2024-10-17 22:31:08.466615
>>>[Create Words] stop>>> 2024-10-17 22:31:12.370278
>>>[Create Words] gap>>> 3.903663
words: ['serene', 'adventure', 'harmony']
sentence: In the serene landscape, an adventure unfolds, bringing harmony to the hearts of all who experience it.
>>>[Generate Sentence: serene] start>>> 2024-10-17 22:31:12.370724
>>>[Total] stop>>> 2024-10-17 22:31:12.370740
>>>[Total] gap>>> 3.904303
>>>[Generate Sentence: adventure] start>>> 2024-10-17 22:31:12.389722
>>>[Total] stop>>> 2024-10-17 22:31:12.389742
>>>[Total] gap>>> 3.923305
>>>[Generate Sentence: harmony] start>>> 2024-10-17 22:31:12.407565
>>>[Total] stop>>> 2024-10-17 22:31:12.407582
>>>[Total] gap>>> 3.941145
>>>[Generate Sentence: serene] stop>>> 2024-10-17 22:31:13.971482
>>>[Generate Sentence: serene] gap>>> 1.600758
>>>[Total] stop>>> 2024-10-17 22:31:13.971656
>>>[Total] gap>>> 5.505219
sentence: The serene lake mirrored the tranquil sky, creating a peaceful oasis amidst the bustling world.
>>>[Generate Sentence: adventure] stop>>> 2024-10-17 22:31:14.002872
>>>[Generate Sentence: adventure] gap>>> 1.613150
>>>[Total] stop>>> 2024-10-17 22:31:14.002923
>>>[Total] gap>>> 5.536486
sentence: Embarking on an adventure, Sarah discovered hidden treasures and forged lifelong friendships along the mysterious path.
>>>[Generate Sentence: harmony] stop>>> 2024-10-17 22:31:14.540299
>>>[Generate Sentence: harmony] gap>>> 2.132734
>>>[Total] stop>>> 2024-10-17 22:31:14.540425
>>>[Total] gap>>> 6.73988
sentence: In the symphony of life, true harmony is found when diverse voices blend seamlessly, creating a beautiful and resonant melody that transcends individual differences.
>>>[Total] stop>>> 2024-10-17 22:31:14.540728
>>>[Total] gap>>> 6.74291
Agently Instant + 并发模式
import asyncio
import nest_asyncio
nest_asyncio.apply()
async def generate_sentence(word):
tick.start(f"Generate Sentence: { word }")
tick.stop("Total")
result = (
# 注意,因为使用instant模式时,原本的agent还在处理过程中
# 不能使用同一个agent对象处理两个任务,需要创建一个新的agent对象来执行任务
await agent_factory.create_agent()
.input(f"Generate 1 sentence using word '{ word }'")
.start_async()
)
tick.stop(f"Generate Sentence: { word }")
tick.stop("Total")
print("sentence:", result)
async def run():
tasks = []
tick.start("Total")
tick.start("Create Words")
agent = agent_factory.create_agent()
@agent.on_event("instant:words.[].$complete")
def handler(data):
print("word:", data["value"])
tasks.append(asyncio.create_task(generate_sentence(data["value"])))
result = (await agent
.use_instant()
.input("Don't use your cache, Generate 3 words, then use those 3 words to make a sentence.")
.output({
"words": [("str", )],
"sentence": ("str", ),
})
.start_async()
)
tick.stop("Create Words")
print("words:", result["words"])
print("sentence:", result["sentence"])
await asyncio.gather(*tasks)
tick.stop("Total")
asyncio.run(run())
输出结果:
>>>[Total] start>>> 2024-10-17 22:36:17.683348
>>>[Create Words] start>>> 2024-10-17 22:36:17.683667
word: sunset
>>>[Generate Sentence: sunset] start>>> 2024-10-17 22:36:19.347812
>>>[Total] stop>>> 2024-10-17 22:36:19.347920
>>>[Total] gap>>> 1.664572
word: ocean
>>>[Generate Sentence: ocean] start>>> 2024-10-17 22:36:19.609852
>>>[Total] stop>>> 2024-10-17 22:36:19.609875
>>>[Total] gap>>> 1.926527
word: breeze
>>>[Generate Sentence: breeze] start>>> 2024-10-17 22:36:19.975757
>>>[Total] stop>>> 2024-10-17 22:36:19.975794
>>>[Total] gap>>> 2.292446
>>>[Create Words] stop>>> 2024-10-17 22:36:20.909244
>>>[Create Words] gap>>> 3.225577
words: ['sunset', 'ocean', 'breeze']
sentence: The sunset over the ocean was accompanied by a gentle breeze.
>>>[Generate Sentence: breeze] stop>>> 2024-10-17 22:36:21.585640
>>>[Generate Sentence: breeze] gap>>> 1.609883
>>>[Total] stop>>> 2024-10-17 22:36:21.585787
>>>[Total] gap>>> 3.902439
sentence: The gentle breeze whispered through the trees, carrying the scent of blooming flowers.
>>>[Generate Sentence: ocean] stop>>> 2024-10-17 22:36:21.721056
>>>[Generate Sentence: ocean] gap>>> 2.111204
>>>[Total] stop>>> 2024-10-17 22:36:21.721112
>>>[Total] gap>>> 4.37764
sentence: The vast ocean, with its endless waves and hidden depths, holds the secrets of ancient mysteries and the promise of uncharted adventures.
>>>[Generate Sentence: sunset] stop>>> 2024-10-17 22:36:22.353727
>>>[Generate Sentence: sunset] gap>>> 3.5915
>>>[Total] stop>>> 2024-10-17 22:36:22.353899
>>>[Total] gap>>> 4.670551
sentence: As the vibrant hues of the sunset painted the sky, casting a warm glow over the tranquil landscape, it felt as though time itself had paused to savor the ephemeral beauty.
>>>[Total] stop>>> 2024-10-17 22:36:22.354214
>>>[Total] gap>>> 4.670866