Skip to content

Commit

Permalink
Add GPT-3 and Llama 2 7b chatbot endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Chen authored and Mark Chen committed Feb 6, 2024
1 parent 654c5bf commit 3988d7e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 13 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
This course provides a comprehensive guide to designing and developing a backend for an AI chatbot using Python. Participants will learn how to create a robust LLM backend, deploy it with modern CI/CD pipelines and GitHub, and monitor its performance using Microsoft Azure. The course is targeted towards software engineers and developers who want to build an LLM-powered chatbot for their applications. Basic understanding of Python programming is required, and previous experience in software development is a plus. Participants will need an Azure subscription, GitHub account, OpenAI API subscription, Visual Studio Code, Python version 3.8 or higher, and Pip installed. The course covers various topics, including the fundamentals of AI and large-language models, utilizing FastAPI for building an LLM backend, setting up development environments, containerization with Docker, implementing CI/CD pipelines with GitHub Actions, and monitoring AI applications using status pages. Upon completion of the course, participants will have gained the necessary skills to develop and deploy a powerful LLM backend for their AI chatbot projects.

## Getting started
#### Prerequisites
- Python 3.8 or higher
- Docker Installed

#### Installation
```
pip3 install -r requirements.txt
Expand Down
17 changes: 14 additions & 3 deletions src/ai/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import os
from openai import OpenAI
import requests

API_TOKEN = os.getenv("HUGGINGFACE_API_TOKEN")

API_URL = "https://api-inference.huggingface.co/models/meta-llama/Llama-2-7b-chat-hf"
headers = {"Authorization": f"Bearer {API_TOKEN}"}

client = OpenAI(
organization=os.getenv('OPENAI_ORG_ID'),
api_key=os.getenv('OPENAI_API_KEY')
organization=os.getenv("OPENAI_ORG_ID"), api_key=os.getenv("OPENAI_API_KEY")
)


def chatbot(user_request:str):
def gpt_chatbot(user_request: str):
completion = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
Expand All @@ -17,3 +22,9 @@ def chatbot(user_request:str):
)

return completion.choices[0].message


def llama_chatbot(user_request: str):
response = requests.post(API_URL, headers=headers, json={"inputs": user_request})

return response.json()
26 changes: 18 additions & 8 deletions src/index/main.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
from fastapi import FastAPI, Form
from fastapi.middleware.cors import CORSMiddleware
from ai import chatbot
from ai import gpt_chatbot, llama_chatbot

isProduction = True
isProduction = False

origins = ["*"]

if isProduction:
app = FastAPI(
title="LLM API Endpoints",
docs_url=None, # Disable docs (Swagger UI)
redoc_url=None, # Disable redoc
docs_url=None, # Disable docs (Swagger UI)
redoc_url=None, # Disable redoc
)
else:
app = FastAPI(title="LLM API Endpoints")
Expand All @@ -30,12 +30,22 @@ def index():
return {"server ok": True}


@app.post("/api/chat")
async def chat(user_request: str = Form(...)):
@app.post("/api/chat/gpt3", tags=["OpenAI GPT-3"])
async def gpt_chat(user_request: str = Form(...)):
"""
Chat with LLM Backend
Chat with LLM Backend - GPT-3
"""
# Get the text content in the user request
result = chatbot(user_request=user_request)
result = gpt_chatbot(user_request=user_request)

return {"result": result}

@app.post("/api/chat/llama", tags=["Llama 2 7B Chat"])
async def llama_chat(user_request: str = Form(...)):
"""
Chat with LLM Backend - Llama 2 7b Chat
"""
# Get the text content in the user request
result = llama_chatbot(user_request=user_request)

return {"result": result}
9 changes: 7 additions & 2 deletions src/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ def test_read_main():
assert response.status_code == 200
assert response.json() == {"server ok": True}

def test_chat():
response = client.post("/api/chat", data={"user_request": "What is OpenAI?"})
def test_gpt_chat():
response = client.post("/api/chat/gpt3", data={"user_request": "What is OpenAI?"})
assert response.status_code == 200
assert response.json()["result"] != ""

def test_llama_chat():
response = client.post("/api/chat/llama", data={"user_request": "What is LLM?"})
assert response.status_code == 200
assert response.json()["result"] != ""

0 comments on commit 3988d7e

Please sign in to comment.