File size: 4,374 Bytes
0e9ef68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2393995
0e9ef68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2393995
0e9ef68
 
 
 
2393995
 
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
import asyncio
import json
import chainlit as cl
from datetime import datetime
from utils_chain_parameters import prepare_chain_parameters
from utils_output import display_evaluation_results 
from utils_voice import reply_with_voice


async def do_simulation(client, session_state, message):
    if session_state.status == "active":
        chain = cl.user_session.get("chain")
        history = cl.user_session.get("history", [])
        history.append({"role": "user", "content": message})
        session_state.previous_answer = message.content
        prompt_parm = prepare_chain_parameters(session_state, message, history)
        session_state.queries.append(prompt_parm)
        response_content = chain.invoke(prompt_parm)
        json_str = response_content.content.strip('```json\n').strip('```')
        try:
            this_response = json.loads(json_str)
        except json.JSONDecodeError as e:
            print(f"JSON Decode Error: {e}")
            print(response_content.content)
            print(f"Error at position {e.pos}: {json_str[max(0, e.pos-10):e.pos+10]}")
            this_response = {"Response": "Error receiving response from LLM"}  
        llm_response = this_response.get("Response", "No response from LLM")
        print("LLM Response:")
        print(llm_response)
        session_state.llm_responses.append(this_response)
        print("Next question:")
        print(this_response.get("Question", "No question"))

        if session_state.question != "":
            session_state.responses.append({
                "question_number": session_state.current_question_index,
                "question": session_state.question,
                "response": session_state.rep_answer,
                "ground_truth": session_state.ground_truth,
                "response_score": this_response.get("Score", "No score"),
                "response_evaluation": this_response.get("Evaluation", "No evaluation"),
                "mood_score": this_response.get("Mood Score", "No mood score"),
                "overall_score": this_response.get("Overall Score", "No overall score"),
                "overall_evaluation": this_response.get("Overall Evaluation", "No overall evaluation"),
            })
        message_to_rep = llm_response + "\n\n" + this_response.get("Question", "No question")
        print("Checking to continue")
        print(session_state.current_question_index)
        print(len(session_state.questions))
        if session_state.current_question_index < len(session_state.questions):
            if session_state.do_voice:
                await reply_with_voice(cl, client, message_to_rep)
            else:
                await cl.Message(content=message_to_rep, author="John Smith").send()
            # await cl.Message(this_response).send()
            history.append({"role": "assistant", "content": response_content})
            cl.user_session.set("history", history)
            session_state.current_question_index += 1
        else:
            final_message = message_to_rep
            conclusion = this_response.get("Conclusion", "")
            if conclusion != "":
                final_message = final_message + "\n\n" + conclusion
            if session_state.do_voice:
                await reply_with_voice(cl, client, final_message)
            else:
                await cl.Message(message_to_rep).send()
            session_state.status = "complete"
            end_time = datetime.now()
            duration = end_time - session_state.start_time
            duration_minutes = round(duration.total_seconds() / 60)
            session_state.end_time = end_time
            session_state.duration_minutes = duration_minutes
            if session_state.do_evaluation:
                await display_evaluation_results(cl, session_state)
            else:
                await cl.Message(content="**Simulation Complete**").send()
                evaluate_actions = [
                    cl.Action(name="Evaluate Performance", value="evaluate", description="Evaluate Performance"),
                    cl.Action(name="Display Queries and Responses", value="display_llm_responses", description="Display LLM Responses")
                ]
                await cl.Message(content="Click to evaluate performance", actions=evaluate_actions).send()
                await cl.Message(content="\n\n").send()