Skip to content

Commit 7a4c95f

Browse files
author
liuhuiqi.7
committed
feat(local cert): update
Change-Id: I3f2ca9dbc3a751f321469750a31f31d54bf6b29f
1 parent a53597c commit 7a4c95f

File tree

1 file changed

+27
-54
lines changed

1 file changed

+27
-54
lines changed

volcenginesdkarkruntime/resources/chat/completions.py

Lines changed: 27 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,31 @@
3333

3434
__all__ = ["Completions", "AsyncCompletions"]
3535

36+
37+
def _process_messages(messages: Iterable[ChatCompletionMessageParam],
38+
f: Callable[[str], str]):
39+
for message in messages:
40+
if message.get("content", None) is not None:
41+
current_content = message.get("content")
42+
if isinstance(current_content, str):
43+
message["content"] = f(current_content)
44+
elif isinstance(current_content, Iterable):
45+
for part in current_content:
46+
if part.get("type", None) == "text":
47+
part["text"] = f(part["text"])
48+
elif part.get("type", None) == "image_url":
49+
if part["image_url"]["url"].startswith('data:'):
50+
part["image_url"]["url"] = f(part["image_url"]["url"])
51+
else:
52+
warnings.warn("encryption is not supported for image url, "
53+
"please use base64 image if you want encryption")
54+
else:
55+
raise TypeError("encryption is not supported for content type {}".
56+
format(type(part)))
57+
else:
58+
raise TypeError("encryption is not supported for content type {}".
59+
format(type(message.get('content'))))
60+
3661
class Completions(SyncAPIResource):
3762
@cached_property
3863
def with_raw_response(self) -> CompletionsWithRawResponse:
@@ -42,38 +67,12 @@ def with_raw_response(self) -> CompletionsWithRawResponse:
4267
def with_streaming_response(self) -> CompletionsWithStreamingResponse:
4368
return CompletionsWithStreamingResponse(self)
4469

45-
def _process_messages(self, messages: Iterable[ChatCompletionMessageParam],
46-
f: Callable[[str], str]):
47-
for message in messages:
48-
if message.get("content", None) is not None:
49-
current_content = message.get("content")
50-
if isinstance(current_content, str):
51-
message["content"] = f(current_content)
52-
elif isinstance(current_content, Iterable):
53-
for part in current_content:
54-
if part.get("type", None) == "text":
55-
part["text"] = f(part["text"])
56-
elif part.get("type", None) == "image_url":
57-
if part["image_url"]["url"].startswith('data:'):
58-
part["image_url"]["url"] = f(part["image_url"]["url"])
59-
else:
60-
warnings.warn("encryption is not supported for image url, "
61-
"please use base64 image if you want encryption")
62-
else:
63-
raise TypeError("encryption is not supported for content type {}".
64-
format(type(part)))
65-
else:
66-
raise TypeError("encryption is not supported for content type {}".
67-
format(type(message.get('content'))))
68-
6970
def _encrypt(self, model: str, messages: Iterable[ChatCompletionMessageParam], extra_headers: Headers
7071
) -> tuple[bytes, bytes]:
7172
client = self._client._get_endpoint_certificate(model)
7273
_crypto_key, _crypto_nonce, session_token = client.generate_ecies_key_pair()
7374
extra_headers['X-Session-Token'] = session_token
74-
self._process_messages(messages, lambda x: client.encrypt_string_with_key(_crypto_key,
75-
_crypto_nonce,
76-
x))
75+
_process_messages(messages, lambda x: client.encrypt_string_with_key(_crypto_key, _crypto_nonce, x))
7776
return _crypto_key, _crypto_nonce
7877

7978
def _decrypt_chunk(self, key: bytes, nonce: bytes, resp: Stream[ChatCompletionChunk]) -> Iterator[ChatCompletionChunk]:
@@ -180,38 +179,12 @@ def with_raw_response(self) -> AsyncCompletionsWithRawResponse:
180179
def with_streaming_response(self) -> AsyncCompletionsWithStreamingResponse:
181180
return AsyncCompletionsWithStreamingResponse(self)
182181

183-
def _process_messages(self, messages: Iterable[ChatCompletionMessageParam],
184-
f: Callable[[str], str]):
185-
for message in messages:
186-
if message.get("content", None) is not None:
187-
current_content = message.get("content")
188-
if isinstance(current_content, str):
189-
message["content"] = f(current_content)
190-
elif isinstance(current_content, Iterable):
191-
for part in current_content:
192-
if part.get("type", None) == "text":
193-
part["text"] = f(part["text"])
194-
elif part.get("type", None) == "image_url":
195-
if part["image_url"]["url"].startswith('data:'):
196-
part["image_url"]["url"] = f(part["image_url"]["url"])
197-
else:
198-
warnings.warn("encryption is not supported for image url, "
199-
"please use base64 image if you want encryption")
200-
else:
201-
raise TypeError("encryption is not supported for content type {}".
202-
format(type(part)))
203-
else:
204-
raise TypeError("encryption is not supported for content type {}".
205-
format(type(message.get('content'))))
206-
207182
def _encrypt(self, model: str, messages: Iterable[ChatCompletionMessageParam], extra_headers: Headers
208183
) -> tuple[bytes, bytes]:
209184
client = self._client._get_endpoint_certificate(model)
210185
_crypto_key, _crypto_nonce, session_token = client.generate_ecies_key_pair()
211186
extra_headers['X-Session-Token'] = session_token
212-
self._process_messages(messages, lambda x: client.encrypt_string_with_key(_crypto_key,
213-
_crypto_nonce,
214-
x))
187+
_process_messages(messages, lambda x: client.encrypt_string_with_key(_crypto_key, _crypto_nonce, x))
215188
return _crypto_key, _crypto_nonce
216189

217190
async def _decrypt_chunk(self, key: bytes, nonce: bytes, resp: AsyncStream[ChatCompletionChunk]

0 commit comments

Comments
 (0)