11import asyncio
2- from typing import List , Optional
2+ import warnings
3+ from typing import List
34
45from ldai .models import AICompletionConfig , LDMessage
56from ldai .providers .model_runner import ModelRunner
6- from ldai .providers .types import JudgeResult , ModelResponse
7+ from ldai .providers .types import JudgeResult , ManagedResult , ModelResponse
78from ldai .tracker import LDAIConfigTracker
89
910
@@ -25,17 +26,62 @@ def __init__(
2526 self ._model_runner = model_runner
2627 self ._messages : List [LDMessage ] = []
2728
28- async def invoke (self , prompt : str ) -> ModelResponse :
29+ async def run (self , prompt : str ) -> ManagedResult :
2930 """
30- Invoke the model with a prompt string.
31+ Run the model with a prompt string.
3132
3233 Appends the prompt to the conversation history, prepends any
3334 system messages from the config, delegates to the runner, and
3435 appends the response to the history.
3536
37+ :param prompt: The user prompt to send to the model
38+ :return: ManagedResult containing the model's response, metric summary,
39+ and an optional evaluations task
40+ """
41+ tracker = self ._ai_config .create_tracker ()
42+
43+ user_message = LDMessage (role = 'user' , content = prompt )
44+ self ._messages .append (user_message )
45+
46+ config_messages = self ._ai_config .messages or []
47+ all_messages = config_messages + self ._messages
48+
49+ response = await tracker .track_metrics_of_async (
50+ lambda result : result .metrics ,
51+ lambda : self ._model_runner .invoke_model (all_messages ),
52+ )
53+
54+ content = response .message .content
55+ input_text = '\r \n ' .join (m .content for m in self ._messages ) if self ._messages else ''
56+
57+ evaluations_task = self ._track_judge_results (tracker , input_text , content )
58+
59+ self ._messages .append (response .message )
60+
61+ return ManagedResult (
62+ content = content ,
63+ metrics = tracker .get_summary (),
64+ raw = getattr (response , 'raw' , None ),
65+ parsed = getattr (response , 'parsed' , None ),
66+ evaluations = evaluations_task ,
67+ )
68+
69+ async def invoke (self , prompt : str ) -> ModelResponse :
70+ """
71+ Invoke the model with a prompt string.
72+
73+ .. deprecated::
74+ Use :meth:`run` instead. This method will be removed in a future
75+ release once the migration to :class:`ManagedResult` is complete.
76+
3677 :param prompt: The user prompt to send to the model
3778 :return: ModelResponse containing the model's response and metrics
3879 """
80+ warnings .warn (
81+ "ManagedModel.invoke() is deprecated. Use run() instead." ,
82+ DeprecationWarning ,
83+ stacklevel = 2 ,
84+ )
3985 tracker = self ._ai_config .create_tracker ()
4086
4187 user_message = LDMessage (role = 'user' , content = prompt )
0 commit comments