File size: 2,837 Bytes
c7b3e89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import requests
from flask import Flask, request, jsonify
import json

app = Flask(__name__)

# Define the endpoint to interact with the Amigo API
AMIGO_API_URL = "https://api.amigochat.io/v1/chat/completions"

headers = {
    'accept': '*/*',
    'accept-language': 'en-US,en;q=0.9,ru;q=0.8',
    'content-type': 'application/json',
    'origin': 'https://amigochat.io',
    'priority': 'u=1, i',
    'referer': 'https://amigochat.io/',
    'sec-ch-ua': '"Google Chrome";v="131", "Chromium";v="131", "Not_A Brand";v="24"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-site',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
    'x-device-language': 'en-US',
    'x-device-platform': 'web',
    'x-device-uuid': 'bce7b660-d84c-463f-b975-fa8979fd2e64',  # Replace with your actual device UUID if needed
    'x-device-version': '1.0.41',
}

@app.route('/v1/chat/completions', methods=['POST'])
def chat_completions():
    data = request.get_json()

    # Prepare the payload for the Amigo API based on user input
    payload = {
        "messages": data['messages'],
        "model": "gpt-4o-mini",
        "personaId": "amigo",
        "frequency_penalty": 0,
        "max_tokens": 4000,
        "presence_penalty": 0,
        "stream": True,
        "temperature": 0.5,
        "top_p": 0.95
    }

    # Send the request to Amigo API
    response = requests.post(AMIGO_API_URL, headers=headers, json=payload)

    # If the response is successful, return the response in OpenAI's format
    if response.status_code == 200:
        amigo_response = response.json()

        openai_response = {
            "id": amigo_response.get('id', 'unknown'),
            "object": "chat.completion",
            "created": amigo_response.get('created', 0),
            "model": amigo_response.get('model', 'gpt-4o-mini'),
            "choices": [
                {
                    "message": {
                        "role": amigo_response.get('messages', [{}])[0].get('role', 'user'),
                        "content": amigo_response.get('messages', [{}])[0].get('content', '')
                    },
                    "finish_reason": amigo_response.get('finish_reason', 'stop'),
                    "index": 0
                }
            ],
            "usage": amigo_response.get('usage', {})
        }

        return jsonify(openai_response)

    # If something goes wrong with the Amigo API request
    return jsonify({
        'error': {
            'message': 'Failed to fetch from Amigo API',
            'type': 'internal_error'
        }
    }), 500

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=5000)