End to end LLMOps Pipeline - Part 2 - FastAPI
Prashant Lakhera
Lead System Engineer @ Salesforce | Ex-Redhat, GenAI, Author of 3 books, Blogger, YouTuber,kubestronaut, MLOps, AWS Bedrock, Hugging Face
Welcome to Day 2. Yesterday, we explored Hugging Face, a leading platform in Natural Language Processing (NLP), which simplifies the process of building and deploying state-of-the-art machine learning models. Today, we will build on the code we wrote yesterday and integrate it with FastAPI.
What is FastAPI?
FastAPI is designed to create robust, fast, and secure APIs with minimal effort. It leverages Python type hints to enable features like automatic generation of interactive API documentation, which is a significant advantage for both development and user experience. Whether you’re a beginner or an experienced developer, FastAPI offers tools that streamline API development, from easy parameter validation to detailed error messages.
Key Features:
Getting Started with FastAPI
To begin using FastAPI, you need to set up a few prerequisites:
Prerequisites:
Installation
You can install the necessary libraries using pip:
pip install fastapi uvicorn transformers pydantic
Note: While manual installation via pip is demonstrated here, it's recommended to create a requirements.txt file and install these dependencies via GitHub Action for a more streamlined and reproducible setup.
Importing Necessary Libraries
Let's start by importing the required libraries:
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from transformers import pipeline import uvicorn
Creating the FastAPI Application
The core of your API is initialized as follows:
app = FastAPI()
领英推荐
Initializing the Question-Answering Pipeline
We use the Hugging Face Transformers library to initialize a question-answering model:
qa_pipeline = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
This pipeline will handle the question-answering task by leveraging the distilbert-base-uncased-distilled-squad model from Hugging Face's model hub.
Defining Data Models
Pydantic models are defined to structure the request and response data:
class ChatRequest(BaseModel):
question: str
context: str
class ChatResponse(BaseModel):
answer: str
Creating the /chat Endpoint
Here’s how to define an endpoint for the chat functionality:
@app.post("/chat", response_model=ChatResponse)
async def chat(request: ChatRequest):
try:
result = qa_pipeline(question=request.question, context=request.context)
return ChatResponse(answer=result['answer'])
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
Running the Server
To start the FastAPI server, use the following script:
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)
Conclusion
FastAPI is an excellent choice for developers looking to build fast, modern, and efficient APIs with Python. Its native support for asynchronous programming, automatic documentation generation, and ease of use make it a standout framework. Whether you're building a small application or a large-scale project, FastAPI provides the tools and features needed to create a robust API effortlessly.
?? If you’d like to learn more about this topic, please check out my book. Building an LLMOps Pipeline Using Hugging Face https://pratimuniyal.gumroad.com/l/BuildinganLLMOpsPipelineUsingHuggingFace
Cyber Security Specialist
1 个月Congratulations Prashant bhai