Skip to content

Instantly share code, notes, and snippets.

@acheong08
Created May 27, 2023 10:58
Show Gist options
  • Save acheong08/be0b43bb89f9f4797079e5948c48738c to your computer and use it in GitHub Desktop.
Save acheong08/be0b43bb89f9f4797079e5948c48738c to your computer and use it in GitHub Desktop.
A guide on how to reverse engineer character.ai

Authentication:

  • Set cookies from your browser

Step 1: GET https://beta.character.ai/chat/curated_categories/characters/. It returns JSON. You are looking for external_id of the character you want. This will be used for all following steps (external_id or character_external_id)

Step 2: POST https://beta.character.ai/chat/character/info/ with {"external_id":"..."} as payload. Keep track of identifier from the response as tgt for step 4

Step 3: POST https://beta.character.ai/chat/history/create/ with the same payload as step 2. Keep track of external_id in the response as history_external_id.

Step 4: POST https://beta.character.ai/chat/streaming/ with payload:

{
    "history_external_id": "<HISTORY ID from STEP 3>",
    "character_external_id": "<CHARACTER ID from STEP 1>",
    "text": "<YOUR PROMPT>",
    "tgt": "<TGT from STEP 2>",
    "ranking_method": "random",
    "staging": false,
    "model_server_address": null,
    "model_server_address_exp_chars": null,
    "override_prefix": null,
    "override_rank": null,
    "rank_candidates": null,
    "filter_candidates": null,
    "unsanitized_characters": null,
    "prefix_limit": null,
    "prefix_token_limit": null,
    "stream_params": null,
    "model_properties_version_keys": "",
    "enable_tti": null,
    "initial_timeout": null,
    "insert_beginning": null,
    "stream_every_n_steps": 16,
    "chunks_to_pad": 8,
    "is_proactive": false,
    "image_rel_path": "",
    "image_description": "",
    "image_description_type": "",
    "image_origin_type": "",
    "voice_enabled": false,
    "parent_msg_uuid": null,
    "seen_msg_uuids": [],
    "retry_last_user_msg_uuid": null,
    "num_candidates": 1,
    "give_room_introductions": true,
    "mock_response": false
}

CLOUDFLARE!!! Use TLS spoofing! If you need help with this, look at my ChatGPT-Proxy-V4 code. You must write the program in Go or else it won't stream.

@Yorddyyorddy
Copy link

Yorddyyorddy commented Jun 5, 2023

import requests

headers = {
    'Authorization': 'Token e3e0912cea43c60c6a3d1c75fa3dd2700e2ee8d9',
     'content-type': 'application/json',
    'User-Agent': 'Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Mobile Safari/537.36',
    'Referer': 'https://beta.character.ai/chat?char=iV5qb8ttzD7Ytl69U_-ONcW2tW_lrFrOVKExyKJHlJM',
}

json_data = {
    'history_external_id': 'SOoqwaqybvE9m5fmLixK0YFlLYDXTetYh6cBiYdnNv4',
    'character_external_id': 'iV5qb8ttzD7Ytl69U_-ONcW2tW_lrFrOVKExyKJHlJM',
    'text': 'Hi',
    'tgt': 'internal_id:4fb39932-eae5-4cce-86f1-e2c75704fee1',
    'ranking_method': 'random',
    'staging': False,
    'model_server_address': None,
    'model_server_address_exp_chars': None,
    'override_prefix': None,
    'override_rank': None,
    'rank_candidates': None,
    'filter_candidates': None,
    'unsanitized_characters': None,
    'prefix_limit': None,
    'prefix_token_limit': None,
    'stream_params': None,
    'model_properties_version_keys': '',
    'enable_tti': None,
    'initial_timeout': None,
    'insert_beginning': None,
    'stream_every_n_steps': 16,
    'is_proactive': False,
    'image_rel_path': '',
    'image_description': '',
    'image_description_type': '',
    'image_origin_type': '',
    'voice_enabled': False,
    'parent_msg_uuid': None,
    'seen_msg_uuids': [],
    'retry_last_user_msg_uuid': None,
    'num_candidates': 1,
    'give_room_introductions': True,
    'mock_response': False,
}

response = requests.post('https://beta.character.ai/chat/streaming/', headers=headers, json=json_data)

I get blocked
Yeah I have issue with cloudflare but I'm only familiar with python and html how can I write with Go😒

@acheong08
Copy link
Author

acheong08 commented Jun 5, 2023

I'll write up an example when I get time. Alternatively, you can edit https://github.com/acheong08/ChatGPT-Proxy-V4 to proxy for character.ai for use in Python

@Yorddyyorddy
Copy link

I'll write up an example when I get time. Alternatively, you can edit https://github.com/acheong08/ChatGPT-Proxy-V4 to proxy for character.ai for use in Python

Thanks, but as idea what if you made cloudflare bypassing library or module bc nowadays most sensitive sites use cloudflare server

@acheong08
Copy link
Author

Difficult in Python with streaming

@Yorddyyorddy
Copy link

I have been trying some libs to bypass cloudflare this week python request falls within a seconds but httpx works until the js rendering page for all protected sites ......is there a way that the request wait for the js ends

@acheong08
Copy link
Author

acheong08 commented Jun 10, 2023

@Yorddyyorddy
Copy link

Still can't bypass but I haven't used proxy as I don't have any

Btw they have launched an app called c chat can we figure out how it calls their api end point

@fw-real
Copy link

fw-real commented Apr 30, 2024

yall still workin on this?

@acheong08
Copy link
Author

I wrote this as a response to some random Discord user and posted it as a gist as reference for anyone interested. There was never any intention to do anything with it.

AI hasn't really been that interesting beyond the initial hype.

@fw-real
Copy link

fw-real commented May 1, 2024

I wrote this as a response to some random Discord user and posted it as a gist as reference for anyone interested. There was never any intention to do anything with it.

AI hasn't really been that interesting beyond the initial hype.

oh alr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment