Convert a LiteLLM model name string to a :class:LanguageModel callable.
The returned callable conforms to the LanguageModel protocol and
accepts a plain str prompt, a list[dict] chat-messages list, or
a multimodal messages list (with content arrays containing images).
Source code in gepa/optimize_anything.py
| def make_litellm_lm(model_name: str) -> LanguageModel:
"""Convert a LiteLLM model name string to a :class:`LanguageModel` callable.
The returned callable conforms to the ``LanguageModel`` protocol and
accepts a plain ``str`` prompt, a ``list[dict]`` chat-messages list, or
a multimodal messages list (with content arrays containing images).
"""
import litellm
def _lm(prompt: str | list[dict[str, Any]]) -> str:
if isinstance(prompt, str):
messages: list[dict[str, Any]] = [{"role": "user", "content": prompt}]
else:
messages = prompt
completion = litellm.completion(model=model_name, messages=messages)
return completion.choices[0].message.content # type: ignore[union-attr]
return _lm
|