Update app.py
This commit is contained in:
@@ -46,6 +46,16 @@ if not TEST_MODE:
|
||||
print("Executando em modo de teste...")
|
||||
TEST_MODE = True
|
||||
|
||||
# --------------------------
|
||||
# Modelos suportados
|
||||
# --------------------------
|
||||
|
||||
SUPPORTED_MODELS = {
|
||||
"gpt5": "ocid1.generativeaimodel.oc1.us-chicago-1.amaaaaaask7dceyasebknceb4ekbiaiisjtu3fj5i7s4io3ignvg4ip2uyma",
|
||||
"grok3mini": "ocid1.generativeaimodel.oc1.us-chicago-1.amaaaaaask7dceyavwbgai5nlntsd5hngaileroifuoec5qxttmydhq7mykq",
|
||||
"llama4maverick": "ocid1.generativeaimodel.oc1.us-chicago-1.amaaaaaask7dceyayjawvuonfkw2ua4bob4rlnnlhs522pafbglivtwlfzta"
|
||||
}
|
||||
|
||||
# --------------------------
|
||||
# Session Store
|
||||
# --------------------------
|
||||
@@ -114,10 +124,13 @@ def session_controller(region, agent_endpoint_id, channel, cuid):
|
||||
# Inferência GenAI
|
||||
# --------------------------
|
||||
|
||||
def call_inference_model(region, compartment_id, model_id, prompt):
|
||||
def call_inference_model(region, compartment_id, model_id, prompt, **kwargs):
|
||||
if TEST_MODE:
|
||||
return {"response": {"text": f"Resposta simulada: {prompt}", "finish_reason": "stop"}}
|
||||
|
||||
if model_id not in SUPPORTED_MODELS.values():
|
||||
return {"error": "Modelo não implementado"}
|
||||
|
||||
try:
|
||||
endpoint = f"https://inference.generativeai.{region}.oci.oraclecloud.com"
|
||||
|
||||
@@ -134,12 +147,18 @@ def call_inference_model(region, compartment_id, model_id, prompt):
|
||||
chat_request = oci.generative_ai_inference.models.GenericChatRequest(
|
||||
api_format=oci.generative_ai_inference.models.BaseChatRequest.API_FORMAT_GENERIC,
|
||||
messages=[message],
|
||||
max_tokens=50000,
|
||||
temperature=1,
|
||||
top_p=1,
|
||||
top_k=0
|
||||
max_tokens=kwargs.get("max_tokens", 600),
|
||||
temperature=kwargs.get("temperature", 1),
|
||||
top_p=kwargs.get("top_p", 1),
|
||||
top_k=kwargs.get("top_k", 0),
|
||||
frequency_penalty=kwargs.get("frequency_penalty", 0),
|
||||
presence_penalty=kwargs.get("presence_penalty", 0)
|
||||
)
|
||||
|
||||
if model_id == SUPPORTED_MODELS["gpt5"]:
|
||||
chat_request.reasoning_effort = kwargs.get("reasoning_effort", "MEDIUM")
|
||||
chat_request.verbosity = kwargs.get("verbosity", "MEDIUM")
|
||||
|
||||
chat_detail = oci.generative_ai_inference.models.ChatDetails(
|
||||
serving_mode=oci.generative_ai_inference.models.OnDemandServingMode(model_id=model_id),
|
||||
chat_request=chat_request,
|
||||
@@ -149,171 +168,12 @@ def call_inference_model(region, compartment_id, model_id, prompt):
|
||||
chat_response = generative_ai_inference_client.chat(chat_detail)
|
||||
choice = chat_response.data.chat_response.choices[0]
|
||||
|
||||
return {"response": {
|
||||
"text": choice.message.content[0].text,
|
||||
"finish_reason": choice.finish_reason
|
||||
}}
|
||||
return {
|
||||
"response": {
|
||||
"text": choice.message.content[0].text,
|
||||
"finish_reason": choice.finish_reason
|
||||
}
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
return {"error": str(e)}
|
||||
|
||||
# --------------------------
|
||||
# Autenticação
|
||||
# --------------------------
|
||||
|
||||
def check_api_key():
|
||||
expected_key = os.environ.get("API_KEY")
|
||||
if not expected_key:
|
||||
print("AVISO: API_KEY não configurada.")
|
||||
return
|
||||
|
||||
auth_header = request.headers.get("Authorization", "")
|
||||
token = ""
|
||||
if auth_header.startswith("Bearer "):
|
||||
token = auth_header.split("Bearer ")[1].strip()
|
||||
else:
|
||||
token = request.headers.get("X-API-Key")
|
||||
|
||||
if token != expected_key:
|
||||
abort(401, description="API key inválida.")
|
||||
|
||||
@app.before_request
|
||||
def before_all_requests():
|
||||
check_api_key()
|
||||
|
||||
# --------------------------
|
||||
# Endpoints REST
|
||||
# --------------------------
|
||||
|
||||
@app.route("/", methods=["GET"])
|
||||
def test():
|
||||
return jsonify({"status": "ok"})
|
||||
|
||||
@app.route("/genai-agent/<region>/<agent_endpoint_id>/session", methods=["POST"])
|
||||
def manage_session(region, agent_endpoint_id):
|
||||
data = request.get_json()
|
||||
channel = data.get("channel")
|
||||
cuid = data.get("cuid")
|
||||
if not all([channel, cuid]):
|
||||
return jsonify({"error": "Parâmetros 'channel' e 'cuid' são obrigatórios"}), 400
|
||||
response_data = session_controller(region, agent_endpoint_id, channel, cuid)
|
||||
return jsonify(response_data)
|
||||
|
||||
@app.route("/genai-agent/<region>/<agent_endpoint_id>/<session_id>/chat", methods=["POST"])
|
||||
def agent_chat(region, agent_endpoint_id, session_id):
|
||||
data = request.get_json()
|
||||
user_message = data.get("userMessage")
|
||||
if not user_message:
|
||||
return jsonify({"error": "userMessage é obrigatório"}), 400
|
||||
|
||||
try:
|
||||
base_url = f"https://agent-runtime.generativeai.{region}.oci.oraclecloud.com/20240531"
|
||||
chat_url = f"{base_url}/agentEndpoints/{agent_endpoint_id}/actions/chat"
|
||||
session_obj = requests.Session()
|
||||
session_obj.auth = signer
|
||||
payload = {
|
||||
"userMessage": user_message,
|
||||
"shouldStream": False,
|
||||
"sessionId": session_id
|
||||
}
|
||||
response = session_obj.post(chat_url, json=payload)
|
||||
response.raise_for_status()
|
||||
return jsonify({"agentResponse": response.json()})
|
||||
|
||||
except Exception as e:
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
@app.route("/genai/<region>/<compartment_id>/<model_id>/inference", methods=["POST"])
|
||||
def inference(region, compartment_id, model_id):
|
||||
data = request.get_json()
|
||||
prompt = data.get("prompt")
|
||||
if not prompt:
|
||||
return jsonify({"error": "Campo 'prompt' é obrigatório."}), 400
|
||||
|
||||
response_data = call_inference_model(region, compartment_id, model_id, prompt)
|
||||
return jsonify(response_data)
|
||||
|
||||
@app.route("/genai/<region>/<compartment_id>/<model_id>/v1/chat/completions", methods=["POST"])
|
||||
def openai_compatible_chat(region, compartment_id, model_id):
|
||||
data = request.get_json()
|
||||
messages = data.get("messages", [])
|
||||
temperature = data.get("temperature", 1)
|
||||
top_p = data.get("top_p", 1)
|
||||
top_k = data.get("top_k", 0)
|
||||
max_tokens = data.get("max_tokens", 1000)
|
||||
|
||||
user_prompt = next((m["content"] for m in reversed(messages) if m["role"] == "user"), None)
|
||||
if not user_prompt:
|
||||
return jsonify({"error": "mensagem do usuário é obrigatória"}), 400
|
||||
|
||||
response = call_inference_model(region, compartment_id, model_id, user_prompt)
|
||||
if "error" in response:
|
||||
return jsonify({"error": response["error"]}), 500
|
||||
|
||||
result_text = response["response"]["text"]
|
||||
finish_reason = response["response"].get("finish_reason", "stop")
|
||||
|
||||
return jsonify({
|
||||
"id": f"chatcmpl-{uuid.uuid4().hex[:12]}",
|
||||
"object": "chat.completion",
|
||||
"created": int(time.time()),
|
||||
"model": model_id,
|
||||
"choices": [
|
||||
{
|
||||
"index": 0,
|
||||
"message": {"role": "assistant", "content": result_text},
|
||||
"finish_reason": finish_reason
|
||||
}
|
||||
]
|
||||
})
|
||||
|
||||
@app.route("/genai/<region>/<compartment_id>/<model_id>/v1/completions", methods=["POST"])
|
||||
def openai_compatible_completion(region, compartment_id, model_id):
|
||||
data = request.get_json()
|
||||
prompt = data.get("prompt")
|
||||
temperature = data.get("temperature", 1)
|
||||
top_p = data.get("top_p", 1)
|
||||
top_k = data.get("top_k", 0)
|
||||
max_tokens = data.get("max_tokens", 1000)
|
||||
stop = data.get("stop")
|
||||
|
||||
if not prompt:
|
||||
return jsonify({"error": "Campo 'prompt' é obrigatório."}), 400
|
||||
|
||||
response = call_inference_model(region, compartment_id, model_id, prompt)
|
||||
if "error" in response:
|
||||
return jsonify({"error": response["error"]}), 500
|
||||
|
||||
result_text = response["response"]["text"]
|
||||
finish_reason = response["response"].get("finish_reason", "stop")
|
||||
|
||||
if stop:
|
||||
if isinstance(stop, list):
|
||||
for s in stop:
|
||||
if s in result_text:
|
||||
result_text = result_text.split(s)[0]
|
||||
break
|
||||
elif isinstance(stop, str) and stop in result_text:
|
||||
result_text = result_text.split(stop)[0]
|
||||
|
||||
return jsonify({
|
||||
"id": f"cmpl-{uuid.uuid4().hex[:12]}",
|
||||
"object": "text_completion",
|
||||
"created": int(time.time()),
|
||||
"model": model_id,
|
||||
"choices": [
|
||||
{
|
||||
"index": 0,
|
||||
"text": result_text,
|
||||
"logprobs": None,
|
||||
"finish_reason": finish_reason
|
||||
}
|
||||
]
|
||||
})
|
||||
|
||||
# --------------------------
|
||||
# Inicialização
|
||||
# --------------------------
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host='0.0.0.0', port=8000)
|
||||
|
||||
Reference in New Issue
Block a user