diff --git a/config/examples/huoshan_ark.yaml b/config/examples/huoshan_ark.yaml new file mode 100644 index 000000000..e019196ec --- /dev/null +++ b/config/examples/huoshan_ark.yaml @@ -0,0 +1,8 @@ +# Full Example: https://github.com/geekan/MetaGPT/blob/main/config/config2.example.yaml +# Reflected Code: https://github.com/geekan/MetaGPT/blob/main/metagpt/config2.py +# Config Docs: https://docs.deepwisdom.ai/main/en/guide/get_started/configuration.html +llm: + api_type: "ark" # or azure / ollama / groq etc. + model: "" # your model endpoint like ep-xxx + base_url: "https://ark.cn-beijing.volces.com/api/v3" # or forward url / other llm url + api_key: "" # your api-key like ey…… \ No newline at end of file diff --git a/metagpt/configs/llm_config.py b/metagpt/configs/llm_config.py index dbf04dac6..12bb8541e 100644 --- a/metagpt/configs/llm_config.py +++ b/metagpt/configs/llm_config.py @@ -33,6 +33,7 @@ class LLMType(Enum): YI = "yi" # lingyiwanwu OPENROUTER = "openrouter" BEDROCK = "bedrock" + ARK = "ark" def __missing__(self, key): return self.OPENAI diff --git a/metagpt/provider/__init__.py b/metagpt/provider/__init__.py index fcb5fa32a..e51dae433 100644 --- a/metagpt/provider/__init__.py +++ b/metagpt/provider/__init__.py @@ -18,6 +18,7 @@ from metagpt.provider.qianfan_api import QianFanLLM from metagpt.provider.dashscope_api import DashScopeLLM from metagpt.provider.anthropic_api import AnthropicLLM from metagpt.provider.bedrock_api import BedrockLLM +from metagpt.provider.ark_api import ArkLLM __all__ = [ "GeminiLLM", @@ -32,4 +33,5 @@ __all__ = [ "DashScopeLLM", "AnthropicLLM", "BedrockLLM", + "ArkLLM" ] diff --git a/metagpt/provider/ark_api.py b/metagpt/provider/ark_api.py new file mode 100644 index 000000000..d5a31b7d6 --- /dev/null +++ b/metagpt/provider/ark_api.py @@ -0,0 +1,43 @@ +from openai import AsyncStream +from openai.types import CompletionUsage +from openai.types.chat import ChatCompletion, ChatCompletionChunk + +from metagpt.provider.openai_api import OpenAILLM +from metagpt.configs.llm_config import LLMType +from metagpt.provider.llm_provider_registry import register_provider +from metagpt.const import USE_CONFIG_TIMEOUT +from metagpt.logs import log_llm_stream + +@register_provider(LLMType.ARK) +class ArkLLM(OpenAILLM): + """ + 用于火山方舟的API + 见:https://www.volcengine.com/docs/82379/1263482 + """ + + async def _achat_completion_stream(self, messages: list[dict], timeout=USE_CONFIG_TIMEOUT) -> str: + response: AsyncStream[ChatCompletionChunk] = await self.aclient.chat.completions.create( + **self._cons_kwargs(messages, timeout=self.get_timeout(timeout)), + stream=True, + extra_body={"stream_options": {"include_usage": True}} + ) + usage = None + collected_messages = [] + async for chunk in response: + chunk_message = chunk.choices[0].delta.content or "" if chunk.choices else "" # extract the message + log_llm_stream(chunk_message) + collected_messages.append(chunk_message) + if chunk.usage: + # the usage of ark when streaming is in the last chunk while others are null + usage=CompletionUsage(**chunk.usage) + + log_llm_stream("\n") + full_reply_content = "".join(collected_messages) + self._update_costs(usage,chunk.model) + return full_reply_content + + async def _achat_completion(self, messages: list[dict], timeout=USE_CONFIG_TIMEOUT) -> ChatCompletion: + kwargs = self._cons_kwargs(messages, timeout=self.get_timeout(timeout)) + rsp: ChatCompletion = await self.aclient.chat.completions.create(**kwargs) + self._update_costs(rsp.usage,rsp.model) + return rsp diff --git a/metagpt/utils/token_counter.py b/metagpt/utils/token_counter.py index a8652e607..cbc11a7e2 100644 --- a/metagpt/utils/token_counter.py +++ b/metagpt/utils/token_counter.py @@ -68,6 +68,13 @@ TOKEN_COSTS = { "openai/gpt-4-turbo-preview": {"prompt": 0.01, "completion": 0.03}, "deepseek-chat": {"prompt": 0.00014, "completion": 0.00028}, "deepseek-coder": {"prompt": 0.00014, "completion": 0.00028}, + # For ark model + "doubao-lite-4k-240515": {"prompt": 0.000042, "completion": 0.000084}, + "doubao-lite-32k-240515": {"prompt": 0.000042, "completion": 0.000084}, + "doubao-lite-128k-240515": {"prompt": 0.00011, "completion": 0.00013}, + "doubao-pro-4k-240515":{"prompt":0.00011,"completion":0.00028}, + "doubao-pro-32k-240515":{"prompt":0.00011,"completion":0.00028}, + "doubao-pro-128k-240515":{"prompt":0.0007,"completion":0.0012}, } diff --git a/volcengine b/volcengine new file mode 160000 index 000000000..e62feb00e --- /dev/null +++ b/volcengine @@ -0,0 +1 @@ +Subproject commit e62feb00e583a64243a58bf77de49cfca2c96210