Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI, Request | |
| import openai | |
| from pydantic import BaseModel | |
| import os | |
| # Define a Pydantic model for the input | |
| class RoadmapLogs(BaseModel): | |
| roadmap_logs: str | |
| # Initialize the FastAPI app | |
| app = FastAPI() | |
| # Define OpenAI API key (replace this with your method to fetch the key, such as an environment variable) | |
| openai.api_key = os.getenv('RaghavOPENAIKey') | |
| # Define the prompt and system role | |
| role = """You are an expert assistant who analyzes student performance on their study roadmap and provides insights based on their behavior. | |
| If the student has rushed through tasks, make them feel guilty. If they have done a good job, appreciate them but still remind them about deep learning and mastery. | |
| MAKE SURE YOU VERY STRICTLY FOLLOW THE OUTPUT STRUCTURE THAT IS 'AI Insight Heading:AI Insight Description'""" | |
| async def generate_insight(request: RoadmapLogs): | |
| prompt = f""" I am giving you some logs for a roadmap feature {request.roadmap_logs}. Your task is to analyze them and answer my queries. | |
| 'Checked' means the task is completed. | |
| Make sure to include the topic details that they have left out. | |
| Always start with a rhetorical question in the case of negative feedback and a compliment in case of positive feedback. | |
| Make sure to instill a sense of guilt in them while also acting as a benevolent guide. | |
| Make sure the insight reflects their approach, score and behavior accordingly. | |
| If you dont recieve any logs then motivate the user. | |
| If you dont revive any logs and just roadmap structure that means the user has not done any tasks it doesnt mean that he/she has completed all of them it means they have not done even a single task. | |
| MAKE SURE YOU DONT GIVE ANY THING ELSE IN THE OUTPUT APART FROM THE STRUCTURE. | |
| MAKE SURE YOU THAT YOU GIVE ME ONLY TWO THINGS 1) AI Insight Heading and 2) The AI Insight description. | |
| MAKE SURE YOU FOLLOW THE FOLLOWING OUTPUT STRCUTURE: | |
| The heading should be a very short 2,3 line summary of the description | |
| output_structure: | |
| '"AI Insight Heading":"AI Insight Description"' | |
| """ | |
| # Call OpenAI API | |
| response = openai.ChatCompletion.create( | |
| model="gpt-4o-mini", | |
| messages=[ | |
| {"role": "system", "content": role}, | |
| {"role": "user", "content": prompt} | |
| ] | |
| ) | |
| # Extract and return the insight | |
| answer = response['choices'][0]['message']['content'].strip() | |
| return {"AI_Insight": answer} | |