| 1 | # Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | # All rights reserved. |
| 3 | # |
| 4 | # This source code is licensed under the terms described in the LICENSE file in |
| 5 | # the root directory of this source tree. |
| 6 | |
| 7 | |
| 8 | import logging |
| 9 | import tempfile |
| 10 | import uuid |
| 11 | from typing import AsyncGenerator |
| 12 | |
| 13 | from llama_stack.apis.inference import Inference |
| 14 | from llama_stack.apis.memory import Memory |
| 15 | from llama_stack.apis.safety import Safety |
| 16 | from llama_stack.apis.agents import * # noqa: F403 |
| 17 | |
| 18 | from .agent_instance import ChatAgent |
| 19 | from .config import MetaReferenceImplConfig |
| 20 | from .tools.builtin import ( |
| 21 | CodeInterpreterTool, |
| 22 | PhotogenTool, |
| 23 | SearchTool, |
| 24 | WolframAlphaTool, |
| 25 | ) |
| 26 | from .tools.safety import with_safety |
| 27 | |
| 28 | |
| 29 | logger = logging.getLogger() |
| 30 | logger.setLevel(logging.INFO) |
| 31 | |
| 32 | |
| 33 | AGENT_INSTANCES_BY_ID = {} |
| 34 | |
| 35 | |
| 36 | class MetaReferenceAgentsImpl(Agents): |
| 37 | def __init__( |
| 38 | self, |
| 39 | config: MetaReferenceImplConfig, |
| 40 | inference_api: Inference, |
| 41 | memory_api: Memory, |
| 42 | safety_api: Safety, |
| 43 | ): |
| 44 | self.config = config |
| 45 | self.inference_api = inference_api |
| 46 | self.memory_api = memory_api |
| 47 | self.safety_api = safety_api |
| 48 | |
| 49 | async def initialize(self) -> None: |
| 50 | pass |
| 51 | |
| 52 | async def create_agent( |
| 53 | self, |
| 54 | agent_config: AgentConfig, |
| 55 | ) -> AgentCreateResponse: |
| 56 | agent_id = str(uuid.uuid4()) |
| 57 | |
| 58 | builtin_tools = [] |
| 59 | for tool_defn in agent_config.tools: |
| 60 | if isinstance(tool_defn, WolframAlphaToolDefinition): |
| 61 | tool = WolframAlphaTool(tool_defn.api_key) |
| 62 | elif isinstance(tool_defn, SearchToolDefinition): |
| 63 | tool = SearchTool(tool_defn.engine, tool_defn.api_key) |
| 64 | elif isinstance(tool_defn, CodeInterpreterToolDefinition): |
| 65 | tool = CodeInterpreterTool() |
| 66 | elif isinstance(tool_defn, PhotogenToolDefinition): |
| 67 | tool = PhotogenTool(dump_dir=tempfile.mkdtemp()) |
| 68 | else: |
| 69 | continue |
| 70 | |
| 71 | builtin_tools.append( |
| 72 | with_safety( |
| 73 | tool, |
| 74 | self.safety_api, |
| 75 | tool_defn.input_shields, |
| 76 | tool_defn.output_shields, |
| 77 | ) |
| 78 | ) |
| 79 | |
| 80 | AGENT_INSTANCES_BY_ID[agent_id] = ChatAgent( |
| 81 | agent_config=agent_config, |
| 82 | inference_api=self.inference_api, |
| 83 | safety_api=self.safety_api, |
| 84 | memory_api=self.memory_api, |
| 85 | builtin_tools=builtin_tools, |
| 86 | ) |
| 87 | |
| 88 | return AgentCreateResponse( |
| 89 | agent_id=agent_id, |
| 90 | ) |
| 91 | |
| 92 | async def create_agent_session( |
| 93 | self, |
| 94 | agent_id: str, |
| 95 | session_name: str, |
| 96 | ) -> AgentSessionCreateResponse: |
| 97 | assert agent_id in AGENT_INSTANCES_BY_ID, f"System {agent_id} not found" |
| 98 | agent = AGENT_INSTANCES_BY_ID[agent_id] |
| 99 | |
| 100 | session = agent.create_session(session_name) |
| 101 | return AgentSessionCreateResponse( |
| 102 | session_id=session.session_id, |
| 103 | ) |
| 104 | |
| 105 | async def create_agent_turn( |
| 106 | self, |
| 107 | agent_id: str, |
| 108 | session_id: str, |
| 109 | messages: List[ |
| 110 | Union[ |
| 111 | UserMessage, |
| 112 | ToolResponseMessage, |
| 113 | ] |
| 114 | ], |
| 115 | attachments: Optional[List[Attachment]] = None, |
| 116 | stream: Optional[bool] = False, |
| 117 | ) -> AsyncGenerator: |
| 118 | # wrapper request to make it easier to pass around (internal only, not exposed to API) |
| 119 | request = AgentTurnCreateRequest( |
| 120 | agent_id=agent_id, |
| 121 | session_id=session_id, |
| 122 | messages=messages, |
| 123 | attachments=attachments, |
| 124 | stream=stream, |
| 125 | ) |
| 126 | |
| 127 | agent_id = request.agent_id |
| 128 | assert agent_id in AGENT_INSTANCES_BY_ID, f"System {agent_id} not found" |
| 129 | agent = AGENT_INSTANCES_BY_ID[agent_id] |
| 130 | |
| 131 | assert ( |
| 132 | request.session_id in agent.sessions |
| 133 | ), f"Session {request.session_id} not found" |
| 134 | async for event in agent.create_and_execute_turn(request): |
| 135 | yield event |