File size: 10,032 Bytes
a73e772
 
 
 
664d175
c61e41b
0065184
4b4c28d
 
0065184
 
 
a73e772
 
 
 
 
 
 
 
 
0065184
 
 
 
4b4c28d
 
 
 
 
 
 
 
 
 
 
 
a73e772
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
664d175
a73e772
 
 
 
 
 
 
 
 
 
 
664d175
a73e772
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe46a24
 
 
 
 
 
a73e772
 
fe46a24
a73e772
 
 
 
 
 
 
 
 
 
664d175
 
 
 
 
 
 
a73e772
 
 
 
 
 
664d175
 
 
 
 
 
 
a73e772
 
4b4c28d
 
 
 
 
 
 
 
 
 
 
 
0065184
 
 
 
4b4c28d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a73e772
c61e41b
 
 
 
 
 
 
 
a73e772
 
0065184
 
a73e772
 
 
 
 
0065184
4b4c28d
0065184
 
4b4c28d
 
a73e772
 
 
 
0065184
 
664d175
 
 
4b4c28d
a73e772
 
4b4c28d
a73e772
 
 
0065184
a73e772
 
0065184
a73e772
664d175
fe46a24
 
 
0065184
fe46a24
 
 
664d175
 
fe46a24
664d175
a73e772
 
 
 
 
4b4c28d
a73e772
 
c61e41b
 
 
 
4b4c28d
c61e41b
 
a73e772
664d175
0065184
4b4c28d
0065184
 
4b4c28d
 
664d175
 
 
 
0065184
 
664d175
 
 
4b4c28d
664d175
 
4b4c28d
664d175
 
 
0065184
664d175
 
0065184
664d175
 
0065184
664d175
fe46a24
 
 
 
 
 
 
664d175
 
fe46a24
664d175
 
 
 
 
 
4b4c28d
664d175
 
c61e41b
 
 
 
4b4c28d
c61e41b
 
664d175
a73e772
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
from functools import partial
from fastapi.responses import JSONResponse
from fastapi import Security, Depends, Request
from fastapi.security.api_key import APIKeyHeader, APIKey
from fastapi.middleware.cors import CORSMiddleware
from slowapi import Limiter, _rate_limit_exceeded_handler
from slowapi.util import get_remote_address
from slowapi.errors import RateLimitExceeded

from langchain_core.messages import HumanMessage, AIMessage
from langgraph.checkpoint.memory import MemorySaver
from langgraph.graph import START, MessagesState, StateGraph

import os
from dotenv import load_dotenv
load_dotenv()

# Rate Limiter configuration
limiter = Limiter(key_func=get_remote_address)

# API Key configuration
API_KEY_NAME = "X-API-Key"
API_KEY = os.getenv("API_KEY")
api_key_header = APIKeyHeader(name=API_KEY_NAME, auto_error=False)

async def get_api_key(api_key_header: str = Security(api_key_header)):
    if api_key_header == API_KEY:
        return api_key_header
    raise HTTPException(
        status_code=403,
        detail="Could not validate API KEY"
    )

# Initialize the model and tokenizer
print("Loading model and tokenizer...")
device = "cuda" if torch.cuda.is_available() else "cpu"
model_name = "HuggingFaceTB/SmolLM2-1.7B-Instruct"

try:
    # Load the model in BF16 format for better performance and lower memory usage
    tokenizer = AutoTokenizer.from_pretrained(model_name)
    
    if device == "cuda":
        print("Using GPU for the model...")
        model = AutoModelForCausalLM.from_pretrained(
            model_name,
            torch_dtype=torch.bfloat16,
            device_map="auto",
            low_cpu_mem_usage=True
        )
    else:
        print("Using CPU for the model...")
        model = AutoModelForCausalLM.from_pretrained(
            model_name,
            device_map={"": device},
            torch_dtype=torch.float32
        )

    print(f"Model loaded successfully on: {device}")
except Exception as e:
    print(f"Error loading the model: {str(e)}")
    raise

# Define the function that calls the model
def call_model(state: MessagesState, system_prompt: str):
    """
    Call the model with the given messages

    Args:
        state: MessagesState

    Returns:
        dict: A dictionary containing the generated text and the thread ID
    """
    # Convert LangChain messages to chat format
    messages = [
        {"role": "system", "content": system_prompt}
    ]
    
    for msg in state["messages"]:
        if isinstance(msg, HumanMessage):
            messages.append({"role": "user", "content": msg.content})
        elif isinstance(msg, AIMessage):
            messages.append({"role": "assistant", "content": msg.content})
    
    # Prepare the input using the chat template
    input_text = tokenizer.apply_chat_template(messages, tokenize=False)
    inputs = tokenizer.encode(input_text, return_tensors="pt").to(device)
    
    # Generate response
    outputs = model.generate(
        inputs,
        max_new_tokens=512,  # Increase the number of tokens for longer responses
        temperature=0.7,
        top_p=0.9,
        do_sample=True,
        pad_token_id=tokenizer.eos_token_id
    )
    
    # Get just the new tokens (excluding the input prompt tokens)
    input_length = inputs.shape[1]
    generated_tokens = outputs[0][input_length:]
    
    # Decode only the new tokens to get just the assistant's response
    assistant_response = tokenizer.decode(generated_tokens, skip_special_tokens=True).strip()
    
    # Convert the response to LangChain format
    ai_message = AIMessage(content=assistant_response)
    return {"messages": state["messages"] + [ai_message]}

