Phi2 - Developer Guide

Phi2 - Developer Guide

Welcome to the Phi-2 Developer Guide! This guide is designed to assist you in seamlessly integrating Phi-2 into your AWS environment through detailed, step-by-step instructions.

Phi-2 is a 2.7 billion-parameter language model, a significant advancement in language understanding and reasoning capabilities. Despite its smaller size compared to models with up to 25 times more parameters, Phi-2 achieves state-of-the-art performance on complex benchmarks.

Video
Blog
How to Install Phi-2 AI on AWS Easily via Pre-configured AMI
Unlock the full potential of Phi-2 AI on AWS with our step-by-step guide. Learn how to seamlessly deploy, configure, and optimize Phi-2 AI, leveraging AWS’s scalability and robustness

Prerequisites

Before you get started with the Phi-2 AMI, ensure you have the following prerequisites:

Launching the AMI

Step 1: Find and Select 'Phi-2' AMI

  1. Log in to your AWS Management Console.
  2. Navigate to the 'Phi-2' in  AWS Marketplace.

Step 2:  Initial Setup & Configuration

  1. Click the "Continue to Subscribe" button.
  2. After subscribing, you will need to accept the terms and conditions. Click on "Accept Terms" to proceed.
  3. Please wait for a few minutes while the processing takes place. Once it's completed, click on "Continue to Configuration".
  4. Select the "CloudFormation Template for Phi-2 deployment" as the fulfilment option and choose your preferred region on the "Configure this software" page. Afterward, click the "Continue to Launch" button.
  5. From the "Choose Action" dropdown menu in "Launch this software" page, select "Launch CloudFormation" and click "Launch" button.

Create CloudFormation Stack

Step1: Create stack

  1. Ensure the "Template is ready" radio button is selected under "Prepare template".

2. Click "Next".

Step2: Specify stack options

  1. Provide a unique "Stack name".
  2. Provide the "Admin Email" for SSL generation.
  3. For "DeploymentName", enter a name of your choice.
  4. Provide a public domain name for "DomainName". (Phi-2 will automatically try to setup SSL based on provided domain name, if that domain hosted on Route53. Please make sure your domain name hosted on route53. If its unsuccessful then you have to setup SSL manually)
  5. Choose an instance type, "InstanceType" (Recommended: g4dn.xlarge).
  6. Select your preferred "keyName".
  7. Set "SSHLocation" as "0.0.0.0/0".
  8. Keep "SubnetCidrBlock" as "10.0.0.0/24".
  9. Keep "VpcCidrBlock" as "10.0.0.0/16".
  10. Click "Next".

Step3: Configure stack options

  1. Choose "Roll back all stack resources" and "Delete all newly created resources" under the "Stack failure options" section.
  2. click "Next".

Step4: Review

  1. Review and verify the details you've entered.‌

2. Tick the box that says, "I acknowledge that AWS CloudFormation might create IAM resources with custom names".‌

3. Click "Submit".

Afterward, you'll be directed to the CloudFormation stacks page.‌

Please wait for 5-10 minutes until the stack has been successfully created.‌

Update DNS

Step1: Copy IP Address

  1. Copy the public Ip labeled "PublicIp" in the "Outputs" tab.‌

Step2: Update DNS

  1. Go to AWS Route 53 and navigate to "Hosted Zones".
  2. From there, select the hosted zone which will host the domain you provided as "DomainName".‌

3. Click "Create record" and then add the "domain name prefix" in "Record Name" and paste the copied "PublicIp" into the "value" textbox.

4. Click "Create Records".

Access Phi-2

You can access the Phi-2 application through the "DashboardUrl" or 'DashboardUrlIp' provided in the "Outputs" tab.‌

(If you encounter a "502 Bad Gateway error", please wait for about 5 minutes before refreshing the page)‌

Generate SSL Manually

Phi-2 will automatically try to setup SSL based on provided domain name, if that domain hosted on Route53. If its unsuccessful then you have to setup SSL manually.

Step1: Copy IP Address

  1. Proceed with the instructions outlined in the above "Update DNS" section, if you have not already done so.

2. Copy the Public IP address indicated as "PublicIp" in the "Outputs" tab.‌

Step2: Log in to the server

  1. Open the terminal and go to the directory where your private key is located.
  2. Paste the following command into your terminal and press Enter: ssh -i <your key name> ubuntu@<Public IP address>.‌

3. Type "yes" and press Enter. This will log you into the server.

Step3: Generate SSL

Paste the following command into your terminal and press Enter and follow the instructions:

