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: Update Gemini implementation with openai compatibility (#1225)

Wendong-Fanb44ec7f9e15b
diff --git a/camel/agents/chat_agent.py b/camel/agents/chat_agent.py
index 26739a402..18a4716f1 100644
--- a/camel/agents/chat_agent.py
+++ b/camel/agents/chat_agent.py
@@ -634,7 +634,7 @@ class ChatAgent(BaseAgent):
if (
not self.is_tools_added()
or not isinstance(response, ChatCompletion)
- or response.choices[0].message.tool_calls is None
+ or not response.choices[0].message.tool_calls
):
break
diff --git a/camel/configs/gemini_config.py b/camel/configs/gemini_config.py
index eb6dfb744..caa6df723 100644
--- a/camel/configs/gemini_config.py
+++ b/camel/configs/gemini_config.py
@@ -12,145 +12,103 @@
# limitations under the License.
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
+from __future__ import annotations
-from collections.abc import Iterable
-from typing import Any, Optional
+from typing import Any, Optional, Sequence, Type, Union
-from pydantic import model_validator
+from pydantic import BaseModel
from camel.configs.base_config import BaseConfig
+from camel.types import NOT_GIVEN, NotGiven
class GeminiConfig(BaseConfig):
- r"""A simple dataclass used to configure the generation parameters of
- `GenerativeModel.generate_content`.
+ r"""Defines the parameters for generating chat completions using the
+ Gemini API.
Args:
- candidate_count (int, optional): Number of responses to return.
- stop_sequences (Iterable[str], optional): The set of character
- sequences (up to 5) that will stop output generation. If specified
- the API will stop at the first appearance of a stop sequence.
- The stop sequence will not be included as part of the response.
- max_output_tokens (int, optional): The maximum number of tokens to
- include in a candidate. If unset, this will default to
- output_token_limit specified in the model's specification.
- temperature (float, optional): Controls the randomness of the output.
- Note: The default value varies by model, see the
- `Model.temperature` attribute of the `Model` returned
- the `genai.get_model` function. Values can range from [0.0,1.0],
- inclusive. A value closer to 1.0 will produce responses that are
- more varied and creative, while a value closer to 0.0 will
- typically result in more straightforward responses from the model.
- top_p (int, optional): The maximum cumulative probability of tokens to
- consider when sampling. The model uses combined Top-k and nucleus
- sampling. Tokens are sorted based on their assigned probabilities
- so that only the most likely tokens are considered. Top-k sampling
- directly limits the maximum number of tokens to consider, while
- Nucleus sampling limits number of tokens
- based on the cumulative probability. Note: The default value varies
- by model, see the `Model.top_p` attribute of the `Model` returned
- the `genai.get_model` function.
- top_k (int, optional): The maximum number of tokens to consider when
- sampling. The model uses combined Top-k and nucleus sampling.Top-k
- sampling considers the set of `top_k` most probable tokens.
- Defaults to 40. Note: The default value varies by model, see the
- `Model.top_k` attribute of the `Model` returned the
- `genai.get_model` function.
- response_mime_type (str, optional): Output response mimetype of the
- generated candidate text. Supported mimetype:
- `text/plain`: (default) Text output.
- `application/json`: JSON response in the candidates.
- response_schema (Schema, optional): Specifies the format of the
- JSON requested if response_mime_type is `application/json`.
- safety_settings (SafetySettingOptions, optional):
- Overrides for the model's safety settings.
- tools (FunctionLibraryType, optional):
- `protos.Tools` more info coming soon.
- tool_config (ToolConfigType, optional):
- more info coming soon.
- request_options (RequestOptionsType, optional):
- Options for the request.
+ temperature (float, optional): Sampling temperature to use, between
+ :obj:`0` and :obj:`2`. Higher values make the output more random,
+ while lower values make it more focused and deterministic.
+ (default: :obj:`0.2`)
+ top_p (float, optional): An alternative to sampling with temperature,
+ called nucleus sampling, where the model considers the results of
+ the tokens with top_p probability mass. So :obj:`0.1` means only
+ the tokens comprising the top 10% probability mass are considered.
+ (default: :obj:`1.0`)
+ n (int, optional): How many chat completion choices to generate for
+ each input message. (default: :obj:`1`)
+ response_format (object, optional): An object specifying the format
+ that the model must output. Compatible with GPT-4 Turbo and all
+ GPT-3.5 Turbo models newer than gpt-3.5-turbo-1106. Setting to
+ {"type": "json_object"} enables JSON mode, which guarantees the
+ message the model generates is valid JSON. Important: when using
+ JSON mode, you must also instruct the model to produce JSON
+ yourself via a system or user message. Without this, the model
+ may generate an unending stream of whitespace until the generation
+ reaches the token limit, resulting in a long-running and seemingly
+ "stuck" request. Also note that the message content may be
+ partially cut off if finish_reason="length", which indicates the
+ generation exceeded max_tokens or the conversation exceeded the
+ max context length.
+ stream (bool, optional): If True, partial message deltas will be sent
+ as data-only server-sent events as they become available.
+ (default: :obj:`False`)
+ stop (str or list, optional): Up to :obj:`4` sequences where the API
+ will stop generating further tokens. (default: :obj:`None`)
+ max_tokens (int, optional): The maximum number of tokens to generate
+ in the chat completion. The total length of input tokens and
+ generated tokens is limited by the model's context length.
+ (default: :obj:`None`)
+ tools (list[FunctionTool], optional): A list of tools the model may
+ call. Currently, only functions are supported as a tool. Use this
+ to provide a list of functions the model may generate JSON inputs
+ for. A max of 128 functions are supported.
+ tool_choice (Union[dict[str, str], str], optional): Controls which (if
+ any) tool is called by the model. :obj:`"none"` means the model
+ will not call any tool and instead generates a message.
+ :obj:`"auto"` means the model can pick between generating a
+ message or calling one or more tools. :obj:`"required"` means the
+ model must call one or more tools. Specifying a particular tool
+ via {"type": "function", "function": {"name": "my_function"}}
+ forces the model to call that tool. :obj:`"none"` is the default
+ when no tools are present. :obj:`"auto"` is the default if tools
+ are present.
"""
- candidate_count: Optional[int] = None
- stop_sequences: Optional[Iterable[str]] = None
- max_output_tokens: Optional[int] = None
- temperature: Optional[float] = None
- top_p: Optional[float] = None
- top_k: Optional[int] = None
- response_mime_type: Optional[str] = None
- response_schema: Optional[Any] = None
- safety_settings: Optional[Any] = None
- tool_config: Optional[Any] = None
- request_options: Optional[Any] = None
+ temperature: float = 0.2 # openai default: 1.0
+ top_p: float = 1.0
+ n: int = 1
+ stream: bool = False
+ stop: Union[str, Sequence[str], NotGiven] = NOT_GIVEN
+ max_tokens: Union[int, NotGiven] = NOT_GIVEN
+ response_format: Union[Type[BaseModel], dict, NotGiven] = NOT_GIVEN
+ tool_choice: Optional[Union[dict[str, str], str]] = None
- @model_validator(mode="before")
- @classmethod
- def model_type_checking(cls, data: Any):
- r"""Validate the type of tools in the configuration.
+ def as_dict(self) -> dict[str, Any]:
+ r"""Convert the current configuration to a dictionary.
- This method ensures that the tools provided in the configuration are
- instances of `FunctionTool`. If any tool is not an instance of
- `FunctionTool`, it raises a ValueError.
- """
- if isinstance(data, dict):
- response_schema = data.get("response_schema")
- safety_settings = data.get("safety_settings")
- tools = data.get("tools")
- tool_config = data.get("tool_config")
- request_options = data.get("request_options")
-
- if response_schema:
- from google.generativeai.protos import Schema
- from google.generativeai.types.content_types import (
- FunctionLibraryType,
- ToolConfigType,
- )
- from google.generativeai.types.helper_types import (
- RequestOptionsType,
- )
- from google.generativeai.types.safety_types import (
- SafetySettingOptions,
- )
- else:
- return data
+ This method converts the current configuration object to a dictionary
+ representation, which can be used for serialization or other purposes.
- if response_schema and not isinstance(response_schema, Schema):
- raise ValueError(
- "The response_schema should be an instance of "
- "google.generativeai.protos.Schema."
- )
-
- if safety_settings and not isinstance(
- safety_settings, SafetySettingOptions
- ):
- raise ValueError(
- "The safety_settings should be an instance of "
- "google.generativeai.types.safety_types."
- "SafetySettingOptions."
- )
+ Returns:
+ dict[str, Any]: A dictionary representation of the current
+ configuration.
+ """
+ config_dict = self.model_dump()
+ if self.tools:
+ from camel.toolkits import FunctionTool
- if tools is not None:
- for tool in tools:
- if not isinstance(tool, FunctionLibraryType):
+ tools_schema = []
+ for tool in self.tools:
+ if not isinstance(tool, FunctionTool):
raise ValueError(
- "The tool should be an instance of "
- "google.generativeai.types.content_types."
- "FunctionLibraryType."
+ f"The tool {tool} should "
+ "be an instance of `FunctionTool`."
)
- if tool_config and not isinstance(tool_config, ToolConfigType):
- raise ValueError(
- "The tool_config should be an instance of "
- "google.generativeai.types.content_types.ToolConfigType."
- )
- if request_options and not isinstance(
- request_options, RequestOptionsType
- ):
- raise ValueError(
- "The request_options should be an instance of "
- "google.generativeai.types.helper_types.RequestOptionsType."
- )
- return data
+ tools_schema.append(tool.get_openai_tool_schema())
+ config_dict["tools"] = NOT_GIVEN
+ return config_dict
-Gemini_API_PARAMS = {param for param in GeminiConfig().model_fields.keys()}
+Gemini_API_PARAMS = {param for param in GeminiConfig.model_fields.keys()}
diff --git a/camel/models/gemini_model.py b/camel/models/gemini_model.py
index 8d5a39486..fd1b4c4db 100644
--- a/camel/models/gemini_model.py
+++ b/camel/models/gemini_model.py
@@ -12,52 +12,46 @@
# limitations under the License.
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
import os
-from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union
+from typing import Any, Dict, List, Optional, Union
+
+from openai import OpenAI, Stream
from camel.configs import Gemini_API_PARAMS, GeminiConfig
from camel.messages import OpenAIMessage
from camel.models import BaseModelBackend
from camel.types import (
ChatCompletion,
- ChatCompletionMessage,
- Choice,
+ ChatCompletionChunk,
ModelType,
)
from camel.utils import (
BaseTokenCounter,
- GeminiTokenCounter,
+ OpenAITokenCounter,
api_keys_required,
- dependencies_required,
)
-if TYPE_CHECKING:
- from google.generativeai.types import ContentsType, GenerateContentResponse
-
class GeminiModel(BaseModelBackend):
r"""Gemini API in a unified BaseModelBackend interface.
Args:
model_type (Union[ModelType, str]): Model for which a backend is
- created.
+ created, one of Gemini series.
model_config_dict (Optional[Dict[str, Any]], optional): A dictionary
- that will be fed into:obj:`genai.GenerativeModel.generate_content()
- `. If:obj:`None`, :obj:`GeminiConfig().as_dict()` will be used.
+ that will be fed into:obj:`openai.ChatCompletion.create()`. If
+ :obj:`None`, :obj:`GeminiConfig().as_dict()` will be used.
(default: :obj:`None`)
api_key (Optional[str], optional): The API key for authenticating with
- the gemini service. (default: :obj:`None`)
- url (Optional[str], optional): The url to the gemini service.
- (default: :obj:`None`)
+ the Gemini service. (default: :obj:`None`)
+ url (Optional[str], optional): The url to the Gemini service.
+ (default: :obj:`https://generativelanguage.googleapis.com/v1beta/
+ openai/`)
token_counter (Optional[BaseTokenCounter], optional): Token counter to
- use for the model. If not provided, :obj:`GeminiTokenCounter` will
- be used. (default: :obj:`None`)
-
- Notes:
- Currently :obj:`"stream": True` is not supported with Gemini due to the
- limitation of the current camel design.
+ use for the model. If not provided, :obj:`OpenAITokenCounter(
+ ModelType.GPT_4O_MINI)` will be used.
+ (default: :obj:`None`)
"""
- @dependencies_required('google')
def __init__(
self,
model_type: Union[ModelType, str],
@@ -66,29 +60,45 @@ class GeminiModel(BaseModelBackend):
url: Optional[str] = None,
token_counter: Optional[BaseTokenCounter] = None,
) -> None:
- import google.generativeai as genai
- from google.generativeai.types.generation_types import GenerationConfig
-
if model_config_dict is None:
model_config_dict = GeminiConfig().as_dict()
-
- api_key = api_key or os.environ.get("GOOGLE_API_KEY")
+ api_key = api_key or os.environ.get("GEMINI_API_KEY")
+ url = url or os.environ.get(
+ "GEMINI_API_BASE_URL",
+ "https://generativelanguage.googleapis.com/v1beta/openai/",
+ )
super().__init__(
model_type, model_config_dict, api_key, url, token_counter
)
- genai.configure(api_key=self._api_key)
- self._client = genai.GenerativeModel(self.model_type)
+ self._client = OpenAI(
+ timeout=60,
+ max_retries=3,
+ api_key=self._api_key,
+ base_url=self._url,
+ )
- keys = list(self.model_config_dict.keys())
- generation_config_dict = {
- k: self.model_config_dict.pop(k)
- for k in keys
- if hasattr(GenerationConfig, k)
- }
- generation_config = genai.types.GenerationConfig(
- **generation_config_dict
+ @api_keys_required("GEMINI_API_KEY")
+ def run(
+ self,
+ messages: List[OpenAIMessage],
+ ) -> Union[ChatCompletion, Stream[ChatCompletionChunk]]:
+ r"""Runs inference of Gemini chat completion.
+
+ Args:
+ messages (List[OpenAIMessage]): Message list with the chat history
+ in OpenAI API format.
+
+ Returns:
+ Union[ChatCompletion, Stream[ChatCompletionChunk]]:
+ `ChatCompletion` in the non-stream mode, or
+ `Stream[ChatCompletionChunk]` in the stream mode.
+ """
+ response = self._client.chat.completions.create(
+ messages=messages,
+ model=self.model_type,
+ **self.model_config_dict,
)
- self.model_config_dict["generation_config"] = generation_config
+ return response
@property
def token_counter(self) -> BaseTokenCounter:
@@ -99,125 +109,30 @@ class GeminiModel(BaseModelBackend):
tokenization style.
"""
if not self._token_counter:
- self._token_counter = GeminiTokenCounter(self.model_type)
+ self._token_counter = OpenAITokenCounter(ModelType.GPT_4O_MINI)
return self._token_counter
- @api_keys_required("GOOGLE_API_KEY")
- def run(
- self,
- messages: List[OpenAIMessage],
- ) -> ChatCompletion:
- r"""Runs inference of Gemini model.
- This method can handle multimodal input
-
- Args:
- messages: Message list or Message with the chat history
- in OpenAi format.
-
- Returns:
- response: A ChatCompletion object formatted for the OpenAI API.
- """
- response = self._client.generate_content(
- contents=self.to_gemini_req(messages),
- **self.model_config_dict,
- )
- response.resolve()
- return self.to_openai_response(response)
-
def check_model_config(self):
r"""Check whether the model configuration contains any
unexpected arguments to Gemini API.
Raises:
ValueError: If the model configuration dictionary contains any
- unexpected arguments to OpenAI API.
+ unexpected arguments to Gemini API.
"""
- if self.model_config_dict is not None:
- for param in self.model_config_dict:
- if param not in Gemini_API_PARAMS:
- raise ValueError(
- f"Unexpected argument `{param}` is "
- "input into Gemini model backend."
- )
+ for param in self.model_config_dict:
+ if param not in Gemini_API_PARAMS:
+ raise ValueError(
+ f"Unexpected argument `{param}` is "
+ "input into Gemini model backend."
+ )
@property
def stream(self) -> bool:
- r"""Returns whether the model is in stream mode,
- which sends partial results each time.
+ r"""Returns whether the model is in stream mode, which sends partial
+ results each time.
Returns:
bool: Whether the model is in stream mode.
"""
return self.model_config_dict.get('stream', False)
-
- def to_gemini_req(self, messages: List[OpenAIMessage]) -> 'ContentsType':
- r"""Converts the request from the OpenAI API format to the Gemini API
- request format.
-
- Args:
- messages: The request object from the OpenAI API.
-
- Returns:
- converted_messages: A list of messages formatted for Gemini API.
- """
- # role reference
- # https://ai.google.dev/api/python/google/generativeai/protos/Content
- converted_messages = []
- for message in messages:
- role = message.get('role')
- if role == 'assistant':
- role_to_gemini = 'model'
- else:
- role_to_gemini = 'user'
- converted_message = {
- "role": role_to_gemini,
- "parts": message.get("content"),
- }
- converted_messages.append(converted_message)
- return converted_messages
-
- def to_openai_response(
- self,
- response: 'GenerateContentResponse',
- ) -> ChatCompletion:
- r"""Converts the response from the Gemini API to the OpenAI API
- response format.
-
- Args:
- response: The response object returned by the Gemini API
-
- Returns:
- openai_response: A ChatCompletion object formatted for
- the OpenAI API.
- """
- import time
- import uuid
-
- openai_response = ChatCompletion(
- id=f"chatcmpl-{uuid.uuid4().hex!s}",
- object="chat.completion",
- created=int(time.time()),
- model=self.model_type,
- choices=[],
- )
- for i, candidate in enumerate(response.candidates):
- content = ""
- if candidate.content and len(candidate.content.parts) > 0:
- content = candidate.content.parts[0].text
- finish_reason = candidate.finish_reason
- finish_reason_mapping = {
- "FinishReason.STOP": "stop",
- "FinishReason.SAFETY": "content_filter",
- "FinishReason.RECITATION": "content_filter",
- "FinishReason.MAX_TOKENS": "length",
- }
- finish_reason = finish_reason_mapping.get(finish_reason, "stop")
- choice = Choice(
- index=i,
- message=ChatCompletionMessage(
- role="assistant", content=content
- ),
- finish_reason=finish_reason,
- )
- openai_response.choices.append(choice)
- return openai_response
diff --git a/examples/models/role_playing_with_cohere.py b/examples/models/role_playing_with_cohere.py
index 72d3d6af0..ff93313d0 100644
--- a/examples/models/role_playing_with_cohere.py
+++ b/examples/models/role_playing_with_cohere.py
@@ -30,7 +30,7 @@ from camel.utils import print_text_animated
def main(
model_platform=ModelPlatformType.COHERE,
- model_type=ModelType.COHERE_COMMAND_R,
+ model_type=ModelType.COHERE_COMMAND_R_PLUS,
chat_turn_limit=3,
) -> None:
task_prompt = (
diff --git a/test/models/test_gemini_model.py b/test/models/test_gemini_model.py
index 390b7487b..3bf6a39a3 100644
--- a/test/models/test_gemini_model.py
+++ b/test/models/test_gemini_model.py
@@ -18,7 +18,7 @@ import pytest
from camel.configs import GeminiConfig
from camel.models import GeminiModel
from camel.types import ModelType
-from camel.utils import GeminiTokenCounter
+from camel.utils import OpenAITokenCounter
@pytest.mark.model_backend
@@ -35,7 +35,7 @@ def test_gemini_model(model_type: ModelType):
model = GeminiModel(model_type, model_config_dict)
assert model.model_type == model_type
assert model.model_config_dict == model_config_dict
- assert isinstance(model.token_counter, GeminiTokenCounter)
+ assert isinstance(model.token_counter, OpenAITokenCounter)
assert isinstance(model.model_type.value_for_tiktoken, str)
assert isinstance(model.model_type.token_limit, int)