camel-ai 0.2.19__py3-none-any.whl → 0.2.20a1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of camel-ai might be problematic. Click here for more details.

camel/__init__.py CHANGED
@@ -14,7 +14,7 @@
14
14
 
15
15
  from camel.logger import disable_logging, enable_logging, set_log_level
16
16
 
17
- __version__ = '0.2.19'
17
+ __version__ = '0.2.20a1'
18
18
 
19
19
  __all__ = [
20
20
  '__version__',
@@ -86,18 +86,18 @@ except (ImportError, AttributeError):
86
86
  from camel.utils import track_agent
87
87
 
88
88
 
89
- class FunctionCallingRecord(BaseModel):
90
- r"""Historical records of functions called in the conversation.
89
+ class ToolCallingRecord(BaseModel):
90
+ r"""Historical records of tools called in the conversation.
91
91
 
92
92
  Attributes:
93
- func_name (str): The name of the function being called.
93
+ tool_name (str): The name of the tool being called.
94
94
  args (Dict[str, Any]): The dictionary of arguments passed to
95
- the function.
96
- result (Any): The execution result of calling this function.
95
+ the tools.
96
+ result (Any): The execution result of calling this tool.
97
97
  tool_call_id (str): The ID of the tool call, if available.
98
98
  """
99
99
 
100
- func_name: str
100
+ tool_name: str
101
101
  args: Dict[str, Any]
102
102
  result: Any
103
103
  tool_call_id: str
@@ -106,10 +106,10 @@ class FunctionCallingRecord(BaseModel):
106
106
  r"""Overridden version of the string function.
107
107
 
108
108
  Returns:
109
- str: Modified string to represent the function calling.
109
+ str: Modified string to represent the tool calling.
110
110
  """
111
111
  return (
112
- f"Function Execution: {self.func_name}\n"
112
+ f"Tool Execution: {self.tool_name}\n"
113
113
  f"\tArgs: {self.args}\n"
114
114
  f"\tResult: {self.result}\n"
115
115
  )
@@ -489,7 +489,7 @@ class ChatAgent(BaseAgent):
489
489
  usage: Optional[Dict[str, int]],
490
490
  termination_reasons: List[str],
491
491
  num_tokens: int,
492
- tool_calls: List[FunctionCallingRecord],
492
+ tool_calls: List[ToolCallingRecord],
493
493
  external_tool_request: Optional[ChatCompletionMessageToolCall] = None,
494
494
  ) -> Dict[str, Any]:
495
495
  r"""Returns a dictionary containing information about the chat session.
@@ -501,7 +501,7 @@ class ChatAgent(BaseAgent):
501
501
  termination_reasons (List[str]): The reasons for the termination
502
502
  of the chat session.
503
503
  num_tokens (int): The number of tokens used in the chat session.
504
- tool_calls (List[FunctionCallingRecord]): The list of function
504
+ tool_calls (List[ToolCallingRecord]): The list of function
505
505
  calling records, containing the information of called tools.
506
506
  external_tool_request
507
507
  (Optional[ChatCompletionMessageToolCall], optional):
@@ -645,7 +645,7 @@ class ChatAgent(BaseAgent):
645
645
  )
646
646
 
647
647
  # Record function calls made during the session
648
- tool_call_records: List[FunctionCallingRecord] = []
648
+ tool_call_records: List[ToolCallingRecord] = []
649
649
 
650
650
  external_tool_request = None
651
651
 
@@ -885,7 +885,7 @@ class ChatAgent(BaseAgent):
885
885
 
886
886
  self.update_memory(input_message, OpenAIBackendRole.USER)
887
887
 
888
- tool_call_records: List[FunctionCallingRecord] = []
888
+ tool_call_records: List[ToolCallingRecord] = []
889
889
  while True:
890
890
  try:
891
891
  openai_messages, num_tokens = self.memory.get_context()
@@ -970,7 +970,7 @@ class ChatAgent(BaseAgent):
970
970
 
