Download app.py from Pepsr/Chatbot: direct link, hf CLI and curl.
- Browser
- Download file 4.54 kB
-
https://huggingface.co/spaces/Pepsr/Chatbot/resolve/main/app.py
- Command line
-
hf download hf://spaces/Pepsr/Chatbot/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Pepsr/Chatbot/resolve/main/app.py
4.54 kB
| import os | |
| from datetime import datetime | |
| import json | |
| import gradio as gr | |
| from getFlightData import getFlightInfo | |
| from langchain.agents import initialize_agent, Tool, load_tools, ZeroShotAgent, AgentExecutor, ConversationalAgent | |
| from langchain.chat_models import ChatOpenAI | |
| from langchain.output_parsers import StructuredOutputParser, ResponseSchema | |
| from langchain.prompts import PromptTemplate, ChatPromptTemplate, HumanMessagePromptTemplate | |
| from langchain.memory import ConversationBufferMemory | |
| from langchain import LLMChain | |
| import openai | |
| os.environ["OPENAI_API_KEY"] = "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" | |
| os.environ["SERPER_API_KEY"] = "488a16eee2a618380bd1cfbf6ad4fcec9f0a3b76" | |
| today = datetime.today().strftime("%Y-%m-%d") | |
| memory = ConversationBufferMemory(memory_key="chat_history") | |
| llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0) | |
| openai.api_base = "https://oai.hconeai.com/v1" | |
| def parsing_infoJson(string): | |
| #Define the response schemas we want to receive | |
| response_schemas = [ | |
| ResponseSchema(name="Flight_number", description="Flight number contained in the user sentence, if you cant find it, then it's equal to 0"), | |
| ResponseSchema(name="Date", description=f"Date contained in the user sentence, if you can't find it then equal to 0. today's date is: {today}. date format has to be: YYYY-MM-DD"), | |
| ] | |
| output_parser = StructuredOutputParser.from_response_schemas(response_schemas) | |
| #We now get a string that contains instructions on how to format the user input, we instert this into the prompt | |
| format_instructions = output_parser.get_format_instructions() | |
| prompt = PromptTemplate( | |
| template="extract the flight number and the date from the user input. if cant find them, then they are equal to 0 \n{format_instructions}\n{question}", | |
| input_variables=["question"], | |
| partial_variables={"format_instructions": format_instructions} | |
| ) | |
| #send prompt to model | |
| _input = prompt.format_prompt(question=string) | |
| output = llm(_input.to_messages()) | |
| result = output_parser.parse(output.content) | |
| if result["Flight_number"] == 0: | |
| return("I need the flight number and date to give you more info") | |
| elif result["Date"] == 0: | |
| return("the user didnt provide flight number or date, ask him to do so") | |
| else: | |
| date = datetime.strptime(result["Date"].strip(), "%Y-%m-%d").date() | |
| info = getFlightInfo(result["Flight_number"], date) | |
| summarize = PromptTemplate( | |
| template="summarize this information to the user \n{question}", | |
| input_variables=["question"] | |
| ) | |
| inp= summarize.format_prompt(question=str(info)) | |
| out = llm(inp.to_messages()) | |
| return out.content | |
| premade_tools = load_tools(["google-serper"],llm=llm) | |
| premade_tools[0].description = "Useful when a customer asks for anything related to atihad, always use this tool first to get the most accurate answer" | |
| premade_tools[0].return_direct = False | |
| custom_tools = [ | |
| Tool( | |
| name = "getSpecificInfoFlight", | |
| func=parsing_infoJson, | |
| description= f"Useful for when the user wants to know specific more about their flight, input the flight number and date. today's date is: {today}. date format has to be: YYYY-MM-DD", | |
| return_direct=True | |
| ), | |
| ] | |
| tools = premade_tools | |
| #### | |
| prefix = """You are an AI assistant that works for the Airline company 'Etihad'. Your objective is to assist customer with their inquires. You have access to the following tools:""" | |
| suffix = """Begin! " | |
| Question: {input} | |
| {agent_scratchpad}""" | |
| prompt = ConversationalAgent.create_prompt( | |
| tools, | |
| prefix=prefix, | |
| suffix=suffix, | |
| input_variables=["input", "agent_scratchpad"] | |
| ) | |
| llm_chain = LLMChain(llm=llm, prompt=prompt) | |
| #### | |
| tool_names = [tool.name for tool in tools] | |
| agent = ConversationalAgent(llm_chain=llm_chain, required_tools=tool_names) | |
| agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True) | |
| #agent = initialize_agent(tools, llm, agent="conversational-react-description", verbose=True, memory=memory) | |
| def main(user_input, history=[]): | |
| answer = agent_executor.run(user_input) | |
| history.append((user_input, answer)) | |
| return history, history | |
| inputs = gr.components.Textbox(lines=7, label="Chat with AI") | |
| outputs = gr.components.Textbox(label="Reply") | |
| iface = gr.Interface(fn=main, inputs=["text","state"], outputs=["chatbot","state"], title="AI Chatbot", | |
| theme="compact") | |
| iface.launch() | |