Service to bypass OpenAI API blocking in Russia or your first API

Hi all! Today I will tell you how I made a service to bypass OpenAI blocking in Russia by OpenAI (not even Roskomnadzor) using FastAPI And Docker. The motivation is simple. I have a server in the Russian Federation on which services run and I want to connect OpenAI to them, but they do not provide access, recognizing IP from Russia. I thought that I would write a server to forward the request. If you are wondering how to package work with AI in a beautiful and simple shell in a few steps, then get comfortable. We will dive into this exciting process together!

What are we even talking about?

At first I thought it would be better to make a relay of HTTP requests, but it turned out that everything is much more complicated there and OpenAI uses the Cookie system embedded in requests and the server in a different time zone will not work, so I changed the strategy:

The idea is simple: I wanted to create a web service that can take messages and, using the OpenAI API, produce meaningful and even funny responses. However, instead of directly accessing the OpenAI API through scripts, I decided to do something more elegant – an API for an API! And for this I chose FastAPIwhich promises high performance, ease of use, and asynchrony out of the box.

And so that all this can be easily launched anywhere (whether locally or on a server), I wrapped the project in Docker. Why not? After all, it’s a pleasure to work with Docker – once you’ve raised the container, you can forget about any problems with the environment.

Well, let's begin!

FastAPI: how can you not love this framework?

If you still haven’t heard about FastAPI, then go read it urgently! This framework is a real boon for those who are tired of the bulkiness of old frameworks and want something simple but powerful.

My goal was simple: to make POST– a request that takes a list of messages (for example, phrases from the user and instructions from the system) and returns a response generated by the OpenAI model (whether GPT-4 or GPT-3.5). And, of course, I wanted to add authentication – I didn’t want anyone to be able to access the service.

The service code is a few lines that show the full power of FastAPI:

from fastapi import FastAPI, HTTPException, Request, Depends
from pydantic import BaseModel
import openai
import os
from dotenv import load_dotenv

# Загружаем переменные окружения из .env файла
load_dotenv()

OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
AUTH_TOKEN = os.getenv("AUTH_TOKEN")

app = FastAPI()

class OpenAIRequest(BaseModel):
    model_name: str
    messages: list[dict[str, str]]

def verify_token(request: Request):
    token = request.headers.get("Authorization")
    if token != AUTH_TOKEN:
        raise HTTPException(status_code=403, detail="Unauthorized")

@app.post("/api/open_ai_request")
async def open_ai_request(openai_request: OpenAIRequest, token: None = Depends(verify_token)):
    openai.api_key = OPENAI_API_KEY
    response = openai.ChatCompletion.create(
        model=openai_request.model_name,
        messages=openai_request.messages
    )
    return {"response": response.choices[0].message['content']}

What's going on here?

  • FastAPI provides a convenient interface for creating HTTP methods.

  • We use asynchronous functions for maximum performance.

  • Added basic token verification via header Authorizationso that no one can use your service without permission.

  • The most important thing is that requests to OpenAI are made using an asynchronous API call so that everything works as it should!

Docker: so that there is no “works on my computer”

Now let's wrap our service in Docker. This will allow you to run it in any environment without having to worry about installing dependencies.

Our Dockerfile is very simple:

FROM python:3.10-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

What's going on here?

  • We take a lightweight Python 3.10 image.

  • We install all the necessary dependencies (FastAPI, OpenAI, etc.).

  • Copy the application sources.

  • And we launch the service via Uvicornwhich is an excellent server for asynchronous applications.

But that's not all! For convenience we will add docker-composeso that everything is raised with one command:

version: '3.8'

services:
  fastapi-openai-service:
    build: .
    env_file:
      - .env
    ports:
      - "8000:8000"
    restart: unless-stopped

Now, to start the service, it’s quite simple:

docker-compose up --build

And in a few seconds you have a ready-made service for communicating with AI, which runs on any server or even locally.

Let's check the work

Once you have started the container, your service will be available at http://localhost:8000. You can send a POST request with an authorization header and a request body, for example with curl:

curl -X POST "http://localhost:8000/api/open_ai_request" \
-H "Authorization: your_secret_auth_token_here" \
-H "Content-Type: application/json" \
-d '{
  "model_name": "gpt-4",
  "messages": [
    {"role": "system", "content": "Ты умный ассистент."},
    {"role": "user", "content": "Расскажи шутку."}
  ]
}'

And you will receive a response something like:

{  "response": "Почему программисты не любят ходить в лес? Потому что там слишком много багов!"
}

Not bad, right?

What can be improved?

Like any project, this service can be modified and expanded to increase its functionality and reliability. Here are some ideas for improvements:

  • Verifying a token not by a simple comparison, but by comparing hashes. Token hash or constant rate comparison.

  • At the moment we have a simple token verification through the header Authorization. You can improve security by adding an authorization system based on JWT or OAuth 2.0, and also differentiate access rights for different types of users.

  • It is important to add automated tests to ensure the stability of the service as it is further developed. Can be used pytest for writing unit tests, as well as load testing tools such as Locust or k6to check performance.

Let's sum it up

We have created a simple but functional service that can be deployed on your VPS after which it will send requests to OpenAI and return responses. At the same time, the entire project is easily scalable and ready for deployment thanks to Docker and FastAPI.

What's good about this approach:

  • Ease of Deployment: Docker helps you avoid problems with dependencies and environments.

  • Scalability: FastAPI is a fast and asynchronous framework that allows you to process many requests without losing performance.

  • Safety: Simple token verification allows you to restrict access to your service.

Now you know how you can make an API to work with OpenAI in just a couple of hours. Forward to new achievements, and let AI help you in all your endeavors!

I look forward to your comments and suggestions for improvement!

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *