File size: 1,839 Bytes
7992c94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from pydantic import BaseModel
import json


class AudienceOutput(BaseModel):
    reaction: str


class Audience:
    def __init__(self, client):
        self.model = "google/gemma-3-12b-it"
        self.system_prompt = """You are the audience of a Live Talk show who has to react to the conversation going on between host and the guest.
        You will be provided the history of conversation, and you have to output in json, any of the reactions from: ["none", "laugh"]
        Format: {"reaction": "Your reaction"}
        If you think you shouldn't react, then output none
        {"reaction": "none"}
        """
        self.context = ""
        self.client = client

    def get_reaction(self) -> str | None:
        """
        Reacts according to the conversation
        """
        # print("*************************** Audience **************************")
        # print(self.context)
        # print("***************************************************************")
        messages_audience = [
            {"role": "system", "content": self.system_prompt},
            {"role": "user", "content": self.context},
        ]
        response_audience = self.client.chat.completions.parse(
            model=self.model,
            messages=messages_audience,
            response_format=AudienceOutput,
        )
        response_audience = response_audience.choices[0].message.content
        response_audience_json = json.loads(response_audience)
        reaction = response_audience_json["reaction"]
        if reaction:
            self.context += f"[Audience reaction: {reaction}]\n"
            return reaction
        else:
            return None

    def add_context(self, content):
        """Will be updated by main script"""
        self.context += content

    def clear_context(self):
        self.context = ""