最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - How to add API key for deepseek installed on my server for others to use this service? - Stack Overflow

programmeradmin3浏览0评论

I have installed deepseek-r1:7b and 1.5b on my CentOS7.9 server using ollama, and I can use it on command and win11 client like Cherry Studio using API url like http://my-ip:8088 when using Nginx to redirect the origin URL http://127.0.0.1:11434/.

The deepseek service now installed on my server only has a API URL and can work well in client like Cherry Studio using the API url.

But how can I add a simple API key when providing this service to my friends for security issues?

I want to provide deepseek service with API keys (already in txt files below) which does NOT suport now, for users to use in client like CherryStudio.

  • API url: http://my-ip:8088
  • API key: key-xxyy11 # NOT support now

What I have tried: I wrote the API key, expiration date, user IP to a txt file, as only less than 10 people will use this service.

$ head Api_key.txt
key-xxyy11,2025/2/24,10.12.100.200
key-xxyy22,2025/3/24,10.12.200.201

The code below ignores IP info when checking API keys, which I think will be easy to implement later.

(1) When runing the code below on server, I can get the model names in cherrybox, which means the function getModels() works.

(2) But when I talk to the LLM after using the Flask service with a wrong API key, it show error message in Cherry Studio, which means the validation part works.

(3) But it doesn't answer anything when using the right API key in CherryStudio, even no error message.

That is to say, the chat() function below can check the API key, but when the API key is valid, it can NOT continue the chat with deepseek.

from flask import Flask, request, jsonify, Response, stream_with_context
import datetime
import csv
import requests

app = Flask(__name__)

# load API key info
def load_api_keys():
    api_keys = {}
    with open('/path/to/Api_key.txt', mode='r') as file:
        reader = csv.reader(file)
        for row in reader:
            api_keys[row[0]] = row[1]
    return api_keys



# get model name list: OK
@app.route('/deepseek/models', methods=['GET'])
def getModels():
    url = "http://127.0.0.1:11434/v1/models"
    headers = {
        #"Authorization": "Bearer sk-test2",
        "Content-Type": "application/json"
    }
    response = requests.get(url, headers=headers)

    if response.status_code == 200:
        json_response = response.json()
    else:
        json_response={"Error":"get model list error from deepseek"}

    return jsonify(json_response), response.status_code



# chat with deepseek, if API key is valid. Not work
@app.route('/deepseek/chat/completions', methods=['POST'])
def chat():
    print("[0]>> begin")
    api_keys = load_api_keys()
    api_key = request.headers.get('Authorization').split(" ")[1]
    if api_key in api_keys:
        expiry_date = datetime.datetime.strptime(api_keys[api_key], "%Y/%m/%d")
        if expiry_date >= datetime.datetime.now():
            # how to call DeepSeek service as if calling the service directly?
            #prompt = request.json.get('prompt')
            prompt = request.json.get('messages')
            print("[1]>> prompt=", prompt)

            base_url = "http://127.0.0.1:11434/v1/chat/completions"
            headers = {
                "Authorization": "Bearer sk-test2",
                "Content-Type": "application/json"
            }
            data = {
                "model": "deepseek-r1:1.5b",
                "messages":  [
                    {
                        "role": "user",
                        "content": prompt #"Hello"
                    }
                ],
                "stream": True
            }
            # Send the request to DeepSeek API
            response = requests.post(base_url, headers=headers, json=data)

            if response.status_code == 200:
                    return jsonify(response.json()), 200
            else:
                error_message = response.json() if response.content else {'error': 'Unknown server error'}
                return jsonify(error_message), response.status_code
        else:
            return jsonify({"error": "API key has expired."}), 403
    else:
        return jsonify({"error": "Invalid API key."}), 403


if __name__ == '__main__':
    print("init: 01")
    app.run(host='0.0.0.0', port=8089, debug=True)
发布评论

评论列表(0)

  1. 暂无评论