File size: 4,544 Bytes
534c0fb 29e2b7b 534c0fb 29e2b7b c8c86d2 534c0fb 29e2b7b 534c0fb c8c86d2 9d6d510 eb58b7a c8c86d2 534c0fb 9d6d510 c8c86d2 29e2b7b 534c0fb 29e2b7b 534c0fb 29e2b7b 534c0fb c8c86d2 75a7b93 534c0fb 29e2b7b 534c0fb c8c86d2 534c0fb c8c86d2 534c0fb 760198a | 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 | 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()
|