updated parser
Browse files
app.py
CHANGED
|
@@ -1,11 +1,18 @@
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from getFlightData import getFlightInfo
|
|
|
|
| 3 |
from langchain.agents import initialize_agent, Tool, load_tools
|
| 4 |
from langchain.chat_models import ChatOpenAI
|
| 5 |
-
from
|
| 6 |
-
import
|
| 7 |
from langchain.memory import ConversationBufferMemory
|
| 8 |
|
|
|
|
|
|
|
| 9 |
os.environ["OPENAI_API_KEY"] = "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"
|
| 10 |
|
| 11 |
|
|
@@ -14,32 +21,60 @@ today = datetime.today().strftime("%Y-%m-%d")
|
|
| 14 |
memory = ConversationBufferMemory(memory_key="chat_history")
|
| 15 |
llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0)
|
| 16 |
|
| 17 |
-
|
| 18 |
-
flight_n, date = string.split(",")
|
| 19 |
-
if flight_n == "flight_number":
|
| 20 |
-
return "Please provide the flight number"
|
| 21 |
-
elif date == "date":
|
| 22 |
-
return "Please provide the date"
|
| 23 |
-
else:
|
| 24 |
-
date = datetime.strptime(date.strip(), "%Y-%m-%d").date()
|
| 25 |
-
info = getFlightInfo(flight_n, date)
|
| 26 |
-
return info
|
| 27 |
|
| 28 |
-
def
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
|
|
|
|
|
|
| 33 |
|
|
|
|
| 34 |
custom_tools = [
|
| 35 |
Tool(
|
| 36 |
name = "getSpecificInfoFlight",
|
| 37 |
-
func=
|
| 38 |
-
description= f"Useful for when the user wants to know specific
|
|
|
|
| 39 |
),
|
| 40 |
]
|
| 41 |
|
| 42 |
-
tools = custom_tools
|
| 43 |
|
| 44 |
|
| 45 |
agent = initialize_agent(tools, llm, agent="conversational-react-description", verbose=True, memory=memory)
|
|
|
|
| 1 |
import os
|
| 2 |
+
from datetime import datetime
|
| 3 |
+
import json
|
| 4 |
+
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 |
|
| 14 |
+
|
| 15 |
+
|
| 16 |
os.environ["OPENAI_API_KEY"] = "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"
|
| 17 |
|
| 18 |
|
|
|
|
| 21 |
memory = ConversationBufferMemory(memory_key="chat_history")
|
| 22 |
llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0)
|
| 23 |
|
| 24 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
def parsing_infoJson(string):
|
| 27 |
+
#Define the response schemas we want to receive
|
| 28 |
+
response_schemas = [
|
| 29 |
+
ResponseSchema(name="Flight_number", description="Flight number contained in the user sentence, if you cant find it, then it's equal to 0"),
|
| 30 |
+
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"),
|
| 31 |
+
]
|
| 32 |
+
output_parser = StructuredOutputParser.from_response_schemas(response_schemas)
|
| 33 |
+
|
| 34 |
+
#We now get a string that contains instructions on how to format the user input, we instert this into the prompt
|
| 35 |
+
format_instructions = output_parser.get_format_instructions()
|
| 36 |
+
|
| 37 |
+
prompt = PromptTemplate(
|
| 38 |
+
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}",
|
| 39 |
+
input_variables=["question"],
|
| 40 |
+
partial_variables={"format_instructions": format_instructions}
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
#send prompt to model
|
| 44 |
+
_input = prompt.format_prompt(question=string)
|
| 45 |
+
output = llm(_input.to_messages())
|
| 46 |
+
|
| 47 |
+
result = output_parser.parse(output.content)
|
| 48 |
+
|
| 49 |
+
if result["Flight_number"] == 0:
|
| 50 |
+
return("I need the flight number and date to give you more info")
|
| 51 |
+
elif result["Date"] == 0:
|
| 52 |
+
return("the user didnt provide flight number or date, ask him to do so")
|
| 53 |
+
else:
|
| 54 |
+
date = datetime.strptime(result["Date"].strip(), "%Y-%m-%d").date()
|
| 55 |
+
info = getFlightInfo(result["Flight_number"], date)
|
| 56 |
|
| 57 |
+
summarize = PromptTemplate(
|
| 58 |
+
template="summarize this information to the user \n{question}",
|
| 59 |
+
input_variables=["question"]
|
| 60 |
+
)
|
| 61 |
+
inp= summarize.format_prompt(question=str(info))
|
| 62 |
+
out = llm(inp.to_messages())
|
| 63 |
|
| 64 |
+
return out.content
|
| 65 |
+
|
| 66 |
|
| 67 |
+
premade_tools = load_tools(["human"])
|
| 68 |
custom_tools = [
|
| 69 |
Tool(
|
| 70 |
name = "getSpecificInfoFlight",
|
| 71 |
+
func=parsing_infoJson,
|
| 72 |
+
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",
|
| 73 |
+
return_direct=True
|
| 74 |
),
|
| 75 |
]
|
| 76 |
|
| 77 |
+
tools = custom_tools + premade_tools
|
| 78 |
|
| 79 |
|
| 80 |
agent = initialize_agent(tools, llm, agent="conversational-react-description", verbose=True, memory=memory)
|