0% found this document useful (0 votes)
108 views11 pages

Build A Simple Chatbot With Fastapi 1733585637

Chat it
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views11 pages

Build A Simple Chatbot With Fastapi 1733585637

Chat it
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

BUILD

SIMPLE
CHATBOT
USING
FASTAPI
linkedin.com/in/yasaman-r-530256138/
STEP 1 : CREATE PROJECT
STRUCTURE SAME AS
FOLLOWING
STEP 2 : INSTALL FASTAPI
AND UVICORN

pip install fastapi uvicorn


STEP 3 : CREATE FASTAPI APP
In the app/main.py file, you'll initialize the FastAPI app
and include the chatbot API:

from fastapi import FastAPI


from app.api.v1.chatbot import router as
chatbot_router

app = FastAPI()

# Include the chatbot API


app.include_router(chatbot_router, prefix="/
api/v1/chatbot", tags=["chatbot"])
STEP 4 : CREATE MODEL FOR
CHAT REQUEST AND RESPONSE
before defining api routes, we need to define structures of
user request and response in /models/chatbot.py

from pydantic import BaseModel

# Model for the incoming request to the


chatbot
class ChatRequest(BaseModel):
message: str # The message from the user

# Model for the outgoing response from the


chatbot
class ChatResponse(BaseModel):
response: str # The response message
from the bot
STEP 5 : CREATE THE CHATBOT
API (CHATBOT.PY)
Now, create the chatbot API route in app/api/v1/chatbot.py.
For simplicity, the chatbot will respond with a predefined
message.

from fastapi import APIRouter


from app.models.chatbot import ChatRequest,
ChatResponse

router = APIRouter()

@router.post("/",
response_model=ChatResponse)
async def get_response(chat_request:
ChatRequest):
user_message = chat_request.message
# Generate a response (for simplicity,
echo the message)
response = f"Bot: I received your
message: '{user_message}'. How can I assist
you?"
return ChatResponse(response=response)
STEP 6 : RUN THE FASTAPI
APPLICATION USING FOLLOWING
COMMAND:

uvicorn app.main:app --reload

By default, FastAPI will run on


http://127.0.0.1:8000. You can navigate to
http://127.0.0.1:8000/docs to see the auto-
generated Swagger UI for your API.
STEP 7 : TEST THE API WITH
POSTMAN OR CURL
STEP 8.1 : CREATE DOCKER FILE

CREATE A DOCKER FILE


IN THE ROOT DIRECTORY (CHATBOT/), CREATE A DOCKER FILE FOR
THE FASTAPI APPLICATION.

I HAVE UPLOADED THE COMPLETE DOCKER FILE INSIDE THE PROEJCT


IN MY GITHUB REPOSITORY
STEP 8.2 : DEPLOY PROJECT WITH
DOCKER
BUILD AND RUN THE DOCKER CONTAINER:

DOCKER BUILD -T CHATBOT-FASTAPI .


DOCKER RUN -D -P 8000:8000 CHATBOT-FASTAPI
THANK YOU
"IF YOU NEED A RELIABLE
BACKEND SOLUTION FOR
YOUR PROJECTS, FEEL FREE
TO REACH OUT.

LINKEDIN.COM/IN/YASAMAN-R-530256138/

You might also like