
1편 참고!
client.batches.retrieve("저번에 보낸 batch_id").status
위 코드를 통해서 상태를 확인해보자.(client 선언 코드는 1편 참고)

'completed'가 뜨면 이제 결과를 읽을 수 있다!
잠깐 설명하자면 다음과 같은 순서로 결과를 받을 것이다.
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 저장해두면 좋을거 같음!

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