add huoshan_ark

This commit is contained in:
usamimeri_renko 2024-05-20 15:12:49 +08:00
parent e03db313a2
commit 48189e98d8
6 changed files with 62 additions and 0 deletions

View file

@ -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……

View file

@ -33,6 +33,7 @@ class LLMType(Enum):
YI = "yi" # lingyiwanwu
OPENROUTER = "openrouter"
BEDROCK = "bedrock"
ARK = "ark"
def __missing__(self, key):
return self.OPENAI

View file

@ -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"
]

View file

@ -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

View file

@ -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},
}

1
volcengine Submodule

@ -0,0 +1 @@
Subproject commit e62feb00e583a64243a58bf77de49cfca2c96210