Spaces:
Sleeping
Sleeping
shengwei62
commited on
Commit
•
3e05c26
1
Parent(s):
0aef005
Upload 9 files
Browse files- Dockerfile +43 -0
- README.md +34 -0
- api/.DS_Store +0 -0
- api/app/__init__.py +0 -0
- api/app/ai/__init__.py +0 -0
- api/app/ai/chatbot.py +30 -0
- api/app/main.py +56 -0
- api/test_main.py +19 -0
- requirements.txt +10 -0
Dockerfile
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Use the custom FastAPI image
|
2 |
+
# FROM tiangolo/uvicorn-gunicorn-fastapi:python3.9
|
3 |
+
# Use ubuntu as base image
|
4 |
+
FROM ubuntu:20.04
|
5 |
+
|
6 |
+
# Avoid prompts from apt
|
7 |
+
ENV DEBIAN_FRONTEND=noninteractive
|
8 |
+
|
9 |
+
# Update apt repositories and install Python and Pip
|
10 |
+
RUN apt-get update && \
|
11 |
+
apt-get install -y python3-pip python3-dev
|
12 |
+
|
13 |
+
# Check if the symbolic links already exist and create them if they don't
|
14 |
+
RUN if [ ! -e /usr/bin/python ]; then ln -s /usr/bin/python3 /usr/bin/python; fi && \
|
15 |
+
if [ ! -e /usr/bin/pip ]; then ln -s /usr/bin/pip3 /usr/bin/pip; fi
|
16 |
+
|
17 |
+
# Set default values for environment variables
|
18 |
+
ENV OPENAI_ORG_ID=default_org_id
|
19 |
+
ENV OPENAI_API_KEY=default_api_key
|
20 |
+
ENV HUGGINGFACE_API_TOKEN=default_huggingface_token
|
21 |
+
|
22 |
+
# Set environment variables for Matplotlib and Fontconfig
|
23 |
+
ENV MPLCONFIGDIR=/app/matplotlib_cache
|
24 |
+
ENV FONTCONFIG_PATH=/app/fontconfig
|
25 |
+
|
26 |
+
# Create the directories for Matplotlib cache and Fontconfig
|
27 |
+
RUN mkdir -p /app/matplotlib_cache /app/fontconfig && \
|
28 |
+
chmod -R 777 /app/matplotlib_cache /app/fontconfig
|
29 |
+
|
30 |
+
# Create a writable directory for Fontconfig cache
|
31 |
+
RUN mkdir -p /app/fontconfig_cache && chmod -R 777 /app/fontconfig_cache
|
32 |
+
|
33 |
+
# Set the environment variable so Fontconfig uses the writable directory
|
34 |
+
ENV FONTCONFIG_PATH=/app/fontconfig_cache
|
35 |
+
|
36 |
+
# Copy the requirements file and install dependencies
|
37 |
+
COPY ./requirements.txt /code/requirements.txt
|
38 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
39 |
+
|
40 |
+
# Copy your application source code and script
|
41 |
+
COPY ./api /app
|
42 |
+
|
43 |
+
CMD ["uvicorn", "app.app.main:app", "--host", "0.0.0.0", "--port", "7860"]
|
README.md
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: Building your generative AI Apps in Python
|
3 |
+
emoji: 🤖
|
4 |
+
colorFrom: "green"
|
5 |
+
colorTo: "green"
|
6 |
+
sdk: docker
|
7 |
+
sdk_version: 3.8.5
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
---
|
11 |
+
# 生成式AI後端開發部署實戰
|
12 |
+
# Building your generative AI API in Python
|
13 |
+
|
14 |
+
## Course intro:
|
15 |
+
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.
|
16 |
+
|
17 |
+
## Getting started
|
18 |
+
#### Prerequisites
|
19 |
+
- Python 3.8 or higher
|
20 |
+
- Docker Installed
|
21 |
+
|
22 |
+
#### Installation
|
23 |
+
```
|
24 |
+
pip3 install -r requirements.txt
|
25 |
+
```
|
26 |
+
#### Start the application
|
27 |
+
```
|
28 |
+
cd src/
|
29 |
+
python3 -m uvicorn index.main:app --reload
|
30 |
+
```
|
31 |
+
#### Run unit tests
|
32 |
+
```
|
33 |
+
python3 -m pytest
|
34 |
+
```
|
api/.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
api/app/__init__.py
ADDED
File without changes
|
api/app/ai/__init__.py
ADDED
File without changes
|
api/app/ai/chatbot.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from openai import OpenAI
|
3 |
+
import requests
|
4 |
+
|
5 |
+
API_TOKEN = os.getenv("HUGGINGFACE_API_TOKEN")
|
6 |
+
|
7 |
+
API_URL = "https://api-inference.huggingface.co/models/meta-llama/Llama-2-7b-chat-hf"
|
8 |
+
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
9 |
+
|
10 |
+
client = OpenAI(
|
11 |
+
organization=os.getenv("OPENAI_ORG_ID"), api_key=os.getenv("OPENAI_API_KEY")
|
12 |
+
)
|
13 |
+
|
14 |
+
|
15 |
+
def gpt_chatbot(user_request: str):
|
16 |
+
completion = client.chat.completions.create(
|
17 |
+
model="gpt-3.5-turbo",
|
18 |
+
messages=[
|
19 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
20 |
+
{"role": "user", "content": user_request},
|
21 |
+
],
|
22 |
+
)
|
23 |
+
|
24 |
+
return completion.choices[0].message
|
25 |
+
|
26 |
+
|
27 |
+
def llama_chatbot(user_request: str):
|
28 |
+
response = requests.post(API_URL, headers=headers, json={"inputs": user_request})
|
29 |
+
|
30 |
+
return response.json()
|
api/app/main.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, Form
|
2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
3 |
+
from fastapi.staticfiles import StaticFiles
|
4 |
+
from fastapi.responses import HTMLResponse
|
5 |
+
from .ai.chatbot import gpt_chatbot, llama_chatbot
|
6 |
+
|
7 |
+
isProduction = False
|
8 |
+
|
9 |
+
origins = ["*"]
|
10 |
+
|
11 |
+
if isProduction:
|
12 |
+
app = FastAPI(
|
13 |
+
title="LLM API Endpoints",
|
14 |
+
docs_url=None, # Disable docs (Swagger UI)
|
15 |
+
redoc_url=None, # Disable redoc
|
16 |
+
)
|
17 |
+
#app.mount("/static", StaticFiles(directory="static"), name="static")
|
18 |
+
else:
|
19 |
+
app = FastAPI(title="LLM API Endpoints")
|
20 |
+
#app.mount("/static", StaticFiles(directory="static"), name="static")
|
21 |
+
|
22 |
+
app.add_middleware(
|
23 |
+
CORSMiddleware,
|
24 |
+
allow_origins=origins,
|
25 |
+
allow_credentials=True,
|
26 |
+
allow_methods=["POST", "GET", "PUT", "DELETE"],
|
27 |
+
allow_headers=["*"],
|
28 |
+
)
|
29 |
+
|
30 |
+
|
31 |
+
# Create a homepage route
|
32 |
+
@app.get("/")
|
33 |
+
async def index():
|
34 |
+
return {"server ok": True}
|
35 |
+
|
36 |
+
|
37 |
+
@app.post("/api/chat/gpt3", tags=["OpenAI GPT-3"])
|
38 |
+
async def gpt_chat(user_request: str = Form(...)):
|
39 |
+
"""
|
40 |
+
Chat with LLM Backend - GPT-3
|
41 |
+
"""
|
42 |
+
# Get the text content in the user request
|
43 |
+
result = gpt_chatbot(user_request=user_request)
|
44 |
+
|
45 |
+
return {"result": result}
|
46 |
+
|
47 |
+
|
48 |
+
@app.post("/api/chat/llama", tags=["Llama 2 7B Chat"])
|
49 |
+
async def llama_chat(user_request: str = Form(...)):
|
50 |
+
"""
|
51 |
+
Chat with LLM Backend - Llama 2 7b Chat
|
52 |
+
"""
|
53 |
+
# Get the text content in the user request
|
54 |
+
result = llama_chatbot(user_request=user_request)
|
55 |
+
|
56 |
+
return {"result": result}
|
api/test_main.py
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi.testclient import TestClient
|
2 |
+
from app.main import app
|
3 |
+
|
4 |
+
client = TestClient(app)
|
5 |
+
|
6 |
+
def test_read_main():
|
7 |
+
response = client.get("/")
|
8 |
+
assert response.status_code == 200
|
9 |
+
assert response.json() == {"server ok": True}
|
10 |
+
|
11 |
+
def test_gpt_chat():
|
12 |
+
response = client.post("/api/chat/gpt3", data={"user_request": "What is OpenAI?"})
|
13 |
+
assert response.status_code == 200
|
14 |
+
assert response.json()["result"] != ""
|
15 |
+
|
16 |
+
def test_llama_chat():
|
17 |
+
response = client.post("/api/chat/llama", data={"user_request": "What is LLM?"})
|
18 |
+
assert response.status_code == 200
|
19 |
+
assert response.json()["result"] != ""
|
requirements.txt
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
openai
|
2 |
+
uvicorn
|
3 |
+
fastapi
|
4 |
+
python-dotenv
|
5 |
+
python-multipart
|
6 |
+
config
|
7 |
+
PyGithub
|
8 |
+
httpx
|
9 |
+
pytest
|
10 |
+
gradio
|