|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from functools import cached_property |
| 4 | +from typing import Any, Iterable, Mapping, cast |
| 5 | + |
| 6 | +import httpx |
| 7 | +from openai import AsyncOpenAI, BaseModel, _legacy_response |
| 8 | +from openai._base_client import make_request_options |
| 9 | +from openai._resource import AsyncAPIResource |
| 10 | +from openai._response import async_to_streamed_response_wrapper |
| 11 | +from openai._types import Body, Headers, NotGiven, Query, not_given |
| 12 | +from openai.resources.models import AsyncModels |
| 13 | +from openai.types import Model |
| 14 | +from openai.types.chat.chat_completion import Choice |
| 15 | +from openai.types.completion_usage import CompletionUsage |
| 16 | + |
| 17 | +from art.types import MessageOrChoice, MessagesAndChoices, Tools |
| 18 | + |
| 19 | +ParsedMessageOrChoice = Choice | dict[str, Any] |
| 20 | +ParsedMessagesAndChoices = list[ParsedMessageOrChoice] |
| 21 | + |
| 22 | + |
| 23 | +def _message_or_choice_to_dict(message_or_choice: MessageOrChoice) -> dict[str, Any]: |
| 24 | + if isinstance(message_or_choice, dict): |
| 25 | + return cast(dict[str, Any], message_or_choice) |
| 26 | + return cast(dict[str, Any], message_or_choice.to_dict()) |
| 27 | + |
| 28 | + |
| 29 | +class MessagesAndChoicesWithLogprobs(BaseModel): |
| 30 | + messages_and_choices: ParsedMessagesAndChoices |
| 31 | + usages: list[CompletionUsage] |
| 32 | + |
| 33 | + |
| 34 | +class TinkerAsyncModels(AsyncModels): |
| 35 | + @cached_property |
| 36 | + def with_raw_response(self) -> "TinkerAsyncModelsWithRawResponse": |
| 37 | + return TinkerAsyncModelsWithRawResponse(self) |
| 38 | + |
| 39 | + @cached_property |
| 40 | + def with_streaming_response(self) -> "TinkerAsyncModelsWithStreamingResponse": |
| 41 | + return TinkerAsyncModelsWithStreamingResponse(self) |
| 42 | + |
| 43 | + async def put( |
| 44 | + self, |
| 45 | + model: str, |
| 46 | + *, |
| 47 | + target: str, |
| 48 | + extra_headers: Headers | None = None, |
| 49 | + extra_query: Query | None = None, |
| 50 | + extra_body: Body | None = None, |
| 51 | + timeout: float | httpx.Timeout | None | NotGiven = not_given, |
| 52 | + ) -> Model: |
| 53 | + if not model: |
| 54 | + raise ValueError( |
| 55 | + f"Expected a non-empty value for `model` but received {model!r}" |
| 56 | + ) |
| 57 | + |
| 58 | + return await self._put( |
| 59 | + f"/models/{model}", |
| 60 | + body={"target": target}, |
| 61 | + options=make_request_options( |
| 62 | + extra_headers=extra_headers, |
| 63 | + extra_query=extra_query, |
| 64 | + extra_body=extra_body, |
| 65 | + timeout=timeout, |
| 66 | + ), |
| 67 | + cast_to=Model, |
| 68 | + ) |
| 69 | + |
| 70 | + |
| 71 | +class TinkerAsyncModelsWithRawResponse: |
| 72 | + def __init__(self, models: TinkerAsyncModels) -> None: |
| 73 | + self._models = models |
| 74 | + |
| 75 | + self.put = _legacy_response.async_to_raw_response_wrapper(models.put) |
| 76 | + self.retrieve = _legacy_response.async_to_raw_response_wrapper(models.retrieve) |
| 77 | + self.list = _legacy_response.async_to_raw_response_wrapper(models.list) |
| 78 | + self.delete = _legacy_response.async_to_raw_response_wrapper(models.delete) |
| 79 | + |
| 80 | + |
| 81 | +class TinkerAsyncModelsWithStreamingResponse: |
| 82 | + def __init__(self, models: TinkerAsyncModels) -> None: |
| 83 | + self._models = models |
| 84 | + |
| 85 | + self.put = async_to_streamed_response_wrapper(models.put) |
| 86 | + self.retrieve = async_to_streamed_response_wrapper(models.retrieve) |
| 87 | + self.list = async_to_streamed_response_wrapper(models.list) |
| 88 | + self.delete = async_to_streamed_response_wrapper(models.delete) |
| 89 | + |
| 90 | + |
| 91 | +class TinkerAsyncMessagesAndChoices(AsyncAPIResource): |
| 92 | + @cached_property |
| 93 | + def with_raw_response(self) -> "TinkerAsyncMessagesAndChoicesWithRawResponse": |
| 94 | + return TinkerAsyncMessagesAndChoicesWithRawResponse(self) |
| 95 | + |
| 96 | + @cached_property |
| 97 | + def with_streaming_response( |
| 98 | + self, |
| 99 | + ) -> "TinkerAsyncMessagesAndChoicesWithStreamingResponse": |
| 100 | + return TinkerAsyncMessagesAndChoicesWithStreamingResponse(self) |
| 101 | + |
| 102 | + async def with_logprobs( |
| 103 | + self, |
| 104 | + messages_and_choices: MessagesAndChoices, |
| 105 | + *, |
| 106 | + models: Iterable[str], |
| 107 | + model_aliases: Mapping[str, str] | None = None, |
| 108 | + tools: Tools | None, |
| 109 | + extra_headers: Headers | None = None, |
| 110 | + extra_query: Query | None = None, |
| 111 | + extra_body: Body | None = None, |
| 112 | + timeout: float | httpx.Timeout | None | NotGiven = not_given, |
| 113 | + ) -> MessagesAndChoicesWithLogprobs: |
| 114 | + return await self._post( |
| 115 | + "/messages_and_choices/with_logprobs", |
| 116 | + body={ |
| 117 | + "messages_and_choices": [ |
| 118 | + _message_or_choice_to_dict(item) for item in messages_and_choices |
| 119 | + ], |
| 120 | + "models": list(models), |
| 121 | + "model_aliases": dict(model_aliases or {}), |
| 122 | + "tools": tools, |
| 123 | + }, |
| 124 | + options=make_request_options( |
| 125 | + extra_headers=extra_headers, |
| 126 | + extra_query=extra_query, |
| 127 | + extra_body=extra_body, |
| 128 | + timeout=timeout, |
| 129 | + ), |
| 130 | + cast_to=MessagesAndChoicesWithLogprobs, |
| 131 | + ) |
| 132 | + |
| 133 | + |
| 134 | +class TinkerAsyncMessagesAndChoicesWithRawResponse: |
| 135 | + def __init__(self, messages_and_choices: TinkerAsyncMessagesAndChoices) -> None: |
| 136 | + self._messages_and_choices = messages_and_choices |
| 137 | + |
| 138 | + self.with_logprobs = _legacy_response.async_to_raw_response_wrapper( |
| 139 | + messages_and_choices.with_logprobs |
| 140 | + ) |
| 141 | + |
| 142 | + |
| 143 | +class TinkerAsyncMessagesAndChoicesWithStreamingResponse: |
| 144 | + def __init__(self, messages_and_choices: TinkerAsyncMessagesAndChoices) -> None: |
| 145 | + self._messages_and_choices = messages_and_choices |
| 146 | + |
| 147 | + self.with_logprobs = async_to_streamed_response_wrapper( |
| 148 | + messages_and_choices.with_logprobs |
| 149 | + ) |
| 150 | + |
| 151 | + |
| 152 | +class TinkerAsyncOpenAI(AsyncOpenAI): |
| 153 | + @cached_property |
| 154 | + def models(self) -> TinkerAsyncModels: |
| 155 | + return TinkerAsyncModels(self) |
| 156 | + |
| 157 | + @cached_property |
| 158 | + def messages_and_choices(self) -> TinkerAsyncMessagesAndChoices: |
| 159 | + return TinkerAsyncMessagesAndChoices(self) |
0 commit comments