Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ watsonx = [

together = [
"together",
"instructor",
]

sambanova = [
Expand Down
33 changes: 12 additions & 21 deletions src/any_llm/providers/together/together.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

from typing import TYPE_CHECKING, Any, cast

from pydantic import BaseModel

from any_llm.any_llm import AnyLLM
from any_llm.utils.instructor import _convert_instructor_response

MISSING_PACKAGES_ERROR = None
try:
import instructor
import together

from .utils import (
Expand Down Expand Up @@ -55,10 +55,19 @@ class TogetherProvider(AnyLLM):
@staticmethod
def _convert_completion_params(params: CompletionParams, **kwargs: Any) -> dict[str, Any]:
"""Convert CompletionParams to kwargs for Together API."""
# Together does not support providing reasoning effort
converted_params = params.model_dump(exclude_none=True, exclude={"model_id", "messages", "response_format"})
if converted_params.get("reasoning_effort") == "auto":
converted_params.pop("reasoning_effort")
if (
params.response_format is not None
and isinstance(params.response_format, type)
and issubclass(params.response_format, BaseModel)
):
converted_params["response_format"] = {
"type": "json_schema",
"schema": params.response_format.model_json_schema(),
}

converted_params.update(kwargs)
return converted_params

Expand Down Expand Up @@ -124,24 +133,6 @@ async def _acompletion(
params: CompletionParams,
**kwargs: Any,
) -> ChatCompletion | AsyncIterator[ChatCompletionChunk]:
"""Make the API call to Together AI with instructor support for structured outputs."""

if params.response_format:
instructor_client = instructor.patch(self.client, mode=instructor.Mode.JSON) # type: ignore [call-overload]

instructor_response = await instructor_client.chat.completions.create(
model=params.model_id,
messages=cast("Any", params.messages),
response_model=params.response_format,
**params.model_dump(
exclude_none=True,
exclude={"model_id", "messages", "reasoning_effort", "response_format"},
),
**kwargs,
)

return _convert_instructor_response(instructor_response, params.model_id, self.PROVIDER_NAME)

completion_kwargs = self._convert_completion_params(params, **kwargs)

if params.stream:
Expand Down