Chat gpt api를 활용해서 볼링 점수 계산을 해보자
1. 크롤링 할 사이트(블로그) 찾기
기본적으로도 연산은 잘 해 줄 테지만, 계산의 정확도를 높이기 위해 볼링 점수를 계산하는 방법이 적힌 사이트 혹은 블로그를 둘러보다
http://sports.koreanpc.kr:9000/sports_follow/sub/bowling/bowling_1_04_02.asp?naviNum=2
생활체육따라하기 | 볼링 | 볼링 | 기초지식 | 점수계산방법
HOME > 볼링 > 기초지식 > 점수계산방법 점수계산방법 --> --> 스트라이크 각 프레임의 제1투구에서 10개 핀이 전부 넘어지게 되면 '스트라이크'가 된다. 이때, 스트라이크의 득점은 10점이며, 보너스
sports.koreanpc.kr:9000
이 사이트를 발견하게 되어 예제로 탁월하다 생각하고 크롤링을 진행하였다.
2. assistant_id와 thread_id 생성
html = tf.fetch_url("https://kini.kr/510")
result = tf.extract(html)
personal_trainer_assis = client.beta.assistants.create(
name ="bowling",
instructions="""너는 그 누구보다 볼링에 대해 잘 알고 있고, 볼링 점수 능력 계산도 탁월하다.""" + result,
model = model
)
print(personal_trainer_assis.id)
thread = client.beta.threads.create(
messages=[
{
"role": "user",
"content": "점수를 계산해 줄래?"
}
]
)
print(thread.id)
위와 같이 크롤링한 정보를 넣어 assistant_id와 thread_id를 받는 코드다. 한번 실행한 뒤에는 print로 받은 값만 저장하고 주석처리를 하도록 하자.
3. 문제 입력하기
message = """거터는(G), 스페어는(/), 스트라이크는(X), 파울을 (F), 아무것도 치지 못하면(-)로 입력할거야. 프레임 구분은(|), 제1투구, 2투구 구분은(,)로 할거야
9,-|G,3|X|7,2|X|9,/|X|X|X|XXX"""
message = client.beta.threads.messages.create(
thread_id = thread_id,
role="user",
content=message
)
run = client.beta.threads.runs.create(
assistant_id=assistant_id,
thread_id=thread_id,
instructions="사용자를 박수빈님으로 불러주세요."
)
def wait_for_run_completion(client, thread_id, run_id, sleep_interval=5):
"""
Waits for a run to complete and prints the elapsed time.:param client: The OpenAI client object.
:param thread_id: The ID of the thread.
:param run_id: The ID of the run.
:param sleep_interval: Time in seconds to wait between checks.
"""
while True:
try:
run = client.beta.threads.runs.retrieve(thread_id=thread_id, run_id=run_id)
if run.completed_at:
elapsed_time = run.completed_at - run.created_at
formatted_elapsed_time = time.strftime(
"%H:%M:%S", time.gmtime(elapsed_time)
)
print(f"Run completed in {formatted_elapsed_time}")
logging.info(f"Run completed in {formatted_elapsed_time}")
# Get messages here once Run is completed!
messages = client.beta.threads.messages.list(thread_id=thread_id)
last_message = messages.data[0]
response = last_message.content[0].text.value
print(f"Assistant Response: {response}")
break
except Exception as e:
logging.error(f"An error occurred while retrieving the run: {e}")
break
logging.info("Waiting for run to complete...")
time.sleep(sleep_interval)
# === Run ===
wait_for_run_completion(client=client, thread_id=thread_id, run_id=run.id)
# ==== Steps --- Logs ==
run_steps = client.beta.threads.runs.steps.list(thread_id=thread_id, run_id=run.id)
print(f"Steps---> {run_steps.data[0]}")
위와 같이 message 부분만 원하는 값으로 수정하여 입력하면 된다.
이제 이 코드를 스트림 릿으로 변환하면 된다.
코드는 gpt에게 바꿔달라하면 잘 해준다
계산을 잘 해주는 것 같다.
'AI' 카테고리의 다른 글
테트리오 랭크 예측 (0) | 2024.06.12 |
---|---|
과제 (0) | 2024.04.03 |
사진을 다양하게 변형시켜보자 (0) | 2024.03.27 |
영상인식과 opencv (0) | 2024.03.20 |
[인공지능] 인공지능 (0) | 2024.03.06 |