# Define the graph
workflow = StateGraph(state_schema=MessagesState)

# Define the node in the graph
workflow.add_edge(START, "model")

# Add memory
memory = MemorySaver()

# Define the default system prompt
DEFAULT_SYSTEM_PROMPT = "You are a friendly Chatbot. Always reply in the language in which the user is writing to you."

# Use partial to create a version of the function with the default system prompt
workflow.add_node("model", partial(call_model, system_prompt=DEFAULT_SYSTEM_PROMPT))

graph_app = workflow.compile(checkpointer=memory)

# Define the data model for the request
class QueryRequest(BaseModel):
    query: str
    thread_id: str = "default"
    system_prompt: str = DEFAULT_SYSTEM_PROMPT

# Define the model for summary requests
class SummaryRequest(BaseModel):
    text: str
    thread_id: str = "default"
    max_length: int = 200

# Create the FastAPI application
app = FastAPI(
    title="LangChain FastAPI",
    description="API to generate text using LangChain and LangGraph - Máximo Fernández Núñez IriusRisk test challenge",
    version="1.0.0",
    openapi_tags=[
        {
            "name": "Authentication",
            "description": "Endpoints require API Key authentication via X-API-Key header"
        }
    ]
)

# Configure the rate limiter in the application
app.state.limiter = limiter
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)

# Configure the security scheme in the OpenAPI documentation
app.openapi_tags = [
    {"name": "Authentication", "description": "Protected endpoints that require API Key"}
]

# Import and configure CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Configure the security scheme
app.openapi_components = {
    "securitySchemes": {
        "api_key": {
            "type": "apiKey",
            "name": API_KEY_NAME,
            "in": "header",
            "description": "Enter your API key"
        }
    }
}

app.openapi_security = [{"api_key": []}]

# Add general exception handler
@app.exception_handler(Exception)
async def general_exception_handler(request, exc):
    return JSONResponse(
        status_code=500,
        content={"error": f"Error interno: {str(exc)}", "type": type(exc).__name__}
    )

# Welcome endpoint
@app.get("/")
@limiter.limit("10/minute")
async def api_home(request: Request):
    """Welcome endpoint"""
    return {"detail": "Welcome to Máximo Fernández Núñez IriusRisk test challenge"}

# Generate endpoint
@app.post("/generate")
@limiter.limit("5/minute")
async def generate(
    request: Request,
    query_request: QueryRequest,
    api_key: APIKey = Depends(get_api_key)
):
    """
    Endpoint to generate text using the language model
    
    Args:
        request: Request - FastAPI request object for rate limiting
        query_request: QueryRequest
            query: str
            thread_id: str = "default"
            system_prompt: str = DEFAULT_SYSTEM_PROMPT
        api_key: APIKey - API key for authentication

    Returns:
        dict: A dictionary containing the generated text
    """
    try:
        # Configure the thread ID
        config = {"configurable": {"thread_id": query_request.thread_id}}
        
        # Create the input message
        input_messages = [HumanMessage(content=query_request.query)]
        
        # Invoke the graph with custom system prompt
        # Combine config parameters into a single dictionary
        combined_config = {
            **config,
            "model": {"system_prompt": query_request.system_prompt}
        }
        
        # Invoke the graph with proper argument count
        output = graph_app.invoke(
            {"messages": input_messages}, 
            combined_config
        )
        
        # Get the model response
        response = output["messages"][-1].content
        
        return {
            "generated_text": response
        }
    except Exception as e:
        return JSONResponse(
            status_code=500,
            content={
                "error": f"Error generando texto: {str(e)}", 
                "type": type(e).__name__
            }
        )

@app.post("/summarize")
@limiter.limit("5/minute")
async def summarize(
    request: Request,
    summary_request: SummaryRequest,
    api_key: APIKey = Depends(get_api_key)
):
    """
    Endpoint to generate a summary using the language model
    
    Args:
        request: Request - FastAPI request object for rate limiting
        summary_request: SummaryRequest
            text: str - The text to summarize
            thread_id: str = "default"
            max_length: int = 200 - Maximum summary length
        api_key: APIKey - API key for authentication

    Returns:
        dict: A dictionary containing the summary
    """
    try:
        # Configure the thread ID
        config = {"configurable": {"thread_id": summary_request.thread_id}}
        
        # Create a specific system prompt for summarization
        summary_system_prompt = f"Make a summary of the following text in no more than {summary_request.max_length} words. Keep the most important information and eliminate unnecessary details."
        
        # Create the input message
        input_messages = [HumanMessage(content=summary_request.text)]
        
        # Combine config parameters into a single dictionary
        combined_config = {
            **config,
            "model": {"system_prompt": summary_system_prompt}
        }
        
        # Invoke the graph with proper argument count
        output = graph_app.invoke(
            {"messages": input_messages}, 
            combined_config
        )
        
        # Get the model response
        response = output["messages"][-1].content
        
        return {
            "summary": response
        }
    except Exception as e:
        return JSONResponse(
            status_code=500,
            content={
                "error": f"Error generando resumen: {str(e)}", 
                "type": type(e).__name__
            }
        )

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860)