-
Notifications
You must be signed in to change notification settings - Fork 0
/
LLM_api.py
382 lines (312 loc) · 14.3 KB
/
LLM_api.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import os
from http import HTTPStatus
import requests
import json
from openai import OpenAI
import time
from abc import ABC, abstractmethod
import copy
from typing import List, Dict, Tuple
# local LLM server
# LLM abstract class
class LLM(ABC):
def __init__(self, api_key, base_url, mode, model_name: str, translate_prompt):
if os.getenv("OPENAPI_API_KEY"):
self.api_key = os.getenv("OPENAPI_API_KEY")
else:
self.api_key = api_key
self.base_url = base_url
self.history_limit = 30
self.mode = "chat" if mode == "chat" else "completion"
self.model_name = model_name
self.translate_prompt = translate_prompt
def completion_translate(self):
pass
def chat_translate(self, transcribe_result, translate_prompt):
pass
def load(self):
pass
def batch_translate(self, transcribe_result):
pass
class Ollama(LLM):
"""
This a large language mode class for ollama local platform.
The specific calling api reference to https://github.com/ollama/ollama/blob/main/docs/api.md
"""
default_baseurl = "http://localhost:11434/api/chat"
def __init__(self, model_name, api_key, translate_prompt, base_url=default_baseurl, mode="chat", system_prompt=""):
super().__init__(api_key, base_url, mode, model_name, translate_prompt)
if self.mode == "chat":
if not system_prompt:
self.system_prompt = "you are a professional subtitle translator"
# self.translate_prompt=""
self.data = {
"model": f"{model_name}",
"messages": [
{
"role": "system",
"content": f"{self.system_prompt}",
}
],
"stream": False,
"keepalive": "5m",
# "options": {
# # "seed": 1000
# "temperature": 0.4
# }
}
def chat_translate(self, transcribe_result, translate_prompt):
translation(transcribe_result,
data=self.data,
base_url=self.base_url,
translate_prompt=translate_prompt,
)
def completion_translate(self):
pass
@staticmethod
def close_model(model_name, baseurl):
"""
By sending a package with empty message or set the "keepalive" to "0".
You can close the running model and retrieve the GPU memory
:return:
"""
data = {"model": model_name, "keep_alive": 0}
response = requests.post(baseurl, json=data)
print(response.content)
time.sleep(1)
# 32b model is unable to perform batch translation for unstable output format
def batch_translate(self, transcribe_result: list):
data = self.data
translate_prompt = self.translate_prompt
base_url = self.base_url
translate_prompt= "Please translate the following continuous 10 lines of English subtitles into Chinese, without any explanations. One sentence at each line"
# each we translate 10 line at once
for i in range(0, len(transcribe_result), 10):
tmp = ""
if len(data["messages"]) > 5:
data["messages"] = data["messages"][-2:]
sentences_list = [transcribe_result[j][2] for j in range(i, i+10)]
sentences_list = MessageFormatter.format_message_list(sentences_list)
tmp = "\n".join(sentences_list)
data["messages"].append({"role": "user", "content": translate_prompt + "\n" + tmp})
response = requests.post(f'{base_url}', json=data)
print("count: ", i)
content = json.loads(response.content)
if response.status_code == 200:
print(response.status_code)
message = content["message"]
answer = message["content"]
print(answer)
try:
ret_list = content["message"]["content"].split("\n\n")
if len(ret_list) == 10:
for j in range(len(ret_list)):
transcribe_result[i + j][2] = ret_list[j]
except KeyError as e:
print(e)
finally:
data["messages"].append(message)
else:
print(response.status_code)
message = content["message"]
data["messages"].append(message)
continue
class MessageFormatter:
def format_message_list(message_list: List)-> List:
"""
format the message to facilitate LLM translation, incluing captinlize the first alphabet.
add . or ? to the end of each sentence.
:param message_list:
:return:
"""
for i in range(len(message_list)):
message_list[i] = message_list[i].strip()
message_list[i] = message_list[i][0].capitalize() + message_list[i][1:]
# if (message_list[i][-1] == ","
# not in [".", "?", "!"]):
# message_list[i] += "."
return message_list
def translation(transcribe_result: list, data: dict, translate_prompt: str, base_url):
count = 1
for i in transcribe_result:
if len(data["messages"]) > 30:
data["messages"] = data["messages"][-5:]
data["messages"].append({"role": "user", "content": translate_prompt + " " + i[2]})
response = requests.post(f'{base_url}', json=data)
print(count, end=" ")
if response.status_code == HTTPStatus.OK:
# print(json.loads(response.content)['message'], flush=True)
content = json.loads(response.content)
print(content['message']["content"], flush=True)
current_answer = content['message']
i[2] = content['message']["content"]
else:
print('Request count: %s, Status code: %s, error code: %s, error message: %s' % (
count, response.status_code, response.json(), response.content
))
print(response.json(), flush=True)
current_answer = ""
data["messages"].append(current_answer)
count += 1
# if count > limit:
# break
# here we send a message that set keepalive to False
class OPENAI_General_Interface(LLM):
"""
A lot of LLM platforms provide Openai-format compatible interface including ChatGLM Qwen and Ollama
So you can call this interface to utilize a lot LLM
help yourself.
"""
def __init__(self, model_name: str, api_key, base_url, mode, translate_prompt, system_prompt=""):
super().__init__(api_key, base_url, mode="chat", model_name=model_name, translate_prompt=translate_prompt)
print("Your model_name is ", model_name)
if not system_prompt:
self.system_prompt = "you are a professional subtitle translator"
# self.translate_prompt=""
self.messages = [
{
"role": "system",
"content": f"{self.system_prompt}",
}
]
# calling openai compatible interface
self.client = OpenAI(
api_key=self.api_key,
base_url=self.base_url,
)
def chat_translate(self, transcribe_result, translate_prompt):
messages = copy.deepcopy(self.messages)
if not translate_prompt:
translate_prompt = "Translate this English sentence into Chinese."
for i in range(len(transcribe_result)):
if len(messages) > 10:
messages = messages[-4:]
tmp = MessageFormatter.format_message_list([transcribe_result[i][2]])[0]
messages.append(
{
"role": "user",
"content": translate_prompt + " " + tmp
})
try:
resp = self.client.chat.completions.create(
messages=messages,
model=self.model_name,
stream=False
)
print("count: ", i)
print(f"input_tokens: {resp.usage.prompt_tokens}, output_tokens: {resp.usage.completion_tokens}, total_tokens: {resp.usage.total_tokens}", flush=True)
message = resp.choices[0].message.content
print(message, flush=True)
transcribe_result[i][2] = message
except Exception as e:
print(f"Error: {e}")
message = " "
finally:
messages.append({
"role": "assistant",
"content": message
})
# here we send a message that set keepalive to False
# def messge_constructor(batch_size:int, preserve_length:int, ):
def batch_translate(self, transcribe_result: list):
messages = copy.deepcopy(self.messages)
translate_prompt = self.translate_prompt
base_url = self.base_url
client = self.client
fail_list = []
translate_prompt= "Please translate the following continuous 10 lines of English subtitles into Chinese, without any explanations. One sentence at each line."
# each we translate 10 line at once
for i in range(0, len(transcribe_result), 10):
tmp = ""
if len(messages) > 4:
messages = messages[-2:]
if (i+10) > len(transcribe_result):
sentences_list = [transcribe_result[j][2] for j in range(i, len(transcribe_result))]
else:
sentences_list = [transcribe_result[j][2] for j in range(i, i+10)]
sentences_list = MessageFormatter.format_message_list(sentences_list)
tmp = "\n".join(sentences_list)
messages.append({"role": "user", "content": translate_prompt + "\n" + tmp})
try:
resp = client.chat.completions.create(
messages=messages,
model= self.model_name,
stream=False)
# the return resp is a namedtuple, like this
"""
ChatCompletion(id='chatcmpl-93d159a2a94a9e90a54822ec01d9f5d0', choices=[Choice(finish_reason='stop', index=0, logprobs=None,
message=ChatCompletionMessage(content='我正在导入OpenAI库。', role='assistant', function_call=None, tool_calls=None))],
created=1716647537, model='qwen1.5-110b-chat', object='chat.completion', system_fingerprint=None,
usage=CompletionUsage(completion_tokens=149, prompt_tokens=241, total_tokens=390))
"""
print("count: ", i)
print(f"input_tokens: {resp.usage.prompt_tokens}, output_tokens: {resp.usage.completion_tokens}, total_tokens: {resp.usage.total_tokens}", flush=True)
message = resp.choices[0].message.content
print(message, "\n")
ret_list = (message.split("\n"))
print("length of ret_list", len(ret_list))
if len(ret_list) == len(sentences_list):
for j in range(len(ret_list)):
transcribe_result[i + j][2] = ret_list[j]
else:
fail_list.append(i)
except Exception as e:
# print the exception with red color
print(e)
message = " "
fail_list.append(i)
finally:
messages.append({"role": "assistant", "content": message})
if fail_list:
remain_transcribe_result = []
for i in fail_list:
if i < len(transcribe_result)-10:
remain_transcribe_result += transcribe_result[i:i+10]
if fail_list[-1]+10 > len(transcribe_result):
remain_transcribe_result.append(transcribe_result[fail_list[-1]:])
self.chat_translate(remain_transcribe_result, translate_prompt="")
for index, value in enumerate(remain_transcribe_result):
transcribe_result[int(value[0])] = value
return transcribe_result
if __name__ == '__main__':
from srt_util import srt_reader
from srt_util import srt_writer
from config import translation_model_name, base_url, srt_file_name, is_using_local_model, api_key
if not translation_model_name:
print("Please set the translation_model_name in config.py")
exit(0)
if not base_url:
print("Please set the base_url in config.py")
exit(0)
if not srt_file_name:
print("Please set the srt_file_name in config.py")
exit(0)
if not is_using_local_model and not api_key:
print("Please set the api_key in config.py")
exit(0)
print(f"translation_model_name: {translation_model_name}, base_url: {base_url}, srt_file_name: {srt_file_name}")
print("Start translating...")
srt_content = srt_reader(srt_file_name)
if is_using_local_model:
# initial your LLM
ollama = Ollama(model_name=translation_model_name,
api_key="",
base_url=base_url,
mode="chat",
translate_prompt="Translate this English sentence into Chinese. Keep the puncutaion if possible.")
ollama.batch_translate(srt_content)
else:
# base_url = "https://dashscope.aliyuncs.com/compatible-mode/v1"
# API_KEY = ""
openai_model = OPENAI_General_Interface(model_name=translation_model_name,
api_key=api_key,
base_url=base_url,
mode="chat",
translate_prompt="",
system_prompt=""
)
openai_model.batch_translate(srt_content)
# openai_model.chat_translate(srt_content, translate_prompt="")
print([i[2] for i in srt_content])
output_name = srt_file_name.split("/")[-1].split(".")[0] + "-zh-CN" + ".srt"
srt_writer(srt_content, "./output/" + output_name)