MarkChenX commited on
Commit
9d906be
·
1 Parent(s): e9a8b6b

update codebase

Browse files
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-4o-mini",
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.content
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()[0]["generated_text"]
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/gpt4o/mini", tags=["OpenAI GPT-4o mini"])
38
+ async def gpt_chat(user_request: str = Form(...)):
39
+ """
40
+ Chat with LLM Backend - GPT-4o mini
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"] != ""