xintongsong commented on code in PR #128: URL: https://github.com/apache/flink-agents/pull/128#discussion_r2303140309
########## python/flink_agents/integrations/chat_models/openai/utils.py: ########## @@ -0,0 +1,131 @@ +################################################################################ +# 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. +################################################################################# +import json +import os +from typing import List, Optional, Sequence, Tuple, cast + +import openai +from openai.types.chat import ChatCompletionMessage, ChatCompletionMessageParam + +from flink_agents.api.chat_message import ChatMessage, MessageRole + +DEFAULT_OPENAI_API_BASE_URL = "https://api.openai.com/v1" +DEFAULT_OPENAI_API_VERSION = "" + + +def resolve_openai_credentials( + api_key: Optional[str] = None, + api_base_url: Optional[str] = None, + api_version: Optional[str] = None, +) -> Tuple[Optional[str], str, str]: + """Resolve OpenAI credentials. + + The order of precedence is: + 1. param + 2. env + 3. openai module + 4. default + """ + # resolve from param or env + api_key = get_from_param_or_env("api_key", api_key, "OPENAI_API_KEY", "") + api_base_url = get_from_param_or_env( + "api_base", api_base_url, "OPENAI_API_BASE", "" + ) + api_version = get_from_param_or_env( + "api_version", api_version, "OPENAI_API_VERSION", "" + ) + + # resolve from openai module or default + final_api_key = api_key or openai.api_key or "" + final_api_base_url = api_base_url or openai.base_url or DEFAULT_OPENAI_API_BASE_URL + final_api_version = api_version or openai.api_version or DEFAULT_OPENAI_API_VERSION + + return final_api_key, str(final_api_base_url), final_api_version + + +def get_from_param_or_env( + key: str, + param: Optional[str] = None, + env_key: Optional[str] = None, + default: Optional[str] = None, +) -> str: Review Comment: ```suggestion def resolve_parameter( param_name: str, value_from_args: Optional[str] = None, env_var_name: Optional[str] = None, default_value: Optional[str] = None, ) -> str: ``` ########## python/flink_agents/integrations/chat_models/openai/utils.py: ########## @@ -0,0 +1,131 @@ +################################################################################ +# 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. +################################################################################# +import json +import os +from typing import List, Optional, Sequence, Tuple, cast + +import openai +from openai.types.chat import ChatCompletionMessage, ChatCompletionMessageParam + +from flink_agents.api.chat_message import ChatMessage, MessageRole + +DEFAULT_OPENAI_API_BASE_URL = "https://api.openai.com/v1" +DEFAULT_OPENAI_API_VERSION = "" + + +def resolve_openai_credentials( Review Comment: We have too many `utils.py` files. Just in this PR, there are two of them. Let's give them some meaningful names. ########## python/flink_agents/integrations/chat_models/openai/utils.py: ########## @@ -0,0 +1,131 @@ +################################################################################ +# 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. +################################################################################# +import json +import os +from typing import List, Optional, Sequence, Tuple, cast + +import openai +from openai.types.chat import ChatCompletionMessage, ChatCompletionMessageParam + +from flink_agents.api.chat_message import ChatMessage, MessageRole + +DEFAULT_OPENAI_API_BASE_URL = "https://api.openai.com/v1" +DEFAULT_OPENAI_API_VERSION = "" + + +def resolve_openai_credentials( + api_key: Optional[str] = None, + api_base_url: Optional[str] = None, + api_version: Optional[str] = None, +) -> Tuple[Optional[str], str, str]: + """Resolve OpenAI credentials. + + The order of precedence is: + 1. param + 2. env + 3. openai module + 4. default + """ + # resolve from param or env + api_key = get_from_param_or_env("api_key", api_key, "OPENAI_API_KEY", "") + api_base_url = get_from_param_or_env( + "api_base", api_base_url, "OPENAI_API_BASE", "" + ) + api_version = get_from_param_or_env( + "api_version", api_version, "OPENAI_API_VERSION", "" + ) + + # resolve from openai module or default + final_api_key = api_key or openai.api_key or "" + final_api_base_url = api_base_url or openai.base_url or DEFAULT_OPENAI_API_BASE_URL + final_api_version = api_version or openai.api_version or DEFAULT_OPENAI_API_VERSION + + return final_api_key, str(final_api_base_url), final_api_version + + +def get_from_param_or_env( + key: str, + param: Optional[str] = None, + env_key: Optional[str] = None, + default: Optional[str] = None, +) -> str: Review Comment: And we need more explain about the arguments and behavior of this function. -- 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: issues-unsubscr...@flink.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org