gpt-3.5-turbo ```= import os import openai from dotenv import load_dotenv openai.api_key = os.getenv("OPENAI_API_KEY") # 讀取 CSV 檔案並將文章存儲為列表 def read_articles_from_csv(file_path): articles = [] with open(file_path, newline="", encoding="utf-8") as csvfile: reader = csv.reader(csvfile) for row in reader: # 假設 CSV 檔案中每一行是一篇文章,如果不是,請適當調整程式碼以讀取正確的欄位 article = row[0] # 假設文章在第一個欄位,如果不是,請根據實際情況修改索引 articles.append(article) return articles # CSV 檔案路徑 csv_file_path = r"C:\Users\USER\Desktop\選舉\fine tuning\p.txt" # 讀取 CSV 檔案中的文章 articles = read_articles_from_csv(csv_file_path) # 讀取 CSV 檔案中的文章 articles = read_articles_from_csv(csv_file_path) # 將文章合併成一個字串,每篇文章以換行符號連接 articles_combined = "\n".join(articles) response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[ {"role": "system", "content": "您將獲得這些文章,您的任務是對這些文章進行如下總結:討論的總體摘要,讓候選人知道社會大眾對於該議題的想法"}, {"role": "user", "content": articles_combined}, ] ) summary = response['choices'][0]['message']['content'].strip() print("總結摘要:\n" + summary + "\n") with open("summary.txt", "w", encoding="utf-8") as f: f.write(summary) ``` GPT-3 ```= import os import openai from dotenv import load_dotenv openai.api_key = os.getenv("OPENAI_API_KEY") # 指定使用的 GPT-3 模型 model_name = "text-davinci-002" # 讀取 CSV 檔案並將文章存儲為列表 def read_articles_from_csv(file_path): articles = [] with open(file_path, newline="", encoding="utf-8") as csvfile: reader = csv.reader(csvfile) for row in reader: # 假設 CSV 檔案中每一行是一篇文章,如果不是,請適當調整程式碼以讀取正確的欄位 article = row[0] # 假設文章在第一個欄位,如果不是,請根據實際情況修改索引 articles.append(article) return articles # CSV 檔案路徑 csv_file_path = r"C:\Users\USER\Desktop\選舉\fine tuning\p.txt" # 讀取 CSV 檔案中的文章 articles = read_articles_from_csv(csv_file_path) # 將文章合併成一個字串,每篇文章以換行符號連接 articles_combined = "\n".join(articles) response = openai.Completion.create( engine=model_name, prompt="您將獲得這些文章,您的任務是對這些文章進行如下總結:討論的總體摘要,讓候選人知道社會大眾對於該議題的想法\n" + articles_combined, temperature=1, max_tokens=256, top_p=1, frequency_penalty=0, presence_penalty=0 ) summary = response['choices'][0]['message']['content'].strip() print("總結摘要:\n" + summary + "\n") with open("summary.txt", "w", encoding="utf-8") as f: f.write(summary) ``` temperature:這是控制生成文本多樣性的參數。較高的溫度(temperature)值(通常在0.7到1.0之間)會產生更加隨機和多樣的輸出,而較低的溫度(通常在0.2到0.5之間)會產生更加保守和穩定的輸出。溫度設置為1時,輸出會在一個合理的範圍內保持隨機性。 max_tokens:這是生成文本的最大長度。您可以設置這個值來控制生成的文本長度,防止生成過長的輸出。請注意,較短的文本可能不足以涵蓋所有要點,而較長的文本可能會超過 API 的限制。 top_p:這是使用 nucleus sampling (top-p sampling) 時的概率閾值。當設置這個值時,模型會計算每個 token 的累積概率,直到概率超過閾值。這可以用來控制生成文本的多樣性,當top_p 較小時,生成文本會更加保守和穩定。 frequency_penalty:這是用於降低頻率的參數。當 frequency_penalty設置為正數時,模型會傾向選擇那些較少出現的 token,從而產生更加新穎的文本。 presence_penalty:這是用於增加多樣性的參數。當 presence_penalty設置為正數時,模型會傾向選擇那些更常見的 token,從而產生更加常見的文本。