Delete main.py
Browse files
main.py
DELETED
@@ -1,59 +0,0 @@
|
|
1 |
-
from fastapi import FastAPI, Request
|
2 |
-
from pydantic import BaseModel
|
3 |
-
import json
|
4 |
-
import os
|
5 |
-
from llama_index.core import SimpleDirectoryReader, VectorStoreIndex
|
6 |
-
from llama_index.core.schema import Document
|
7 |
-
from llama_index.llms.openai import OpenAI
|
8 |
-
|
9 |
-
# Load and prepare user data once at startup
|
10 |
-
with open("sdluk.json", "r", encoding="utf-8") as f:
|
11 |
-
raw_data = json.load(f)
|
12 |
-
|
13 |
-
user_id = "IpJycOGcYkPNs0iYhpzUigcvbBG2"
|
14 |
-
user_data = {
|
15 |
-
"profile": raw_data["__collections__"]["users"].get(user_id, {}),
|
16 |
-
"cart": [v for v in raw_data["__collections__"]["carts"].values() if v.get("userId") == user_id],
|
17 |
-
"favorites": [v for v in raw_data["__collections__"]["favorites"].values() if v.get("userId") == user_id],
|
18 |
-
"issues": [v for v in raw_data["__collections__"]["issue_reports"].values() if v.get("userId") == user_id],
|
19 |
-
"plans": list(raw_data["__collections__"]["pricingPlans"].values())[0]["plans"],
|
20 |
-
"products": list(raw_data["__collections__"]["products"].values()),
|
21 |
-
}
|
22 |
-
|
23 |
-
# Convert to document
|
24 |
-
document = Document(text=json.dumps(user_data, indent=2))
|
25 |
-
|
26 |
-
# Set OpenAI key (ensure this is set securely)
|
27 |
-
os.environ["OPENAI_API_KEY"] = "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"
|
28 |
-
|
29 |
-
# Initialize LLM and index
|
30 |
-
llm = OpenAI(model="gpt-4o")
|
31 |
-
index = VectorStoreIndex.from_documents([document])
|
32 |
-
query_engine = index.as_query_engine(llm=llm)
|
33 |
-
|
34 |
-
# Chatbot instruction
|
35 |
-
instruction = """
|
36 |
-
You are a friendly and smart assistant for a retail telecom company. Use the given user data (profile, cart, favorites, plans, issues, products) to answer questions.
|
37 |
-
|
38 |
-
1. Be polite and personalized if possible. Greetings and Politeness (e.g., "Hi", "Good morning", "How are you?")
|
39 |
-
- Respond politely and warmly like a friendly customer support assistant.
|
40 |
-
2. If the question is about products, look in 'products'.
|
41 |
-
3. For current plan or registration, use 'profile' and 'plans'.
|
42 |
-
4. For cart/favorite items, check 'cart' and 'favorites'.
|
43 |
-
5. For issues or help, refer to 'issues'.
|
44 |
-
6. If the info is missing, say: "I couldn't find that in your records. Want me to check online?"
|
45 |
-
|
46 |
-
Always answer clearly using available data.
|
47 |
-
"""
|
48 |
-
|
49 |
-
# FastAPI app
|
50 |
-
app = FastAPI()
|
51 |
-
|
52 |
-
class QueryInput(BaseModel):
|
53 |
-
query: str
|
54 |
-
|
55 |
-
@app.post("/chat")
|
56 |
-
def retail_chatbot(input: QueryInput):
|
57 |
-
full_prompt = f"instruction: {instruction}\nquery: {input.query}"
|
58 |
-
response = query_engine.query(full_prompt)
|
59 |
-
return {"response": response.response}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|