|
2 | 2 |
|
3 | 3 | from typing import TYPE_CHECKING, Any, cast |
4 | 4 |
|
| 5 | +from pydantic import BaseModel |
| 6 | + |
5 | 7 | from any_llm.any_llm import AnyLLM |
6 | | -from any_llm.utils.instructor import _convert_instructor_response |
7 | 8 |
|
8 | 9 | MISSING_PACKAGES_ERROR = None |
9 | 10 | try: |
10 | | - import instructor |
11 | 11 | import together |
12 | 12 |
|
13 | 13 | from .utils import ( |
@@ -55,10 +55,19 @@ class TogetherProvider(AnyLLM): |
55 | 55 | @staticmethod |
56 | 56 | def _convert_completion_params(params: CompletionParams, **kwargs: Any) -> dict[str, Any]: |
57 | 57 | """Convert CompletionParams to kwargs for Together API.""" |
58 | | - # Together does not support providing reasoning effort |
59 | 58 | converted_params = params.model_dump(exclude_none=True, exclude={"model_id", "messages", "response_format"}) |
60 | 59 | if converted_params.get("reasoning_effort") == "auto": |
61 | 60 | converted_params.pop("reasoning_effort") |
| 61 | + if ( |
| 62 | + params.response_format is not None |
| 63 | + and isinstance(params.response_format, type) |
| 64 | + and issubclass(params.response_format, BaseModel) |
| 65 | + ): |
| 66 | + converted_params["response_format"] = { |
| 67 | + "type": "json_schema", |
| 68 | + "schema": params.response_format.model_json_schema(), |
| 69 | + } |
| 70 | + |
62 | 71 | converted_params.update(kwargs) |
63 | 72 | return converted_params |
64 | 73 |
|
@@ -124,24 +133,6 @@ async def _acompletion( |
124 | 133 | params: CompletionParams, |
125 | 134 | **kwargs: Any, |
126 | 135 | ) -> ChatCompletion | AsyncIterator[ChatCompletionChunk]: |
127 | | - """Make the API call to Together AI with instructor support for structured outputs.""" |
128 | | - |
129 | | - if params.response_format: |
130 | | - instructor_client = instructor.patch(self.client, mode=instructor.Mode.JSON) # type: ignore [call-overload] |
131 | | - |
132 | | - instructor_response = await instructor_client.chat.completions.create( |
133 | | - model=params.model_id, |
134 | | - messages=cast("Any", params.messages), |
135 | | - response_model=params.response_format, |
136 | | - **params.model_dump( |
137 | | - exclude_none=True, |
138 | | - exclude={"model_id", "messages", "reasoning_effort", "response_format"}, |
139 | | - ), |
140 | | - **kwargs, |
141 | | - ) |
142 | | - |
143 | | - return _convert_instructor_response(instructor_response, params.model_id, self.PROVIDER_NAME) |
144 | | - |
145 | 136 | completion_kwargs = self._convert_completion_params(params, **kwargs) |
146 | 137 |
|
147 | 138 | if params.stream: |
|
0 commit comments