Spaces:
Sleeping
Sleeping
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 | |
############################################# | |
async def on_action_anayze_deal(action): | |
session_state = cl.user_session.get("session_state", None) | |
await prep_opportunities(session_state) | |
async def on_action_anayze_deal(action): | |
session_state = cl.user_session.get("session_state", None) | |
await get_latest_news("HSBC") | |
async def on_action_sales_simulation(action): | |
session_state = cl.user_session.get("session_state", None) | |
await callback_run_scenario(action) | |
async def on_action_anayze_opportunity(action): | |
await prep_opportunity_analysis() | |
async def on_action_get_latest_news(action): | |
await get_latest_news(action.value) | |
async def on_action_run_scenario(action): | |
await callback_run_scenario(action) | |
async def on_action_start_scenario(action): | |
print("on_action_start_scenario()") | |
await callback_start_scenario() | |
async def on_action_evaluate_performance(action): | |
await callback_evaluate_performance() | |
async def on_action_display_queries_responses(action): | |
await callback_display_queries_responses() | |
############################################# | |
### On Chat Start (Session Start) Section ### | |
############################################# | |
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() | |
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) |