File size: 6,649 Bytes
01f10c0
f44405d
 
79fd807
f44405d
 
 
e2436f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79fd807
 
 
 
 
 
 
 
 
 
e2436f1
79fd807
 
e2436f1
79fd807
 
 
 
 
3ee5d26
e2436f1
 
 
 
 
 
 
 
3ee5d26
e2436f1
 
 
f44405d
 
 
 
 
 
 
 
 
 
 
 
 
 
79fd807
 
 
 
 
f44405d
79fd807
f44405d
e2436f1
f44405d
 
79fd807
 
 
f44405d
 
 
 
e2436f1
f44405d
3ee5d26
f44405d
79fd807
 
 
f44405d
e2436f1
 
3ee5d26
f44405d
3ee5d26
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
def recommend(payload):

    from typing import Optional, List
    from langchain.output_parsers import PydanticOutputParser

    from langchain_core.pydantic_v1 import BaseModel, Field

    class InputGoals(BaseModel):
        """
            The existing goals that the user has associated with this particular ambition.
            You should pay close attention to this, and avoid repetition.
            You might build on goals that the user has already resolved, or that have been accepted.
            You should pay attention to goals that the user has rejected, and avoid recommending similar goals.
        """

        goal: str = Field(description="the goal itself in a clear format")
        bucket: str = Field(description="""
            The timescale within which the goal is currently scheduled to be completed. This will have one of three values:
                - 'now' a goal that could be completed immediately with ease
                - 'soon' a goal that requires a bit more planning or research, but that should be  achievable within the current academic year
                - 'during' a goal that might take considerable research and effort to achieve, and may take longer than a single academic year.
            In making your recommendations, you should consider how they fit in with these other goals
                """)
        status: str = Field(description="""
                indicates the origin and current progress towards the goal. 
                This will have one of three values, and these should guide your response: 
                    - 'user_provided' the user has provided this goal themselves.
                    - 'suggestion' you have suggested this goal previously, and the user has not yet accepted or rejected it.
                    - 'accepted' you have suggested this goal previously, and the user has accepted it.
                    - 'rejected' you have suggested this goal previously, and the user has rejected it.
                    - 'resolved' the user has completed this goal.
                    - 'removed' the user has removed this goal.
            """)

    class UserInput(BaseModel):
        """The format of information that will be provided by the user"""

        ambition: Optional[str] = Field(default=None, description="the ambition that the user is working on")
        bucket: Optional[str] = Field(default=None, description="""
            The timescale within which the goal should be able to be completed. This will have one of three values, and these should guide your response:
            - 'now' you should provide a goal that could be completed easily and immediately
            - 'soon' you should recommend a goal that might require a bit more research or planning, but that should still be achievable within the current academic year
            - 'during' you should provide a goal that might take considerable research and effort to achieve, and may take longer than a single academic year.
            Please be mindful that the user should always be able to complete all of their goals whilst they are on their current course. 
            Whilst you can and should recommend small standalone courses, and applying for other full time or substantial courses after the current course is completed, you should NEVER suggest that the user should take on a full time course whilst they are already studying.
            Part time additional effort is reasonable, but do not recommend things that will require a full time commitment. 
        """)
        plan: List[InputGoals] = Field(description="The existing goals that the user has associated with this particular ambition.")

    inputformat = PydanticOutputParser(pydantic_object=UserInput).get_format_instructions();
    inputformat = inputformat.replace("The output should be formatted as a JSON instance that conforms to the JSON schema below.", "The user will provide their input as a JSON instance that conforms to the JSON schema below")
    inputformat = inputformat.replace("Here is the output schema:", "Here is the input schema:")
    
    class Goal(BaseModel):
        """
            Your suggestion for a suggested goal. 
            In forming this you should ALWAYS pay attention to the information provided in the user input, and the guidance provided in the JSON schema for it
            Where possible, avoid recommending that the user 'researches' something, as this is a very vague goal
            And avoid referring to the careers service or advisers.
        """

        goal: Optional[str] = Field(default=None, description="a target for the user - this should be as concise as possible")
        rationale: Optional[str] = Field(default=None, description="explain why you have chosen this goal")
        guidance: Optional[str] = Field(default=None, description="provide any additional guidance that the user might need to achieve this goal")
        #inputstring: Optional[str] = Field(default=None, description="the previous goals that have been provided by the user")
        count_reject: Optional[int] = Field(default=0, description="the number of times that the user has rejected a goal")

    from typing import Optional

    from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
    from langchain_core.pydantic_v1 import BaseModel, Field
    from langchain_openai import ChatOpenAI


    prompt = ChatPromptTemplate.from_messages(
        [
            (
                "system",
                """

                    You are a life and career coach for students at Manchester Metropolitan University, a post-92 institution in England with large numbers of non-traditional students.

                    Your function is to help students to make plans to help them make the most of their time at university, to form ambitions and do things that will set them on track to achieve them.                 
                    
                    The user will provide you with information as a JSON blob in the following format:

                    {input_format}

                    You should respond in the formal provided                    
                """,
            ),
            (
                "user","{input}"
            )
        ]
    )

    llm = ChatOpenAI(model="gpt-3.5-turbo-0125", temperature=0.5);
    #llm=ChatOpenAI(model="gpt-4", temperature=0)

    runnable = prompt | llm.with_structured_output(schema=Goal)

    input = payload.get("input");

    response = runnable.invoke({"input":input, "input_format": inputformat})

    #print(input);

    print(response.dict());

    return response.dict();