Skip to content

Commit 3b8cbfe

Browse files
committed
use litellm for tutor
1 parent d7af090 commit 3b8cbfe

File tree

5 files changed

+27
-49
lines changed

5 files changed

+27
-49
lines changed

ai_chatbots/chatbots.py

Lines changed: 18 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,25 @@
1818
from langchain_core.messages import HumanMessage, SystemMessage
1919
from langchain_core.messages.ai import AIMessageChunk
2020
from langchain_core.tools.base import BaseTool
21-
from langchain_openai import ChatOpenAI
2221
from langgraph.checkpoint.base import BaseCheckpointSaver
2322
from langgraph.graph import MessagesState, StateGraph
2423
from langgraph.graph.graph import CompiledGraph
2524
from langgraph.prebuilt import ToolNode, create_react_agent, tools_condition
2625
from langgraph.prebuilt.chat_agent_executor import AgentState
27-
from openai import BadRequestError
28-
from typing_extensions import TypedDict
29-
30-
from ai_chatbots import tools
31-
from ai_chatbots.api import get_search_tool_metadata
32-
from ai_chatbots.models import TutorBotOutput
33-
from ai_chatbots.tools import get_video_transcript_chunk, search_content_files
3426
from open_learning_ai_tutor.message_tutor import message_tutor
3527
from open_learning_ai_tutor.tools import tutor_tools
3628
from open_learning_ai_tutor.utils import (
3729
json_to_intent_list,
3830
json_to_messages,
3931
tutor_output_to_json,
4032
)
33+
from openai import BadRequestError
34+
from typing_extensions import TypedDict
35+
36+
from ai_chatbots import tools
37+
from ai_chatbots.api import get_search_tool_metadata
38+
from ai_chatbots.models import TutorBotOutput
39+
from ai_chatbots.tools import get_video_transcript_chunk, search_content_files
4140

4241
log = logging.getLogger(__name__)
4342

@@ -473,28 +472,6 @@ def __init__( # noqa: PLR0913
473472
edx_module_id, block_siblings
474473
)
475474

476-
def get_llm(self, **kwargs) -> BaseChatModel:
477-
"""
478-
Return the LLM instance for the chatbot.
479-
Set it up to use a proxy, with required proxy kwargs, if applicable.
480-
"""
481-
llm = ChatOpenAI(
482-
model=f"{self.proxy_prefix}{self.model}",
483-
**(
484-
self.proxy.get_api_kwargs(
485-
base_url_key="base_url", api_key_key="openai_api_key"
486-
)
487-
if self.proxy
488-
else {}
489-
),
490-
**(self.proxy.get_additional_kwargs(self) if self.proxy else {}),
491-
**kwargs,
492-
)
493-
# Set the temperature if it's supported by the model
494-
if self.temperature and self.model not in settings.AI_UNSUPPORTED_TEMP_MODELS:
495-
llm.temperature = self.temperature
496-
return llm
497-
498475
async def get_tool_metadata(self) -> str:
499476
"""Return the metadata for the tool"""
500477
return json.dumps(
@@ -534,18 +511,18 @@ async def get_completion(
534511
response = ""
535512

536513
try:
537-
new_history, new_intent_history, new_assessment_history, metadata = (
538-
message_tutor(
539-
self.problem,
540-
self.problem_set,
541-
self.llm,
542-
[HumanMessage(content=message)],
543-
chat_history,
544-
assessment_history,
545-
intent_history,
546-
tools=tutor_tools,
547-
)
514+
new_history, new_intent_history, new_assessment_history = message_tutor(
515+
self.problem,
516+
self.problem_set,
517+
self.llm,
518+
[HumanMessage(content=message)],
519+
chat_history,
520+
assessment_history,
521+
intent_history,
522+
tools=tutor_tools,
548523
)
524+
525+
metadata = {"edx_module_id": self.edx_module_id, "tutor_model": self.model}
549526
json_output = tutor_output_to_json(
550527
new_history, new_intent_history, new_assessment_history, metadata
551528
)

ai_chatbots/chatbots_test.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,6 @@ async def test_tutor_get_completion(posthog_settings, mocker, mock_checkpointer)
554554
response_metadata={},
555555
),
556556
],
557-
{"tutor_model": "test_model"},
558557
)
559558
mocker.patch(
560559
"ai_chatbots.chatbots.get_problem_from_edx_block",
@@ -579,7 +578,9 @@ async def test_tutor_get_completion(posthog_settings, mocker, mock_checkpointer)
579578

580579
history = await get_history(thread_id)
581580
assert history.thread_id == thread_id
582-
assert history.chat_json == tutor_output_to_json(*output)
581+
metadata = {"edx_module_id": "block1", "tutor_model": chatbot.model}
582+
583+
assert history.chat_json == tutor_output_to_json(*output, metadata)
583584
mock_posthog.Posthog.return_value.capture.assert_called_once_with(
584585
"anonymous",
585586
event="TUTOR_JOB",

main/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ def get_all_config_keys():
609609
"AI_DEFAULT_RECOMMENDATION_MODEL", AI_DEFAULT_MODEL
610610
)
611611
AI_DEFAULT_SYLLABUS_MODEL = get_string("AI_DEFAULT_SYLLABUS_MODEL", AI_DEFAULT_MODEL)
612-
AI_DEFAULT_TUTOR_MODEL = get_string("AI_DEFAULT_TUTOR_MODEL", "gpt-4o")
612+
AI_DEFAULT_TUTOR_MODEL = get_string("AI_DEFAULT_TUTOR_MODEL", AI_DEFAULT_MODEL)
613613
AI_DEFAULT_VIDEO_GPT_MODEL = get_string("AI_DEFAULT_VIDEO_GPT_MODEL", AI_DEFAULT_MODEL)
614614
AI_DEFAULT_TEMPERATURE = get_float(name="AI_DEFAULT_TEMPERATURE", default=0.1)
615615
OPENAI_API_KEY = get_string(name="OPENAI_API_KEY", default="")

poetry.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ opentelemetry-instrumentation-celery = ">=0.52b0"
6464
opentelemetry-instrumentation-requests = ">=0.52b0"
6565
opentelemetry-exporter-otlp = ">=1.31.0"
6666
django-filter = "^25.1"
67-
open-learning-ai-tutor = "^0.0.6"
67+
open-learning-ai-tutor = "^0.0.7"
6868

6969
[tool.poetry.group.dev.dependencies]
7070
bpython = "^0.25"

0 commit comments

Comments
 (0)