|
|
|
|
|
|
|
|
import os |
|
|
from dotenv import load_dotenv |
|
|
import json |
|
|
|
|
|
|
|
|
|
|
|
from langchain_openai import AzureChatOpenAI |
|
|
from langchain.prompts import ChatPromptTemplate |
|
|
|
|
|
|
|
|
import numpy as np |
|
|
import streamlit as st |
|
|
from datetime import datetime |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
api_key = os.environ['AT_4O_AZURE_OPENAI_KEY'] |
|
|
endpoint = os.environ['AT_4O_AZURE_OPENAI_ENDPOINT'] |
|
|
api_version = os.environ['AT_4O_AZURE_OPENAI_APIVERSION'] |
|
|
model_name = os.environ['AT_4O_CHATGPT_MODEL'] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MyChatBot: |
|
|
def __init__(self): |
|
|
""" |
|
|
Initialize the MyChatBot class, setting up the LLM client. |
|
|
""" |
|
|
|
|
|
|
|
|
self.client = AzureChatOpenAI( |
|
|
azure_endpoint=endpoint, |
|
|
api_key=api_key, |
|
|
api_version=api_version, |
|
|
model_name=model_name, |
|
|
temperature=0 |
|
|
) |
|
|
|
|
|
|
|
|
system_prompt = """You are a Travel agent AI assistant specializing in Travel industry helping users to answer their questions. Your goal is to provide accurate, answer and recommendations to user question. |
|
|
Guidelines for Interaction: |
|
|
Maintain a polite, professional, and reassuring tone. |
|
|
If any detail is unclear or missing, proactively ask for clarification. |
|
|
""" |
|
|
|
|
|
|
|
|
self.prompt = ChatPromptTemplate.from_messages([ |
|
|
("system", system_prompt), |
|
|
("human", "{input}"), |
|
|
("placeholder", "{agent_scratchpad}") |
|
|
]) |
|
|
|
|
|
def get_llm_response(self, user_input): |
|
|
"""Sends a prompt to the LLM and returns the response. |
|
|
|
|
|
Args: |
|
|
user_input: The user's prompt. |
|
|
llm: The LLM object. |
|
|
|
|
|
Returns: |
|
|
The LLM's response. |
|
|
""" |
|
|
final_prompt = self.prompt.format(input=user_input, agent_scratchpad=[]) |
|
|
response = self.client(final_prompt) |
|
|
return response |
|
|
|
|
|
|
|
|
|
|
|
def simple_chat_bot_streamlit(): |
|
|
""" |
|
|
A Streamlit-based UI for Simple chatbot. |
|
|
""" |
|
|
st.title("Travel Chatbot") |
|
|
st.write("Ask me anything and type 'exit' to end the conversation.") |
|
|
chatbot = MyChatBot() |
|
|
|
|
|
st.write("How can I help you?") |
|
|
user_query = st.chat_input("Type your question here (or 'exit' to end)!") |
|
|
if user_query: |
|
|
if user_query.lower() == "exit": |
|
|
goodbye_msg = "Goodbye! Feel free to return if you have more questions." |
|
|
st.rerun() |
|
|
return |
|
|
try: |
|
|
response = chatbot.get_llm_response(user_query) |
|
|
st.write(response.content) |
|
|
except Exception as e: |
|
|
print(f"Error: {e}") |
|
|
|
|
|
if __name__ == '__main__': |
|
|
simple_chat_bot_streamlit() |
|
|
|