|
| 1 | +### @flyoutOnly true |
| 2 | +### @hideIteration false |
| 3 | +### @explicitHints true |
| 4 | + |
| 5 | +# Classifying Materials |
| 6 | + |
| 7 | +## Step 1 |
| 8 | +Before we're able to train the agent's AI, we'll need to learn how to access its Application Programming Interface (API). The first step is to define the API's endpoint (what part of the API you're accessing) and authentication parameters |
| 9 | + |
| 10 | +```python |
| 11 | +api_url = 'minecraft://agent.ai/' |
| 12 | +api_endpoint = 'classify' |
| 13 | +api_key = "dsf3sSFssf42" |
| 14 | +``` |
| 15 | + |
| 16 | +## Step 2 |
| 17 | +Now that we've defined the API's endpoint we want to use, we'll want to write a function that takes an input and sends it to the API endpoint. |
| 18 | + |
| 19 | +```python |
| 20 | +def make_classify_api_request(resource, data): |
| 21 | + client = MinecraftAI( |
| 22 | + url = api_url, |
| 23 | + endpoint= api_endpoint, |
| 24 | + key = api_key |
| 25 | + ) |
| 26 | + |
| 27 | + try: |
| 28 | + response = client.chat.completions.create( |
| 29 | + messages=[ |
| 30 | + {resource : data} |
| 31 | + ] |
| 32 | + ) |
| 33 | + ai_message = response.content |
| 34 | + print("AI Response") |
| 35 | + print(ai_message) |
| 36 | + |
| 37 | + except Exception as e: |
| 38 | + print("An error occurred: {e}") |
| 39 | + |
| 40 | +``` |
| 41 | + |
| 42 | +## Step 3 |
| 43 | +Now we need to call the function when the code is run. |
| 44 | + |
| 45 | +```python |
| 46 | +make_classify_api_request("oak log","wood") |
| 47 | +``` |
| 48 | + |
| 49 | +## Step 4 |
| 50 | +When we put it all together it looks like this |
| 51 | + |
| 52 | + |
| 53 | +```python |
| 54 | +api_url = 'minecraft://agent.ai/' |
| 55 | +api_endpoint = 'classify' |
| 56 | +api_key = "dsf3sSFssf42" |
| 57 | + |
| 58 | +def make_classify_api_request(resource, data): |
| 59 | + client = MinecraftAI( |
| 60 | + url = api_url, |
| 61 | + endpoint= api_endpoint, |
| 62 | + key = api_key |
| 63 | + ) |
| 64 | + |
| 65 | + try: |
| 66 | + response = client.chat.completions.create( |
| 67 | + messages=[ |
| 68 | + {resource : data} |
| 69 | + ] |
| 70 | + ) |
| 71 | + ai_message = response.content |
| 72 | + print("AI Response") |
| 73 | + print(ai_message) |
| 74 | + |
| 75 | + except Exception as e: |
| 76 | + print("An error occurred: {e}") |
| 77 | + |
| 78 | +``` |
0 commit comments