Spaces:
Running
Running
Update api.py
Browse files
api.py
CHANGED
|
@@ -1,81 +1,81 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
| 2 |
-
from pydantic import BaseModel
|
| 3 |
-
from langchain_groq import ChatGroq
|
| 4 |
-
from langchain_huggingface import HuggingFaceEmbeddings
|
| 5 |
-
from langchain_community.vectorstores import Chroma
|
| 6 |
-
from langchain_core.prompts import ChatPromptTemplate
|
| 7 |
-
from dotenv import load_dotenv
|
| 8 |
-
import os
|
| 9 |
-
import re
|
| 10 |
-
|
| 11 |
-
load_dotenv()
|
| 12 |
-
|
| 13 |
-
app = FastAPI()
|
| 14 |
-
|
| 15 |
-
CHROMA_PATH = "./chroma_db_wilson"
|
| 16 |
-
|
| 17 |
-
PROMPT_TEMPLATE = """
|
| 18 |
-
Answer the question based only on the following context:
|
| 19 |
-
|
| 20 |
-
{context}
|
| 21 |
-
|
| 22 |
-
---
|
| 23 |
-
|
| 24 |
-
Answer the question based on the above context: {question}
|
| 25 |
-
"""
|
| 26 |
-
|
| 27 |
-
llm = ChatGroq(
|
| 28 |
-
model="qwen/qwen3-32b",
|
| 29 |
-
api_key=os.getenv("GROQ_API_KEY")
|
| 30 |
-
)
|
| 31 |
-
|
| 32 |
-
# Load RAG components sekali saat startup
|
| 33 |
-
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
| 34 |
-
db = Chroma(persist_directory=CHROMA_PATH, embedding_function=embeddings)
|
| 35 |
-
|
| 36 |
-
class PromptRequest(BaseModel):
|
| 37 |
-
prompt: str
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
def clean_text(text: str) -> str:
|
| 41 |
-
# Hapus <think> tags
|
| 42 |
-
text = re.sub(r'<think>.*?</think>', '', text, flags=re.DOTALL)
|
| 43 |
-
# Hapus bold/italic (**text**, *text*, __text__)
|
| 44 |
-
text = re.sub(r'\*\*(.+?)\*\*', r'\1', text)
|
| 45 |
-
text = re.sub(r'\*(.+?)\*', r'\1', text)
|
| 46 |
-
text = re.sub(r'__(.+?)__', r'\1', text)
|
| 47 |
-
# Hapus bullet points (- item atau * item)
|
| 48 |
-
text = re.sub(r'^\s*[-*]\s+', '', text, flags=re.MULTILINE)
|
| 49 |
-
# Hapus heading (## Title)
|
| 50 |
-
text = re.sub(r'#+\s+', '', text)
|
| 51 |
-
# Hapus \n (newline characters)
|
| 52 |
-
text = text.replace('\n', ' ') # ← tambahkan ini
|
| 53 |
-
# Hapus spasi berlebihan
|
| 54 |
-
text = re.sub(r' {2,}', ' ', text) # ← tambahkan ini
|
| 55 |
-
return text.strip()
|
| 56 |
-
|
| 57 |
-
@app.post("/generate")
|
| 58 |
-
def generate(request: PromptRequest):
|
| 59 |
-
# 1. Cari dokumen relevan dari Chroma
|
| 60 |
-
results = db.similarity_search_with_relevance_scores(request.prompt, k=3)
|
| 61 |
-
|
| 62 |
-
# 2. Cek threshold
|
| 63 |
-
if len(results) == 0 or results[0][1] < 0.
|
| 64 |
-
return {"response": "I’m not sure yet. For more information, please contact Wilson in the email below."}
|
| 65 |
-
|
| 66 |
-
# 3. Gabungkan context
|
| 67 |
-
context_text = "\n\n---\n\n".join([doc.page_content for doc, _ in results])
|
| 68 |
-
|
| 69 |
-
# 4. Buat prompt dengan context
|
| 70 |
-
prompt = ChatPromptTemplate.from_template(PROMPT_TEMPLATE).format(
|
| 71 |
-
context=context_text,
|
| 72 |
-
question=request.prompt
|
| 73 |
-
)
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
# 5. Kirim ke LLM
|
| 78 |
-
response = llm.invoke(prompt)
|
| 79 |
-
clean_content = clean_text(response.content)
|
| 80 |
-
|
| 81 |
return {"response": clean_content}
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from langchain_groq import ChatGroq
|
| 4 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 5 |
+
from langchain_community.vectorstores import Chroma
|
| 6 |
+
from langchain_core.prompts import ChatPromptTemplate
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
import os
|
| 9 |
+
import re
|
| 10 |
+
|
| 11 |
+
load_dotenv()
|
| 12 |
+
|
| 13 |
+
app = FastAPI()
|
| 14 |
+
|
| 15 |
+
CHROMA_PATH = "./chroma_db_wilson"
|
| 16 |
+
|
| 17 |
+
PROMPT_TEMPLATE = """
|
| 18 |
+
Answer the question based only on the following context:
|
| 19 |
+
|
| 20 |
+
{context}
|
| 21 |
+
|
| 22 |
+
---
|
| 23 |
+
|
| 24 |
+
Answer the question based on the above context: {question}
|
| 25 |
+
"""
|
| 26 |
+
|
| 27 |
+
llm = ChatGroq(
|
| 28 |
+
model="qwen/qwen3-32b",
|
| 29 |
+
api_key=os.getenv("GROQ_API_KEY")
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
# Load RAG components sekali saat startup
|
| 33 |
+
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
| 34 |
+
db = Chroma(persist_directory=CHROMA_PATH, embedding_function=embeddings)
|
| 35 |
+
|
| 36 |
+
class PromptRequest(BaseModel):
|
| 37 |
+
prompt: str
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def clean_text(text: str) -> str:
|
| 41 |
+
# Hapus <think> tags
|
| 42 |
+
text = re.sub(r'<think>.*?</think>', '', text, flags=re.DOTALL)
|
| 43 |
+
# Hapus bold/italic (**text**, *text*, __text__)
|
| 44 |
+
text = re.sub(r'\*\*(.+?)\*\*', r'\1', text)
|
| 45 |
+
text = re.sub(r'\*(.+?)\*', r'\1', text)
|
| 46 |
+
text = re.sub(r'__(.+?)__', r'\1', text)
|
| 47 |
+
# Hapus bullet points (- item atau * item)
|
| 48 |
+
text = re.sub(r'^\s*[-*]\s+', '', text, flags=re.MULTILINE)
|
| 49 |
+
# Hapus heading (## Title)
|
| 50 |
+
text = re.sub(r'#+\s+', '', text)
|
| 51 |
+
# Hapus \n (newline characters)
|
| 52 |
+
text = text.replace('\n', ' ') # ← tambahkan ini
|
| 53 |
+
# Hapus spasi berlebihan
|
| 54 |
+
text = re.sub(r' {2,}', ' ', text) # ← tambahkan ini
|
| 55 |
+
return text.strip()
|
| 56 |
+
|
| 57 |
+
@app.post("/generate")
|
| 58 |
+
def generate(request: PromptRequest):
|
| 59 |
+
# 1. Cari dokumen relevan dari Chroma
|
| 60 |
+
results = db.similarity_search_with_relevance_scores(request.prompt, k=3)
|
| 61 |
+
|
| 62 |
+
# 2. Cek threshold
|
| 63 |
+
if len(results) == 0 or results[0][1] < 0.5:
|
| 64 |
+
return {"response": "I’m not sure yet. For more information, please contact Wilson in the email below."}
|
| 65 |
+
|
| 66 |
+
# 3. Gabungkan context
|
| 67 |
+
context_text = "\n\n---\n\n".join([doc.page_content for doc, _ in results])
|
| 68 |
+
|
| 69 |
+
# 4. Buat prompt dengan context
|
| 70 |
+
prompt = ChatPromptTemplate.from_template(PROMPT_TEMPLATE).format(
|
| 71 |
+
context=context_text,
|
| 72 |
+
question=request.prompt
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
# 5. Kirim ke LLM
|
| 78 |
+
response = llm.invoke(prompt)
|
| 79 |
+
clean_content = clean_text(response.content)
|
| 80 |
+
|
| 81 |
return {"response": clean_content}
|