2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

csvファイルとChatGPTの連携

Posted at

ChatGPTのCodeInterpreterでのExcelの処理がまだまだだったので、Excelの内容をPythonで取得して、ChatGTPに聞くような仕組みにしてみました。

変数から分かるとおもいますが、論文のアブストラクトの一覧がExcelに入っており、全部読むのが大変なのでChatGTPで楽をしようという取り組みの一部でこの連携を使用しました。

import csv
import openai
openai.api_key = ""
prompt = ""

def get_pico_from_gpt(abstract):
    response = openai.ChatCompletion.create(
            model="gpt-3.5-turbo",
            messages=[
                {"role": "system", "content":prompt},
                {"role": "user", "content":abstract},
            ]
        )
    return response["choices"][0]["message"]["content"].strip()
    
def main():
    input_filename = 'input.csv'  
    output_filename = 'output.csv'
    
    with open(input_filename, 'r', newline='', encoding='utf-8') as infile, open(output_filename, 'w', newline='', encoding='utf-8') as outfile:
        reader = csv.reader(infile)
        writer = csv.writer(outfile)
        
        # ヘッダーを処理
        header = next(reader)
        writer.writerow(header) 
        
        # 以降の行を処理
        for row in reader:
            abstract_content = row[0]
            pico_content = get_pico_from_gpt(abstract_content)
            row[1]=pico_content 
            writer.writerow(row)

if __name__ == '__main__':
    main()

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?