Hiệu chỉnh đoạn code trong sách AI-Agent của Alpha Books
Nhiều đoạn code đã khá cũ và sử dụng mô hình “text-davinci-003" khá cũ, OpenAI đã ngưng hỗ trợ hoàn toàn, cần thay đổi cách gọi hàm và dùng mô hình mới hơn.
Trong trang 223, 224 của chương 7 bản mềm gửi hiệu định (hay trang 237,238) bản in.
3. Triển khai bằng Python: dịch đa ngôn ngữ và đảm bảo tính nhất quán của thuật ngữ
có đoạn code sau:
Lời gọi hàm OpenAPI sử dụng mô hình text-davinci-003 khá cũ, OpenAI đã ngưng hỗ trợ hoàn toàn text-davinci-003 từ đầu năm 2024. Theo các thông báo chính thức của OpenAI và cộng đồng lập trình, text-davinci-003 đã bị deprecated (ngưng hỗ trợ) kể từ ngày 4/1/2024.
Khi chạy, bạn nhận được dòng báo lỗi sau:
Traceback (most recent call last):
File "D:\Scripts\AI_Agent_Alphabooks\translate.py", line 36, in <module>
translated_output = translate_text(input_text, target_language="zh")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\Scripts\AI_Agent_Alphabooks\translate.py", line 19, in translate_text
response = openai.Completion.create(
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\DELL\.pyenv\pyenv-win\versions\3.12.9\Lib\site-packages\openai\lib\_old_api.py", line 39, in __call__
raise APIRemovedInV1(symbol=self._symbol)
openai.lib._old_api.APIRemovedInV1:
You tried to access openai.Completion, but this is no longer supported in openai>=1.0.0 - see the README at https://github.com/openai/openai-python for the API.
You can run `openai migrate` to automatically upgrade your codebase to use the 1.0.0 interface.
Alternatively, you can pin your installation to the old version, e.g. `pip install openai==0.28`
A detailed migration guide is available here: https://github.com/openai/openai-python/discussions/742
Điều này có nghĩa là bạn sẽ không thể sử dụng model này qua API OpenAI nữa, và nếu cố gắng gọi model này sẽ nhận được lỗi báo model không còn tồn tại hoặc không được hỗ trợ. OpenAI khuyến nghị thay thế text-davinci-003 bằng gpt-3.5-turbo hơn cho các tác vụ "completion" dạng truyền thống, hoặc sử dụng các dòng model GPT-4 mới hơn như gpt-4o cho các yêu cầu nâng cao hơn về khả năng sinh ngôn ngữ, reasoning và đa phương thức.
Cách gọi hàm khá cũ, sử dụng kiểu gọi hàm từ phiên bản 0.28 trở xuống, sau ngày 8/11/2023, OpenAI ra mắt phiên bản 1.0.0 thì các code này không còn chạy được nữa.
Bạn cần thay code bằng đoạn sau:
messages = [
{"role": "system", "content": "You are a helpful assistant that translates text."},
{"role": "user", "content": f"Translate the following text from {source_language} to {target_language}: \n{input_text}"}
]
# Gọi API OpenAI để thực hiện dịch thuật
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=messages,
max_tokens=512,
temperature=0.5
)
Tôi đã chạy thử dịch tiếng Anh sang tiếng Pháp, kết quả như sau:
$ python translate.py
Detected source language: en
Initial Translation: Le réseau 5G utilise des technologies de MIMO massif et de formation de faisceau pour améliorer les performances.
Final Translation with Terminology Adjustments: Le réseau 5G utilise des technologies de MIMO massif et de formation de faisceau pour améliorer les performances.
Final Output:
Le réseau 5G utilise des technologies de MIMO massif et de formation de faisceau pour améliorer les performances.
Tôi đếm có khoảng 38 lần xuất hiện “text-davinci-003" trong sách và bạn cần thay hết mới có thể chạy được.
Cập nhật: Cuốn sách khi xuất bản đã thay hết “text-davinci-003" bằng “gpt-4-0125-preview" tuy nhiên lời gọi hàm vẫn khá cũ
response = openai.ChatCompletion.create(
model="gpt-4-0125-preview",
messages=messages,
max_tokens=512,
temperature=0.5
)
Và gây cảnh báo:
You tried to access openai.ChatCompletion, but this is no longer supported in openai>=1.0.0 - see the README at https://github.com/openai/openai-python for the API.You can run `openai migrate` to automatically upgrade your codebase to use the 1.0.0 interface.
Alternatively, you can pin your installation to the old version, e.g. `pip install openai==0.28`
A detailed migration guide is available here: https://github.com/openai/openai-python/discussions/742
Bạn nên sửa lại
response = client.chat.completions.create(
model="gpt-4-0125-preview",
messages=messages,
max_tokens=512,
temperature=0.5
)
Bạn có thể tải đoạn code hiệu đính trên Github tại đây.