grok provider
feat: Add support for xAI's grok
This PR adds support for xAI's grok models via their OpenAI-compatible API. Since the xAI API is compatible with the OpenAI SDK, this implementation extends the existing OpenAI provider.
The xAI documentation provides an example of using the OpenAI Python SDK:
```python
import os
from openai import OpenAI
XAI_API_KEY = os.getenv("XAI_API_KEY")
client = OpenAI(
api_key=XAI_API_KEY,
base_url="https://api.x.ai/v1",
)
completion = client.chat.completions.create(
model="grok-2-latest",
messages=[
{"role": "system", "content": "You are Grok, a chatbot inspired by the Hitchhikers Guide to the Galaxy."},
{"role": "user", "content": "What is the meaning of life, the universe, and everything?"},
],
)
print(completion.choices[0].message.content)
```
To facilitate code reuse and accommodate xAI's pricing structure, the OpenAI provider interface was slightly modified to move pricing logic into the class itself.
This change allows for seamless integration with xAI while maintaining compatibility with existing OpenAI models. Pricing for `grok-beta` and `grok-2` models has been included.
Tested and working (see attached screenshot).

合并状态:未合并 4 条评论