Skip to content

Commit 1fd4edc

Browse files
author
liuhuiqi.7
committed
feat(ark e2e): support decrypt
Change-Id: I7b07a4aaefb9cdadcaf6153435f5f9e0751290b5
1 parent 51ba7fc commit 1fd4edc

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

volcenginesdkarkruntime/_utils/_key_agreement.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,17 @@ def encrypt_string(self, plaintext: str) -> tuple[bytes, bytes, str, str]:
8686
return key, nonce, token, ciphertext
8787

8888
def encrypt_string_with_key(self, key: bytes, nonce: bytes, plaintext: str) -> str:
89-
"""encrypt_string encrypt plaintext with ECIES DH protocol
89+
"""encrypt_string_with_key encrypt plaintext with ECIES DH protocol
9090
"""
9191
# Encrypt message using AES-GCM
9292
ciphertext = aes_gcm_encrypt_base64_string(key, nonce, plaintext)
9393
return ciphertext
94+
95+
def decrypt_string_with_key(self, key: bytes, nonce: bytes, ciphertext: str) -> str:
96+
"""decrypt_string_with_key decrypt ciphertext with ECIES DH protocol
97+
"""
98+
# Decrypt message using AES-GCM
99+
return aes_gcm_decrypt_base64_string(key, nonce, ciphertext)
94100

95101
def generate_ecies_key_pair(self) -> tuple[bytes, bytes, str]:
96102
"""generate_ecies_key_pair generate ECIES key pair

volcenginesdkarkruntime/resources/chat/completions.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,22 @@ def _process_messages(self, messages: Iterable[ChatCompletionMessageParam],
6666

6767
def _encrypt(self, model: str, messages: Iterable[ChatCompletionMessageParam], extra_headers: Headers):
6868
client = self._client._get_endpoint_certificate(model)
69+
self._ka_client = client
6970
self._crypto_key, self._crypto_nonce, session_token = client.generate_ecies_key_pair()
7071
extra_headers['X-Session-Token'] = session_token
7172
self._process_messages(messages, lambda x: client.encrypt_string_with_key(self._crypto_key,
7273
self._crypto_nonce,
7374
x))
7475

76+
def _decrypt(self, completion: ChatCompletion):
77+
if completion.choices is not None:
78+
for choice in completion.choices:
79+
if choice.message.content is not None:
80+
if isinstance(choice.message.content, str):
81+
choice.message.content = self._ka_client.decrypt_string_with_key(self._crypto_key,
82+
self._crypto_nonce,
83+
choice.message.content)
84+
7585
@with_sts_token
7686
def create(
7787
self,
@@ -104,9 +114,8 @@ def create(
104114
) -> ChatCompletion | Stream[ChatCompletionChunk]:
105115
if is_encrypt:
106116
self._encrypt(model, messages, extra_headers)
107-
print(messages)
108117

109-
return self._post(
118+
resp = self._post(
110119
"/chat/completions",
111120
body={
112121
"messages": messages,
@@ -141,6 +150,10 @@ def create(
141150
stream_cls=Stream[ChatCompletionChunk],
142151
)
143152

153+
if is_encrypt:
154+
return self._decrypt(resp)
155+
return resp
156+
144157

145158
class AsyncCompletions(AsyncAPIResource):
146159
@cached_property

0 commit comments

Comments
 (0)