Create main.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113import osimport torchimport streamlit as stfrom transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipelinefrom langchain import PromptTemplate, LLMChainfrom langchain.llms import HuggingFacePipelinefrom langchain.agents import AgentExecutor, Tool, ZeroShotAgentfrom langchain.memory import ConversationBufferMemoryimport pandas as pdfrom sqlalchemy import create_engineimport smtplibfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipart
# Set up the open-source LLM@st.cache_resourcedef load_model(): model_name = "google/flan-t5-large" tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForSeq2SeqLM.from_pretrained(model_name) pipe = pipeline( "text2text-generation", model=model, tokenizer=tokenizer, max_length=512 ) return HuggingFacePipeline(pipeline=pipe)
local_llm = load_model()
# Set up the database connectiondb_connection_string = "sqlite:///leads.db" # Replace with your actual database connection stringengine = create_engine(db_connection_string)
# Define the tools for the agentdef search_leads(query): df = pd.read_sql(f"SELECT * FROM leads WHERE name LIKE '%{query}%'", engine) return df.to_dict(orient='records')
def send_email(to_email, subject, body): # For demo purposes, we'll just print the email details st.write(f"Email sent to: {to_email}") st.write(f"Subject: {subject}") st.write(f"Body: {body}") return "Email sent successfully"
tools = [ Tool( name="Search Leads", func=search_leads, description="Useful for searching leads in the database" ), Tool( name="Send Email", func=send_email, description="Useful for sending emails to leads" )]
# Set up the agentprefix = """You are an AI CyberSecurity Program Advisor. Your goal is to engage with leads and get them to book a video call for an in-person sales meeting. You have access to a database of leads and can send emails.
You have access to the following tools:"""
suffix = """Begin!
{chat_history}Human: {human_input}AI: Let's approach this step-by-step:"""
prompt = ZeroShotAgent.create_prompt( tools, prefix=prefix, suffix=suffix, input_variables=["human_input", "chat_history"])
llm_chain = LLMChain(llm=local_llm, prompt=prompt)memory = ConversationBufferMemory(memory_key="chat_history")
agent = ZeroShotAgent(llm_chain=llm_chain, tools=tools, verbose=True)agent_executor = AgentExecutor.from_agent_and_tools( agent=agent, tools=tools, verbose=True, memory=memory)
# Streamlit interfacest.title("AI CyberSecurity Program Advisor Demo")
st.write("This demo showcases an AI agent that can engage with leads and attempt to book video calls for in-person sales meetings.")
lead_name = st.text_input("Enter a lead's name to engage with:")
if lead_name: lead_info = search_leads(lead_name) if not lead_info: st.write(f"No lead found with the name {lead_name}") else: lead = lead_info[0] st.write(f"Lead found: {lead['name']} (Email: {lead['email']})") initial_message = f"Hello {lead['name']}, I'd like to discuss our cybersecurity program with you. Are you available for a quick video call?" if st.button("Engage with Lead"): with st.spinner("AI is generating a response..."): response = agent_executor.run(initial_message) st.write("AI Response:") st.write(response)
st.sidebar.title("About")st.sidebar.info("This is a demo of an AI CyberSecurity Program Advisor using an open-source LLM and LangChain. It's designed to engage with leads and attempt to book video calls for sales meetings.")
# To run this script, use: streamlit run your_script_name.pymain.pyAdded+113