Spaces:
Sleeping
Sleeping
File size: 4,709 Bytes
0e9ef68 e8bbe1c 0e9ef68 ef651f8 6b522c0 ef651f8 0e9ef68 2393995 0e9ef68 2393995 0e9ef68 28d8ff0 ef651f8 0e9ef68 2393995 0e9ef68 |
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 133 |
import chainlit as cl
import dotenv
import os
import pandas as pd
import whisper
from datetime import datetime
from langchain_core.globals import set_llm_cache
from langchain_core.caches import InMemoryCache
from openai import OpenAI
from classes import SessionState
from utils_callbacks import callback_run_scenario, callback_start_scenario, callback_evaluate_performance, callback_display_queries_responses
from utils_customer_research import get_latest_news
from utils_pose_objections import pose_objections
from utils_prep import prep_research, prep_opportunities, prep_start, prep_opportunity_analysis
from utils_simulation import do_simulation
dotenv.load_dotenv()
openai_api_key = os.getenv("OPENAI_API_KEY")
tavily_api_key = os.getenv("TAVILY_API_KEY")
langchain_api_key = os.getenv("LANGCHAIN_API_KEY")
def set_session_state_variables(session_state):
if "DO_CUSTOMER_RESEARCH" in os.environ:
do_customer_research = os.getenv("DO_CUSTOMER_RESEARCH")
if do_customer_research.lower() == "true":
session_state.do_customer_research = True
else:
session_state.do_customer_research = False
if "DO_OPPORTUNITY_ANALYSIS" in os.environ:
do_opportunity_analysis = os.getenv("DO_OPPORTUNITY_ANALYSIS")
if do_opportunity_analysis.lower() == "true":
session_state.do_opportunity_analysis = True
else:
session_state.do_opportunity_analysis = False
llm_model = "gpt-4o-mini"
set_llm_cache(InMemoryCache())
client = OpenAI(api_key=openai_api_key)
whisper_model = whisper.load_model("base")
#############################################
# Action callbacks
#############################################
@cl.action_callback("Deal Analysis")
async def on_action_anayze_deal(action):
session_state = cl.user_session.get("session_state", None)
await prep_opportunities(session_state)
@cl.action_callback("Customer Research")
async def on_action_anayze_deal(action):
session_state = cl.user_session.get("session_state", None)
await get_latest_news("HSBC")
@cl.action_callback("Sales Simulation")
async def on_action_sales_simulation(action):
session_state = cl.user_session.get("session_state", None)
await callback_run_scenario(action)
@cl.action_callback("HSBC: Lending - Loan Origination System (Proposal)")
async def on_action_anayze_opportunity(action):
await prep_opportunity_analysis()
@cl.action_callback("Get Latest News on this Customer")
async def on_action_get_latest_news(action):
await get_latest_news(action.value)
@cl.action_callback("Enter Meeting Simulation")
async def on_action_run_scenario(action):
await callback_run_scenario(action)
@cl.action_callback("Start Simulation")
async def on_action_start_scenario(action):
print("on_action_start_scenario()")
await callback_start_scenario()
@cl.action_callback("Evaluate Performance")
async def on_action_evaluate_performance(action):
await callback_evaluate_performance()
@cl.action_callback("Display Queries and Responses")
async def on_action_display_queries_responses(action):
await callback_display_queries_responses()
#############################################
### On Chat Start (Session Start) Section ###
#############################################
@cl.on_chat_start
async def on_chat_start():
session_state = SessionState()
print("Creating new session state")
print(session_state.responses)
set_session_state_variables(session_state)
cl.user_session.set("session_state", session_state)
session_state.llm_model = llm_model
print(session_state)
cl.user_session.set("messages", [])
if client is None:
await cl.Message(content="Error: OpenAI client not initialized. Please check your API key.").send()
if whisper_model is None:
await cl.Message(content="Error: Whisper model not loaded. Please check your installation.").send()
await prep_start(session_state)
# await prep_opportunities(session_state)
# await prep_opportunity_analysis(session_state)
# await prep_research(session_state)
# session_state.session_stage = "research"
# Ask for the first PDF file (Potential Customer Business Domain)
# await process_pdf_files()
@cl.on_message
async def main(message):
content = message.content.strip()
session_state = cl.user_session.get("session_state", None)
if session_state is None:
await cl.Message(content="Error: Session state not initialized. Please check your installation.").send()
return
if session_state.do_objections:
await pose_objections(session_state)
else:
await do_simulation(client, session_state, message) |