971
971
  def _step_tool_call_and_update(
972
972
  self, response: ChatCompletion
973
- ) -> FunctionCallingRecord:
973
+ ) -> ToolCallingRecord:
974
974
  r"""Processes a function call within the chat completion response,
975
975
  records the function call in the provided list of tool calls and
976
976
  updates the memory of the current agent.
@@ -980,7 +980,7 @@ class ChatAgent(BaseAgent):
980
980
  completion.
981
981
 
982
982
  Returns:
983
- FunctionCallingRecord: The record of calling the function.
983
+ ToolCallingRecord: The record of calling the function.
984
984
  """
985
985
 
986
986
  # Perform function calling
@@ -996,7 +996,7 @@ class ChatAgent(BaseAgent):
996
996
 
997
997
  async def _step_tool_call_and_update_async(
998
998
  self, response: ChatCompletion
999
- ) -> FunctionCallingRecord:
999
+ ) -> ToolCallingRecord:
1000
1000
  (
1001
1001
  func_assistant_msg,
1002
1002
  func_result_msg,
@@ -1015,7 +1015,7 @@ class ChatAgent(BaseAgent):
1015
1015
  List[str],
1016
1016
  Dict[str, int],
1017
1017
  str,
1018
- FunctionCallingRecord,
1018
+ ToolCallingRecord,
1019
1019
  int,
1020
1020
  ]:
1021
1021
  r"""Internal function of structuring the output of the agent based on
@@ -1027,7 +1027,7 @@ class ChatAgent(BaseAgent):
1027
1027
 
1028
1028
  Returns:
1029
1029
  Tuple[List[BaseMessage], List[str], Dict[str, int], str,
1030
- FunctionCallingRecord, int]:
1030
+ ToolCallingRecord, int]:
1031
1031
  A tuple containing the output messages, finish reasons, usage
1032
1032
  dictionary, response ID, function calling record, and number of
1033
1033
  tokens.
@@ -1141,7 +1141,7 @@ class ChatAgent(BaseAgent):
1141
1141
  finish_reasons: List[str],
1142
1142
  usage_dict: Dict[str, int],
1143
1143
  response_id: str,
1144
- tool_calls: List[FunctionCallingRecord],
1144
+ tool_calls: List[ToolCallingRecord],
1145
1145
  num_tokens: int,
1146
1146
  external_tool_request: Optional[ChatCompletionMessageToolCall] = None,
1147
1147
  ) -> Dict[str, Any]:
@@ -1160,7 +1160,7 @@ class ChatAgent(BaseAgent):
1160
1160
  usage_dict (Dict[str, int]): Dictionary containing token usage
1161
1161
  information.
1162
1162
  response_id (str): The ID of the response from the model.
1163
- tool_calls (List[FunctionCallingRecord]): Records of function calls
1163
+ tool_calls (List[ToolCallingRecord]): Records of function calls
1164
1164
  made during this step.
1165
1165
  num_tokens (int): The number of tokens used in this step.
1166
1166
  external_tool_request (Optional[ChatCompletionMessageToolCall]):
@@ -1335,7 +1335,7 @@ class ChatAgent(BaseAgent):
1335
1335
  def _step_token_exceed(
1336
1336
  self,
1337
1337
  num_tokens: int,
1338
- tool_calls: List[FunctionCallingRecord],
1338
+ tool_calls: List[ToolCallingRecord],
1339
1339
  termination_reason: str,
1340
1340
  ) -> ChatAgentResponse:
1341
1341
  r"""Return trivial response containing number of tokens and information
@@ -1343,7 +1343,7 @@ class ChatAgent(BaseAgent):
1343
1343
 
1344
1344
  Args:
1345
1345
  num_tokens (int): Number of tokens in the messages.
1346
- tool_calls (List[FunctionCallingRecord]): List of information
1346
+ tool_calls (List[ToolCallingRecord]): List of information
1347
1347
  objects of functions called in the current step.
1348
1348
  termination_reason (str): String of termination reason.
1349
1349
 
@@ -1372,7 +1372,7 @@ class ChatAgent(BaseAgent):
1372
1372
  self,
1373
1373
  response: ChatCompletion,
