/keys2text_proxy

Proxy for novelcrafter.

Primary LanguagePythonMIT LicenseMIT

Keys2Text Proxy

Keys2Text Proxy is a Python-based application that acts as an API proxy server, offering OpenAI API compatibility while seamlessly routing requests to multiple AI providers using your own API keys. You can use the familiar OpenAI endpoints in your existing code and effortlessly switch between providers—Anthropic, Google AI Studio, Groq, OpenRouter, DeepSeek, LM Studio, Ollama, or OpenAI itself—simply by configuring your API keys.


Keys2Text Chat is a similar app but independent of novelcrafter.
It's a standalone app also intended to capture chat conversations as plain text (no Markdown, no HTML, no XML).


A video of installing and using Keys2Text Proxy with novelcrafter


Key Features

  • Free and Open-source forever
    Keys2Text Proxy is a Python FastAPI based app that is free and open-sourced.
    This technology relies on two key foundations: the essential infrastructure provided by AI companies and their APIs, along with innovative writing tools like novelcrafter that help bring these capabilities to life.

  • OpenAI API Compatibility
    Mimics OpenAI's endpoints and request/response formats, but only the text-based chat completion endpoints.
    And now that each AI provider returns a list of available models by using client.models.list() the app can stay up-to-date with model releases.

  • Multi-Provider Support
    Supply your own API keys to these services, and the proxy will handle the request translation behind the scenes.
    The API keys are found if/when you've exported them into the environment.

    With Keys2Text Proxy, you can easily connect to:

    • OpenAI
    • Anthropic (Claude)
    • Google AI Studio (Gemini)    free as of Jan 2025
    • Groq    free as of Jan 2025
    • OpenRouter    a few free models as of Jan 2025
    • DeepSeek
    • LM Studio
    • Ollama
  • Timestamped Chat History as plain text file

    • The app keeps a plain text log of all requests and responses for reference and as an aid in writing.
    • Conversations are saved with timestamps and paired using Me: and AI: labels.
    • A new text file is created whenever the app is started up, which allows for organizing
      your AI chats simply by starting, stopping, and restarting this app.

This was the result of a personal itch; I find repeatedly doing copy/paste's tedious, as it's
much easier to just have everything then do editing.

