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: integrate Langfuse (#2467)

Tao Sunf88d5625e1d3
diff --git a/.env b/.env
index 5cfd49d21..d323826bc 100644
--- a/.env
+++ b/.env
@@ -118,4 +118,8 @@
# LINKED_ACCOUNT_OWNER="Fill your Linked Account Owner here"
#Bohrium API(https://www.bohrium.com/settings/user)
-#BOHRIUM_API_KEY="Fill your API key here"
\ No newline at end of file
+#BOHRIUM_API_KEY="Fill your API key here"
+
+#Langfuse API(https://langfuse.com/)
+#LANGFUSE_SECRET_KEY="Fill your API key here"
+#LANGFUSE_PUBLIC_KEY="Fill your API key here"
diff --git a/.github/actions/camel_install/action.yml b/.github/actions/camel_install/action.yml
index 8de764a42..79ceef1c8 100644
--- a/.github/actions/camel_install/action.yml
+++ b/.github/actions/camel_install/action.yml
@@ -16,24 +16,37 @@ runs:
run: |
pip install uv
shell: bash
- - name: Setup virtual environment
- run: |
- uv venv .venv --python=${{ inputs.python-version }}
- shell: bash
- uses: actions/cache/restore@v3
id: cache-restore
name: Restore caches for the virtual environment based on uv.lock
with:
path: ./.venv
key: venv-${{ hashFiles('uv.lock') }}
+ - name: Validate cached virtual environment
+ id: validate-venv
+ if: steps.cache-restore.outputs.cache-hit == 'true'
+ run: |
+ if [ -f .venv/bin/python3 ] && .venv/bin/python3 --version; then
+ echo "cache-valid=true" >> $GITHUB_OUTPUT
+ else
+ echo "cache-valid=false" >> $GITHUB_OUTPUT
+ rm -rf .venv
+ fi
+ shell: bash
+ - name: Setup virtual environment
+ if: steps.cache-restore.outputs.cache-hit != 'true' || steps.validate-venv.outputs.cache-valid == 'false'
+ run: |
+ uv venv .venv --python=${{ inputs.python-version }}
+ shell: bash
- name: Install the project dependencies
+ if: steps.cache-restore.outputs.cache-hit != 'true' || steps.validate-venv.outputs.cache-valid == 'false'
run: |
source .venv/bin/activate
uv pip install -e ".[all, dev, docs]"
shell: bash
- uses: actions/cache/save@v3
name: Save caches based on uv.lock
- if: ${{ !steps.cache-restore.outputs.cache-hit }}
+ if: steps.cache-restore.outputs.cache-hit != 'true' || steps.validate-venv.outputs.cache-valid == 'false'
with:
path: ./.venv
key: venv-${{ hashFiles('uv.lock') }}
diff --git a/camel/agents/chat_agent.py b/camel/agents/chat_agent.py
index c95299d2f..7bc6add3f 100644
--- a/camel/agents/chat_agent.py
+++ b/camel/agents/chat_agent.py
@@ -95,6 +95,15 @@ try:
except (ImportError, AttributeError):
from camel.utils import track_agent
+# Langfuse decorator setting
+if os.environ.get("LANGFUSE_ENABLED", "False").lower() == "true":
+ try:
+ from langfuse.decorators import observe
+ except ImportError:
+ from camel.utils import observe
+else:
+ from camel.utils import observe
+
SIMPLE_FORMAT_PROMPT = TextPrompt(
textwrap.dedent(
@@ -730,6 +739,7 @@ class ChatAgent(BaseAgent):
message.content = response.output_messages[0].content
self._try_format_message(message, response_format)
+ @observe()
def step(
self,
input_message: Union[BaseMessage, str],
@@ -752,6 +762,14 @@ class ChatAgent(BaseAgent):
flag, and session information.
"""
+ # Set Langfuse session_id using agent_id for trace grouping
+ try:
+ from camel.utils.langfuse import set_current_agent_session_id
+
+ set_current_agent_session_id(self.agent_id)
+ except ImportError:
+ pass # Langfuse not available
+
# Convert input message to BaseMessage if necessary
if isinstance(input_message, str):
input_message = BaseMessage.make_user_message(
@@ -846,6 +864,7 @@ class ChatAgent(BaseAgent):
openai_messages, _ = self.memory.get_context()
return openai_messages
+ @observe()
async def astep(
self,
input_message: Union[BaseMessage, str],
@@ -872,6 +891,13 @@ class ChatAgent(BaseAgent):
a boolean indicating whether the chat session has terminated,
and information about the chat session.
"""
+ try:
+ from camel.utils.langfuse import set_current_agent_session_id
+
+ set_current_agent_session_id(self.agent_id)
+ except ImportError:
+ pass # Langfuse not available
+
if isinstance(input_message, str):
input_message = BaseMessage.make_user_message(
role_name="User", content=input_message
diff --git a/camel/models/azure_openai_model.py b/camel/models/azure_openai_model.py
index 6cbea6c93..c597f8a78 100644
--- a/camel/models/azure_openai_model.py
+++ b/camel/models/azure_openai_model.py
@@ -25,11 +25,26 @@ from camel.types import (
ChatCompletionChunk,
ModelType,
)
-from camel.utils import BaseTokenCounter, OpenAITokenCounter
+from camel.utils import (
+ BaseTokenCounter,
+ OpenAITokenCounter,
+ get_current_agent_session_id,
+ is_langfuse_available,
+ update_langfuse_trace,
+)
AzureADTokenProvider = Callable[[], str]
+if os.environ.get("LANGFUSE_ENABLED", "False").lower() == "true":
+ try:
+ from langfuse.decorators import observe
+ except ImportError:
+ from camel.utils import observe
+else:
+ from camel.utils import observe
+
+
class AzureOpenAIModel(BaseModelBackend):
r"""Azure OpenAI API in a unified BaseModelBackend interface.
@@ -108,27 +123,52 @@ class AzureOpenAIModel(BaseModelBackend):
"or `AZURE_DEPLOYMENT_NAME` environment variable."
)
- self._client = AzureOpenAI(
- azure_endpoint=str(self._url),
- azure_deployment=self._azure_deployment_name,
- api_version=self.api_version,
- api_key=self._api_key,
- azure_ad_token=self._azure_ad_token,
- azure_ad_token_provider=self.azure_ad_token_provider,
- timeout=self._timeout,
- max_retries=3,
- )
+ if is_langfuse_available():
+ from langfuse.openai import AsyncAzureOpenAI as LangfuseAsyncOpenAI
+ from langfuse.openai import AzureOpenAI as LangfuseOpenAI
+
+ self._client = LangfuseOpenAI(
+ azure_endpoint=str(self._url),
+ azure_deployment=self._azure_deployment_name,
+ api_version=self.api_version,
+ api_key=self._api_key,
+ azure_ad_token=self._azure_ad_token,
+ azure_ad_token_provider=self.azure_ad_token_provider,
+ timeout=self._timeout,
+ max_retries=3,
+ )
+ self._async_client = LangfuseAsyncOpenAI(
+ azure_endpoint=str(self._url),
+ azure_deployment=self._azure_deployment_name,
+ api_version=self.api_version,
+ api_key=self._api_key,
+ azure_ad_token=self._azure_ad_token,
+ azure_ad_token_provider=self.azure_ad_token_provider,
+ timeout=self._timeout,
+ max_retries=3,
+ )
+ else:
+ self._client = AzureOpenAI(
+ azure_endpoint=str(self._url),
+ azure_deployment=self._azure_deployment_name,
+ api_version=self.api_version,
+ api_key=self._api_key,
+ azure_ad_token=self._azure_ad_token,
+ azure_ad_token_provider=self.azure_ad_token_provider,
+ timeout=self._timeout,
+ max_retries=3,
+ )
- self._async_client = AsyncAzureOpenAI(
- azure_endpoint=str(self._url),
- azure_deployment=self._azure_deployment_name,
- api_version=self.api_version,
- api_key=self._api_key,
- azure_ad_token=self._azure_ad_token,
- azure_ad_token_provider=self.azure_ad_token_provider,
- timeout=self._timeout,
- max_retries=3,
- )
+ self._async_client = AsyncAzureOpenAI(
+ azure_endpoint=str(self._url),
+ azure_deployment=self._azure_deployment_name,
+ api_version=self.api_version,
+ api_key=self._api_key,
+ azure_ad_token=self._azure_ad_token,
+ azure_ad_token_provider=self.azure_ad_token_provider,
+ timeout=self._timeout,
+ max_retries=3,
+ )
@property
def token_counter(self) -> BaseTokenCounter:
@@ -142,6 +182,7 @@ class AzureOpenAIModel(BaseModelBackend):
self._token_counter = OpenAITokenCounter(self.model_type)
return self._token_counter
+ @observe()
def _run(
self,
messages: List[OpenAIMessage],
@@ -163,14 +204,32 @@ class AzureOpenAIModel(BaseModelBackend):
`ChatCompletion` in the non-stream mode, or
`Stream[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)],
+ )
+
response_format = response_format or self.model_config_dict.get(
"response_format", None
)
if response_format:
- return self._request_parse(messages, response_format, tools)
+ result: Union[ChatCompletion, Stream[ChatCompletionChunk]] = (
+ self._request_parse(messages, response_format, tools)
+ )
else:
- return self._request_chat_completion(messages, tools)
+ result = self._request_chat_completion(messages, tools)
+
+ return result
+ @observe()
async def _arun(
self,
messages: List[OpenAIMessage],
@@ -192,13 +251,30 @@ class AzureOpenAIModel(BaseModelBackend):
`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)],
+ )
+
response_format = response_format or self.model_config_dict.get(
"response_format", None
)
if response_format:
- return await self._arequest_parse(messages, response_format, tools)
+ result: Union[
+ ChatCompletion, AsyncStream[ChatCompletionChunk]
+ ] = await self._arequest_parse(messages, response_format, tools)
else:
- return await self._arequest_chat_completion(messages, tools)
+ result = await self._arequest_chat_completion(messages, tools)
+
+ return result
def _request_chat_completion(
self,
diff --git a/camel/models/cohere_model.py b/camel/models/cohere_model.py
index 6beee324c..498a1219d 100644
--- a/camel/models/cohere_model.py
+++ b/camel/models/cohere_model.py
@@ -32,8 +32,19 @@ from camel.utils import (
BaseTokenCounter,
OpenAITokenCounter,
api_keys_required,
+ get_current_agent_session_id,
+ update_current_observation,
+ 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
+else:
+ from camel.utils import observe
+
try:
if os.getenv("AGENTOPS_API_KEY") is not None:
from agentops import LLMEvent, record
@@ -271,6 +282,7 @@ class CohereModel(BaseModelBackend):
return request_config
+ @observe(as_type="generation")
def _run(
self,
messages: List[OpenAIMessage],
@@ -285,6 +297,28 @@ class CohereModel(BaseModelBackend):
Returns:
ChatCompletion.
"""
+ update_current_observation(
+ input={
+ "messages": messages,
+ "tools": tools,
+ },
+ model=str(self.model_type),
+ model_parameters=self.model_config_dict,
+ )
+ # 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={
+ "source": "camel",
+ "agent_id": agent_session_id,
+ "agent_type": "camel_chat_agent",
+ "model_type": str(self.model_type),
+ },
+ tags=["CAMEL-AI", str(self.model_type)],
+ )
+
from cohere.core.api_error import ApiError
request_config = self._prepare_request(
@@ -309,6 +343,10 @@ class CohereModel(BaseModelBackend):
openai_response = self._to_openai_response(response)
+ update_current_observation(
+ usage=openai_response.usage,
+ )
+
# Add AgentOps LLM Event tracking
if LLMEvent:
llm_event = LLMEvent(
@@ -325,6 +363,7 @@ class CohereModel(BaseModelBackend):
return openai_response
+ @observe(as_type="generation")
async def _arun(
self,
messages: List[OpenAIMessage],
@@ -339,6 +378,28 @@ class CohereModel(BaseModelBackend):
Returns:
ChatCompletion.
"""
+ update_current_observation(
+ input={
+ "messages": messages,
+ "tools": tools,
+ },
+ model=str(self.model_type),
+ model_parameters=self.model_config_dict,
+ )
+ # 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={
+ "source": "camel",
+ "agent_id": agent_session_id,
+ "agent_type": "camel_chat_agent",
+ "model_type": str(self.model_type),
+ },
+ tags=["CAMEL-AI", str(self.model_type)],
+ )
+
from cohere.core.api_error import ApiError
request_config = self._prepare_request(
@@ -363,6 +424,10 @@ class CohereModel(BaseModelBackend):
openai_response = self._to_openai_response(response)
+ update_current_observation(
+ usage=openai_response.usage,
+ )
+
# Add AgentOps LLM Event tracking
if LLMEvent:
llm_event = LLMEvent(
diff --git a/camel/models/deepseek_model.py b/camel/models/deepseek_model.py
index 4123e7377..627db2f8f 100644
--- a/camel/models/deepseek_model.py
+++ b/camel/models/deepseek_model.py
@@ -28,7 +28,21 @@ from camel.types import (
ChatCompletionChunk,
ModelType,
)
-from camel.utils import BaseTokenCounter, api_keys_required
+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
+else:
+ from camel.utils import observe
+
logger = get_logger(__name__)
@@ -176,6 +190,7 @@ class DeepSeekModel(OpenAICompatibleModel):
)
return response
+ @observe()
def _run(
self,
messages: List[OpenAIMessage],
@@ -193,6 +208,19 @@ class DeepSeekModel(OpenAICompatibleModel):
`ChatCompletion` in the non-stream mode, or
`Stream[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._prepare_request(
messages, response_format, tools
)
@@ -205,6 +233,7 @@ class DeepSeekModel(OpenAICompatibleModel):
return self._post_handle_response(response)
+ @observe()
async def _arun(
self,
messages: List[OpenAIMessage],
@@ -222,6 +251,19 @@ class DeepSeekModel(OpenAICompatibleModel):
`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._prepare_request(
messages, response_format, tools
)
diff --git a/camel/models/gemini_model.py b/camel/models/gemini_model.py
index e637c985e..500fb84cb 100644
--- a/camel/models/gemini_model.py
+++ b/camel/models/gemini_model.py
@@ -28,8 +28,18 @@ 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
+else:
+ from camel.utils import observe
+
class GeminiModel(OpenAICompatibleModel):
r"""Gemini API in a unified OpenAICompatibleModel interface.
@@ -99,6 +109,7 @@ class GeminiModel(OpenAICompatibleModel):
processed_messages.append(msg_copy)
return processed_messages
+ @observe()
def _run(
self,
messages: List[OpenAIMessage],
@@ -120,6 +131,19 @@ class GeminiModel(OpenAICompatibleModel):
`ChatCompletion` in the non-stream mode, or
`Stream[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)],
+ )
+
response_format = response_format or self.model_config_dict.get(
"response_format", None
)
@@ -130,10 +154,15 @@ class GeminiModel(OpenAICompatibleModel):
"Gemini does not support function calling with "
"response format."
)
- return self._request_parse(messages, response_format)
+ result: Union[ChatCompletion, Stream[ChatCompletionChunk]] = (
+ self._request_parse(messages, response_format)
+ )
else:
- return self._request_chat_completion(messages, tools)
+ result = self._request_chat_completion(messages, tools)
+
+ return result
+ @observe()
async def _arun(
self,
messages: List[OpenAIMessage],
@@ -155,6 +184,19 @@ class GeminiModel(OpenAICompatibleModel):
`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)],
+ )
+
response_format = response_format or self.model_config_dict.get(
"response_format", None
)
@@ -165,9 +207,13 @@ class GeminiModel(OpenAICompatibleModel):
"Gemini does not support function calling with "
"response format."
)
- return await self._arequest_parse(messages, response_format)
+ result: Union[
+ ChatCompletion, AsyncStream[ChatCompletionChunk]
+ ] = await self._arequest_parse(messages, response_format)
else:
- return await self._arequest_chat_completion(messages, tools)
+ result = await self._arequest_chat_completion(messages, tools)
+
+ return result
def _request_chat_completion(
self,
diff --git a/camel/models/litellm_model.py b/camel/models/litellm_model.py
index c76bb4c20..88588a640 100644
--- a/camel/models/litellm_model.py
+++ b/camel/models/litellm_model.py
@@ -24,8 +24,19 @@ from camel.utils import (
BaseTokenCounter,
LiteLLMTokenCounter,
dependencies_required,
+ get_current_agent_session_id,
+ update_current_observation,
+ 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
+else:
+ from camel.utils import observe
+
class LiteLLMModel(BaseModelBackend):
r"""Constructor for LiteLLM backend with OpenAI compatibility.
@@ -117,6 +128,7 @@ class LiteLLMModel(BaseModelBackend):
async def _arun(self) -> None: # type: ignore[override]
raise NotImplementedError
+ @observe(as_type='generation')
def _run(
self,
messages: List[OpenAIMessage],
@@ -132,6 +144,28 @@ class LiteLLMModel(BaseModelBackend):
Returns:
ChatCompletion
"""
+ update_current_observation(
+ input={
+ "messages": messages,
+ "tools": tools,
+ },
+ model=str(self.model_type),
+ model_parameters=self.model_config_dict,
+ )
+ # 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={
+ "source": "camel",
+ "agent_id": agent_session_id,
+ "agent_type": "camel_chat_agent",
+ "model_type": str(self.model_type),
+ },
+ tags=["CAMEL-AI", str(self.model_type)],
+ )
+
response = self.client(
timeout=self._timeout,
api_key=self._api_key,
@@ -141,6 +175,10 @@ class LiteLLMModel(BaseModelBackend):
**self.model_config_dict,
)
response = self._convert_response_from_litellm_to_openai(response)
+
+ update_current_observation(
+ usage=response.usage,
+ )
return response
def check_model_config(self):
diff --git a/camel/models/mistral_model.py b/camel/models/mistral_model.py
index 7c8b1a1ad..d91b862cd 100644
--- a/camel/models/mistral_model.py
+++ b/camel/models/mistral_model.py
@@ -35,6 +35,9 @@ from camel.utils import (
OpenAITokenCounter,
api_keys_required,
dependencies_required,
+ get_current_agent_session_id,
+ update_current_observation,
+ update_langfuse_trace,
)
logger = get_logger(__name__)
@@ -47,6 +50,14 @@ try:
except (ImportError, AttributeError):
LLMEvent = None
+if os.environ.get("LANGFUSE_ENABLED", "False").lower() == "true":
+ try:
+ from langfuse.decorators import observe
+ except ImportError:
+ from camel.utils import observe
+else:
+ from camel.utils import observe
+
class MistralModel(BaseModelBackend):
r"""Mistral API in a unified BaseModelBackend interface.
@@ -232,6 +243,7 @@ class MistralModel(BaseModelBackend):
)
return self._token_counter
+ @observe(as_type="generation")
async def _arun(
self,
messages: List[OpenAIMessage],
@@ -242,6 +254,29 @@ class MistralModel(BaseModelBackend):
"Mistral does not support async inference, using sync "
"inference instead."
)
+ update_current_observation(
+ input={
+ "messages": messages,
+ "response_format": response_format,
+ "tools": tools,
+ },
+ model=str(self.model_type),
+ model_parameters=self.model_config_dict,
+ )
+ # 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={
+ "source": "camel",
+ "agent_id": agent_session_id,
+ "agent_type": "camel_chat_agent",
+ "model_type": str(self.model_type),
+ },
+ tags=["CAMEL-AI", str(self.model_type)],
+ )
+
request_config = self._prepare_request(
messages, response_format, tools
)
@@ -255,6 +290,10 @@ class MistralModel(BaseModelBackend):
openai_response = self._to_openai_response(response) # type: ignore[arg-type]
+ update_current_observation(
+ usage=openai_response.usage,
+ )
+
# Add AgentOps LLM Event tracking
if LLMEvent:
llm_event = LLMEvent(
@@ -271,6 +310,7 @@ class MistralModel(BaseModelBackend):
return openai_response
+ @observe(as_type="generation")
def _run(
self,
messages: List[OpenAIMessage],
@@ -290,6 +330,28 @@ class MistralModel(BaseModelBackend):
Returns:
ChatCompletion: The response from the model.
"""
+ update_current_observation(
+ input={
+ "messages": messages,
+ "tools": tools,
+ },
+ model=str(self.model_type),
+ model_parameters=self.model_config_dict,
+ )
+ # 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={
+ "source": "camel",
+ "agent_id": agent_session_id,
+ "agent_type": "camel_chat_agent",
+ "model_type": str(self.model_type),
+ },
+ tags=["CAMEL-AI", str(self.model_type)],
+ )
+
request_config = self._prepare_request(
messages, response_format, tools
)
@@ -303,6 +365,10 @@ class MistralModel(BaseModelBackend):
openai_response = self._to_openai_response(response) # type: ignore[arg-type]
+ update_current_observation(
+ usage=openai_response.usage,
+ )
+
# Add AgentOps LLM Event tracking
if LLMEvent:
llm_event = LLMEvent(
diff --git a/camel/models/model_factory.py b/camel/models/model_factory.py
index d55d27d37..7f7303d12 100644
--- a/camel/models/model_factory.py
+++ b/camel/models/model_factory.py
@@ -12,6 +12,7 @@
# limitations under the License.
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
import json
+import os
from typing import ClassVar, Dict, Optional, Type, Union
from camel.models.aiml_model import AIMLModel
@@ -51,7 +52,7 @@ from camel.models.watsonx_model import WatsonXModel
from camel.models.yi_model import YiModel
from camel.models.zhipuai_model import ZhipuAIModel
from camel.types import ModelPlatformType, ModelType, UnifiedModelType
-from camel.utils import BaseTokenCounter
+from camel.utils import BaseTokenCounter, configure_langfuse
class ModelFactory:
@@ -144,6 +145,12 @@ class ModelFactory:
Raises:
ValueError: If there is no backend for the model.
"""
+
+ # Auto-configure Langfuse only if explicitly enabled
+ env_enabled_str = os.environ.get("LANGFUSE_ENABLED")
+ if env_enabled_str and env_enabled_str.lower() == "true":
+ configure_langfuse()
+
# Convert string to ModelPlatformType enum if needed
if isinstance(model_platform, str):
try:
diff --git a/camel/models/openai_compatible_model.py b/camel/models/openai_compatible_model.py
index c53a0b00f..a49a29791 100644
--- a/camel/models/openai_compatible_model.py
+++ b/camel/models/openai_compatible_model.py
@@ -31,8 +31,20 @@ from camel.types import (
from camel.utils import (
BaseTokenCounter,
OpenAITokenCounter,
+ get_current_agent_session_id,
+ is_langfuse_available,
+ 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
+else:
+ from camel.utils import observe
+
+
logger = get_logger(__name__)
@@ -72,20 +84,37 @@ class OpenAICompatibleModel(BaseModelBackend):
super().__init__(
model_type, model_config_dict, api_key, url, token_counter, timeout
)
- self._client = OpenAI(
- timeout=self._timeout,
- max_retries=3,
- api_key=self._api_key,
- base_url=self._url,
- )
-
- self._async_client = AsyncOpenAI(
- timeout=self._timeout,
- max_retries=3,
- api_key=self._api_key,
- base_url=self._url,
- )
+ if is_langfuse_available():
+ from langfuse.openai import AsyncOpenAI as LangfuseAsyncOpenAI
+ from langfuse.openai import OpenAI as LangfuseOpenAI
+
+ self._client = LangfuseOpenAI(
+ timeout=self._timeout,
+ max_retries=3,
+ base_url=self._url,
+ api_key=self._api_key,
+ )
+ self._async_client = LangfuseAsyncOpenAI(
+ timeout=self._timeout,
+ max_retries=3,
+ base_url=self._url,
+ api_key=self._api_key,
+ )
+ else:
+ self._client = OpenAI(
+ timeout=self._timeout,
+ max_retries=3,
+ base_url=self._url,
+ api_key=self._api_key,
+ )
+ self._async_client = AsyncOpenAI(
+ timeout=self._timeout,
+ max_retries=3,
+ base_url=self._url,
+ api_key=self._api_key,
+ )
+ @observe()
def _run(
self,
messages: List[OpenAIMessage],
@@ -107,14 +136,32 @@ class OpenAICompatibleModel(BaseModelBackend):
`ChatCompletion` in the non-stream mode, or
`Stream[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)],
+ )
+
response_format = response_format or self.model_config_dict.get(
"response_format", None
)
if response_format:
- return self._request_parse(messages, response_format, tools)
+ result: Union[ChatCompletion, Stream[ChatCompletionChunk]] = (
+ self._request_parse(messages, response_format, tools)
+ )
else:
- return self._request_chat_completion(messages, tools)
+ result = self._request_chat_completion(messages, tools)
+
+ return result
+ @observe()
async def _arun(
self,
messages: List[OpenAIMessage],
@@ -136,13 +183,30 @@ class OpenAICompatibleModel(BaseModelBackend):
`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)],
+ )
+
response_format = response_format or self.model_config_dict.get(
"response_format", None
)
if response_format:
- return await self._arequest_parse(messages, response_format, tools)
+ result: Union[
+ ChatCompletion, AsyncStream[ChatCompletionChunk]
+ ] = await self._arequest_parse(messages, response_format, tools)
else:
- return await self._arequest_chat_completion(messages, tools)
+ result = await self._arequest_chat_completion(messages, tools)
+
+ return result
def _request_chat_completion(
self,
diff --git a/camel/models/openai_model.py b/camel/models/openai_model.py
index b5e52e801..74a52be05 100644
--- a/camel/models/openai_model.py
+++ b/camel/models/openai_model.py
@@ -30,8 +30,20 @@ from camel.utils import (
BaseTokenCounter,
OpenAITokenCounter,
api_keys_required,
+ get_current_agent_session_id,
+ is_langfuse_available,
+ 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
+else:
+ from camel.utils import observe
+
+
UNSUPPORTED_PARAMS = {
"temperature",
"top_p",
@@ -90,18 +102,35 @@ class OpenAIModel(BaseModelBackend):
model_type, model_config_dict, api_key, url, token_counter, timeout
)
- self._client = OpenAI(
- timeout=self._timeout,
- max_retries=3,
- base_url=self._url,
- api_key=self._api_key,
- )
- self._async_client = AsyncOpenAI(
- timeout=self._timeout,
- max_retries=3,
- base_url=self._url,
- api_key=self._api_key,
- )
+ if is_langfuse_available():
+ from langfuse.openai import AsyncOpenAI as LangfuseAsyncOpenAI
+ from langfuse.openai import OpenAI as LangfuseOpenAI
+
+ self._client = LangfuseOpenAI(
+ timeout=self._timeout,
+ max_retries=3,
+ base_url=self._url,
+ api_key=self._api_key,
+ )
+ self._async_client = LangfuseAsyncOpenAI(
+ timeout=self._timeout,
+ max_retries=3,
+ base_url=self._url,
+ api_key=self._api_key,
+ )
+ else:
+ self._client = OpenAI(
+ timeout=self._timeout,
+ max_retries=3,
+ base_url=self._url,
+ api_key=self._api_key,
+ )
+ self._async_client = AsyncOpenAI(
+ timeout=self._timeout,
+ max_retries=3,
+ base_url=self._url,
+ api_key=self._api_key,
+ )
def _sanitize_config(self, config_dict: Dict[str, Any]) -> Dict[str, Any]:
r"""Sanitize the model configuration for O1 models."""
@@ -185,6 +214,7 @@ class OpenAIModel(BaseModelBackend):
self._token_counter = OpenAITokenCounter(self.model_type)
return self._token_counter
+ @observe()
def _run(
self,
messages: List[OpenAIMessage],
@@ -206,15 +236,35 @@ class OpenAIModel(BaseModelBackend):
`ChatCompletion` in the non-stream mode, or
`Stream[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={
+ "source": "camel",
+ "agent_id": agent_session_id,
+ "agent_type": "camel_chat_agent",
+ "model_type": str(self.model_type),
+ },
+ tags=["CAMEL-AI", str(self.model_type)],
+ )
+
messages = self._adapt_messages_for_o1_models(messages)
response_format = response_format or self.model_config_dict.get(
"response_format", None
)
if response_format:
- return self._request_parse(messages, response_format, tools)
+ result: Union[ChatCompletion, Stream[ChatCompletionChunk]] = (
+ self._request_parse(messages, response_format, tools)
+ )
else:
- return self._request_chat_completion(messages, tools)
+ result = self._request_chat_completion(messages, tools)
+
+ return result
+ @observe()
async def _arun(
self,
messages: List[OpenAIMessage],
@@ -236,13 +286,33 @@ class OpenAIModel(BaseModelBackend):
`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={
+ "source": "camel",
+ "agent_id": agent_session_id,
+ "agent_type": "camel_chat_agent",
+ "model_type": str(self.model_type),
+ },
+ tags=["CAMEL-AI", str(self.model_type)],
+ )
+
+ messages = self._adapt_messages_for_o1_models(messages)
response_format = response_format or self.model_config_dict.get(
"response_format", None
)
if response_format:
- return await self._arequest_parse(messages, response_format, tools)
+ result: Union[
+ ChatCompletion, AsyncStream[ChatCompletionChunk]
+ ] = await self._arequest_parse(messages, response_format, tools)
else:
- return await self._arequest_chat_completion(messages, tools)
+ result = await self._arequest_chat_completion(messages, tools)
+
+ return result
def _request_chat_completion(
self,
diff --git a/camel/models/reka_model.py b/camel/models/reka_model.py
index a487bea1c..fc7508369 100644
--- a/camel/models/reka_model.py
+++ b/camel/models/reka_model.py
@@ -11,6 +11,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
+import os
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Type, Union
from pydantic import BaseModel
@@ -24,8 +25,19 @@ from camel.utils import (
OpenAITokenCounter,
api_keys_required,
dependencies_required,
+ get_current_agent_session_id,
+ update_current_observation,
+ 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
+else:
+ from camel.utils import observe
+
if TYPE_CHECKING:
from reka.types import ChatMessage, ChatResponse
@@ -188,6 +200,7 @@ class RekaModel(BaseModelBackend):
)
return self._token_counter
+ @observe(as_type="generation")
async def _arun(
self,
messages: List[OpenAIMessage],
@@ -203,6 +216,29 @@ class RekaModel(BaseModelBackend):
Returns:
ChatCompletion.
"""
+
+ update_current_observation(
+ input={
+ "messages": messages,
+ "tools": tools,
+ },
+ model=str(self.model_type),
+ model_parameters=self.model_config_dict,
+ )
+ # 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={
+ "source": "camel",
+ "agent_id": agent_session_id,
+ "agent_type": "camel_chat_agent",
+ "model_type": str(self.model_type),
+ },
+ tags=["CAMEL-AI", str(self.model_type)],
+ )
+
reka_messages = self._convert_openai_to_reka_messages(messages)
response = await self._async_client.chat.create(
@@ -213,6 +249,10 @@ class RekaModel(BaseModelBackend):
openai_response = self._convert_reka_to_openai_response(response)
+ update_current_observation(
+ usage=openai_response.usage,
+ )
+
# Add AgentOps LLM Event tracking
if LLMEvent:
llm_event = LLMEvent(
@@ -229,6 +269,7 @@ class RekaModel(BaseModelBackend):
return openai_response
+ @observe(as_type="generation")
def _run(
self,
messages: List[OpenAIMessage],
@@ -244,6 +285,30 @@ class RekaModel(BaseModelBackend):
Returns:
ChatCompletion.
"""
+
+ update_current_observation(
+ input={
+ "messages": messages,
+ "tools": tools,
+ },
+ model=str(self.model_type),
+ model_parameters=self.model_config_dict,
+ )
+
+ # 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={
+ "source": "camel",
+ "agent_id": agent_session_id,
+ "agent_type": "camel_chat_agent",
+ "model_type": str(self.model_type),
+ },
+ tags=["CAMEL-AI", str(self.model_type)],
+ )
+
reka_messages = self._convert_openai_to_reka_messages(messages)
response = self._client.chat.create(
@@ -254,6 +319,10 @@ class RekaModel(BaseModelBackend):
openai_response = self._convert_reka_to_openai_response(response)
+ update_current_observation(
+ usage=openai_response.usage,
+ )
+
# Add AgentOps LLM Event tracking
if LLMEvent:
llm_event = LLMEvent(
diff --git a/camel/models/samba_model.py b/camel/models/samba_model.py
index 8830e04b6..43e5af469 100644
--- a/camel/models/samba_model.py
+++ b/camel/models/samba_model.py
@@ -38,6 +38,9 @@ from camel.utils import (
BaseTokenCounter,
OpenAITokenCounter,
api_keys_required,
+ get_current_agent_session_id,
+ update_current_observation,
+ update_langfuse_trace,
)
try:
@@ -48,6 +51,14 @@ try:
except (ImportError, AttributeError):
LLMEvent = None
+if os.environ.get("LANGFUSE_ENABLED", "False").lower() == "true":
+ try:
+ from langfuse.decorators import observe
+ except ImportError:
+ from camel.utils import observe
+else:
+ from camel.utils import observe
+
class SambaModel(BaseModelBackend):
r"""SambaNova service interface.
@@ -161,6 +172,7 @@ class SambaModel(BaseModelBackend):
" SambaNova service"
)
+ @observe(as_type="generation")
async def _arun( # type: ignore[misc]
self,
messages: List[OpenAIMessage],
@@ -178,13 +190,42 @@ class SambaModel(BaseModelBackend):
`ChatCompletion` in the non-stream mode, or
`AsyncStream[ChatCompletionChunk]` in the stream mode.
"""
+
+ update_current_observation(
+ input={
+ "messages": messages,
+ "tools": tools,
+ },
+ model=str(self.model_type),
+ model_parameters=self.model_config_dict,
+ )
+
+ # 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={
+ "source": "camel",
+ "agent_id": agent_session_id,
+ "agent_type": "camel_chat_agent",
+ "model_type": str(self.model_type),
+ },
+ tags=["CAMEL-AI", str(self.model_type)],
+ )
+
if "tools" in self.model_config_dict:
del self.model_config_dict["tools"]
if self.model_config_dict.get("stream") is True:
return await self._arun_streaming(messages)
else:
- return await self._arun_non_streaming(messages)
+ response = await self._arun_non_streaming(messages)
+ update_current_observation(
+ usage=response.usage,
+ )
+ return response
+ @observe(as_type="generation")
def _run( # type: ignore[misc]
self,
messages: List[OpenAIMessage],
@@ -202,12 +243,38 @@ class SambaModel(BaseModelBackend):
`ChatCompletion` in the non-stream mode, or
`Stream[ChatCompletionChunk]` in the stream mode.
"""
+ update_current_observation(
+ input={
+ "messages": messages,
+ "tools": tools,
+ },
+ model=str(self.model_type),
+ model_parameters=self.model_config_dict,
+ )
+ # 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={
+ "source": "camel",
+ "agent_id": agent_session_id,
+ "agent_type": "camel_chat_agent",
+ "model_type": str(self.model_type),
+ },
+ tags=["CAMEL-AI", str(self.model_type)],
+ )
+
if "tools" in self.model_config_dict:
del self.model_config_dict["tools"]
if self.model_config_dict.get("stream") is True:
return self._run_streaming(messages)
else:
- return self._run_non_streaming(messages)
+ response = self._run_non_streaming(messages)
+ update_current_observation(
+ usage=response.usage,
+ )
+ return response
def _run_streaming(
self, messages: List[OpenAIMessage]
diff --git a/camel/models/sglang_model.py b/camel/models/sglang_model.py
index 8651212ac..5056b741c 100644
--- a/camel/models/sglang_model.py
+++ b/camel/models/sglang_model.py
@@ -29,7 +29,21 @@ from camel.types import (
ChatCompletionChunk,
ModelType,
)
-from camel.utils import BaseTokenCounter, OpenAITokenCounter
+from camel.utils import (
+ BaseTokenCounter,
+ OpenAITokenCounter,
+ get_current_agent_session_id,
+ update_current_observation,
+ 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
+else:
+ from camel.utils import observe
class SGLangModel(BaseModelBackend):
@@ -195,6 +209,7 @@ class SGLangModel(BaseModelBackend):
"input into SGLang model backend."
)
+ @observe(as_type='generation')
async def _arun(
self,
messages: List[OpenAIMessage],
@@ -213,6 +228,28 @@ class SGLangModel(BaseModelBackend):
`AsyncStream[ChatCompletionChunk]` in the stream mode.
"""
+ update_current_observation(
+ input={
+ "messages": messages,
+ "tools": tools,
+ },
+ model=str(self.model_type),
+ model_parameters=self.model_config_dict,
+ )
+ # 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={
+ "source": "camel",
+ "agent_id": agent_session_id,
+ "agent_type": "camel_chat_agent",
+ "model_type": str(self.model_type),
+ },
+ tags=["CAMEL-AI", str(self.model_type)],
+ )
+
# Ensure server is running
self._ensure_server_running()
@@ -230,9 +267,16 @@ class SGLangModel(BaseModelBackend):
model=self.model_type,
**self.model_config_dict,
)
-
+ update_current_observation(
+ usage_details={
+ "prompt_tokens": response.usage.prompt_tokens,
+ "completion_tokens": response.usage.completion_tokens,
+ "total_tokens": response.usage.total_tokens,
+ },
+ )
return response
+ @observe(as_type='generation')
def _run(
self,
messages: List[OpenAIMessage],
@@ -250,6 +294,27 @@ class SGLangModel(BaseModelBackend):
`ChatCompletion` in the non-stream mode, or
`Stream[ChatCompletionChunk]` in the stream mode.
"""
+ update_current_observation(
+ input={
+ "messages": messages,
+ "tools": tools,
+ },
+ model=str(self.model_type),
+ model_parameters=self.model_config_dict,
+ )
+ # 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={
+ "source": "camel",
+ "agent_id": agent_session_id,
+ "agent_type": "camel_chat_agent",
+ "model_type": str(self.model_type),
+ },
+ tags=["CAMEL-AI", str(self.model_type)],
+ )
# Ensure server is running
self._ensure_server_running()
@@ -268,6 +333,13 @@ class SGLangModel(BaseModelBackend):
model=self.model_type,
**self.model_config_dict,
)
+ update_current_observation(
+ usage_details={
+ "prompt_tokens": response.usage.prompt_tokens,
+ "completion_tokens": response.usage.completion_tokens,
+ "total_tokens": response.usage.total_tokens,
+ },
+ )
return response
diff --git a/camel/models/watsonx_model.py b/camel/models/watsonx_model.py
index 15f8b81ad..4c4d360b5 100644
--- a/camel/models/watsonx_model.py
+++ b/camel/models/watsonx_model.py
@@ -26,8 +26,19 @@ from camel.utils import (
BaseTokenCounter,
OpenAITokenCounter,
api_keys_required,
+ get_current_agent_session_id,
+ update_current_observation,
+ 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
+else:
+ from camel.utils import observe
+
logger = get_logger(__name__)
@@ -151,6 +162,7 @@ class WatsonXModel(BaseModelBackend):
return request_config
+ @observe(as_type='generation')
def _run(
self,
messages: List[OpenAIMessage],
@@ -170,6 +182,27 @@ class WatsonXModel(BaseModelBackend):
Returns:
ChatCompletion.
"""
+ update_current_observation(
+ input={
+ "messages": messages,
+ "tools": tools,
+ },
+ model=str(self.model_type),
+ model_parameters=self.model_config_dict,
+ )
+ # 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={
+ "source": "camel",
+ "agent_id": agent_session_id,
+ "agent_type": "camel_chat_agent",
+ "model_type": str(self.model_type),
+ },
+ tags=["CAMEL-AI", str(self.model_type)],
+ )
try:
request_config = self._prepare_request(
messages, response_format, tools
@@ -183,12 +216,16 @@ class WatsonXModel(BaseModelBackend):
)
openai_response = self._to_openai_response(response)
+ update_current_observation(
+ usage=openai_response.usage,
+ )
return openai_response
except Exception as e:
logger.error(f"Unexpected error when calling WatsonX API: {e!s}")
raise
+ @observe(as_type='generation')
async def _arun(
self,
messages: List[OpenAIMessage],
@@ -208,6 +245,28 @@ class WatsonXModel(BaseModelBackend):
Returns:
ChatCompletion.
"""
+ update_current_observation(
+ input={
+ "messages": messages,
+ "tools": tools,
+ },
+ model=str(self.model_type),
+ model_parameters=self.model_config_dict,
+ )
+ # 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={
+ "source": "camel",
+ "agent_id": agent_session_id,
+ "agent_type": "camel_chat_agent",
+ "model_type": str(self.model_type),
+ },
+ tags=["CAMEL-AI", str(self.model_type)],
+ )
+
try:
request_config = self._prepare_request(
messages, response_format, tools
@@ -221,6 +280,9 @@ class WatsonXModel(BaseModelBackend):
)
openai_response = self._to_openai_response(response)
+ update_current_observation(
+ usage=openai_response.usage,
+ )
return openai_response
except Exception as e:
diff --git a/camel/utils/__init__.py b/camel/utils/__init__.py
index 1f873216f..9b8e4c3a8 100644
--- a/camel/utils/__init__.py
+++ b/camel/utils/__init__.py
@@ -44,6 +44,15 @@ from .commons import (
from .constants import Constants
from .deduplication import DeduplicationResult, deduplicate_internally
from .filename import sanitize_filename
+from .langfuse import (
+ configure_langfuse,
+ get_current_agent_session_id,
+ get_langfuse_status,
+ is_langfuse_available,
+ observe,
+ update_current_observation,
+ update_langfuse_trace,
+)
from .mcp import MCPServer
from .response_format import get_pydantic_model, model_from_json_schema
from .token_counting import (
@@ -97,4 +106,11 @@ __all__ = [
"sanitize_filename",
"browser_toolkit_save_auth_cookie",
"run_async",
+ "configure_langfuse",
+ "is_langfuse_available",
+ "get_current_agent_session_id",
+ "update_langfuse_trace",
+ "observe",
+ "update_current_observation",
+ "get_langfuse_status",
]
diff --git a/camel/utils/langfuse.py b/camel/utils/langfuse.py
new file mode 100644
index 000000000..ce6cd9f84
--- /dev/null
+++ b/camel/utils/langfuse.py
@@ -0,0 +1,258 @@
+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
+# Licensed 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.
+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
+import os
+import threading
+from typing import Any, Dict, List, Optional
+
+from camel.logger import get_logger
+from camel.utils import dependencies_required
+
+logger = get_logger(__name__)
+# Thread-local storage for agent session IDs
+_local = threading.local()
+
+# Global flag to track if Langfuse has been configured
+_langfuse_configured = False
+
+try:
+ from langfuse.decorators import langfuse_context
+
+ LANGFUSE_AVAILABLE = True
+except ImportError:
+ LANGFUSE_AVAILABLE = False
+
+
+@dependencies_required('langfuse')
+def configure_langfuse(
+ public_key: Optional[str] = None,
+ secret_key: Optional[str] = None,
+ host: Optional[str] = None,
+ debug: Optional[bool] = None,
+ enabled: Optional[bool] = None,
+):
+ r"""Configure Langfuse for CAMEL models.
+
+ Args:
+ public_key(Optional[str]): Langfuse public key. Can be set via LANGFUSE_PUBLIC_KEY.
+ (default: :obj:`None`)
+ secret_key(Optional[str]): Langfuse secret key. Can be set via LANGFUSE_SECRET_KEY.
+ (default: :obj:`None`)
+ host(Optional[str]): Langfuse host URL. Can be set via LANGFUSE_HOST.
+ (default: :obj:`https://cloud.langfuse.com`)
+ debug(Optional[bool]): Enable debug mode. Can be set via LANGFUSE_DEBUG.
+ (default: :obj:`None`)
+ enabled(Optional[bool]): Enable/disable tracing. Can be set via LANGFUSE_ENABLED.
+ (default: :obj:`None`)
+
+ Note:
+ This function configures the native langfuse_context which works with
+ @observe() decorators. Set enabled=False to disable all tracing.
+ """ # noqa: E501
+ global _langfuse_configured
+
+ # Get configuration from environment or parameters
+ public_key = public_key or os.environ.get("LANGFUSE_PUBLIC_KEY")
+ secret_key = secret_key or os.environ.get("LANGFUSE_SECRET_KEY")
+ host = host or os.environ.get(
+ "LANGFUSE_HOST", "https://cloud.langfuse.com"
+ )
+ debug = (
+ debug
+ if debug is not None
+ else os.environ.get("LANGFUSE_DEBUG", "False").lower() == "true"
+ )
+
+ # Handle enabled parameter
+ if enabled is None:
+ env_enabled_str = os.environ.get("LANGFUSE_ENABLED")
+ if env_enabled_str is not None:
+ enabled = env_enabled_str.lower() == "true"
+ else:
+ enabled = False # Default to disabled
+
+ # If not enabled, don't configure anything and don't call langfuse function
+ if not enabled:
+ _langfuse_configured = False
+ logger.info("Langfuse tracing disabled for CAMEL models")
+
+ logger.debug(
+ f"Configuring Langfuse - enabled: {enabled}, "
+ f"public_key: {'***' + public_key[-4:] if public_key else None}, "
+ f"host: {host}, debug: {debug}"
+ )
+ if enabled and public_key and secret_key and LANGFUSE_AVAILABLE:
+ _langfuse_configured = True
+ else:
+ _langfuse_configured = False
+
+ try:
+ # Configure langfuse_context with native method
+ langfuse_context.configure(
+ public_key=public_key,
+ secret_key=secret_key,
+ host=host,
+ debug=debug,
+ enabled=True, # Always True here since we checked enabled above
+ )
+
+ logger.info("Langfuse tracing enabled for CAMEL models")
+
+ except Exception as e:
+ logger.error(f"Failed to configure Langfuse: {e}")
+
+
+def is_langfuse_available() -> bool:
+ r"""Check if Langfuse is configured."""
+ return _langfuse_configured
+
+
+def set_current_agent_session_id(session_id: str) -> None:
+ r"""Set the session ID for the current agent in thread-local storage.
+
+ Args:
+ session_id(str): The session ID to set for the current agent.
+ """
+
+ _local.agent_session_id = session_id
+
+
+def get_current_agent_session_id() -> Optional[str]:
+ r"""Get the session ID for the current agent from thread-local storage.
+
+ Returns:
+ Optional[str]: The session ID for the current agent.
+ """
+ if is_langfuse_available():
+ return getattr(_local, 'agent_session_id', None)
+ return None
+
+
+def update_langfuse_trace(
+ session_id: Optional[str] = None,
+ user_id: Optional[str] = None,
+ metadata: Optional[Dict[str, Any]] = None,
+ tags: Optional[List[str]] = None,
+) -> bool:
+ r"""Update the current Langfuse trace with session ID and metadata.
+
+ Args:
+ session_id(Optional[str]): Optional session ID to use. If :obj:`None`
+ uses the current agent's session ID. (default: :obj:`None`)
+ user_id(Optional[str]): Optional user ID for the trace.
+ (default: :obj:`None`)
+ metadata(Optional[Dict[str, Any]]): Optional metadata dictionary.
+ (default: :obj:`None`)
+ tags(Optional[List[str]]): Optional list of tags.
+ (default: :obj:`None`)
+
+ Returns:
+ bool: True if update was successful, False otherwise.
+ """
+ if not is_langfuse_available():
+ return False
+
+ # Use provided session_id or get from thread-local storage
+ final_session_id = session_id or get_current_agent_session_id()
+
+ update_data: Dict[str, Any] = {}
+ if final_session_id:
+ update_data["session_id"] = final_session_id
+ if user_id:
+ update_data["user_id"] = user_id
+ if metadata:
+ update_data["metadata"] = metadata
+ if tags:
+ update_data["tags"] = tags
+
+ if update_data:
+ langfuse_context.update_current_trace(**update_data)
+ return True
+
+ return False
+
+
+def update_current_observation(
+ input: Optional[Dict[str, Any]] = None,
+ output: Optional[Dict[str, Any]] = None,
+ model: Optional[str] = None,
+ model_parameters: Optional[Dict[str, Any]] = None,
+ usage_details: Optional[Dict[str, Any]] = None,
+ **kwargs,
+) -> None:
+ r"""Update the current Langfuse observation with input, output,
+ model, model_parameters, and usage_details.
+
+ Args:
+ input(Optional[Dict[str, Any]]): Optional input dictionary.
+ (default: :obj:`None`)
+ output(Optional[Dict[str, Any]]): Optional output dictionary.
+ (default: :obj:`None`)
+ model(Optional[str]): Optional model name. (default: :obj:`None`)
+ model_parameters(Optional[Dict[str, Any]]): Optional model parameters
+ dictionary. (default: :obj:`None`)
+ usage_details(Optional[Dict[str, Any]]): Optional usage details
+ dictionary. (default: :obj:`None`)
+
+ Returns:
+ None
+ """
+ if not is_langfuse_available():
+ return
+
+ langfuse_context.update_current_observation(
+ input=input,
+ output=output,
+ model=model,
+ model_parameters=model_parameters,
+ usage_details=usage_details,
+ **kwargs,
+ )
+
+
+def get_langfuse_status() -> Dict[str, Any]:
+ r"""Get detailed Langfuse configuration status for debugging.
+
+ Returns:
+ Dict[str, Any]: Status information including configuration state.
+ """
+ env_enabled_str = os.environ.get("LANGFUSE_ENABLED")
+ env_enabled = (
+ env_enabled_str.lower() == "true" if env_enabled_str else None
+ )
+
+ status = {
+ "configured": _langfuse_configured,
+ "has_public_key": bool(os.environ.get("LANGFUSE_PUBLIC_KEY")),
+ "has_secret_key": bool(os.environ.get("LANGFUSE_SECRET_KEY")),
+ "env_enabled": env_enabled,
+ "host": os.environ.get("LANGFUSE_HOST", "https://cloud.langfuse.com"),
+ "debug": os.environ.get("LANGFUSE_DEBUG", "false").lower() == "true",
+ "current_session_id": get_current_agent_session_id(),
+ }
+
+ if _langfuse_configured:
+ try:
+ # Try to get some context information
+ status["langfuse_context_available"] = True
+ except Exception as e:
+ status["langfuse_context_error"] = str(e)
+
+ return status
+
+
+def observe(*args, **kwargs):
+ def decorator(func):
+ return func
+
+ return decorator
diff --git a/examples/observability/README_Langfuse.md b/examples/observability/README_Langfuse.md
new file mode 100644
index 000000000..f80817806
--- /dev/null
+++ b/examples/observability/README_Langfuse.md
@@ -0,0 +1,114 @@
+# Langfuse Usage Guide
+
+Langfuse is an open-source LLM engineering platform that provides tracing, evaluation, prompt management, and metrics analysis. This guide covers how to use Langfuse with the CAMEL project and how to start a Docker self-hosted service.
+
+## Table of Contents
+
+- [Quick Start](#quick-start)
+- [Environment Configuration](#environment-configuration)
+- [Docker Self-Hosted Deployment](#docker-self-hosted-deployment)
+- [Related Links](#related-links)
+
+## Quick Start
+
+### 1. Install Dependencies
+
+```bash
+pip install 'camel-ai[all]'
+```
+
+### 2. Environment Variables Configuration
+
+Before using Langfuse, set the following environment variables:
+
+```bash
+# Enable Langfuse tracing
+export LANGFUSE_ENABLED="true"
+
+# Langfuse service configuration (optional, if using cloud service)
+export LANGFUSE_PUBLIC_KEY="pk-lf-your-public-key"
+export LANGFUSE_SECRET_KEY="sk-lf-your-secret-key"
+export LANGFUSE_HOST="https://cloud.langfuse.com" # or your self-hosted address
+
+# Debug mode (optional)
+export LANGFUSE_DEBUG="false"
+```
+
+### 3. Basic Usage Example
+
+```python
+from camel.agents import ChatAgent
+from camel.models import ModelFactory
+from camel.types import ModelPlatformType, ModelType
+
+# Create model
+model = ModelFactory.create(
+ model_platform=ModelPlatformType.OPENAI,
+ model_type=ModelType.GPT_4_1_MINI,
+)
+
+# Define system message
+sys_msg = "You are a helpful AI assistant, skilled at answering questions."
+
+# Create agent
+camel_agent = ChatAgent(system_message=sys_msg, model=model)
+
+# User message
+user_msg = "Calculate the square root after adding 222991 and 1111"
+
+# Execute conversation (automatically traced)
+response = camel_agent.step(user_msg)
+print(response.msgs[0].content)
+```
+
+## Environment Configuration
+
+### Environment Variables Description
+
+| Variable | Required | Default | Description |
+|----------|----------|---------|-------------|
+| `LANGFUSE_ENABLED` | Yes | `false` | Enable/disable Langfuse tracing |
+| `LANGFUSE_PUBLIC_KEY` | No* | - | Langfuse public key |
+| `LANGFUSE_SECRET_KEY` | No* | - | Langfuse secret key |
+| `LANGFUSE_HOST` | No | `https://cloud.langfuse.com` | Langfuse service address |
+| `LANGFUSE_DEBUG` | No | `false` | Enable debug mode |
+
+*Note: Required when using cloud service or authenticated self-hosted service
+
+## Docker Self-Hosted Deployment
+
+If you want to self-host the Langfuse service, you can quickly deploy it using Docker Compose.
+
+### Requirements
+
+- git
+- docker & docker compose -> Use Docker Desktop on Mac or Windows
+
+### Clone Langfuse Repository
+
+Get a copy of the latest Langfuse repository:
+
+```bash
+git clone https://github.com/langfuse/langfuse.git
+cd langfuse
+```
+
+### Start the Application
+
+Update the secrets in the docker-compose.yml and then run the langfuse docker compose using:
+
+```bash
+docker compose up
+```
+
+Watch the containers being started and the logs flowing in. After about 2-3 minutes, the langfuse-web-1 container should log "Ready". At this point you can proceed to the next step.
+
+### Done
+
+And you are ready to go! Open `http://localhost:3000` in your browser to access the Langfuse UI.
+
+## Related Links
+
+- [Langfuse Official Documentation](https://langfuse.com/docs)
+- [Langfuse GitHub Repository](https://github.com/langfuse/langfuse)
+- [Docker Compose Deployment Guide](https://langfuse.com/self-hosting/docker-compose)
diff --git a/examples/observability/langfuse_example.py b/examples/observability/langfuse_example.py
new file mode 100644
index 000000000..6709ec039
--- /dev/null
+++ b/examples/observability/langfuse_example.py
@@ -0,0 +1,54 @@
+#!/usr/bin/env python3
+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
+# Licensed 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.
+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
+
+"""
+Preparation before running:
+1. Set environment variables:
+ - LANGFUSE_ENABLED="true" # Enable tracing
+ - LANGFUSE_PUBLIC_KEY="pk-lf-..." # Optional
+ - LANGFUSE_SECRET_KEY="sk-lf-..." # Optional
+
+Note:
+- Tracing will only be enabled if LANGFUSE_ENABLED=true is set
+"""
+
+from camel.agents import ChatAgent
+from camel.models import ModelFactory
+from camel.toolkits import MathToolkit
+from camel.types import ModelPlatformType, ModelType
+from camel.utils import get_langfuse_status
+
+math_toolkit = [*MathToolkit().get_tools()]
+
+model = ModelFactory.create(
+ model_platform=ModelPlatformType.DEFAULT,
+ model_type=ModelType.DEFAULT,
+)
+
+# Define system message
+sys_msg = "You are a helpful AI assistant, skilled at answering questions."
+
+# Create agent
+camel_agent = ChatAgent(
+ system_message=sys_msg, model=model, tools=math_toolkit
+)
+
+# User message
+user_msg = "Calculate the square root after adding 222991 and 1111"
+
+print(get_langfuse_status())
+
+response = camel_agent.step(user_msg)
+print(response.msgs[0].content)
diff --git a/pyproject.toml b/pyproject.toml
index 02b913efc..2f57a279e 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -175,6 +175,7 @@ dev_tools = [
"mcp>=1.3.0",
"daytona-sdk==0.14.0",
"aci-sdk>=1.0.0b1",
+ "langfuse>=2.60.5",
]
model_platforms = [
"litellm>=1.38.1,<2",
@@ -381,6 +382,8 @@ all = [
"chunkr-ai>=0.0.50",
"weaviate-client>=4.15.0",
"python-pptx>=1.0.2",
+ "langfuse>=2.60.5",
+
]
[project.urls]
@@ -578,6 +581,7 @@ module = [
"pylatex.*",
"bohrium.*",
"chunkr_ai.*",
+ "langfuse.*",
]
ignore_missing_imports = true
diff --git a/test/models/test_crynux_model.py b/test/models/test_crynux_model.py
index e1d64e6bf..191f425b2 100644
--- a/test/models/test_crynux_model.py
+++ b/test/models/test_crynux_model.py
@@ -39,7 +39,7 @@ def test_crynux_model(model_type: ModelType):
@pytest.mark.model_backend
def test_crynux_model_unexpected_argument():
- model_type = (ModelType.CRYNUX_QWEN_2_5_7B_INSTRUCT,)
+ model_type = ModelType.CRYNUX_QWEN_2_5_7B_INSTRUCT
model_config_dict = {"model_path": "qwen/qwen_2"}
with pytest.raises(
diff --git a/uv.lock b/uv.lock
index 8ace8eabe..acf7adea5 100644
--- a/uv.lock
+++ b/uv.lock
@@ -578,6 +578,7 @@ dependencies = [
{ name = "docstring-parser" },
{ name = "httpx" },
{ name = "jsonschema" },
+ { name = "langfuse" },
{ name = "mcp" },
{ name = "openai" },
{ name = "pillow" },
@@ -1052,6 +1053,7 @@ requires-dist = [
{ name = "jsonschema", specifier = ">=4,<5" },
{ name = "jupyter-client", marker = "extra == 'all'", specifier = ">=8.6.2,<9" },
{ name = "jupyter-client", marker = "extra == 'dev-tools'", specifier = ">=8.6.2,<9" },
+ { name = "langfuse", specifier = ">=2.60.5" },
{ name = "linkup-sdk", marker = "extra == 'all'", specifier = ">=0.2.1,<0.3" },
{ name = "linkup-sdk", marker = "extra == 'web-tools'", specifier = ">=0.2.1,<0.3" },
{ name = "litellm", marker = "extra == 'all'", specifier = ">=1.38.1,<2" },
@@ -3491,6 +3493,25 @@ dependencies = [
]
sdist = { url = "https://files.pythonhosted.org/packages/0e/72/a3add0e4eec4eb9e2569554f7c70f4a3c27712f40e3284d483e88094cc0e/langdetect-1.0.9.tar.gz", hash = "sha256:cbc1fef89f8d062739774bd51eda3da3274006b3661d199c2655f6b3f6d605a0", size = 981474 }
+[[package]]
+name = "langfuse"
+version = "2.60.5"
+source = { registry = "https://pypi.org/simple" }
+dependencies = [
+ { name = "anyio" },
+ { name = "backoff" },
+ { name = "httpx" },
+ { name = "idna" },
+ { name = "packaging" },
+ { name = "pydantic" },
+ { name = "requests" },
+ { name = "wrapt" },
+]
+sdist = { url = "https://files.pythonhosted.org/packages/d1/19/475034a497a8a6e1156f62b3121522b753a1dad92a7187462402a9238467/langfuse-2.60.5.tar.gz", hash = "sha256:a33ecddc98cf6d12289372e63071b77b72230e7bc8260ee349f1465d53bf425b", size = 152406 }
+wheels = [
+ { url = "https://files.pythonhosted.org/packages/e9/04/8d69112a6b24431bfdb257a2a394f0ab036e5be5dcf4cb3b15db43b367f6/langfuse-2.60.5-py3-none-any.whl", hash = "sha256:fd27d52017f36d6fa5ca652615213a2535dc93dd88c3375eeb811af26384d285", size = 275379 },
+]
+
[[package]]
name = "lark"
version = "1.2.2"