1374
1374
  ) -> Tuple[
1375
- FunctionCallingMessage, FunctionCallingMessage, FunctionCallingRecord
1375
+ FunctionCallingMessage, FunctionCallingMessage, ToolCallingRecord
1376
1376
  ]:
1377
1377
  r"""Execute the function with arguments following the model's response.
1378
1378
 
@@ -1418,8 +1418,8 @@ class ChatAgent(BaseAgent):
1418
1418
  )
1419
1419
 
1420
1420
  # Record information about this function call
1421
- func_record = FunctionCallingRecord(
1422
- func_name=func_name,
1421
+ func_record = ToolCallingRecord(
1422
+ tool_name=func_name,
1423
1423
  args=args,
1424
1424
  result=result,
1425
1425
  tool_call_id=tool_call_id,
@@ -1442,7 +1442,7 @@ class ChatAgent(BaseAgent):
1442
1442
  self,
1443
1443
  response: ChatCompletion,
1444
1444
  ) -> Tuple[
1445
- FunctionCallingMessage, FunctionCallingMessage, FunctionCallingRecord
1445
+ FunctionCallingMessage, FunctionCallingMessage, ToolCallingRecord
1446
1446
  ]:
1447
1447
  r"""Execute the async function with arguments following the model's
1448
1448
  response.
@@ -1488,8 +1488,8 @@ class ChatAgent(BaseAgent):
1488
1488
  )
1489
1489
 
1490
1490
  # Record information about this function call
