Skip to content

Commit e5526bd

Browse files
committed
update peer.py to use course wide api key rather than UMGPT
1 parent 073619d commit e5526bd

1 file changed

Lines changed: 22 additions & 24 deletions

File tree

  • bases/rsptx/web2py_server/applications/runestone/controllers

bases/rsptx/web2py_server/applications/runestone/controllers/peer.py

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import pandas as pd
2626
import redis
2727
from dateutil.parser import parse
28-
from rsptx.db.crud import fetch_lti_version
28+
from rsptx.db.crud import fetch_api_token, fetch_lti_version
2929
from rs_grading import _try_to_send_lti_grade
3030

3131
try:
@@ -861,7 +861,7 @@ def get_async_llm_reflection():
861861
"never clearly describe the final result of the code.\n"
862862
"never fully state what the program prints.\n"
863863
"keep reasoning partial or uncertain.\n"
864-
"use common misconceptions and focus on only one part of the code.\n"
864+
"use common misconceptions relating to the specific problem.\n"
865865
"refer to code loosely like 'that line' or 'the loop' or 'the head' or 'the print'.\n"
866866
"often hedge with uncertainty.\n"
867867
"never agree with the other student's interpretation even if it sounds correct.\n"
@@ -1010,43 +1010,41 @@ def send_lti_scores():
10101010

10111011

10121012

1013-
def _get_umgpt_settings():
1014-
api_key = os.environ.get("UMGPT_API_KEY", "").strip()
1015-
base_url = os.environ.get("UMGPT_BASE_URL", "").strip()
1016-
api_version = os.environ.get("UMGPT_API_VERSION", "").strip()
1017-
deployment = os.environ.get("UMGPT_DEPLOYMENT", "").strip()
1018-
organization = os.environ.get("UMGPT_ORG", "").strip()
1019-
return api_key, base_url, api_version, deployment, organization
1013+
def _get_course_openai_key():
1014+
try:
1015+
token_record = asyncio.get_event_loop().run_until_complete(
1016+
fetch_api_token(course_id=auth.user.course_id, provider="openai")
1017+
)
1018+
if token_record and token_record.token:
1019+
return token_record.token.strip()
1020+
except Exception:
1021+
logger.exception("Failed to fetch course-wide OpenAI token for peer LLM")
1022+
return ""
10201023

10211024
def _call_openai(messages):
10221025
"""
1023-
Minimal HTTP call to U-M GPT Toolkit (Azure OpenAI gateway). No SDK needed.
1026+
Minimal HTTP call using the instructor-provided course-wide OpenAI token.
10241027
messages: list of {role, content}
10251028
returns reply string
10261029
"""
1027-
api_key, base_url, api_version, deployment, organization = _get_umgpt_settings()
1028-
if not api_key or not base_url or not api_version or not deployment:
1029-
return None
1030+
api_key = _get_course_openai_key()
1031+
if not api_key:
1032+
raise Exception("missing api key")
10301033

1031-
url = (
1032-
f"{base_url.rstrip('/')}/openai/deployments/{deployment}/chat/completions"
1033-
f"?api-version={api_version}"
1034-
)
1034+
model = os.environ.get("PI_OPENAI_MODEL", "gpt-4o-mini").strip() or "gpt-4o-mini"
1035+
url = "https://api.openai.com/v1/chat/completions"
10351036
headers = {
1036-
"api-key": api_key,
1037+
"Authorization": f"Bearer {api_key}",
10371038
"Content-Type": "application/json",
10381039
}
1039-
if organization:
1040-
headers["OpenAI-Organization"] = organization
10411040
payload = {
1041+
"model": model,
10421042
"messages": messages,
10431043
"temperature": 0.4,
10441044
"max_tokens": 300,
10451045
}
10461046
resp = requests.post(url, headers=headers, data=json.dumps(payload), timeout=30)
1047-
logger.warning(
1048-
f"UMGPT CALL | deployment={deployment} | api_version={api_version}"
1049-
)
1047+
logger.warning(f"PEER LLM CALL | provider=openai-course-token | model={model}")
10501048
resp.raise_for_status()
10511049
data = resp.json()
1052-
return data["choices"][0]["message"]["content"].strip()
1050+
return data["choices"][0]["message"]["content"].strip()

0 commit comments

Comments
 (0)