Student-Teacher

img

The StudentTeacher class can be used to train a student model using a teacher model, that is preferably an expert in a narrow domain. The student model is trained to generate responses that are evaluated by the teacher model.

StudentTeacher

A class representing a system where a student and teacher interact, with the student generating responses and the teacher evaluating those responses. The teacher uses feedback to train the student.

Attributes:
  • student (Student) –

    The student object, which uses a language model (LLM) to generate responses.

  • teacher (Teacher) –

    The teacher object, which evaluates the student's responses.

Methods:

Name Description
generate_response

Generates a response from both the student and teacher LLMs for a given query.

train_student

Fine-tunes the student model using provided training data.

Source code in llamarch/patterns/student_teacher/__init__.py
 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
class StudentTeacher:
    """
    A class representing a system where a student and teacher interact, with the student generating 
    responses and the teacher evaluating those responses. The teacher uses feedback to train the student.

    Attributes
    ----------
    student : Student
        The student object, which uses a language model (LLM) to generate responses.
    teacher : Teacher
        The teacher object, which evaluates the student's responses.

    Methods
    -------
    generate_response(query)
        Generates a response from both the student and teacher LLMs for a given query.
    train_student(data)
        Fine-tunes the student model using provided training data.
    """

    def __init__(self, student_llm, teacher_llm):
        """
        Initializes the StudentTeacher system with a student and teacher.

        Parameters
        ----------
        student_llm : object
            The language model (LLM) used by the student for generating responses.
        teacher_llm : object
            The language model (LLM) used by the teacher to evaluate student responses.
        """
        self.student = Student(student_llm)
        self.teacher = Teacher(teacher_llm)

    def generate_response(self, query):
        """
        Generates a response from both the student and teacher LLMs based on a query. 
        The student generates a response which is then evaluated by the teacher.

        Parameters
        ----------
        query : str
            The query to be processed by the student and teacher.

        Returns
        -------
        tuple
            A tuple containing:
            - student_response : str
                The response generated by the student.
            - teacher_response : str
                The evaluation of the student response by the teacher.
        """
        student_response = self.student.generate_response(query)
        teacher_response = self.teacher.evaluate_response(
            student_response[:MAX_LENGTH])
        return student_response, teacher_response

    def train_student(self, data):
        """
        Fine-tunes the student model using the provided training data.

        Parameters
        ----------
        data : iterable
            The data used for fine-tuning the student's model.
        """
        self.student.fine_tune(data)

__init__(student_llm, teacher_llm)

Initializes the StudentTeacher system with a student and teacher.

Parameters:
  • student_llm (object) –

    The language model (LLM) used by the student for generating responses.

  • teacher_llm (object) –

    The language model (LLM) used by the teacher to evaluate student responses.

Source code in llamarch/patterns/student_teacher/__init__.py
28
29
30
31
32
33
34
35
36
37
38
39
40
def __init__(self, student_llm, teacher_llm):
    """
    Initializes the StudentTeacher system with a student and teacher.

    Parameters
    ----------
    student_llm : object
        The language model (LLM) used by the student for generating responses.
    teacher_llm : object
        The language model (LLM) used by the teacher to evaluate student responses.
    """
    self.student = Student(student_llm)
    self.teacher = Teacher(teacher_llm)

generate_response(query)

Generates a response from both the student and teacher LLMs based on a query. The student generates a response which is then evaluated by the teacher.

Parameters:
  • query (str) –

    The query to be processed by the student and teacher.

Returns:
  • tuple

    A tuple containing: - student_response : str The response generated by the student. - teacher_response : str The evaluation of the student response by the teacher.

Source code in llamarch/patterns/student_teacher/__init__.py
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
def generate_response(self, query):
    """
    Generates a response from both the student and teacher LLMs based on a query. 
    The student generates a response which is then evaluated by the teacher.

    Parameters
    ----------
    query : str
        The query to be processed by the student and teacher.

    Returns
    -------
    tuple
        A tuple containing:
        - student_response : str
            The response generated by the student.
        - teacher_response : str
            The evaluation of the student response by the teacher.
    """
    student_response = self.student.generate_response(query)
    teacher_response = self.teacher.evaluate_response(
        student_response[:MAX_LENGTH])
    return student_response, teacher_response

train_student(data)

Fine-tunes the student model using the provided training data.

Parameters:
  • data (iterable) –

    The data used for fine-tuning the student's model.

Source code in llamarch/patterns/student_teacher/__init__.py
66
67
68
69
70
71
72
73
74
75
def train_student(self, data):
    """
    Fine-tunes the student model using the provided training data.

    Parameters
    ----------
    data : iterable
        The data used for fine-tuning the student's model.
    """
    self.student.fine_tune(data)