File size: 1,142 Bytes
73fbc5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# agents/teacher.py

import os
from dotenv import load_dotenv
load_dotenv()
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
from langchain_openai import ChatOpenAI
from langchain_core.prompts import PromptTemplate
from langchain_core.output_parsers import StrOutputParser


class TeacherAgent:

    def __init__(self):
        self.llm = ChatOpenAI(
            model="gpt-4o-mini",
            temperature=0.3,
            api_key=OPENAI_API_KEY
        )

        self.prompt = PromptTemplate(
            input_variables=["topic", "question", "context"],
            template="""
            You are an expert teacher on the topic: {topic}.

            TEXTBOOK CONTEXT:
            --------------------
            {context}
            --------------------

            The student asks: {question}

            Provide a clear, structured educational answer.
            """
        )

        self.chain = self.prompt | self.llm | StrOutputParser()

    def answer(self, topic, question, context):
        return self.chain.invoke({
            "topic": topic,
            "question": question,
            "context": context
        })