wenjin272 commented on code in PR #611: URL: https://github.com/apache/flink-agents/pull/611#discussion_r3056626246
########## python/flink_agents/integrations/embedding_models/tongyi_embedding_model.py: ########## @@ -0,0 +1,178 @@ +################################################################################ +# 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 os +from http import HTTPStatus +from typing import Any, Dict + +import dashscope +from pydantic import Field + +from flink_agents.api.embedding_models.embedding_model import ( + BaseEmbeddingModelConnection, + BaseEmbeddingModelSetup, +) + +DEFAULT_REQUEST_TIMEOUT = 30.0 +DEFAULT_MODEL = "text-embedding-v4" + + +class TongyiEmbeddingModelConnection(BaseEmbeddingModelConnection): + """Tongyi Embedding Model Connection which manages connection to DashScope API. + + Visit https://dashscope.console.aliyun.com/ to get your API key. + + Attributes: + ---------- + api_key : str + DashScope API key for authentication. + request_timeout : float + The timeout for making http request to Tongyi API server. + """ + + api_key: str = Field( + default_factory=lambda: os.environ.get("DASHSCOPE_API_KEY"), Review Comment: `os.environ.get("DASHSCOPE_API_KEY")` may return None, but api_key must be `str` ########## python/flink_agents/integrations/embedding_models/tongyi_embedding_model.py: ########## @@ -0,0 +1,178 @@ +################################################################################ +# 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 os +from http import HTTPStatus +from typing import Any, Dict + +import dashscope +from pydantic import Field + +from flink_agents.api.embedding_models.embedding_model import ( + BaseEmbeddingModelConnection, + BaseEmbeddingModelSetup, +) + +DEFAULT_REQUEST_TIMEOUT = 30.0 +DEFAULT_MODEL = "text-embedding-v4" + + +class TongyiEmbeddingModelConnection(BaseEmbeddingModelConnection): + """Tongyi Embedding Model Connection which manages connection to DashScope API. + + Visit https://dashscope.console.aliyun.com/ to get your API key. + + Attributes: + ---------- + api_key : str + DashScope API key for authentication. + request_timeout : float + The timeout for making http request to Tongyi API server. + """ + + api_key: str = Field( + default_factory=lambda: os.environ.get("DASHSCOPE_API_KEY"), + description="Your DashScope API key.", + ) + request_timeout: float = Field( + default=DEFAULT_REQUEST_TIMEOUT, + description="The timeout for making http request to Tongyi API server.", + ) + + def __init__( + self, + api_key: str | None = None, + request_timeout: float | None = DEFAULT_REQUEST_TIMEOUT, + **kwargs: Any, + ) -> None: + """Init method.""" + resolved_api_key = api_key or os.environ.get("DASHSCOPE_API_KEY") + if not resolved_api_key: + msg = ( + "DashScope API key is not provided. " + "Please pass it as an argument or set the 'DASHSCOPE_API_KEY' environment variable." + ) + raise ValueError(msg) + + super().__init__( + api_key=resolved_api_key, + request_timeout=request_timeout, + **kwargs, + ) + + def embed(self, text: str, **kwargs: Any) -> list[float]: Review Comment: It appears that `embed` only supports embedding a single text and does not support `Sequence[str]`, which is inconsistent with the interface in the base class. ########## python/flink_agents/api/resource.py: ########## @@ -296,6 +296,10 @@ class EmbeddingModel: OPENAI_CONNECTION = "flink_agents.integrations.embedding_models.openai_embedding_model.OpenAIEmbeddingModelConnection" OPENAI_SETUP = "flink_agents.integrations.embedding_models.openai_embedding_model.OpenAIEmbeddingModelSetup" + # Tongyi + TONGYI_EMBEDDING_MODEL_CONNECTION = "flink_agents.integrations.embedding_models.tongyi_embedding_model.TongyiEmbeddingModelConnection" + TONGYI_EMBEDDING_MODEL_SETUP = "flink_agents.integrations.embedding_models.tongyi_embedding_model.TongyiEmbeddingModelSetup" Review Comment: Since we use `Class` to distinguish between `EmbeddingModel` and `ChatModel`, there is no need to include `EMBEDDING_MODEL` in the constants ########## python/flink_agents/integrations/embedding_models/tongyi_embedding_model.py: ########## @@ -0,0 +1,178 @@ +################################################################################ +# 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 os +from http import HTTPStatus +from typing import Any, Dict + +import dashscope +from pydantic import Field + +from flink_agents.api.embedding_models.embedding_model import ( + BaseEmbeddingModelConnection, + BaseEmbeddingModelSetup, +) + +DEFAULT_REQUEST_TIMEOUT = 30.0 +DEFAULT_MODEL = "text-embedding-v4" + + +class TongyiEmbeddingModelConnection(BaseEmbeddingModelConnection): + """Tongyi Embedding Model Connection which manages connection to DashScope API. + + Visit https://dashscope.console.aliyun.com/ to get your API key. + + Attributes: + ---------- + api_key : str + DashScope API key for authentication. + request_timeout : float + The timeout for making http request to Tongyi API server. + """ + + api_key: str = Field( + default_factory=lambda: os.environ.get("DASHSCOPE_API_KEY"), + description="Your DashScope API key.", + ) + request_timeout: float = Field( Review Comment: It appears that request_timeout is not being used. -- 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]
