-
-
Notifications
You must be signed in to change notification settings - Fork 655
/
utils.py
222 lines (182 loc) · 6.71 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
from __future__ import annotations
import inspect
import json
import yaml
import logging
from collections.abc import AsyncGenerator, Generator, Iterable
from typing import (
TYPE_CHECKING,
Any,
Callable,
Generic,
Protocol,
TypeVar,
)
from openai.types import CompletionUsage as OpenAIUsage
from openai.types.chat import (
ChatCompletion,
ChatCompletionMessage,
ChatCompletionMessageParam,
)
if TYPE_CHECKING:
from anthropic.types import Usage as AnthropicUsage
logger = logging.getLogger("instructor")
R_co = TypeVar("R_co", covariant=True)
T_Model = TypeVar("T_Model", bound="Response")
from enum import Enum
class Response(Protocol):
usage: OpenAIUsage | AnthropicUsage
class Provider(Enum):
OPENAI = "openai"
ANTHROPIC = "anthropic"
ANYSCALE = "anyscale"
TOGETHER = "together"
GROQ = "groq"
MISTRAL = "mistral"
COHERE = "cohere"
UNKNOWN = "unknown"
def get_provider(base_url: str) -> Provider:
if "anyscale" in str(base_url):
return Provider.ANYSCALE
elif "together" in str(base_url):
return Provider.TOGETHER
elif "anthropic" in str(base_url):
return Provider.ANTHROPIC
elif "groq" in str(base_url):
return Provider.GROQ
elif "openai" in str(base_url):
return Provider.OPENAI
elif "mistral" in str(base_url):
return Provider.MISTRAL
elif "cohere" in str(base_url):
return Provider.COHERE
return Provider.UNKNOWN
def extract_json_from_codeblock(content: str) -> str:
first_paren = content.find("{")
last_paren = content.rfind("}")
return content[first_paren : last_paren + 1]
def extract_json_from_yaml_codeblock(content: str) -> str:
yaml_start = content.find("```yaml")
if yaml_start != -1:
yaml_end = content.find("```", yaml_start + 7)
if yaml_end != -1:
yaml_string = yaml.safe_load(content[yaml_start + 7 : yaml_end].strip())
return json.dumps(yaml_string)
return ""
def extract_json_from_stream(chunks: Iterable[str]) -> Generator[str, None, None]:
capturing = False
brace_count = 0
for chunk in chunks:
for char in chunk:
if char == "{":
capturing = True
brace_count += 1
yield char
elif char == "}" and capturing:
brace_count -= 1
yield char
if brace_count == 0:
capturing = False
break # Cease yielding upon closing the current JSON object
elif capturing:
yield char
async def extract_json_from_stream_async(
chunks: AsyncGenerator[str, None],
) -> AsyncGenerator[str, None]:
capturing = False
brace_count = 0
async for chunk in chunks:
for char in chunk:
if char == "{":
capturing = True
brace_count += 1
yield char
elif char == "}" and capturing:
brace_count -= 1
yield char
if brace_count == 0:
capturing = False
break # Cease yielding upon closing the current JSON object
elif capturing:
yield char
def update_total_usage(
response: T_Model,
total_usage: OpenAIUsage | AnthropicUsage,
) -> T_Model | ChatCompletion:
response_usage = getattr(response, "usage", None)
if isinstance(response_usage, OpenAIUsage) and isinstance(total_usage, OpenAIUsage):
total_usage.completion_tokens += response_usage.completion_tokens or 0
total_usage.prompt_tokens += response_usage.prompt_tokens or 0
total_usage.total_tokens += response_usage.total_tokens or 0
response.usage = total_usage # Replace each response usage with the total usage
return response
# Anthropic usage.
try:
from anthropic.types import Usage as AnthropicUsage
if isinstance(response_usage, AnthropicUsage) and isinstance(
total_usage, AnthropicUsage
):
total_usage.input_tokens += response_usage.input_tokens or 0
total_usage.output_tokens += response_usage.output_tokens or 0
response.usage = total_usage
return response
except ImportError:
pass
logger.debug("No compatible response.usage found, token usage not updated.")
return response
def dump_message(message: ChatCompletionMessage) -> ChatCompletionMessageParam:
"""Dumps a message to a dict, to be returned to the OpenAI API.
Workaround for an issue with the OpenAI API, where the `tool_calls` field isn't allowed to be present in requests
if it isn't used.
"""
ret: ChatCompletionMessageParam = {
"role": message.role,
"content": message.content or "",
}
if hasattr(message, "tool_calls") and message.tool_calls is not None:
ret["tool_calls"] = message.model_dump()["tool_calls"]
if (
hasattr(message, "function_call")
and message.function_call is not None
and ret["content"]
):
ret["content"] += json.dumps(message.model_dump()["function_call"])
return ret
def is_async(func: Callable[..., Any]) -> bool:
"""Returns true if the callable is async, accounting for wrapped callables"""
is_coroutine = inspect.iscoroutinefunction(func)
while hasattr(func, "__wrapped__"):
func = func.__wrapped__ # type: ignore - dynamic
is_coroutine = is_coroutine or inspect.iscoroutinefunction(func)
return is_coroutine
def merge_consecutive_messages(messages: list[dict[str, Any]]) -> list[dict[str, Any]]:
# merge all consecutive user messages into a single message
new_messages: list[dict[str, Any]] = []
for message in messages:
new_content = message["content"]
if isinstance(new_content, str):
new_content = [{"type": "text", "text": new_content}]
if len(new_messages) > 0 and message["role"] == new_messages[-1]["role"]:
new_messages[-1]["content"].extend(new_content)
else:
new_messages.append(
{
"role": message["role"],
"content": new_content,
}
)
return new_messages
class classproperty(Generic[R_co]):
"""Descriptor for class-level properties.
Examples:
>>> from instructor.utils import classproperty
>>> class MyClass:
... @classproperty
... def my_property(cls):
... return cls
>>> assert MyClass.my_property
"""
def __init__(self, method: Callable[[Any], R_co]) -> None:
self.cproperty = method
def __get__(self, instance: object, cls: type[Any]) -> R_co:
return self.cproperty(cls)