added custom prompt
Browse files
app.py
CHANGED
|
@@ -5,17 +5,16 @@ import gradio as gr
|
|
| 5 |
|
| 6 |
from getFlightData import getFlightInfo
|
| 7 |
|
| 8 |
-
from langchain.agents import initialize_agent, Tool, load_tools
|
| 9 |
from langchain.chat_models import ChatOpenAI
|
| 10 |
from langchain.output_parsers import StructuredOutputParser, ResponseSchema
|
| 11 |
from langchain.prompts import PromptTemplate, ChatPromptTemplate, HumanMessagePromptTemplate
|
| 12 |
from langchain.memory import ConversationBufferMemory
|
| 13 |
-
import
|
| 14 |
-
|
| 15 |
-
openai.api_base = "https://oai.hconeai.com/v1"
|
| 16 |
-
|
| 17 |
|
| 18 |
|
|
|
|
|
|
|
| 19 |
|
| 20 |
today = datetime.today().strftime("%Y-%m-%d")
|
| 21 |
|
|
@@ -23,7 +22,8 @@ memory = ConversationBufferMemory(memory_key="chat_history")
|
|
| 23 |
llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0)
|
| 24 |
|
| 25 |
|
| 26 |
-
|
|
|
|
| 27 |
def parsing_infoJson(string):
|
| 28 |
#Define the response schemas we want to receive
|
| 29 |
response_schemas = [
|
|
@@ -65,7 +65,8 @@ def parsing_infoJson(string):
|
|
| 65 |
return out.content
|
| 66 |
|
| 67 |
|
| 68 |
-
premade_tools = load_tools(["
|
|
|
|
| 69 |
custom_tools = [
|
| 70 |
Tool(
|
| 71 |
name = "getSpecificInfoFlight",
|
|
@@ -75,16 +76,47 @@ custom_tools = [
|
|
| 75 |
),
|
| 76 |
]
|
| 77 |
|
| 78 |
-
tools =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
|
| 81 |
-
agent = initialize_agent(tools, llm, agent="conversational-react-description", verbose=True, memory=memory)
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
def main(user_input, history=[]):
|
| 87 |
-
answer =
|
| 88 |
history.append((user_input, answer))
|
| 89 |
|
| 90 |
return history, history
|
|
|
|
| 5 |
|
| 6 |
from getFlightData import getFlightInfo
|
| 7 |
|
| 8 |
+
from langchain.agents import initialize_agent, Tool, load_tools, ZeroShotAgent, AgentExecutor, ConversationalAgent
|
| 9 |
from langchain.chat_models import ChatOpenAI
|
| 10 |
from langchain.output_parsers import StructuredOutputParser, ResponseSchema
|
| 11 |
from langchain.prompts import PromptTemplate, ChatPromptTemplate, HumanMessagePromptTemplate
|
| 12 |
from langchain.memory import ConversationBufferMemory
|
| 13 |
+
from langchain import LLMChain
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
|
| 16 |
+
os.environ["OPENAI_API_KEY"] = "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"
|
| 17 |
+
os.environ["SERPER_API_KEY"] = "488a16eee2a618380bd1cfbf6ad4fcec9f0a3b76"
|
| 18 |
|
| 19 |
today = datetime.today().strftime("%Y-%m-%d")
|
| 20 |
|
|
|
|
| 22 |
llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0)
|
| 23 |
|
| 24 |
|
| 25 |
+
|
| 26 |
+
|
| 27 |
def parsing_infoJson(string):
|
| 28 |
#Define the response schemas we want to receive
|
| 29 |
response_schemas = [
|
|
|
|
| 65 |
return out.content
|
| 66 |
|
| 67 |
|
| 68 |
+
premade_tools = load_tools(["google-serper"],llm=llm)
|
| 69 |
+
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"
|
| 70 |
custom_tools = [
|
| 71 |
Tool(
|
| 72 |
name = "getSpecificInfoFlight",
|
|
|
|
| 76 |
),
|
| 77 |
]
|
| 78 |
|
| 79 |
+
tools = premade_tools
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
####
|
| 85 |
+
|
| 86 |
+
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:"""
|
| 87 |
+
suffix = """Begin! "
|
| 88 |
+
|
| 89 |
+
Question: {input}
|
| 90 |
+
{agent_scratchpad}"""
|
| 91 |
+
|
| 92 |
+
prompt = ConversationalAgent.create_prompt(
|
| 93 |
+
tools,
|
| 94 |
+
prefix=prefix,
|
| 95 |
+
suffix=suffix,
|
| 96 |
+
input_variables=["input", "agent_scratchpad"]
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
llm_chain = LLMChain(llm=llm, prompt=prompt)
|
| 100 |
+
|
| 101 |
+
####
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
tool_names = [tool.name for tool in tools]
|
| 105 |
+
agent = ConversationalAgent(llm_chain=llm_chain, required_tools=tool_names)
|
| 106 |
+
|
| 107 |
+
agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True)
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
#agent = initialize_agent(tools, llm, agent="conversational-react-description", verbose=True, memory=memory)
|
| 111 |
+
|
| 112 |
|
| 113 |
|
|
|
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
def main(user_input, history=[]):
|
| 119 |
+
answer = agent_executor.run(user_input)
|
| 120 |
history.append((user_input, answer))
|
| 121 |
|
| 122 |
return history, history
|