Last active
August 2, 2024 12:23
-
-
Save mrpackethead/e0767b095065fbc884399cc9a7cabbb3 to your computer and use it in GitHub Desktop.
AI Chess - LLama3 vs Antrophic3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
import json | |
import os | |
session = boto3.session.Session(profile_name='yourprofile', region_name='us-east-1') | |
bedrock = session.client('bedrock-runtime', 'us-east-1', endpoint_url='https://bedrock-runtime.us-east-1.amazonaws.com') | |
def answer_query(message_list, who): | |
if who == 'sonnet': | |
MODEL_ID = 'anthropic.claude-3-5-sonnet-20240620-v1:0' | |
if who == 'llama3': | |
MODEL_ID = 'meta.llama3-70b-instruct-v1:0' | |
system = f""" | |
- You are a world champion chess player. You should play to win the game. | |
- You can not cheat. | |
- Provide your moves in Algebraic form. | |
- Your opposistion will provide its moves in Algebraic form as well. | |
- If you have put your opposition into check, Say 'Check!' in your answer | |
- If you have put your opposition into checkmate, Say 'CheckMate!' in your answer | |
- Do not offer suggestions or make a summary. | |
""" | |
response = bedrock.converse( | |
modelId=MODEL_ID, | |
messages=message_list, | |
inferenceConfig={ | |
"maxTokens": 2000, | |
"temperature": 0 | |
}, | |
system=[ | |
{ "text":system }, | |
] | |
) | |
# Extract the output message from the response. | |
return response['output']['message'] | |
messages_sonnet = [] | |
messages_llama3 = [] | |
question = "You are white, so you go first. Good Luck" | |
play = True | |
#Sonnet plays first | |
messages_sonnet.append({ | |
"role": "user", | |
"content": [ | |
{ "text": question } | |
] | |
}) | |
answer = answer_query(messages_sonnet, 'sonnet') | |
messages_sonnet.append(answer) | |
print('\n******\nSonnet: ', answer['content'][0]['text']) | |
while play: | |
messages_llama3.append({ | |
"role": "user", | |
"content": [ | |
{ "text": answer['content'][0]['text'] } | |
] | |
}) | |
answer = answer_query(messages_llama3, 'llama3') | |
messages_llama3.append(answer) | |
if 'CheckMate!' in answer['content'][0]['text']: | |
play = False | |
print('\n******\nLlama: ', answer['content'][0]['text'] ) | |
messages_sonnet.append({ | |
"role": "user", | |
"content": [ | |
{ "text": answer['content'][0]['text'] } | |
] | |
}) | |
answer = answer_query(messages_sonnet, 'sonnet') | |
messages_sonnet.append(answer) | |
if 'CheckMate!' in answer['content'][0]['text']: | |
play = False | |
print('\n******\nSonnet: ', answer['content'][0]['text']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment