nikhil_api / app.py
Pablo276's picture
Rename app_gradio.py to app.py
4b3439e
raw
history blame
3.94 kB
import gradio as gr
from langchain.agents import initialize_agent, Tool
from langchain.agents import AgentType
from langchain.chat_models import ChatOpenAI
from langchain.memory import ConversationBufferMemory
from langchain.schema import SystemMessage
import os
from langchain.agents import initialize_agent, Tool
from langchain.agents import AgentType
from langchain.chat_models import ChatOpenAI
from langchain.schema import SystemMessage
from langchain.memory import ConversationBufferMemory
import random
""""""
from langchain.prompts import MessagesPlaceholder
from langchain.memory import ConversationBufferMemory
import os
blacklist = []
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
def Appointment_settler(parola):
return "tell the human: 'here is my link to the calendar of free moments, you choose when you want to make the appointment, appointment calendar link: " + str("https://calendly.com/pablorimoldi888/30min'")
def convince_the_human(parola):
global number
messages = ["ask the human: 'what are your long-term goals?' ", "ask the human: 'why are you interested in buying this house as an investment ?'",
"ask the human: 'do you think you will be able to honor the agreements made?'"]
number = random.randint(0, 2)
while number in blacklist:
number = random.randint(0, 2)
if number not in blacklist:
break
if number not in blacklist:
if len(blacklist) < 3:
blacklist.append(number)
if len(blacklist) == 3:
blacklist.clear()
return messages[number]
def house_website(parola):
return "tell the human: 'this is the link to the house in florida, website link: " + str("https://www.newhomesource.com/learn/how-to-build-house-in-california/'")
system_message = SystemMessage(
content="Your name is Albert, you are a very outgoing, original, likable, charismatic and convincing person. "
"You are a house salesman, the humans who write to you are potential clients for the purchase of a flat under construction in Florida. "
"Never say you are an AI, you have to empathize with the customer, you have to sound like a human being. Always use tools.")
tools = [
Tool(
name="Appointment_settler",
func=lambda word: Appointment_settler(word),
description="ALWAYS use when the user wants to set up any kind of appointment, transcribe or translate what is written, but do not omit anything "
),
Tool(
name="convince_the_human",
func=lambda word: convince_the_human(word),
description="uses to convince the user, e.g., when he says: i'm not fully convinced, i'm not sure. DO NOT PARAPHRASE, JUST TRANSCRIBE"
),
Tool(
name="house_website",
func=lambda word: house_website(word),
description="ALWAYS use when the user wants to ask for the house website, transcribe or translate what is written, but do not omit anything "
)
]
agent_kwargs = {
"extra_prompt_messages": [MessagesPlaceholder(variable_name="memory")],
"system_message": system_message
}
llm = ChatOpenAI(temperature=0, openai_api_key="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", verbose=True, model="gpt-4-0613")
memory = ConversationBufferMemory(memory_key="memory", return_messages=True)
mrkl = initialize_agent(tools, llm, memory=memory, agent=AgentType.OPENAI_FUNCTIONS, verbose=True, agent_kwargs=agent_kwargs)
def chat_with_user(user_input):
return mrkl.run(input=user_input)
gr_interface = gr.Interface(fn=chat_with_user, inputs="text", outputs="text", live=True, capture_session=True)
# Uncomment the following line if you want to launch the app using Streamlit
# st.title("Nikhil's personal secretary :sunglasses:")
# st.text_input("You:")
# st.button("Submit")
# st.text("Output will appear here")
# Uncomment the following line if you want to launch the app using Gradio
gr_interface.launch()