1491
- func_record = FunctionCallingRecord(
1492
- func_name=func_name,
1491
+ func_record = ToolCallingRecord(
1492
+ tool_name=func_name,
1493
1493
  args=args,
1494
1494
  result=result,
1495
1495
  tool_call_id=tool_call_id,
camel/configs/__init__.py CHANGED
@@ -20,6 +20,7 @@ from .groq_config import GROQ_API_PARAMS, GroqConfig
20
20
  from .internlm_config import INTERNLM_API_PARAMS, InternLMConfig
21
21
  from .litellm_config import LITELLM_API_PARAMS, LiteLLMConfig
22
22
  from .mistral_config import MISTRAL_API_PARAMS, MistralConfig
23
+ from .moonshot_config import MOONSHOT_API_PARAMS, MoonshotConfig
23
24
  from .nvidia_config import NVIDIA_API_PARAMS, NvidiaConfig
24
25
  from .ollama_config import OLLAMA_API_PARAMS, OllamaConfig
25
26
  from .openai_config import OPENAI_API_PARAMS, ChatGPTConfig
@@ -32,6 +33,7 @@ from .samba_config import (
32
33
  SambaVerseAPIConfig,
33
34
  )
34
35
  from .sglang_config import SGLANG_API_PARAMS, SGLangConfig
36
+ from .siliconflow_config import SILICONFLOW_API_PARAMS, SiliconFlowConfig
35
37
  from .togetherai_config import TOGETHERAI_API_PARAMS, TogetherAIConfig
36
38
  from .vllm_config import VLLM_API_PARAMS, VLLMConfig
37
39
  from .yi_config import YI_API_PARAMS, YiConfig
@@ -79,4 +81,8 @@ __all__ = [
79
81
  'DEEPSEEK_API_PARAMS',
80
82
  'InternLMConfig',
81
83
  'INTERNLM_API_PARAMS',
84
+ 'MoonshotConfig',
85
+ "MOONSHOT_API_PARAMS",
86
+ 'SiliconFlowConfig',
87
+ 'SILICONFLOW_API_PARAMS',
82
88
  ]
@@ -0,0 +1,63 @@
1
+ # ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
2
+ # Licensed under the Apache License, Version 2.0 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License at
5
+ #
6
+ # http://www.apache.org/licenses/LICENSE-2.0
7
+ #
8
+ # Unless required by applicable law or agreed to in writing, software
9
+ # distributed under the License is distributed on an "AS IS" BASIS,
10
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ # See the License for the specific language governing permissions and
12
+ # limitations under the License.
13
+ # ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
14
+
15
+ from typing import List, Optional, Union
16
+
17
+ from camel.configs.base_config import BaseConfig
18
+
19
+
20
+ class MoonshotConfig(BaseConfig):
21
+ r"""Defines the parameters for generating chat completions using the
22
+ Moonshot API. You can refer to the following link for more details:
23
+ https://platform.moonshot.cn/docs/api-reference
24
+
25
+ Args:
26
+ temperature (float, optional): Controls randomness in the response.
27
+ Lower values make the output more focused and deterministic.
28
+ (default: :obj:`0.3`)
29
+ max_tokens (int, optional): The maximum number of tokens to generate.
30
+ (default: :obj:`None`)
31
+ stream (bool, optional): Whether to stream the response.
32
+ (default: :obj:`False`)
33
+ tools (list, optional): List of tools that the model can use for
34
+ function calling. Each tool should be a dictionary containing
35
+ type, function name, description, and parameters.
36
+ (default: :obj:`None`)
37
+ top_p (float, optional): Controls diversity via nucleus sampling.
38
+ (default: :obj:`1.0`)
39
+ n (int, optional): How many chat completion choices to generate for
40
+ each input message. (default: :obj:`1`)
41
+ presence_penalty (float, optional): Penalty for new tokens based on
42
+ whether they appear in the text so far.
43
+ (default: :obj:`0.0`)
44
+ frequency_penalty (float, optional): Penalty for new tokens based on
45
+ their frequency in the text so far.
46
+ (default: :obj:`0.0`)
47
+ stop (Optional[Union[str, List[str]]], optional): Up to 4 sequences
48
+ where the API will stop generating further tokens.
49
+ (default: :obj:`None`)
50
+ """
51
+
52
+ temperature: float = 0.3
53
+ max_tokens: Optional[int] = None
54
+ stream: bool = False
55
+ tools: Optional[list] = None
56
+ top_p: float = 1.0
57
+ n: int = 1
58
+ presence_penalty: float = 0.0
59
+ frequency_penalty: float = 0.0
60
+ stop: Optional[Union[str, List[str]]] = None
61
+
62
+
63
+ MOONSHOT_API_PARAMS = {param for param in MoonshotConfig.model_fields.keys()}
@@ -0,0 +1,91 @@
1
+ # ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
2
+ # Licensed under the Apache License, Version 2.0 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License at
5
+ #
6
+ # http://www.apache.org/licenses/LICENSE-2.0
7
+ #
8
+ # Unless required by applicable law or agreed to in writing, software
9
+ # distributed under the License is distributed on an "AS IS" BASIS,
10
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ # See the License for the specific language governing permissions and
12
+ # limitations under the License.
13
+ # ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
14
+ from __future__ import annotations
15
+
16
+ from typing import Any, Sequence, Type, Union
17
+
18
+ from pydantic import BaseModel
19
+
20
+ from camel.configs.base_config import BaseConfig
21
+ from camel.types import NOT_GIVEN, NotGiven
22
+
23
+
24
+ class SiliconFlowConfig(BaseConfig):
25
+ r"""Defines the parameters for generating chat completions using the
26
+ SiliconFlow API.
27
+
28
+ Args:
29
+ temperature (float, optional): Determines the degree of randomness
30
+ in the response. (default: :obj:`0.7`)
31
+ top_p (float, optional): The top_p (nucleus) parameter is used to
32
+ dynamically adjust the number of choices for each predicted token
33
+ based on the cumulative probabilities. (default: :obj:`0.7`)
34
+ n (int, optional): Number of generations to return. (default::obj:`1`)
35
+ response_format (object, optional): An object specifying the format
36
+ that the model must output.
37
+ stream (bool, optional): If set, tokens are returned as Server-Sent
38
+ Events as they are made available. (default: :obj:`False`)
39
+ stop (str or list, optional): Up to :obj:`4` sequences where the API
40
+ will stop generating further tokens. (default: :obj:`None`)
41
+ max_tokens (int, optional): The maximum number of tokens to generate.
42
+ (default: :obj:`None`)
43
+ frequency_penalty (float, optional): Number between :obj:`-2.0` and
44
+ :obj:`2.0`. Positive values penalize new tokens based on their
45
+ existing frequency in the text so far, decreasing the model's
46
+ likelihood to repeat the same line verbatim. See more information
47
+ about frequency and presence penalties. (default: :obj:`0.0`)
48
+ tools (list[FunctionTool], optional): A list of tools the model may
49
+ call. Currently, only functions are supported as a tool. Use this
50
+ to provide a list of functions the model may generate JSON inputs
51
+ for. A max of 128 functions are supported.
52
+ """
53
+
54
+ temperature: float = 0.7
55
+ top_p: float = 0.7
56
+ n: int = 1
57
+ stream: bool = False
58
+ stop: Union[str, Sequence[str], NotGiven] = NOT_GIVEN
59
+ max_tokens: Union[int, NotGiven] = NOT_GIVEN
60
+ response_format: Union[Type[BaseModel], dict, NotGiven] = NOT_GIVEN
61
+ frequency_penalty: float = 0.0
62
+
63
+ def as_dict(self) -> dict[str, Any]:
64
+ r"""Convert the current configuration to a dictionary.
65
+
66
+ This method converts the current configuration object to a dictionary
67
+ representation, which can be used for serialization or other purposes.
68
+
69
+ Returns:
70
+ dict[str, Any]: A dictionary representation of the current
71
+ configuration.
72
+ """
73
+ config_dict = self.model_dump()
74
+ if self.tools:
75
+ from camel.toolkits import FunctionTool
76
+
77
+ tools_schema = []
78
+ for tool in self.tools:
79
+ if not isinstance(tool, FunctionTool):
80
+ raise ValueError(
81
+ f"The tool {tool} should "
82
+ "be an instance of `FunctionTool`."
83
+ )
84
+ tools_schema.append(tool.get_openai_tool_schema())
85
+ config_dict["tools"] = NOT_GIVEN
86
+ return config_dict
87
+
88
+
89
+ SILICONFLOW_API_PARAMS = {
90
+ param for param in SiliconFlowConfig.model_fields.keys()
91
+ }
@@ -32,19 +32,19 @@ class HuggingFaceDatasetManager(BaseDatasetManager):
32
32
 
33
33
  Args:
34
34
  token (str): The Hugging Face API token. If not provided, the token
35
- will be read from the environment variable `HUGGING_FACE_TOKEN`.
35
+ will be read from the environment variable `HF_TOKEN`.
36
36
  """
37
37
 
38
38
  @api_keys_required(
39
39
  [
40
- ("token", "HUGGING_FACE_TOKEN"),
40
+ ("token", "HF_TOKEN"),
41
41
  ]
42
42
  )
43
43
  @dependencies_required('huggingface_hub')
44
44
  def __init__(self, token: Optional[str] = None):
45
45
  from huggingface_hub import HfApi
46
46
 
47
- self._api_key = token or os.getenv("HUGGING_FACE_TOKEN")
47
+ self._api_key = token or os.getenv("HF_TOKEN")
48
48
  self.api = HfApi(token=self._api_key)
49
49
 
50
50
  def create_dataset_card(
@@ -35,6 +35,11 @@ class JinaEmbedding(BaseEmbedding[Union[str, Image.Image]]):
35
35
  Jina AI. (default: :obj:`None`)
36
36
  dimensions (Optional[int], optional): The dimension of the output
37
37
  embeddings. (default: :obj:`None`)
38
+ embedding_type (Optional[str], optional): The type of embedding format
39
+ to generate. Options: 'int8' (binary encoding with higher storage
40
+ and transfer efficiency), 'uint8' (unsigned binary encoding with
41
+ higher storage and transfer efficiency), 'base64' (base64 string
42
+ encoding with higher transfer efficiency). (default: :obj:`None`)
38
43
  task (Optional[str], optional): The type of task for text embeddings.
39
44
  Options: retrieval.query, retrieval.passage, text-matching,
40
45
  classification, separation. (default: :obj:`None`)
@@ -120,7 +125,7 @@ class JinaEmbedding(BaseEmbedding[Union[str, Image.Image]]):
120
125
  else:
121
126
  raise ValueError(
122
127
  f"Input type {type(obj)} is not supported. "
123
- "Must be either str or PIL.Image"
128
+ "Must be either str or PIL.Image."
124
129
  )
125
130
 
126
131
  data = {
camel/models/__init__.py CHANGED
@@ -24,6 +24,7 @@ from .litellm_model import LiteLLMModel
24
24
  from .mistral_model import MistralModel
25
25
  from .model_factory import ModelFactory
26
26
  from .model_manager import ModelManager, ModelProcessingError
27
+ from .moonshot_model import MoonshotModel
27
28
  from .nemotron_model import NemotronModel
28
29
  from .nvidia_model import NvidiaModel
29
30
  from .ollama_model import OllamaModel
@@ -70,4 +71,5 @@ __all__ = [
70
71
  'DeepSeekModel',
71
72
  'FishAudioModel',
72
73
  'InternLMModel',
74
+ 'MoonshotModel',
73
75
  ]
@@ -23,6 +23,7 @@ from camel.models.groq_model import GroqModel
23
23
  from camel.models.internlm_model import InternLMModel
24
24
  from camel.models.litellm_model import LiteLLMModel
25
25
  from camel.models.mistral_model import MistralModel
26
+ from camel.models.moonshot_model import MoonshotModel
26
27
  from camel.models.nvidia_model import NvidiaModel
27
28
  from camel.models.ollama_model import OllamaModel
28
29
  from camel.models.openai_compatible_model import OpenAICompatibleModel
@@ -31,6 +32,7 @@ from camel.models.qwen_model import QwenModel
31
32
  from camel.models.reka_model import RekaModel
32
33
  from camel.models.samba_model import SambaModel
33
34
  from camel.models.sglang_model import SGLangModel
35
+ from camel.models.siliconflow_model import SiliconFlowModel
34
36
  from camel.models.stub_model import StubModel
35
37
  from camel.models.togetherai_model import TogetherAIModel
36
38
  from camel.models.vllm_model import VLLMModel
@@ -100,6 +102,8 @@ class ModelFactory:
100
102
  model_class = LiteLLMModel
101
103
  elif model_platform.is_nvidia:
102
104
  model_class = NvidiaModel
105
+ elif model_platform.is_siliconflow:
106
+ model_class = SiliconFlowModel
103
107
 
104
108
  elif model_platform.is_openai and model_type.is_openai:
105
109
  model_class = OpenAIModel
@@ -127,6 +131,8 @@ class ModelFactory:
127
131
  model_class = DeepSeekModel
128
132
  elif model_platform.is_internlm and model_type.is_internlm:
129
133
  model_class = InternLMModel
134
+ elif model_platform.is_moonshot and model_type.is_moonshot:
135
+ model_class = MoonshotModel
130
136
  elif model_type == ModelType.STUB:
131
137
  model_class = StubModel
132
138
 
@@ -0,0 +1,138 @@
1
+ # ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
2
+ # Licensed under the Apache License, Version 2.0 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License at
5
+ #
6
+ # http://www.apache.org/licenses/LICENSE-2.0
7
+ #
8
+ # Unless required by applicable law or agreed to in writing, software
9
+ # distributed under the License is distributed on an "AS IS" BASIS,
10
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ # See the License for the specific language governing permissions and
12
+ # limitations under the License.
13
+ # ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
14
+
15
+ import os
16
+ from typing import Any, Dict, List, Optional, Union
17
+
18
+ from openai import OpenAI, Stream
19
+
20
+ from camel.configs import MOONSHOT_API_PARAMS, MoonshotConfig
21
+ from camel.messages import OpenAIMessage
22
+ from camel.models import BaseModelBackend
23
+ from camel.types import (
24
+ ChatCompletion,
25
+ ChatCompletionChunk,
26
+ ModelType,
27
+ )
28
+ from camel.utils import (
29
+ BaseTokenCounter,
30
+ OpenAITokenCounter,
31
+ api_keys_required,
32
+ )
33
+
34
+
35
+ class MoonshotModel(BaseModelBackend):
36
+ r"""Moonshot API in a unified BaseModelBackend interface.
37
+
38
+ Args:
39
+ model_type (Union[ModelType, str]): Model for which a backend is
40
+ created, one of Moonshot series.
41
+ model_config_dict (Optional[Dict[str, Any]], optional): A dictionary
42
+ that will be fed into :obj:`openai.ChatCompletion.create()`. If
43
+ :obj:`None`, :obj:`MoonshotConfig().as_dict()` will be used.
44
+ (default: :obj:`None`)
45
+ api_key (Optional[str], optional): The API key for authenticating with
46
+ the Moonshot service. (default: :obj:`None`)
47
+ url (Optional[str], optional): The url to the Moonshot service.
48
+ (default: :obj:`https://api.moonshot.cn/v1`)
49
+ token_counter (Optional[BaseTokenCounter], optional): Token counter to
50
+ use for the model. If not provided, :obj:`OpenAITokenCounter(
51
+ ModelType.GPT_4)` will be used.
52
+ (default: :obj:`None`)
53
+ """
54
+
55
+ @api_keys_required([("api_key", "MOONSHOT_API_KEY")])
56
+ def __init__(
57
+ self,
58
+ model_type: Union[ModelType, str],
59
+ model_config_dict: Optional[Dict[str, Any]] = None,
60
+ api_key: Optional[str] = None,
61
+ url: Optional[str] = None,
62
+ token_counter: Optional[BaseTokenCounter] = None,
63
+ ) -> None:
64
+ if model_config_dict is None:
65
+ model_config_dict = MoonshotConfig().as_dict()
66
+ api_key = api_key or os.environ.get("MOONSHOT_API_KEY")
67
+ url = url or os.environ.get(
68
+ "MOONSHOT_API_BASE_URL",
69
+ "https://api.moonshot.cn/v1",
70
+ )
71
+ super().__init__(
72
+ model_type, model_config_dict, api_key, url, token_counter
73
+ )
74
+ self._client = OpenAI(
75
+ api_key=self._api_key,
76
+ timeout=180,
77
+ max_retries=3,
78
+ base_url=self._url,
79
+ )
80
+
81
+ def run(
82
+ self,
83
+ messages: List[OpenAIMessage],
84
+ ) -> Union[ChatCompletion, Stream[ChatCompletionChunk]]:
85
+ r"""Runs inference of Moonshot chat completion.
86
+
87
+ Args:
88
+ messages (List[OpenAIMessage]): Message list with the chat history
89
+ in OpenAI API format.
90
+
91
+ Returns:
92
+ Union[ChatCompletion, Stream[ChatCompletionChunk]]:
93
+ `ChatCompletion` in the non-stream mode, or
94
+ `Stream[ChatCompletionChunk]` in the stream mode.
95
+ """
96
+ response = self._client.chat.completions.create(
97
+ messages=messages,
98
+ model=self.model_type,
99
+ **self.model_config_dict,
100
+ )
101
+ return response
102
+
103
+ @property
104
+ def token_counter(self) -> BaseTokenCounter:
105
+ r"""Initialize the token counter for the model backend.
106
+
107
+ Returns:
108
+ OpenAITokenCounter: The token counter following the model's
109
+ tokenization style.
110
+ """
111
+ if not self._token_counter:
112
+ self._token_counter = OpenAITokenCounter(ModelType.GPT_4O_MINI)
113
+ return self._token_counter
114
+
115
+ def check_model_config(self):
116
+ r"""Check whether the model configuration contains any
117
+ unexpected arguments to Moonshot API.
118
+
119
+ Raises:
120
+ ValueError: If the model configuration dictionary contains any
121
+ unexpected arguments to Moonshot API.
122
+ """
123
+ for param in self.model_config_dict:
124
+ if param not in MOONSHOT_API_PARAMS:
125
+ raise ValueError(
126
+ f"Unexpected argument `{param}` is "
127
+ "input into Moonshot model backend."
128
+ )
129
+
130
+ @property
131
+ def stream(self) -> bool:
132
+ r"""Returns whether the model is in stream mode, which sends partial
133
+ results each time.
134
+
135
+ Returns:
136
+ bool: Whether the model is in stream mode.
137
+ """
138
+ return self.model_config_dict.get('stream', False)