|
|
import os |
|
|
from fastapi import FastAPI, Request |
|
|
from fastapi.responses import PlainTextResponse |
|
|
from pydantic import BaseModel |
|
|
from openai import OpenAI |
|
|
import langdetect |
|
|
from twilio.twiml.messaging_response import MessagingResponse |
|
|
|
|
|
app = FastAPI() |
|
|
|
|
|
client = OpenAI( |
|
|
base_url="https://router.huggingface.co/v1", |
|
|
api_key=os.environ["HF_TOKEN"], |
|
|
) |
|
|
|
|
|
class Query(BaseModel): |
|
|
question: str |
|
|
|
|
|
@app.get("/") |
|
|
async def root(): |
|
|
return {"message": "API está rodando!"} |
|
|
|
|
|
@app.post("/ask") |
|
|
async def ask_model(query: Query): |
|
|
try: |
|
|
idioma = langdetect.detect(query.question) |
|
|
except: |
|
|
idioma = "pt" |
|
|
|
|
|
if idioma == "pt": |
|
|
system_prompt = ( |
|
|
"Você é um assistente que responde sempre em texto puro, SEM formatação Markdown, SEM asteriscos, SEM negrito, SEM itálico, SEM listas, SEM códigos.\n\n" |
|
|
"Quando responder em japonês, escreva sempre os caracteres japoneses reais (kanji, hiragana e katakana) e, logo após, entre parênteses, a transliteração romaji.\n\n" |
|
|
"Não substitua os caracteres japoneses por espaços, asteriscos ou quaisquer símbolos. Use sempre os caracteres reais e mantenha a resposta em texto simples.\n\n" |
|
|
"Exemplo de resposta correta:\n\n" |
|
|
"こんにちは (Konnichiwa) significa \"Olá\".\n\n" |
|
|
"Responda brevemente e de forma direta." |
|
|
) |
|
|
elif idioma == "en": |
|
|
system_prompt = ( |
|
|
"You are an assistant that always responds in plain text, WITHOUT Markdown formatting, WITHOUT asterisks, bold, italics, lists, or code.\n\n" |
|
|
"When responding in Japanese, always write the real Japanese characters (kanji, hiragana, and katakana) followed immediately by the romaji transliteration in parentheses.\n\n" |
|
|
"Do not replace Japanese characters with spaces, asterisks, or any symbols. Always use the real characters and keep the response in plain text.\n\n" |
|
|
"Example of correct response:\n\n" |
|
|
"こんにちは (Konnichiwa) means \"Hello\".\n\n" |
|
|
"Respond briefly and directly." |
|
|
) |
|
|
else: |
|
|
system_prompt = ( |
|
|
"Responda no mesmo idioma da pergunta, de forma direta e curta, sem formatação Markdown." |
|
|
) |
|
|
|
|
|
completion = client.chat.completions.create( |
|
|
model="meta-llama/Llama-3.1-8B-Instruct:fireworks-ai", |
|
|
messages=[ |
|
|
{"role": "system", "content": system_prompt}, |
|
|
{"role": "user", "content": query.question} |
|
|
], |
|
|
max_tokens=250 |
|
|
) |
|
|
answer = completion.choices[0].message.content.strip() |
|
|
|
|
|
return {"answer": answer} |
|
|
|
|
|
|
|
|
@app.post("/whatsapp") |
|
|
async def whatsapp_webhook(request: Request): |
|
|
form = await request.form() |
|
|
incoming_msg = form.get("Body") |
|
|
|
|
|
try: |
|
|
idioma = langdetect.detect(incoming_msg) |
|
|
except: |
|
|
idioma = "pt" |
|
|
|
|
|
if idioma == "pt": |
|
|
system_prompt = ( |
|
|
"Você é um assistente que responde sempre em texto puro, SEM formatação Markdown, SEM asteriscos, SEM negrito, SEM itálico, SEM listas, SEM códigos.\n\n" |
|
|
"Quando responder em japonês, escreva sempre os caracteres japoneses reais (kanji, hiragana e katakana) e, logo após, entre parênteses, a transliteração romaji.\n\n" |
|
|
"Não substitua os caracteres japoneses por espaços, asteriscos ou quaisquer símbolos. Use sempre os caracteres reais e mantenha a resposta em texto simples.\n\n" |
|
|
"Exemplo de resposta correta:\n\n" |
|
|
"こんにちは (Konnichiwa) significa \"Olá\".\n\n" |
|
|
"Responda brevemente e de forma direta." |
|
|
) |
|
|
elif idioma == "en": |
|
|
system_prompt = ( |
|
|
"You are an assistant that always responds in plain text, WITHOUT Markdown formatting, WITHOUT asterisks, bold, italics, lists, or code.\n\n" |
|
|
"When responding in Japanese, always write the real Japanese characters (kanji, hiragana, and katakana) followed immediately by the romaji transliteration in parentheses.\n\n" |
|
|
"Do not replace Japanese characters with spaces, asterisks, or any symbols. Always use the real characters and keep the response in plain text.\n\n" |
|
|
"Example of correct response:\n\n" |
|
|
"こんにちは (Konnichiwa) means \"Hello\".\n\n" |
|
|
"Respond briefly and directly." |
|
|
) |
|
|
else: |
|
|
system_prompt = ( |
|
|
"Responda no mesmo idioma da pergunta, de forma direta e curta, sem formatação Markdown." |
|
|
) |
|
|
|
|
|
completion = client.chat.completions.create( |
|
|
model="meta-llama/Llama-3.1-8B-Instruct:fireworks-ai", |
|
|
messages=[ |
|
|
{"role": "system", "content": system_prompt}, |
|
|
{"role": "user", "content": incoming_msg} |
|
|
], |
|
|
max_tokens=250 |
|
|
) |
|
|
answer = completion.choices[0].message.content.strip() |
|
|
|
|
|
resp = MessagingResponse() |
|
|
msg = resp.message() |
|
|
msg.body(answer) |
|
|
|
|
|
return PlainTextResponse(str(resp), media_type="application/xml") |
|
|
|