mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
feat: org wide ai features check
This commit is contained in:
parent
de93d56945
commit
077c26ce15
24 changed files with 573 additions and 163 deletions
|
|
@ -2,6 +2,9 @@ from uuid import uuid4
|
|||
from fastapi import Depends, HTTPException, Request
|
||||
from requests import session
|
||||
from sqlmodel import Session, select
|
||||
from src.db.organization_config import OrganizationConfig
|
||||
from src.db.organizations import Organization
|
||||
from src.services.ai.utils import check_limits_and_config, count_ai_ask
|
||||
from src.db.courses import Course, CourseRead
|
||||
from src.core.events.database import get_db_session
|
||||
from src.db.users import PublicUser
|
||||
|
|
@ -29,6 +32,7 @@ def ai_start_activity_chat_session(
|
|||
"""
|
||||
Start a new AI Chat session with a Course Activity
|
||||
"""
|
||||
|
||||
# Get the Activity
|
||||
statement = select(Activity).where(
|
||||
Activity.activity_uuid == chat_session_object.activity_uuid
|
||||
|
|
@ -46,6 +50,14 @@ def ai_start_activity_chat_session(
|
|||
course = db_session.exec(statement).first()
|
||||
course = CourseRead.from_orm(course)
|
||||
|
||||
# Get the Organization
|
||||
statement = select(Organization).where(Organization.id == course.org_id)
|
||||
org = db_session.exec(statement).first()
|
||||
|
||||
# Check limits and usage
|
||||
check_limits_and_config(db_session, org) # type: ignore
|
||||
count_ai_ask(org, "increment") # type: ignore
|
||||
|
||||
if not activity:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
|
|
@ -61,28 +73,48 @@ def ai_start_activity_chat_session(
|
|||
structured, course, activity
|
||||
)
|
||||
|
||||
# Get Activity Organization
|
||||
statement = select(Organization).where(Organization.id == course.org_id)
|
||||
org = db_session.exec(statement).first()
|
||||
|
||||
# Get Organization Config
|
||||
statement = select(OrganizationConfig).where(
|
||||
OrganizationConfig.org_id == org.id # type: ignore
|
||||
)
|
||||
result = db_session.exec(statement)
|
||||
org_config = result.first()
|
||||
|
||||
org_config = OrganizationConfig.from_orm(org_config)
|
||||
embeddings = org_config.config["AIConfig"]["embeddings"]
|
||||
ai_model = org_config.config["AIConfig"]["ai_model"]
|
||||
|
||||
chat_session = get_chat_session_history()
|
||||
|
||||
message = "You are a helpful Education Assistant, and you are helping a student with the associated Course. "
|
||||
message += "Use the available tools to get context about this question even if the question is not specific enough."
|
||||
message += "For context, this is the Course name :"
|
||||
message += course.name
|
||||
message += " and this is the Lecture name :"
|
||||
message += activity.name
|
||||
message += "."
|
||||
message += "Use your knowledge to help the student if the context is not enough."
|
||||
|
||||
response = ask_ai(
|
||||
chat_session_object.message,
|
||||
chat_session['message_history'],
|
||||
chat_session["message_history"],
|
||||
ai_friendly_text,
|
||||
"You are a helpful Education Assistant, and you are helping a student with the associated Course. "
|
||||
"Use the available tools to get context about this question even if the question is not specific enough."
|
||||
"For context, this is the Course name :"
|
||||
+ course.name
|
||||
+ " and this is the Lecture name :"
|
||||
+ activity.name
|
||||
+ "."
|
||||
"Use your knowledge to help the student.",
|
||||
message,
|
||||
embeddings,
|
||||
ai_model,
|
||||
)
|
||||
|
||||
return ActivityAIChatSessionResponse(
|
||||
aichat_uuid=chat_session['aichat_uuid'],
|
||||
aichat_uuid=chat_session["aichat_uuid"],
|
||||
activity_uuid=activity.activity_uuid,
|
||||
message=response["output"],
|
||||
)
|
||||
|
||||
|
||||
def ai_send_activity_chat_message(
|
||||
request: Request,
|
||||
chat_session_object: SendActivityAIChatMessage,
|
||||
|
|
@ -109,6 +141,14 @@ def ai_send_activity_chat_message(
|
|||
course = db_session.exec(statement).first()
|
||||
course = CourseRead.from_orm(course)
|
||||
|
||||
# Get the Organization
|
||||
statement = select(Organization).where(Organization.id == course.org_id)
|
||||
org = db_session.exec(statement).first()
|
||||
|
||||
# Check limits and usage
|
||||
check_limits_and_config(db_session, org) # type: ignore
|
||||
count_ai_ask(org, "increment") # type: ignore
|
||||
|
||||
if not activity:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
|
|
@ -116,7 +156,7 @@ def ai_send_activity_chat_message(
|
|||
)
|
||||
|
||||
# Get Activity Content Blocks
|
||||
content = activity.content
|
||||
content = activity.content
|
||||
|
||||
# Serialize Activity Content Blocks to a text comprehensible by the AI
|
||||
structured = structure_activity_content_by_type(content)
|
||||
|
|
@ -124,24 +164,43 @@ def ai_send_activity_chat_message(
|
|||
structured, course, activity
|
||||
)
|
||||
|
||||
# Get Activity Organization
|
||||
statement = select(Organization).where(Organization.id == course.org_id)
|
||||
org = db_session.exec(statement).first()
|
||||
|
||||
# Get Organization Config
|
||||
statement = select(OrganizationConfig).where(
|
||||
OrganizationConfig.org_id == org.id # type: ignore
|
||||
)
|
||||
result = db_session.exec(statement)
|
||||
org_config = result.first()
|
||||
|
||||
org_config = OrganizationConfig.from_orm(org_config)
|
||||
embeddings = org_config.config["AIConfig"]["embeddings"]
|
||||
ai_model = org_config.config["AIConfig"]["ai_model"]
|
||||
|
||||
chat_session = get_chat_session_history(chat_session_object.aichat_uuid)
|
||||
|
||||
message = "You are a helpful Education Assistant, and you are helping a student with the associated Course. "
|
||||
message += "Use the available tools to get context about this question even if the question is not specific enough."
|
||||
message += "For context, this is the Course name :"
|
||||
message += course.name
|
||||
message += " and this is the Lecture name :"
|
||||
message += activity.name
|
||||
message += "."
|
||||
message += "Use your knowledge to help the student if the context is not enough."
|
||||
|
||||
response = ask_ai(
|
||||
chat_session_object.message,
|
||||
chat_session['message_history'],
|
||||
chat_session["message_history"],
|
||||
ai_friendly_text,
|
||||
"You are a helpful Education Assistant, and you are helping a student with the associated Course. "
|
||||
"Use the available tools to get context about this question even if the question is not specific enough."
|
||||
"For context, this is the Course name :"
|
||||
+ course.name
|
||||
+ " and this is the Lecture name :"
|
||||
+ activity.name
|
||||
+ "."
|
||||
"Use your knowledge to help the student if the context is not enough.",
|
||||
message,
|
||||
embeddings,
|
||||
ai_model,
|
||||
)
|
||||
|
||||
return ActivityAIChatSessionResponse(
|
||||
aichat_uuid=chat_session['aichat_uuid'],
|
||||
aichat_uuid=chat_session["aichat_uuid"],
|
||||
activity_uuid=activity.activity_uuid,
|
||||
message=response["output"],
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue