wenjin272 commented on code in PR #121: URL: https://github.com/apache/flink-agents/pull/121#discussion_r2289988461
########## python/flink_agents/integrations/chat_models/tongyi_chat_model.py: ########## @@ -0,0 +1,246 @@ +################################################################################ +# 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 +import uuid +from typing import TYPE_CHECKING, Any, Dict, List, Optional, Sequence, cast + +from openai import NOT_GIVEN, OpenAI +from openai.types.chat.chat_completion_message_param import ChatCompletionMessageParam +from pydantic import Field + +from flink_agents.api.chat_message import ChatMessage, MessageRole +from flink_agents.api.chat_models.chat_model import ( + BaseChatModelConnection, + BaseChatModelSetup, +) +from flink_agents.api.tools.tool import BaseTool + +if TYPE_CHECKING: + from openai.types.chat import ChatCompletionToolParam + +DEFAULT_REQUEST_TIMEOUT = 60.0 +DEFAULT_BASE_URL = "https://dashscope.aliyuncs.com/compatible-mode/v1" +DEFAULT_MODEL = "qwen-plus" + + +class TongyiChatModelConnection(BaseChatModelConnection): + """Tongyi ChatModelConnection which manages the connection to the Tongyi API server. + + This class is based on DashScope's OpenAI-compatible API. + Run `pip install openai` to use. + + Attributes: + ---------- + api_key : str + Your DashScope API key. + base_url : str + Base url for Tongyi API. + model : str + Model name to use. + 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.", + ) + base_url: str = Field( + default=DEFAULT_BASE_URL, + description="Base url for Tongyi API.", + ) + model: str = Field(default=DEFAULT_MODEL, description="Model name to use.") + request_timeout: float = Field( + default=DEFAULT_REQUEST_TIMEOUT, + description="The timeout for making http request to Tongyi API server.", + ) + + __client: OpenAI = None Review Comment: Maybe we should use dashscope api rather than openai api here, and provide openai-like chatmodel alone for chat models can be called by openai api. -- 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]
