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

Added logprobs handling in choice response and user can get the logit… (#1236)

hengzzzhoue14b3b77564c
diff --git a/camel/agents/chat_agent.py b/camel/agents/chat_agent.py
index d4ca76938..fdeeb779b 100644
--- a/camel/agents/chat_agent.py
+++ b/camel/agents/chat_agent.py
@@ -1091,7 +1091,25 @@ class ChatAgent(BaseAgent):
meta_dict=dict(),
content=choice.message.content or "",
)
+ if choice.logprobs is not None:
+ tokens_logprobs = choice.logprobs.content
+ if tokens_logprobs is not None:
+ logprobs_info = [
+ {
+ "token": token_logprob.token,
+ "logprob": token_logprob.logprob,
+ "top_logprobs": [
+ (top_logprob.token, top_logprob.logprob)
+ for top_logprob in token_logprob.top_logprobs
+ ],
+ }
+ for token_logprob in tokens_logprobs
+ ]
+ if chat_message.meta_dict is None:
+ chat_message.meta_dict = {}
+ chat_message.meta_dict["logprobs_info"] = logprobs_info
output_messages.append(chat_message)
+
finish_reasons = [
str(choice.finish_reason) for choice in response.choices
]
diff --git a/camel/configs/vllm_config.py b/camel/configs/vllm_config.py
index b52d015c1..e3439f83b 100644
--- a/camel/configs/vllm_config.py
+++ b/camel/configs/vllm_config.py
@@ -13,7 +13,7 @@
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
from __future__ import annotations
-from typing import Sequence, Union
+from typing import Optional, Sequence, Union
from pydantic import Field
@@ -84,6 +84,12 @@ class VLLMConfig(BaseConfig):
user (str, optional): A unique identifier representing your end-user,
which can help OpenAI to monitor and detect abuse.
(default: :obj:`""`)
+ logprobs: Whether to return log probabilities of the output tokens or not. If true,
+ returns the log probabilities of each output token returned in the `logits` of
+ `message`.
+ top_logprobs: An integer between 0 and 20 specifying the number of most likely tokens to
+ return at each token position, each with an associated log probability.
+ `logprobs` must be set to `true` if this parameter is used.
"""
temperature: float = 0.2 # openai default: 1.0
@@ -97,6 +103,8 @@ class VLLMConfig(BaseConfig):
frequency_penalty: float = 0.0
logit_bias: dict = Field(default_factory=dict)
user: str = ""
+ logprobs: Optional[bool] = None
+ top_logprobs: Optional[int] = None
VLLM_API_PARAMS = {param for param in VLLMConfig.model_fields.keys()}
diff --git a/camel/messages/base.py b/camel/messages/base.py
index a78340df2..56da5fe8c 100644
--- a/camel/messages/base.py
+++ b/camel/messages/base.py
@@ -62,7 +62,7 @@ class BaseMessage:
role_name: str
role_type: RoleType
- meta_dict: Optional[Dict[str, str]]
+ meta_dict: Optional[Dict[str, Any]]
content: str
video_bytes: Optional[bytes] = None
diff --git a/camel/responses/agent_responses.py b/camel/responses/agent_responses.py
index 3fa960f0f..78320331d 100644
--- a/camel/responses/agent_responses.py
+++ b/camel/responses/agent_responses.py
@@ -44,3 +44,26 @@ class ChatAgentResponse(BaseModel):
"for a single message in msgs."
)
return self.msgs[0]
+
+ @property
+ def logits(self):
+ """Return the logits associated with the first message in msgs.
+
+ This property retrieves the `meta_dict` of the first message in `msgs`,
+ which contains information such as token log probabilities and top log
+ probabilities that were computed during message generation.
+
+ Returns:
+ dict: A dictionary containing the logits or log probability
+ information of the tokens.
+
+ Raises:
+ RuntimeError: If the list of messages (`msgs`) does not contain
+ exactly one message.
+ """
+ if len(self.msgs) != 1:
+ raise RuntimeError(
+ "Property msg is only available "
+ "for a single message in msgs."
+ )
+ return self.msgs[0].meta_dict