🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org
Python agentai-societiesartificial-intelligencecommunicative-aicooperative-aideep-learninglarge-language-modelsmulti-agent-systemsnatural-language-processing
<a id="camel.models.xai_model"></a>
<a id="camel.models.xai_model.XAIModel"></a>
## XAIModel
```python
class XAIModel(BaseModelBackend):
```
xAI native SDK model backend using gRPC.
This backend uses the `__INLINE_CODE_0__` package to communicate with xAI's API
via gRPC, providing access to Grok models with native features like
encrypted thinking content and server-side conversation storage.
**Parameters:**
- **model_type** (Union[ModelType, str]): Model for which a backend is created, one of the Grok series (e.g. `__INLINE_CODE_0__`).
- **model_config_dict** (Optional[Dict[str, Any]], optional): A dictionary of configuration parameters. If :obj:`None`, :obj:`XAIConfig().as_dict()` will be used. (default: :obj:`None`)
- **api_key** (Optional[str], optional): The API key for authenticating with the xAI service. Falls back to the `__INLINE_CODE_0____INLINE_CODE_1__None`)
- **url** (Optional[str], optional): Not used for native SDK (gRPC), kept for interface compatibility. (default: :obj:`None`)
- **token_counter** (Optional[BaseTokenCounter], optional): Token counter to use for the model. If not provided, :obj:`OpenAITokenCounter( ModelType.GPT_4O_MINI)` will be used. (default: :obj:`None`)
- **timeout** (Optional[float], optional): The timeout value in seconds for API calls. If not provided, will fall back to the MODEL_TIMEOUT environment variable or default to 180 seconds. (default: :obj:`None`)
- **max_retries** (int, optional): Maximum number of retries for API calls. (default: :obj:`3`)
<a id="camel.models.xai_model.XAIModel.__init__"></a>
### __init__
```python
def __init__(
self,
model_type: Union[ModelType, str],
model_config_dict: Optional[Dict[str, Any]] = None,
api_key: Optional[str] = None,
url: Optional[str] = None,
token_counter: Optional[BaseTokenCounter] = None,
timeout: Optional[float] = None,
max_retries: int = 3,
**kwargs: Any
):
```
<a id="camel.models.xai_model.XAIModel.client"></a>
### client
```python
def client(self):
```
Lazy-initialize the synchronous xAI SDK client.
<a id="camel.models.xai_model.XAIModel.async_client"></a>
### async_client
```python
def async_client(self):
```
Lazy-initialize the asynchronous xAI SDK client.
<a id="camel.models.xai_model.XAIModel.token_counter"></a>
### token_counter
```python
def token_counter(self):
```
**Returns:**
OpenAITokenCounter: The token counter following the model's
tokenization style.
<a id="camel.models.xai_model.XAIModel.stream"></a>
### stream
```python
def stream(self):
```
**Returns:**
bool: Whether the model is in stream mode.
<a id="camel.models.xai_model.XAIModel._convert_openai_tool_to_xai"></a>
### _convert_openai_tool_to_xai
```python
def _convert_openai_tool_to_xai(self, tool: Dict[str, Any]):
```
Convert an OpenAI-format tool dict to xAI SDK Tool proto.
**Parameters:**
- **tool**: OpenAI-format tool dict with `__INLINE_CODE_0____INLINE_CODE_1____INLINE_CODE_2__` keys.
**Returns:**
chat_pb2.Tool: The xAI SDK tool proto.
<a id="camel.models.xai_model.XAIModel._convert_messages"></a>
### _convert_messages
```python
def _convert_messages(self, messages: List[OpenAIMessage]):
```
Convert OpenAI-format messages to xAI SDK message objects.
Handles system, user, assistant (with/without tool calls), and tool
result messages. For assistant messages that carry reasoning or
encrypted content (stored from a previous response), those fields are
injected into the protobuf message so xAI can reconstruct the
reasoning chain.
**Parameters:**
- **messages**: List of OpenAI-format message dicts.
**Returns:**
List of `__INLINE_CODE_0____INLINE_CODE_1____INLINE_CODE_2__`.
<a id="camel.models.xai_model.XAIModel._map_finish_reason"></a>
### _map_finish_reason
```python
def _map_finish_reason(xai_reason: str):
```
Map xAI finish reason enum name to OpenAI-style string.
<a id="camel.models.xai_model.XAIModel._xai_response_to_chat_completion"></a>
### _xai_response_to_chat_completion
```python
def _xai_response_to_chat_completion(self, response: 'XAIResponse'):
```
Convert an xAI SDK Response to an OpenAI ChatCompletion.
Preserves reasoning_content and encrypted_content from the response
for downstream consumers and for injection into future requests.
**Parameters:**
- **response**: The xAI SDK Response object.
**Returns:**
ChatCompletion: The OpenAI-compatible completion object.
<a id="camel.models.xai_model.XAIModel._make_chunk"></a>
### _make_chunk
```python
def _make_chunk(
chunk_id: str,
model: str,
delta_content: Optional[str] = None,
delta_reasoning: Optional[str] = None,
delta_tool_calls: Optional[List[Any]] = None,
finish_reason: Optional[str] = None,
usage: Any = None
):
```
Build a `__INLINE_CODE_0__` with proper pydantic objects.
<a id="camel.models.xai_model.XAIModel._build_delta_tool_calls"></a>
### _build_delta_tool_calls
```python
def _build_delta_tool_calls(self, tool_calls: Sequence):
```
Convert xAI tool calls to ChoiceDeltaToolCall objects.
<a id="camel.models.xai_model.XAIModel._stream_to_chunks"></a>
### _stream_to_chunks
```python
def _stream_to_chunks(self, stream_iter, total_messages: int = 0):
```
Convert xAI SDK stream iterator to ChatCompletionChunk generator.
**Parameters:**
- **stream_iter**: Iterator from `__INLINE_CODE_0____INLINE_CODE_1____INLINE_CODE_2__` tuples.
- **total_messages**: Total message count for chain state bookkeeping.
- **Yields**:
- **ChatCompletionChunk**: OpenAI-compatible streaming chunks.
<a id="camel.models.xai_model.XAIModel._build_chat_kwargs"></a>
### _build_chat_kwargs
```python
def _build_chat_kwargs(self, tools: Optional[List[Dict[str, Any]]] = None):
```
Build keyword arguments for `__INLINE_CODE_0__`.
Translates the CAMEL model config dict into xAI SDK create
parameters, and converts OpenAI-format tools to xAI Tool protos.
**Parameters:**
- **tools**: Optional list of OpenAI-format tool dicts.
**Returns:**
Dict of keyword arguments for `__INLINE_CODE_0__`.
<a id="camel.models.xai_model.XAIModel._compute_delta_messages"></a>
### _compute_delta_messages
```python
def _compute_delta_messages(self, messages: List[OpenAIMessage]):
```
Compute the delta messages for conversation chaining.
If we have a `__INLINE_CODE_0__` from the last turn, only the
new messages since then need to be sent. If the conversation was
truncated (memory reset / context-window management), fall back to
sending the full history.
**Parameters:**
- **messages**: The full conversation history from ChatAgent.
**Returns:**
The messages to actually send in this request.
<a id="camel.models.xai_model.XAIModel._save_response_chain"></a>
### _save_response_chain
```python
def _save_response_chain(self, response_id: Optional[str], total_messages: int):
```
Persist the chain state after a successful response.
<a id="camel.models.xai_model.XAIModel._inject_encrypted_content"></a>
### _inject_encrypted_content
```python
def _inject_encrypted_content(self, messages: List[OpenAIMessage]):
```
Inject stored encrypted/reasoning content into conversation
history for multi-turn reasoning.
When `__INLINE_CODE_0__` is enabled, the encrypted thinking
traces from the previous response must be included in the assistant
message of the conversation history so xAI can reconstruct the
reasoning chain.
**Parameters:**
- **messages**: The original OpenAI-format messages.
**Returns:**
Messages with encrypted content injected into the last assistant
message.
<a id="camel.models.xai_model.XAIModel._prepare_chat"></a>
### _prepare_chat
```python
def _prepare_chat(
self,
messages: List[OpenAIMessage],
tools: Optional[List[Dict[str, Any]]] = None,
response_format: Optional[Type[BaseModel]] = None
):
```
**Returns:**
(chat_kwargs, messages_to_send, total_message_count)
<a id="camel.models.xai_model.XAIModel._handle_response"></a>
### _handle_response
```python
def _handle_response(self, response: 'XAIResponse', total_messages: int):
```
Convert response and update chain state.
<a id="camel.models.xai_model.XAIModel._run"></a>
### _run
```python
def _run(
self,
messages: List[OpenAIMessage],
response_format: Optional[Type[BaseModel]] = None,
tools: Optional[List[Dict[str, Any]]] = None
):
```
Runs inference using the xAI native SDK.
Uses `__INLINE_CODE_0__` to chain conversations on xAI's
server, sending only new (delta) messages instead of the full
history on subsequent turns.
**Parameters:**
- **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, Stream[ChatCompletionChunk]]:
`__INLINE_CODE_0__` in non-stream mode, or a
`__INLINE_CODE_1__` generator in stream mode.