Tst / app.py
Niansuh's picture
Create app.py
c7b3e89 verified
raw
history blame
2.84 kB
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)