sudo /root/certificate_generate_standalone.sh

Admin Email is acquiring for generate SSL certificates.

Shutting Down Phi-2

  1. Click the link labeled "Phi-2" in the "Resources" tab to access the EC2 instance, you will be directed to the Phi-2 instance in EC2.‌

2. Select the Phi-2 instance by marking the checkbox and click "Stop instance" from the "Instance state" dropdown. You can restart the instance at your convenience by selecting "Start instance".‌

Remove Phi-2

Delete the stack that has been created in the AWS Management Console under 'CloudFormation Stacks' by clicking the 'Delete' button.

API Documentation

1. Retrieve Completions

Retrieves completions based on the provided prompt.

  • Endpoint: /v1/completions
  • Method: POST
  • Request Body:‌
{
  "model": "phi-2",
  "prompt": "\n\n### Instructions:\nWhat is the capital of France?\n\n### Response:\n",
  "stop": [
    "\n",
    "###"
  ]
}

  • Response Body:‌
{
  "id": "cmpl-3f3fe4b8-a40d-4cbc-9cd6-765334f6adb3",
  "object": "text_completion",
  "created": 1711514727,
  "model": "phi-2",
  "choices": [
    {
      "text": "The capital of France is Paris.",
      "index": 0,
      "logprobs": null,
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 18,
    "completion_tokens": 8,
    "total_tokens": 26
  }
}

2. Retrieve Embeddings

Retrieves embeddings based on the provided input text.

  • Endpoint: /v1/embeddings
  • Method: POST
  • Request Body:‌
{
  "input": "The food was delicious and the waiter...",
  "model": "phi-2"
}

  • Response Body:‌
{
  "object": "list",
  "data": [
    {
      "object": "embedding",
      "embedding": [
        -0.06866070628166199,
        -0.19843722879886627,
        1.7145146131515503,
		...
		-0.9296887516975403
      ],
      "index": 0
    }
  ],
  "model": "phi-2",
  "usage": {
    "prompt_tokens": 9,
    "total_tokens": 9
  }
}

3. Retrieve Chat Completions

Retrieves chat completions based on the provided chat messages.

  • Endpoint: /v1/chat/completions
  • Method: POST
  • Request Body:‌
{
  "messages": [
    {
      "content": "You are a helpful assistant.",
      "role": "system"
    },
    {
      "content": "What is the capital of France?",
      "role": "user"
    }
  ],
  "model": "phi-2"
}

  • Response Body:‌
{
  "id": "chatcmpl-eb29cf9a-7f7d-4f44-9571-2f1f831c9468",
  "object": "chat.completion",
  "created": 1711514855,
  "model": "phi-2",
  "choices": [
    {
      "index": 0,
      "message": {
        "content": "The capital of France is Paris. Do you have any other questions for me, user? \n",
        "role": "assistant"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 57,
    "completion_tokens": 27,
    "total_tokens": 84
  }
}

4. List Models

Retrieves a list of available models.

  • Endpoint: /v1/models
  • Method: GET
  • Response Body:‌
{
  "object": "list",
  "data": [
    {
      "id": "phi-2",
      "object": "model",
      "owned_by": "me",
      "permissions": []
    },
    {
      "id": "phi-2-dpo",
      "object": "model",
      "owned_by": "me",
      "permissions": []
    },
    {
      "id": "phi-2-orange",
      "object": "model",
      "owned_by": "me",
      "permissions": []
    }
  ]
}

5. Use different Models

To change model,
 Run "List Models"
 Select the preferred model and copy "id" from the response
 Replace the "model" variable in the request body of your preferred endpoint

Note that changing the model will take a bit more time to give the response of the endpoint

Testing the API

  1. Create a directory
  2. Create 3 files (Full codes are given below)‌
    ‌app.js‌
    ‌package.json‌
    ‌.env
  3. Run the following command‌
    npm install
  4. Edit variable file (.env)
  5. Run the following command‌
    npm start
  6. You will get the responses‌
const axios = require('axios');
require('dotenv').config();

const makePostRequest = async (url, data, timeout) => {
  try {
    const response = await axios.post(url, data, { timeout });
    return { success: response.status === 200, data: response.data };
  } catch (error) {
    return { success: false, error: error.message };
  }
};

const makeGetRequest = async (url, timeout) => {
  try {
    const response = await axios.get(url, { timeout });
    return { success: response.status === 200, data: response.data };
  } catch (error) {
    return { success: false, error: error.message };
  }
};

const printResponseData = (endpoint, data) => {
  console.log(`Response for ${endpoint}:`);
  console.log(JSON.stringify(data, null, 2));
  console.log('');
};

const checkEndpoints = async () => {
  const baseUrl = process.env.BASE_URL;
  const model = process.env.MODEL;

  const endpoints = [
    { path: '/completions', method: makePostRequest, data: { "model": model, "prompt": process.env.PROMPT1 }, printEnv: 'PRINT_COMPLETIONS_RESPONSE' },
    { path: '/embeddings', method: makePostRequest, data: { "input": process.env.PROMPT2, "model": model }, printEnv: 'PRINT_EMBEDDINGS_RESPONSE' },
    { path: '/chat/completions', method: makePostRequest, data: { "messages": [{ "content": "You are a helpful assistant.", "role": "system" }, { "content": process.env.PROMPT1, "role": "user" }], "model": model }, printEnv: 'PRINT_CHAT_COMPLETIONS_RESPONSE' },
    { path: '/models', method: makeGetRequest, printEnv: 'PRINT_MODELS_RESPONSE' }
  ];

  for (const endpoint of endpoints) {
    const url = `${baseUrl}${endpoint.path}`;
    const { success, data, error } = await endpoint.method(url, endpoint.method === makePostRequest ? endpoint.data : null, process.env.REQUEST_TIMEOUT || 50000);
    const printResponse = process.env[endpoint.printEnv] === 'true';

    if (success) {
      console.log(`*** Endpoint ${endpoint.path} is reachable.`);
      if (printResponse) {
        printResponseData(endpoint.path, data);
      }
      console.log('');
    } else {
      console.log(`*** Endpoint ${endpoint.path} is not reachable. Error:`, error);
    }
  }
};

checkEndpoints();

javascript

app.js

  • ‌‌
{
  "name": "test-llama",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node app.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "axios": "^1.6.7",
    "dotenv": "^16.4.1"
  }
}

json

package.json

  • ‌‌
# Base URL for the API
BASE_URL=https://mixtral-test-prod.meetrix.io/v1

# Model to be used in requests
MODEL=mixtral-8x7b-instruct-v0.1

# Prompts for different endpoints
# /completions and /chat/completions
PROMPT1=What is the capital of France?
# /embeddings
PROMPT2=The food was delicious and the waiter...

# Whether to print responses for each endpoint
PRINT_COMPLETIONS_RESPONSE=true
PRINT_EMBEDDINGS_RESPONSE=false
PRINT_CHAT_COMPLETIONS_RESPONSE=true
PRINT_MODELS_RESPONSE=true

# Timeout for requests in milliseconds (default is 50000)
REQUEST_TIMEOUT=50000

.env

.env

Check Server Logs

Step1: Log in to the server

  1. Open the terminal and go to the directory where your private key is located.
  2. Paste the following command into your terminal and press Enter: ‌
    ssh -i <your key name> ubuntu@<Public IP address>

3. Type "yes" and press Enter. This will log you into the server.

Step2: Check the logs

sudo tail -f /var/log/syslog

Upgrades

When there is an upgrade, we will update the product with a newer version. You can check the product version in AWS Marketplace. If a newer version is available, you can remove the previous version and launch the product again using the newer version. Remember to backup the necessary server data before removing.

Troubleshoot

  1. If you face the following error, please follow https://meetrix.io/articles/how-to-increase-aws-quota/ blog to increase vCPU quota.‌

2.  If you face the following error (do not have sufficient <instance_type> capacity...) while creating the stack, try changing the region or try creating the stack at a later time.‌

3. If you face the below error, when you try to access the API dashboard, please wait 5-10 minutes and then try.‌

Conclusion

In conclusion, the Meetrix Mistrallite Developer Guide is your essential resource for seamlessly integrating advanced capabilities into your AWS environment. Mistrallite's improved model, which is based on Mistral, performs better by analyzing longer contexts. With Mistrallite's seamless connection, you can confidently elevate your AI apps and have access to cutting-edge features and efficiency in the artificial intelligence space.

Concluding the Phi-2 Developer Guide, this resource stands as your indispensable tool for integrating advanced AI capabilities effortlessly into your AWS environment. Leveraging Phi-2's enhanced model, which boasts 2.7 billion parameters, ensures superior performance in language understanding and reasoning tasks. Welcome to a future of innovation and excellence with Phi-2 at your fingertips.

Technical Support

Reach out to Meetrix Support (support@meetrix.io)  for assistance with Phi-2 issues.


Discover Seamless Meetings with >>>
Meetrix