Batch API : Chat GPT API 반값으로 사용하기! (2)

Joo·2024년 5월 4일
0
post-thumbnail

글 구성

1편 : 요청 보내기 & 확인 & 취소

2편 : 요청 받기

 

Batch API란?


1편 참고!

  • 시간 여유가 있을때 GPT한테 미리 질문 보내두고 나중에 요청 받는 것
  • 24h 안에 응답!
  • 가격은 반값

 

요청 받기 코드


client.batches.retrieve("저번에 보낸 batch_id").status

위 코드를 통해서 상태를 확인해보자.(client 선언 코드는 1편 참고)

'completed'가 뜨면 이제 결과를 읽을 수 있다!

잠깐 설명하자면 다음과 같은 순서로 결과를 받을 것이다.

  • 우리가 보낸 batch파일에 대한 response의 파일의 id를 알아내기
  • 해당 output_file_id에 해당하는 결과 받아서 jsonl 파일로 저장
    파일 저장 안하고 하려고 했는데 encoding이 한글이 깨져서 와서 그냥 저장했다가 utf-8로 encoding해서 읽는게 편해보임
output_file_id = client.batches.retrieve("batch_5gwXWG9KF2bP8NGLL95ghZ0r").output_file_id
print(output_file_id)

우선 output_file_id를 받는 코드.(batch_id만 잘 넣어주자)

import json

result = client.files.content(output_file_id).content
result_file_name = "batch_results.jsonl"

with open(result_file_name, 'wb') as file:
    file.write(result)

파일로 저장하는 코드.

# Loading data from saved file
results = []
with open(result_file_name, 'r', encoding='utf-8') as file:
    for line in file:
        # Parsing the JSON string into a dict and appending to the list of results
        json_object = json.loads(line.strip())
        results.append(json_object)

이제 파일을 읽어서 결과를 list로 저장하자. (encoding = 'utf-8'은 한글을 읽기 위해서 추가)

query_list = ["사과를 먹고 잠을 자면 안 좋은 이유는 머야?", "AI는 장점을 알려줘"]
for res in results[:5]:
    task_id = res['custom_id']
    print(task_id)
    query = query_list[int(task_id)]
    result = res['response']['body']['choices'][0]['message']['content']
    print(f'query : {query}')
    print(f'result : {result}\n')

위 코드는 결과를 읽는 부분인데. result에 해당하는 부분이 응답을 가져오는 코드라고 생각하면 된다.
다만 OpenAI의 공식문서에도 result는 list의 순서는 보장되어 나오지 않으니 custom_id를 기반으로 미리 저장해둔 질문과 매칭시켜서 잘 읽도록 하자.

=> 저도 처음 써보는 거라...
이후에 사용할때는 dictionary 형태로 custom_id를 key로 사용해서 query 저장해두면 좋을거 같음!

 

끝!

 

Reference

https://platform.openai.com/docs/guides/batch/getting-started
https://cookbook.openai.com/examples/batch_processing

profile
Don't let the AI snack on random internet bits...

0개의 댓글