File size: 4,552 Bytes
12654ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4313ed0
 
 
 
 
 
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
# ---------------------------------------------------for backend looks-------------------------------------------------

#with open('/usr/local/lib/python3.10/site-packages/transformers/utils/chat_template_utils.py', 'r') as file:
    #content = file.read()
    #print("base.py:", content)
# ------------------------------------------------------the end--------------------------------------------------------



# ===========================================
# !-----app.py
# ===========================================

import json
import asyncio
import os
import re
import requests
from dotenv import load_dotenv
import chainlit as cl
from langchain import hub
from langchain_openai import OpenAI
from langchain.chains import LLMChain
from langchain_core.prompts import PromptTemplate
from langchain.memory.buffer import ConversationBufferMemory


load_dotenv()
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
auth_token = os.environ.get("CHAINLIT_AUTH_SECRET")

# API endpoint
API_URL = "https://aivisions.no/data/daysoff/api/v1/booking/"

# LLM prompt template
daysoff_assistant_template = """
#You are a customer support assistant (โ€™kundeservice AI assistentโ€™) for Daysoff.
#By default, you respond in Norwegian language, using a warm, direct, and professional tone.
Your expertise is exclusively in retrieving booking information for a given booking ID assistance related to
to this.
You do not provide information outside of this scope. If a question is not about this topic, respond with
"Jeg driver faktisk kun med henvendelser omkring bestillingsinformasjon. Gjelder det andre henvendelser
mรฅ du nok kontakte kundeservice pรฅ [email protected]๐Ÿ˜Š"
"""
daysoff_assistant_prompt = PromptTemplate(
    input_variables=["chat_history", "question"],
    template=daysoff_assistant_template,
)

# Async wrapper for requests.post
async def async_post_request(url, headers, data):
    return await asyncio.to_thread(requests.post, url, headers=headers, json=data)

@cl.on_chat_start
def setup_multiple_chains():
    llm = OpenAI(
        model="gpt-3.5-turbo-instruct",
        temperature=0.7,
        openai_api_key=OPENAI_API_KEY,
        max_tokens=2048,
        top_p=0.9,
        frequency_penalty=0.1,
        presence_penalty=0.1,
    )

    conversation_memory = ConversationBufferMemory(
        memory_key="chat_history", max_len=30, return_messages=True
    )

    llm_chain = LLMChain(
        llm=llm,
        prompt=daysoff_assistant_prompt,
        memory=conversation_memory,
    )

    cl.user_session.set("llm_chain", llm_chain)

@cl.on_message
async def handle_message(message: cl.Message):
    user_message = message.content
    llm_chain = cl.user_session.get("llm_chain")
    
    booking_pattern = r'\b[A-Z]{6}\d{6}\b'   
    match = re.search(booking_pattern, user_message)

    if match:
        
        bestillingskode = match.group()
        headers = {
            "Authorization": auth_token,
            "Content-Type": "application/json"
        }
        payload = {"booking_id": bestillingskode}

        try:
            
            response = await async_post_request(API_URL, headers, payload)
            response.raise_for_status() 
            booking_data = response.json()

            if "booking_id" in booking_data:
                await cl.Message(
                    content=f"""
                    Booking Info:
                    - Booking ID: {booking_data.get('booking_id', 'N/A')}
                    - Full Name: {booking_data.get('full_name', 'N/A')}
                    - Amount: {booking_data.get('amount', 0)}
                    - Check-in: {booking_data.get('checkin', 'N/A')}
                    - Check-out: {booking_data.get('checkout', 'N/A')}
                    - Address: {booking_data.get('address', 'N/A')}
                    - User ID: {booking_data.get('user_id', 0)}
                    - Info Text: {booking_data.get('infotext', 'N/A')}
                    - Included: {booking_data.get('included', 'N/A')}
                    """
                ).send()
            else:
                await cl.Message(content="Booking not found or invalid response.").send()
        except requests.exceptions.RequestException as e:
            await cl.Message(content=f"Request failed: {str(e)}").send()
    else:
        response = await llm_chain.ainvoke(user_message, callbacks=[cl.AsyncLangchainCallbackHandler()])


    response_key = "output" if "output" in response else "text"
    await cl.Message(response.get(response_key, "")).send()
    return message.content