Spaces:
Sleeping
Sleeping
Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request
|
| 2 |
+
import openai
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Define a Pydantic model for the input
|
| 7 |
+
class RoadmapLogs(BaseModel):
|
| 8 |
+
roadmap_logs: str
|
| 9 |
+
|
| 10 |
+
# Initialize the FastAPI app
|
| 11 |
+
app = FastAPI()
|
| 12 |
+
|
| 13 |
+
# Define OpenAI API key (replace this with your method to fetch the key, such as an environment variable)
|
| 14 |
+
openai.api_key = os.getenv('RaghavOPENAIKey')
|
| 15 |
+
|
| 16 |
+
# Define the prompt and system role
|
| 17 |
+
role = """You are an expert assistant who analyzes student performance on their study roadmap and provides insights based on their behavior.
|
| 18 |
+
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.
|
| 19 |
+
MAKE SURE YOU VERY STRICTLY FOLLOW THE OUTPUT STRUCTURE THAT IS 'AI Insight Heading:AI Insight Description'"""
|
| 20 |
+
|
| 21 |
+
@app.post("/generate_insight")
|
| 22 |
+
async def generate_insight(request: RoadmapLogs):
|
| 23 |
+
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.
|
| 24 |
+
'Checked' means the task is completed.
|
| 25 |
+
Make sure to include the topic details that they have left out.
|
| 26 |
+
Always start with a rhetorical question in the case of negative feedback and a compliment in case of positive feedback.
|
| 27 |
+
Make sure to instill a sense of guilt in them while also acting as a benevolent guide.
|
| 28 |
+
Make sure the insight reflects their approach, score and behavior accordingly.
|
| 29 |
+
MAKE SURE YOU THAT YOU GIVE ME ONLY TWO THINGS 1) AI Insight Heading and 2) The AI Insight description.
|
| 30 |
+
MAKE SURE YOU FOLLOW THE FOLLOWING OUTPUT STRCUTURE:
|
| 31 |
+
|
| 32 |
+
The heading should be a very short 2,3 line summary of the description
|
| 33 |
+
output_structure
|
| 34 |
+
'AI Insight Heading:AI Insight Description'
|
| 35 |
+
"""
|
| 36 |
+
# Call OpenAI API
|
| 37 |
+
response = openai.ChatCompletion.create(
|
| 38 |
+
model="gpt-4",
|
| 39 |
+
messages=[
|
| 40 |
+
{"role": "system", "content": role},
|
| 41 |
+
{"role": "user", "content": prompt}
|
| 42 |
+
]
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
# Extract and return the insight
|
| 46 |
+
answer = response['choices'][0]['message']['content'].strip()
|
| 47 |
+
return {"AI_Insight": answer}
|
| 48 |
+
|