我想要一天分享一點「LLM從底層堆疊的技術」,並且每篇文章長度控制在三分鐘以內,讓大家不會壓力太大,但是又能夠每天成長一點。
from Ipython.core.display import display, HTML
def display_response(input_text, response, bb_task):
html_content = f"""
<!DOCTYPE html>
<html>
<head>
<title>Big-bench Tasks</title>
<style>
p {{
max-width: 600px;
}}
</style>
</head>
<body>
<h1>{bb_task}</h1>
<p>{task}</p>
</body>
</html>
"""
display(HTML(html_content))
html_file = open("output.html", "a")
html_file.write(html_content)
html_file.close()
html_file = open("output.html", "w")
html_file.close()
接著撰寫一個自動一直問 GPT-4 的函數,因為 OpenAI 有使用率限制政策,所以新增等待時間:
import time
counter = 0
nb_tasks = nbt
for index, row in df.iterrows():
input_text = row['Tasks']
counter += 1
if counter > nb_tasks:
break
task = openai_chat(input_text)
task = task.replace('\n', '<br>') # HTML 不支援直接換行,因此需要將換行符替換為 <br> 標籤
parts = input_text.split('Solve it:')
bb_task = parts[1].strip() # 用來去除 parts[1] 字串兩側的所有空白字符
display_response(input_text, task, bb_task)
if counter % 50 == 0:
print(f"Processed {counter} tasks. Pausing for 60 seconds.")
time.sleep(60)