How It Works

  1. Local HTTP Server
    Keys2Text Proxy starts an HTTP server (default: http://localhost:8000) with
    routes matching the OpenAI API —-for example, /v1/chat/completions.

  2. Request Translation
    When a request arrives (in OpenAI-compatible format), the proxy translates it to
    the corresponding provider’s format using the model named in the request.

  3. Response Translation
    The provider’s response is then converted back into the OpenAI-like responses.

  4. Provider-Specific API Keys
    Users configure environment variables, API keys, for their preferred AI services.


Installation

Pick your ☠️ poison based on your setup:

  • Option 1: Install via pip (if you already have Python)
  • Option 2: Install via Miniconda (for a fresh start)

Option 1: Install via pip

  1. Open a terminal (mac) or Command Prompt (Windows).
  2. Run:
    python --version
    pip --version
    pip install keys2text_proxy
  3. Start the proxy:
    keys2text_proxy
    Note: this will fail if you do not already have API keys defined in your environment.

Option 2: Install via Miniconda to install python/pip in a separate environment

Windows 💾

  1. Open Command Prompt.
  2. Run:
    curl https://repo.anaconda.com/miniconda/Miniconda3-latest-Windows-x86_64.exe -o miniconda.exe
    start /wait "" .\miniconda.exe /S
    del miniconda.exe
  3. Make a new folder, create a conda environment, and install Keys2Text Proxy:
    mkdir someFolder
    cd someFolder
    conda create -n keysapp python=3.11
    conda activate keysapp
    pip install keys2text_proxy
    keys2text_proxy

macOS 🍎

  1. Open Terminal.
  2. Run:
    mkdir -p ~/miniconda3
    curl https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-arm64.sh -o ~/miniconda3/miniconda.sh
    bash ~/miniconda3/miniconda.sh -b -u -p ~/miniconda3
    rm ~/miniconda3/miniconda.sh
  3. Initialize your shell:
    source ~/miniconda3/bin/activate
    conda init --all
  4. Close and reopen Terminal, then:
    mkdir someFolder
    cd someFolder
    conda create -n keysapp python=3.11
    conda activate keysapp
    pip install keys2text_proxy
    keys2text_proxy

Troubleshooting 😈

  • Windows: If you get permission errors, try running Command Prompt as administrator.
  • macOS: If conda isn’t recognized, make sure you ran conda init --all and then reopened your terminal.
  • Conda environment: Double-check you’ve activated it (conda activate keysapp) before starting the proxy.

After configuring your API keys, Keys2Text Proxy will start on your machine, ready for novelcrafter or any other app that needs it.


Configuration of API 🔑 keys 🔑

You can set up your API keys in two ways:

Option A: Using a .env file (Recommended)

  1. Locate the .example.env file in the project root directory
  2. Make a copy of this file and/or rename it to .env
  3. Open the .env file and replace the placeholder values with your actual API keys:
OPENAI_API_KEY=api-key-here
ANTHROPIC_API_KEY=api-key-here
GOOGLE_AI_STUDIO_API_KEY=api-key-here
GROQ_API_KEY=api-key-here
OPENROUTER_API_KEY=api-key-here
DEEPSEEK_API_KEY=api-key-here
# Note: these are not used and not required:
LM_STUDIO_API_KEY=lmstudio
OLLAMA_API_KEY=ollama

Option B: Using Environment Variables

If you prefer setting environment variables directly:

For Linux/macOS:

export OPENAI_API_KEY=api-key-here
export ANTHROPIC_API_KEY=api-key-here
export GOOGLE_AI_STUDIO_API_KEY=api-key-here
export GROQ_API_KEY=api-key-here
export OPENROUTER_API_KEY=api-key-here
export DEEPSEEK_API_KEY=api-key-here
# note: these are not used and not required:
export LM_STUDIO_API_KEY=lmstudio
export OLLAMA_API_KEY=ollama

For Windows:

set OPENAI_API_KEY=api-key-here
set ANTHROPIC_API_KEY=api-key-here
set GOOGLE_AI_STUDIO_API_KEY=api-key-here
set GROQ_API_KEY=api-key-here
set OPENROUTER_API_KEY=api-key-here
set DEEPSEEK_API_KEY=api-key-here
REM note: these are not used and not required:
set LM_STUDIO_API_KEY=lmstudio
set OLLAMA_API_KEY=ollama
  • Each key is only used if/when you send requests to a model from that provider.

Run Ollama, if installed

Ollama with novelcrafter

for Mac do:

OLLAMA_ORIGINS=https://app.novelcrafter.com ollama serve

Run the local proxy server

keys2text_proxy

By default, the server runs on http://localhost:8000.

You can now direct any OpenAI-compatible client to this URL, well maybe,
as I have only used this app with novelcrafter (read more under Usage).


Usage

  • This app is a personal proxy server

    Keys2Text Proxy is intended to be run locally, i.e. on your computer, and not in the
    cloud/internet which may violate the AI providers API Terms of Service/Use (maybe).
    While there are advantages to running it locally; this app will probably only
    run on desktops and laptops.

  • novelcrafter

    • Keys2Text was written for and tested using novelcrafter
    • Keys2Text handles CORS so your web browser can perform behind the scenes
      local fetch-es to your Keys2Text Proxy server

Personally, for novelcrafter settings I use LM Studio in AI Connections and change the port.
When using API keys for all of the AI providers, the list of models is over 300, which is wow and unweildy.

So far it all works great together:
🏴‍☠️ and I'm my own middle-man
🐢 the proxy is not any slower or quirkier in responding than the usual chatters
👽 any issues are between me and the AI provider (429's and such) and handled via their support (if ever)
🔐 my API keys are directly used with AI providers (more secure), and no extra usage fees
💰 yes, you still pay for API usage (when not free) via your API keys directly to the AI provider

😱 No offense to the ever growing number of cloud providers offering a similar service.

  • Chat Logging

    Every conversation is automatically logged in a timestamped text file, e.g., chat_YYYY-MM-DD_HH-MM-SS.txt.
    This log is just a plain text file tracking your prompts as pairs of Me: prompt and AI: response,
    which may be helpful for writers and editing.

Roadmap

  • Additional Providers
    While we (who is we? 🤔) already support several popular AI services,
    the proxy is designed to be extended easily (well, if you can code python/fastapi/generator-yielding/streaming-nonstreaming-apis/wrangle-json-and-text, then sure).
    We plan to continually add new integrations.
    Do we, maybe, perhaps a new AI provider will emerge someday or overnight.🥸

  • Advanced Features
    Next in the hopper 🎡:
    🤖 NER (name entity recognition) for existing writing
    ⏪🤖 reverse-NER for story outlining
    📜 ✍🏽 both are kind of codex related (like in novelcrafter)

Contributing

We welcome contributions (we do?), bug 🐞 reports, and feature 🍿 requests.
Please open an issue or submit a pull request on GitHub.

License

This project is licensed under the MIT License.


With Keys2Text Proxy, you can unify your AI workflows under a single,
OpenAI-compatible interface—while retaining the freedom to choose any provider that best fits your needs.

If any of the above reads like AI writing, for example, all of that "we" speak, well, there is only "I" as in "me" ... 🙋🏽‍♂️ guilty.


Enjoy! ☮️