-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathopenai_handler.py
50 lines (43 loc) · 1.54 KB
/
openai_handler.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
import openai
import json
from tenacity import (
retry,
wait_exponential,
stop_after_attempt,
retry_if_exception_type,
)
from requests.exceptions import SSLError
from openai.error import RateLimitError, APIConnectionError, APIError
# 使用 tenacity 重试装饰器
@retry(
wait=wait_exponential(multiplier=1, max=180),
stop=stop_after_attempt(10),
retry=retry_if_exception_type(
(RateLimitError, APIConnectionError, APIError, SSLError)
),
)
def completion_with_backoff(**kwargs):
return openai.ChatCompletion.create(**kwargs)
# 定义一个翻译函数
def translate_content(content):
# 从配置文件中读取配置
with open("config.json", "r") as f:
config = json.load(f)
# 设置 OpenAI API 密钥
openai.api_key = config["openai_api_key"]
# 调用具有指数退避策略的聊天补全函数
response = completion_with_backoff(
model=config["model"],
messages=[
{"role": "system", "content": config["system"]},
{"role": "user", "content": content},
],
temperature=0.3, # 调整这个值以控制随机性
# max_tokens=None, # 设置生成 token 的最大数量限制
)
# 提取翻译后的文本和使用的总 token 数
translated_text = response.choices[0].message.content
if config['test']:
print(f"这段文本翻译后的内容:\n{translated_text}\n")
total_tokens = response.usage["total_tokens"]
return translated_text, total_tokens