Skip to content

armgit / camel

Public Imported from GitHub
0

🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

feat: add async run in kimi (#2857)

Yifeng Wang(正经人王同学)2cad6171b595
diff --git a/camel/models/moonshot_model.py b/camel/models/moonshot_model.py
index 7e41f0e2e..8b2a21c8c 100644
--- a/camel/models/moonshot_model.py
+++ b/camel/models/moonshot_model.py
@@ -29,8 +29,23 @@ from camel.types import (
from camel.utils import (
BaseTokenCounter,
api_keys_required,
+ get_current_agent_session_id,
+ update_langfuse_trace,
)
+if os.environ.get("LANGFUSE_ENABLED", "False").lower() == "true":
+ try:
+ from langfuse.decorators import observe
+ except ImportError:
+ from camel.utils import observe
+elif os.environ.get("TRACEROOT_ENABLED", "False").lower() == "true":
+ try:
+ from traceroot import trace as observe # type: ignore[import]
+ except ImportError:
+ from camel.utils import observe
+else:
+ from camel.utils import observe
+
class MoonshotModel(OpenAICompatibleModel):
r"""Moonshot API in a unified OpenAICompatibleModel interface.
@@ -91,13 +106,51 @@ class MoonshotModel(OpenAICompatibleModel):
**kwargs,
)
+ @observe()
async def _arun(
self,
messages: List[OpenAIMessage],
response_format: Optional[Type[BaseModel]] = None,
tools: Optional[List[Dict[str, Any]]] = None,
) -> Union[ChatCompletion, AsyncStream[ChatCompletionChunk]]:
- raise NotImplementedError("Moonshot does not support async inference.")
+ r"""Runs inference of Moonshot chat completion asynchronously.
+
+ Args:
+ messages (List[OpenAIMessage]): Message list with the chat history
+ in OpenAI API format.
+ response_format (Optional[Type[BaseModel]]): The format of the
+ response.
+ tools (Optional[List[Dict[str, Any]]]): The schema of the tools to
+ use for the request.
+
+ Returns:
+ Union[ChatCompletion, AsyncStream[ChatCompletionChunk]]:
+ `ChatCompletion` in the non-stream mode, or
+ `AsyncStream[ChatCompletionChunk]` in the stream mode.
+ """
+
+ # Update Langfuse trace with current agent session and metadata
+ agent_session_id = get_current_agent_session_id()
+ if agent_session_id:
+ update_langfuse_trace(
+ session_id=agent_session_id,
+ metadata={
+ "agent_id": agent_session_id,
+ "model_type": str(self.model_type),
+ },
+ tags=["CAMEL-AI", str(self.model_type)],
+ )
+
+ request_config = self.model_config_dict.copy()
+
+ if tools:
+ request_config["tools"] = tools
+
+ return await self._async_client.chat.completions.create(
+ messages=messages,
+ model=self.model_type,
+ **request_config,
+ )
def check_model_config(self):
r"""Check whether the model configuration contains any