xintongsong commented on code in PR #81: URL: https://github.com/apache/flink-agents/pull/81#discussion_r2249746716
########## python/flink_agents/api/prompts/utils.py: ########## @@ -0,0 +1,38 @@ +################################################################################ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +################################################################################# +from string import Formatter +from typing import Any + +from typing_extensions import override + + +class SafeFormatter(Formatter): + """Safe string formatter that does not raise KeyError if key is missing.""" + + @override + def get_value(self, key: Any, args: Any, kwargs: Any) -> Any: + if isinstance(key, int): + return args[key] + else: + if key in kwargs: + return kwargs[key] + else: + return str(key) + + +formatter = SafeFormatter() Review Comment: Uppercase ########## python/flink_agents/plan/resource_provider.py: ########## @@ -44,7 +44,7 @@ class ResourceProvider(BaseModel, ABC): type: ResourceType @abstractmethod - def provide(self) -> Resource: + def provide(self, **kwargs: Any) -> Resource: Review Comment: We should use explicit argument here. ########## python/flink_agents/plan/resource_provider.py: ########## @@ -81,11 +81,27 @@ class PythonResourceProvider(ResourceProvider): clazz: str kwargs: Dict[str, Any] - def provide(self) -> Resource: + def provide(self, **kwargs: Any) -> Resource: """Create resource in runtime.""" module = importlib.import_module(self.module) cls = getattr(module, self.clazz) - return cls(**self.kwargs) + resource = cls(**self.kwargs) + if self.type == ResourceType.CHAT_MODEL: + plan = kwargs["plan"] + # bind tools + if resource.tools is not None: + resource.bind_tools( + [ + plan.get_resource(name, ResourceType.TOOL) + for name in resource.tools + ] + ) + # bind prompt + if resource.prompt is not None and isinstance(resource.prompt, str): + resource.prompt = plan.get_resource( + resource.prompt, ResourceType.PROMPT + ) Review Comment: Where is `resource.tools` set? It is unclear that who is responsible for binding the model with tools. - Looking at this piece of codes, it seems the provider is responsible for binding the model with tools, by calling `bind_tools`. But the problems is how does the provider know which tools to bind? - Looking at `test_build_in_actions.py`, we provide tools as an argument of the model resource provider, which will be passed into the constructor of the model. That means we leave it to the model implementation to decide how to use the arguments. ########## python/flink_agents/plan/resource_provider.py: ########## @@ -81,11 +81,27 @@ class PythonResourceProvider(ResourceProvider): clazz: str kwargs: Dict[str, Any] - def provide(self) -> Resource: + def provide(self, **kwargs: Any) -> Resource: """Create resource in runtime.""" module = importlib.import_module(self.module) cls = getattr(module, self.clazz) - return cls(**self.kwargs) + resource = cls(**self.kwargs) + if self.type == ResourceType.CHAT_MODEL: + plan = kwargs["plan"] + # bind tools + if resource.tools is not None: + resource.bind_tools( + [ + plan.get_resource(name, ResourceType.TOOL) + for name in resource.tools + ] + ) + # bind prompt + if resource.prompt is not None and isinstance(resource.prompt, str): + resource.prompt = plan.get_resource( + resource.prompt, ResourceType.PROMPT + ) Review Comment: This should be done in the chat model provider. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
