Skip to content

Jules wip 6112715387646448010 #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@ venv/
ENV/
env.bak/
venv.bak/
pyenv/
*.env

# VS Code
.vscode/
.history/
*.code-workspace

# Specstory
.specstory/

# Spyder project settings
.spyderproject
Expand Down
64 changes: 62 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Grok3 API

Grok3 is cool, smart, and useful, but there is no official API available. This is an **unofficial Python client** for interacting with the Grok 3 API. It leverages cookies from browser requests to authenticate and access the API endpoints.
Grok3 is cool, smart, and useful, but there is no official API available. This is an **unofficial Python client** for interacting with the Grok 3 API. It leverages cookies from browser requests to authenticate and access the API endpoints. The API also provides OpenAI-compatible endpoints for easy integration with existing applications.

---

Expand Down Expand Up @@ -49,9 +49,11 @@ Example cookie string from a curl command:

### 4. Use the Client

#### 4.1 Direct Client Usage

Pass the extracted cookie values to the GrokClient and send a message:

```
```python
from grok_client import GrokClient

# Your cookie values
Expand All @@ -71,6 +73,64 @@ response = client.send_message("write a poem")
print(response)
```

#### 4.2 OpenAI-Compatible API Server

The package includes an OpenAI-compatible API server that allows you to use Grok with any OpenAI-compatible client library or application.

##### Start the Server

1. Create a `.env` file in the project directory using the provided `.env.example` template:
```bash
cp grok_client/.env.example .env
```

2. Update the `.env` file with your Grok cookie values:
```env
GROK_SSO=your_sso_cookie
GROK_SSO_RW=your_sso_rw_cookie
# Optional configurations
API_HOST=127.0.0.1
API_PORT=8000
MODEL_NAME=grok-3
```

3. Start the API server:
```bash
uvicorn grok_client.server:app --reload --host 0.0.0.0 --port 8000
```

##### Use with OpenAI Python Client

```python
from openai import OpenAI

# Initialize client pointing to local server
client = OpenAI(
base_url="http://localhost:8000/v1",
api_key="dummy-key" # Any non-empty string will work
)

# Create a chat completion
response = client.chat.completions.create(
model="grok-3", # Model name can be configured in .env
messages=[
{"role": "user", "content": "Hello, how can you help me?"}
]
)

print(response.choices[0].message.content)
```

##### Interactive Chat Script

The package includes an interactive chat script that uses the OpenAI-compatible endpoint:

```bash
python grok_client/interactive.py
```

This provides a command-line interface for chatting with Grok using the OpenAI-compatible API.

### 5. Optional: Add Memory with Mem0

If you want Grok to remember conversations, you can integrate it with Mem0. Mem0 provides a memory layer for AI applications.
Expand Down
14 changes: 14 additions & 0 deletions grok_client/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Grok API Configuration

# API Server Configuration
API_HOST=127.0.0.1
API_PORT=8000

# Grok Model Configuration
MODEL_NAME=grok-3

# Authentication Cookies
# Replace these with your actual Grok cookies
# You can obtain these from your browser after logging into Grok
GROK_SSO=your_sso_cookie_value_here
GROK_SSO_RW=your_sso_rw_cookie_value_here
31 changes: 30 additions & 1 deletion grok_client/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,33 @@
"""
Grok Client Package
===================

This package provides client utilities for interacting with a Grok API.
It includes:
- `GrokClient`: A low-level client for sending messages directly to the Grok service.
- `GrokOpenAIClient`: An OpenAI-compatible client that wraps the Grok API,
allowing it to be used with tools and libraries designed for the OpenAI API structure.
- Custom error classes for more specific error handling.
- A FastAPI server (`server.py`) that exposes an OpenAI-compatible API endpoint
backed by the Grok service.

The main components intended for direct use are typically `GrokClient` or
`GrokOpenAIClient`.
"""
from .client import GrokClient
from .grok_openai_client import GrokOpenAIClient
from .errors import GrokClientError, GrokAPIError, AuthenticationError, NetworkError, ConfigurationError

__version__ = "0.1.0"
__all__ = ['GrokClient']
"""The version of the grok_client package."""

__all__ = [
'GrokClient',
'GrokOpenAIClient',
'GrokClientError',
'GrokAPIError',
'AuthenticationError',
'NetworkError',
'ConfigurationError'
]
"""Publicly exposed names from the grok_client package."""
Loading