|
| 1 | +import asyncio |
| 2 | +import tempfile |
| 3 | + |
| 4 | +from agents import Agent, Runner, SQLiteSession, trace |
| 5 | +from agents.tool import function_tool |
| 6 | + |
| 7 | +""" |
| 8 | +This example shows how an agent-as-tool can access the entire conversation history |
| 9 | +using a session. The session information is declared globally or stored in the |
| 10 | +user context and tools can then read that session and have the conversation history |
| 11 | +for all turns up to that point. |
| 12 | +""" |
| 13 | + |
| 14 | +spanish_agent = Agent( |
| 15 | + name="spanish_agent", |
| 16 | + instructions="You translate the user's message to Spanish", |
| 17 | + handoff_description="An english to spanish translator", |
| 18 | +) |
| 19 | + |
| 20 | +french_agent = Agent( |
| 21 | + name="french_agent", |
| 22 | + instructions="You translate the user's message to French", |
| 23 | + handoff_description="An english to french translator", |
| 24 | +) |
| 25 | + |
| 26 | +italian_agent = Agent( |
| 27 | + name="italian_agent", |
| 28 | + instructions="You translate the user's message to Italian", |
| 29 | + handoff_description="An english to italian translator", |
| 30 | +) |
| 31 | + |
| 32 | + |
| 33 | +async def main(session_id, file_path): |
| 34 | + |
| 35 | + @function_tool |
| 36 | + async def check_tool() -> str: |
| 37 | + """A tool that looks at the entire conversation, checks |
| 38 | + your work, and asks for corrections if needed. |
| 39 | +
|
| 40 | + Returns |
| 41 | + ------- |
| 42 | + str |
| 43 | + A string starting with either REJECTED or ACCEPTED, |
| 44 | + followed by instructions to correct the translations, if needed. |
| 45 | + """ |
| 46 | + |
| 47 | + # Open the session and get all the messages as input items |
| 48 | + _session = SQLiteSession(session_id=session_id, db_path=file_path) |
| 49 | + input_messages = await _session.get_items() |
| 50 | + |
| 51 | + check_agent = Agent( |
| 52 | + name="check_agent", |
| 53 | + instructions=( |
| 54 | + "Check the translations for correctness by reviewing the entire conversation and " |
| 55 | + "asking for corrections if needed. Respond with either REJECTED or ACCEPTED, " |
| 56 | + "followed by instructions to correct the translations, if needed." |
| 57 | + ), |
| 58 | + ) |
| 59 | + |
| 60 | + output = await Runner.run( |
| 61 | + check_agent, |
| 62 | + input=input_messages, |
| 63 | + ) |
| 64 | + return output.final_output |
| 65 | + |
| 66 | + |
| 67 | + orchestrator_agent = Agent( |
| 68 | + name="orchestrator_agent", |
| 69 | + instructions=( |
| 70 | + "You are a translation agent. Translate the user's text " |
| 71 | + "to Spanish, Italian and French. Always use the provided tools. " |
| 72 | + "After translating the text, you mush call the check_tool to " |
| 73 | + "ensure correctness." |
| 74 | + ), |
| 75 | + tools=[ |
| 76 | + spanish_agent.as_tool( |
| 77 | + tool_name="translate_to_spanish", |
| 78 | + tool_description="Translate the user's message to Spanish", |
| 79 | + ), |
| 80 | + french_agent.as_tool( |
| 81 | + tool_name="translate_to_french", |
| 82 | + tool_description="Translate the user's message to French", |
| 83 | + ), |
| 84 | + italian_agent.as_tool( |
| 85 | + tool_name="translate_to_italian", |
| 86 | + tool_description="Translate the user's message to Italian", |
| 87 | + ), |
| 88 | + check_tool |
| 89 | + ] |
| 90 | + ) |
| 91 | + |
| 92 | + msg = input("Hi! What would you like translated? ") |
| 93 | + |
| 94 | + # Create a new SQLite session with the provided session ID and file path |
| 95 | + session = SQLiteSession( |
| 96 | + session_id=session_id, |
| 97 | + db_path=file_path, |
| 98 | + ) |
| 99 | + |
| 100 | + # Run the entire orchestration in a single trace |
| 101 | + with trace("Orchestrator evaluator"): |
| 102 | + orchestrator_result = await Runner.run(orchestrator_agent, msg, session=session) |
| 103 | + |
| 104 | + print(f"\n\nFinal response:\n{orchestrator_result.final_output}") |
| 105 | + |
| 106 | + |
| 107 | +if __name__ == "__main__": |
| 108 | + with tempfile.NamedTemporaryFile("+r", suffix=".db") as db_file: |
| 109 | + # Create a new SQLite session with the temporary database file |
| 110 | + session_id = "ABCDEF" |
| 111 | + file_path = db_file |
| 112 | + |
| 113 | + # Run the main function |
| 114 | + asyncio.run(main(session_id, file_path)) |
| 115 | + |
| 116 | + # This input triggers a "REJECTED" response from the check_tool most of the time |
| 117 | + # "hello, guten tag, hola, bonjure" |
0 commit comments