Characters_API / main.py
Yuuki0125's picture
CA.AI API
ba292df
raw
history blame
2.13 kB
from typing import List
from fastapi import FastAPI, HTTPException, Response, Query, Request
from typing import Optional
from pydantic import BaseModel
from characterai import PyCAI
app = FastAPI()
@app.get("/")
def Another():
return {"Another": "Apa Lu Liat-liat?"}
@app.get("/AnotherAPI/{character}/chat/{api_key}/{message}")
async def chat_endpoint(character: str, api_key: str, message: str):
try:
if character == '2B':
client = PyCAI(api_key)
chat = client.chat.get_chat('csTC3hw0Fnj1Whnl0uV1Nb3_oYIillMQtdBH5NEl0Gs')
elif character == 'Narator':
client = PyCAI(api_key)
chat = client.chat.get_chat('oRrOSTDibssHQwoKEfNtwBwgBEFDr1aKfVPXjY1d8nA')
else:
return {"Another": "API Lu Mana?."}
participants = chat.get('participants', [])
if not participants:
return {"error": "Error"}
tgt = participants[1]['user']['username']
data = client.chat.send_message(chat['external_id'], tgt, message)
text = data['replies'][0]['text']
return {character.capitalize(): f"{text}"}
except Exception as e:
return {"error": f"Error: {e}"}
@app.get("/AnotherAPI/{character}/new_chat/{api_key}/{prompt}")
async def new_chat_endpoint(character: str, api_key: str, prompt: str):
try:
if character == '2B':
client = PyCAI(api_key)
chat = client.chat.new_chat('csTC3hw0Fnj1Whnl0uV1Nb3_oYIillMQtdBH5NEl0Gs')
elif character == 'Narator':
client = PyCAI(api_key)
chat = client.chat.new_chat('oRrOSTDibssHQwoKEfNtwBwgBEFDr1aKfVPXjY1d8nA')
else:
return {"Another": "API Lu Mana?."}
participants = chat.get('participants', [])
if not participants:
return {"error": "Error"}
tgt = participants[1]['user']['username']
data = client.chat.send_message(chat['external_id'], tgt, prompt)
text = data['replies'][0]['text']
return {character.capitalize(): f"{text}"}
except Exception as e:
return {"error": f"Error: {e}"}