File size: 4,904 Bytes
360b354
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
from fastapi import FastAPI, HTTPException, Request, BackgroundTasks
from starlette.responses import RedirectResponse
from src.json_creation import (
    final_counts,
    fetch_response,
    OllamaContextQuery,
    ChatManager,
)
from src.whatsapp_integration import gathering_data, normal_message
from datetime import datetime
from typing import Optional
import logging
from pydantic import BaseModel
from config import settings

# PHONE_NUMBER_ID = os.environ.get("PHONE_NUMBER_ID")
# BEARER_TOKEN = os.environ.get("BEARER_TOKEN")
PHONE_NUMBER_ID = settings.PHONE_NUMBER_ID
BEARER_TOKEN = settings.BEARER_TOKEN

logging.basicConfig(
    level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
)

app = FastAPI()
chat_manager = ChatManager()


class UserQueryInput(BaseModel):
    phone_number: str
    user_query: str


@app.get("/")
def read_root():
    return RedirectResponse(url="/docs")


@app.get("/fetch_data")
def fetch_data(
    store_id: str,
    brand_id: str,
    start_date: Optional[str] = None,
    end_date: Optional[str] = None,
):
    if start_date and end_date:
        try:
            start_date_obj = datetime.strptime(start_date, "%Y-%m-%d")
            end_date_obj = datetime.strptime(end_date, "%Y-%m-%d")
        except ValueError:
            return {"error": "Dates must be in YYYY-MM-DD format."}

    data = fetch_response(store_id, brand_id, None, start_date_obj, end_date_obj)

    if data:
        return final_counts(data)
    else:
        return {"error": "Failed to fetch data"}


@app.post("/process_message")
def process_message(phone_number: str, user_query: str):
    str_chat_history = chat_manager.handle_message(phone_number, user_query)
    return {"chat_history": str_chat_history}


@app.post("/process_query")
def process_query(input_data: UserQueryInput):
    user_query = input_data.user_query
    ollama_class = OllamaContextQuery()
    chat_history = chat_manager.handle_message(input_data.phone_number, user_query)
    try:
        context_query, is_tool_invoke = ollama_class.ollama_context_query(
            chat_history, user_query
        )
        main_llm_response = ""
        final_output = context_query
        if is_tool_invoke:
            main_llm_response = ollama_class.ollama_tool_call(user_query)
            final_output = ollama_class.summarised_output(
                messages=main_llm_response,
                chat_history=chat_history,
                context_query=context_query,
                user_query=input_data.user_query,
            )
        session = chat_manager.get_or_create_session(input_data.phone_number)
        # if is_greet:
        #     print("Here 1")
        #     chat_manager.save_message(
        #         session.id, input_data.phone_number, "ASSISTANT", main_llm_response
        #     )
        #     print("Here 2")
        #     return main_llm_response

        print("Here 1")
        chat_manager.save_message(
            session.id, input_data.phone_number, "ASSISTANT", final_output
        )
        print("Here 2")
        return final_output
    except Exception as e:
        print(e)
        raise HTTPException(status_code=500, detail=str(e))


@app.get("/receive_msg", tags=["Whatsapp Webhook"])
async def whatsapp_webhook_get(request: Request):
    # Extract query parameters from the request
    query_params = request.query_params
    hub_mode = query_params.get("hub.mode")
    hub_challenge = query_params.get("hub.challenge")
    hub_verify_token = query_params.get("hub.verify_token")

    if (
        hub_mode == "subscribe"
        and hub_challenge
        and hub_verify_token == "Sahl-analytics-bot"
    ):
        return int(hub_challenge)
    else:
        return {"detail": "Invalid request or verification token mismatch"}, 400


@app.post("/receive_msg", tags=["Whatsapp Webhook"])
async def whatsapp_webhook(request: Request, background_tasks: BackgroundTasks):
    data = await request.json()
    background_tasks.add_task(call_query, data)
    return "200 OK HTTPS."


def call_query(data):
    try:
        if (
            data["entry"][0]["changes"][0]["value"]["metadata"]["phone_number_id"]
            == PHONE_NUMBER_ID
        ):
            print("\n New Line starting from here :=" + "=" * 150)
            print(data)
            try:
                phone_number, text = gathering_data(data)
                print(phone_number)
                print(text)
                sample_input = UserQueryInput(
                    phone_number=phone_number, user_query=text
                )
                res = process_query(sample_input)
                normal_message(res, phone_number)

            except Exception as e:
                print("Exception in post whatsapp request :::::: ", e)
    except BaseException:
        pass


if __name__ == "__main__":
    import uvicorn

    uvicorn.run(app, host="0.0.0.0", port=8000)