rise_ai / test3.py
markpeace's picture
messing
4b31779
#ESTABLISH THE SERVER
from flask import Flask,request
from dotenv import load_dotenv
# Initializing flask app
app = Flask(__name__)
load_dotenv()
@app.route("/", methods=['GET','POST'])
def index():
import os
from langchain.prompts import ChatPromptTemplate, HumanMessagePromptTemplate
from langchain.chat_models import ChatOpenAI
from langchain.output_parsers import PydanticOutputParser
from pydantic import BaseModel, Field
from typing import List
# Define a new Pydantic model with field descriptions and tailored for Twitter.
class FrontEndActions(BaseModel):
"""Structure to pass actions back to the frontend"""
text: str = Field(description="The text to display on the button")
type: str = Field(description="This should be a string that identifies the type of action. It can be one of: SuggestGoal, SuggestRiseActivity")
class ResponseFormat(BaseModel):
"""Final response to the question being asked"""
message: str = Field(description="The final answer to respond to the user")
chat_summary: str = Field(description="Summarise what the user has asked and how you have responded in this chat in a way that so that you can remember the conversation")
actions: List[FrontEndActions] = Field(description="List of suggested actions that should be passed back to the frontend to display. The use will click these to enact them. ")
tokens: int = Field(description="Count the number of used to produce the response")
# Instantiate the parser with the new model.
parser = PydanticOutputParser(pydantic_object=ResponseFormat)
# Update the prompt to match the new query and desired format.
prompt = ChatPromptTemplate(
messages=[
HumanMessagePromptTemplate.from_template(
"""
You are a coach supporting students at post-92 university in the UK. It's students are diverse, and many come from non-traditional backgrounds and minority ethnic groups. Some may have ambitions for particular careers, others may not - and many may not be confident or have the social and financial advantages to reach their goals.
Your purpose is to help students to set aims (long term ambitions), break them into goals (things they want to achieve during their time at university) and objectives (smart targets).
If a student has a sense of what they want to achieve, you should help them to create smart targets. If they don't, you should be reassuring that its ok not to have clear goals yet, but help them to reflect and form some ambitions. These could be career-oriented, or they could be about succeeding in, and making the most of, their university experience.
You should be assertive in opening up and guiding the conversation.
\n{format_instructions}\n{question}
"""
)
],
input_variables=["question"],
partial_variables={
"format_instructions": parser.get_format_instructions(),
},
)
chat_model = ChatOpenAI(
model="gpt-3.5-turbo",
openai_api_key=os.getenv("OPENAI_API_KEY"),
max_tokens=1000
)
# Generate the input using the updated prompt.
user_query = (
"""
I would like to be a teacher, can you give me some goals to achieve this?
"""
)
_input = prompt.format_prompt(question=user_query)
output = chat_model(_input.to_messages())
parsed = parser.parse(output.content)
return parsed.dict()