import asyncio
from agents import Agent, ItemHelpers, Runner
from config import config
"""
This example shows the parallelization pattern. We run the agent three times in parallel, and pick
the best result.
"""
spanish_agent = Agent(
name="spanish_agent",
instructions="You translate the user's message to Spanish",
)
translation_picker = Agent(
name="translation_picker",
instructions="You pick the best Spanish translation from the given options.",
)
async def main():
msg = input("Hi! Enter a message, and we'll translate it to Spanish.\n\n")
# Ensure the entire workflow is a single trace
# with trace("Parallel translation"):
res_1, res_2, res_3 = await asyncio.gather(
Runner.run(spanish_agent, msg, run_config=config),
Runner.run(spanish_agent, msg, run_config=config),
Runner.run(spanish_agent, msg, run_config=config),
)
outputs = [
ItemHelpers.text_message_outputs(res_1.new_items),
ItemHelpers.text_message_outputs(res_2.new_items),
ItemHelpers.text_message_outputs(res_3.new_items),
]
translations = "\n\n|".join(outputs)
print(f"\n\nTranslations:\n\n{translations}")
best_translation = await Runner.run(
translation_picker,
f"Input: {msg}\n\nTranslations:\n{translations}",
run_config=config,
)
print("\n\n-----")
print(f"Best translation: {best_translation.final_output}")
if __name__ == "__main__":
asyncio.run(main())