|
| 1 | +# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from abc import ABC, abstractmethod |
| 16 | + |
| 17 | +from google.adk.agents.readonly_context import ReadonlyContext |
| 18 | +from typing_extensions import override |
| 19 | + |
| 20 | +from veadk.prompts.agent_default_prompt import DEFAULT_INSTRUCTION |
| 21 | +from veadk.utils.logger import get_logger |
| 22 | + |
| 23 | +logger = get_logger(__name__) |
| 24 | + |
| 25 | + |
| 26 | +class BasePromptManager(ABC): |
| 27 | + def __init__(self) -> None: ... |
| 28 | + |
| 29 | + @abstractmethod |
| 30 | + def get_prompt(self, context: ReadonlyContext, **kwargs) -> str: ... |
| 31 | + |
| 32 | + |
| 33 | +class CozeloopPromptManager(BasePromptManager): |
| 34 | + def __init__( |
| 35 | + self, |
| 36 | + cozeloop_workspace_id: str, |
| 37 | + cozeloop_token: str, |
| 38 | + prompt_key: str, |
| 39 | + version: str = "", |
| 40 | + label: str = "", |
| 41 | + ) -> None: |
| 42 | + import cozeloop |
| 43 | + |
| 44 | + self.cozeloop_workspace_id = cozeloop_workspace_id |
| 45 | + self.cozeloop_token = cozeloop_token |
| 46 | + |
| 47 | + self.prompt_key = prompt_key |
| 48 | + self.version = version |
| 49 | + self.label = label |
| 50 | + |
| 51 | + self.client = cozeloop.new_client( |
| 52 | + workspace_id=self.cozeloop_workspace_id, |
| 53 | + api_token=self.cozeloop_token, |
| 54 | + ) |
| 55 | + |
| 56 | + super().__init__() |
| 57 | + |
| 58 | + @override |
| 59 | + def get_prompt(self, context: ReadonlyContext, **kwargs) -> str: |
| 60 | + logger.info(f"Get prompt for agent {context.agent_name} from CozeLoop.") |
| 61 | + |
| 62 | + prompt = self.client.get_prompt( |
| 63 | + prompt_key=self.prompt_key, |
| 64 | + version=self.version, |
| 65 | + label=self.label, |
| 66 | + ) |
| 67 | + if ( |
| 68 | + prompt |
| 69 | + and prompt.prompt_template |
| 70 | + and prompt.prompt_template.messages |
| 71 | + and prompt.prompt_template.messages[0].content |
| 72 | + ): |
| 73 | + return prompt.prompt_template.messages[0].content |
| 74 | + |
| 75 | + logger.warning( |
| 76 | + f"Prompt {self.prompt_key} version {self.version} label {self.label} not found, get prompt result is {prompt}" |
| 77 | + f"return default instruction" |
| 78 | + ) |
| 79 | + return DEFAULT_INSTRUCTION |
0 commit comments