11import asyncio
2- from typing import List , Optional
2+ from typing import List
33
44from ldai import log
55from ldai .models import AICompletionConfig , LDMessage
6- from ldai .providers .model_runner import ModelRunner
7- from ldai .providers .types import JudgeResult , ModelResponse
6+ from ldai .providers .runner import Runner
7+ from ldai .providers .types import JudgeResult , ManagedResult
88from ldai .tracker import LDAIConfigTracker
99
1010
1111class ManagedModel :
1212 """
1313 LaunchDarkly managed wrapper for AI model invocations.
1414
15- Holds a ModelRunner . Handles conversation management, judge evaluation
15+ Holds a Runner . Handles conversation management, judge evaluation
1616 dispatch, and tracking automatically via ``create_tracker()``.
1717 Obtain an instance via ``LDAIClient.create_model()``.
1818 """
1919
2020 def __init__ (
2121 self ,
2222 ai_config : AICompletionConfig ,
23- model_runner : ModelRunner ,
23+ model_runner : Runner ,
2424 ):
2525 self ._ai_config = ai_config
2626 self ._model_runner = model_runner
2727 self ._messages : List [LDMessage ] = []
2828
29- async def invoke (self , prompt : str ) -> ModelResponse :
29+ async def run (self , prompt : str ) -> ManagedResult :
3030 """
31- Invoke the model with a prompt string.
31+ Run the model with a prompt string.
3232
3333 Appends the prompt to the conversation history, prepends any
3434 system messages from the config, delegates to the runner, and
3535 appends the response to the history.
3636
3737 :param prompt: The user prompt to send to the model
38- :return: ModelResponse containing the model's response and metrics
38+ :return: ManagedResult containing the model's response, metric summary,
39+ and an optional evaluations task
3940 """
4041 tracker = self ._ai_config .create_tracker ()
4142
@@ -45,17 +46,26 @@ async def invoke(self, prompt: str) -> ModelResponse:
4546 config_messages = self ._ai_config .messages or []
4647 all_messages = config_messages + self ._messages
4748
48- response = await tracker .track_metrics_of_async (
49- lambda result : result .metrics ,
50- lambda : self ._model_runner .invoke_model (all_messages ),
49+ result = await tracker .track_metrics_of_async (
50+ lambda r : r .metrics ,
51+ lambda : self ._model_runner .run (all_messages ),
5152 )
5253
54+ assistant_message = LDMessage (role = 'assistant' , content = result .content )
55+
5356 input_text = '\r \n ' .join (m .content for m in self ._messages ) if self ._messages else ''
54- output_text = response .message .content
55- response .evaluations = self ._track_judge_results (tracker , input_text , output_text )
5657
57- self ._messages .append (response .message )
58- return response
58+ evaluations_task = self ._track_judge_results (tracker , input_text , result .content )
59+
60+ self ._messages .append (assistant_message )
61+
62+ return ManagedResult (
63+ content = result .content ,
64+ metrics = tracker .get_summary (),
65+ raw = result .raw ,
66+ parsed = result .parsed ,
67+ evaluations = evaluations_task ,
68+ )
5969
6070 def _track_judge_results (
6171 self ,
@@ -98,11 +108,11 @@ def append_messages(self, messages: List[LDMessage]) -> None:
98108 """
99109 self ._messages .extend (messages )
100110
101- def get_model_runner (self ) -> ModelRunner :
111+ def get_model_runner (self ) -> Runner :
102112 """
103- Return the underlying ModelRunner for advanced use.
113+ Return the underlying runner for advanced use.
104114
105- :return: The ModelRunner instance.
115+ :return: The Runner instance.
106116 """
107117 return self ._model_runner
108118
0 commit comments