first and real commit
This commit is contained in:
BIN
api/__pycache__/api.cpython-314.pyc
Normal file
BIN
api/__pycache__/api.cpython-314.pyc
Normal file
Binary file not shown.
BIN
api/__pycache__/router.cpython-314.pyc
Normal file
BIN
api/__pycache__/router.cpython-314.pyc
Normal file
Binary file not shown.
BIN
api/__pycache__/routes.cpython-314.pyc
Normal file
BIN
api/__pycache__/routes.cpython-314.pyc
Normal file
Binary file not shown.
17
api/api.py
Normal file
17
api/api.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from api.routes.root import router as rootRouter
|
||||
from api.routes.ai import router as aiRouter
|
||||
|
||||
|
||||
application = FastAPI()
|
||||
|
||||
application.add_middleware(CORSMiddleware,
|
||||
allow_origins=['*'],
|
||||
allow_credentials=True,
|
||||
allow_headers=['*'],
|
||||
allow_methods=['GET','POST'])
|
||||
|
||||
|
||||
application.include_router(router=rootRouter, prefix='')
|
||||
application.include_router(router=aiRouter, prefix='/ai')
|
||||
BIN
api/routes/__pycache__/ai.cpython-314.pyc
Normal file
BIN
api/routes/__pycache__/ai.cpython-314.pyc
Normal file
Binary file not shown.
BIN
api/routes/__pycache__/root.cpython-314.pyc
Normal file
BIN
api/routes/__pycache__/root.cpython-314.pyc
Normal file
Binary file not shown.
89
api/routes/ai.py
Normal file
89
api/routes/ai.py
Normal file
@@ -0,0 +1,89 @@
|
||||
import anthropic, json
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from fastapi.responses import StreamingResponse
|
||||
from dotenv import load_dotenv
|
||||
from os import getenv
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
load_dotenv(".env_file")
|
||||
client = anthropic.Anthropic(api_key=getenv('CLAUDE_KEY'))
|
||||
|
||||
with open('/home/rodo/Documents/py/LittleAPI/list.json', 'r') as f:
|
||||
config = json.load(f)
|
||||
pdf_file_ids = config.get("files", [])
|
||||
|
||||
@router.post("/using_docs", tags=["AI"])
|
||||
async def send_message_using_docs(message:str):
|
||||
"""
|
||||
Envía un mensaje usando documentos PDF almacenados.
|
||||
|
||||
- **message**: El mensaje de texto a procesar
|
||||
- **Retorna**: Stream de texto con la respuesta de Claude
|
||||
"""
|
||||
try:
|
||||
content = [
|
||||
{
|
||||
"type": "text",
|
||||
"text": message
|
||||
}
|
||||
]
|
||||
|
||||
for file_id in pdf_file_ids:
|
||||
content.append(
|
||||
{
|
||||
"type": "document",
|
||||
"source" : {
|
||||
"type" : "file",
|
||||
"file_id": file_id
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
def event_generator():
|
||||
with client.beta.messages.stream(
|
||||
model="claude-haiku-4-5",
|
||||
max_tokens=2048,
|
||||
messages=[{
|
||||
"role": "user",
|
||||
"content": content
|
||||
}],
|
||||
betas=["files-api-2025-04-14"],
|
||||
) as stream:
|
||||
for text in stream.text_stream:
|
||||
yield text
|
||||
return StreamingResponse(event_generator(), media_type="text/plain")
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
@router.post("/", tags=["AI"])
|
||||
async def send_message(message:str):
|
||||
"""
|
||||
Envía un mensaje normal a Claude.
|
||||
|
||||
- **message**: El mensaje de texto a procesar
|
||||
- **Retorna**: Stream de texto con la respuesta de Claude
|
||||
"""
|
||||
try:
|
||||
content = [
|
||||
{
|
||||
"type": "text",
|
||||
"text": message
|
||||
}
|
||||
]
|
||||
|
||||
def event_generator():
|
||||
with client.beta.messages.stream(
|
||||
model="claude-haiku-4-5",
|
||||
max_tokens=512,
|
||||
messages=[{
|
||||
"role": "user",
|
||||
"content": content
|
||||
}],
|
||||
) as stream:
|
||||
for text in stream.text_stream:
|
||||
yield text
|
||||
|
||||
return StreamingResponse(event_generator(), media_type="text/plain")
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
9
api/routes/root.py
Normal file
9
api/routes/root.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.get('/', tags=["root"])
|
||||
def root():
|
||||
return {
|
||||
"status": "running"
|
||||
}
|
||||
Reference in New Issue
Block a user