Enhance Your Application with Text Generation Using Microsoft Phi-3 Mini-4K-Instruct

23 Apr 2025
Enhance Your Application with Text Generation Using Microsoft Phi-3 Mini-4K-Instruct

In today's rapidly evolving digital landscape, integrating generative AI capabilities into your applications can significantly enhance user experience and engagement. The Microsoft Phi-3 Mini-4K-Instruct is a state-of-the-art lightweight model designed for strong reasoning tasks, making it particularly suitable for applications in coding, mathematics, and logic. With its optimizations for memory-constrained and latency-sensitive environments, this API provides developers with powerful tools for generating high-quality text outputs.

Prerequisites

Before you start using the Microsoft Phi-3 Mini-4K-Instruct Cognitive Actions, ensure you have the following:

  • An API key for the Cognitive Actions platform.
  • Basic knowledge of how to make HTTP requests and handle JSON data.

Authentication typically involves including your API key in the headers of your requests, allowing secure access to the Cognitive Actions functionality.

Cognitive Actions Overview

Generate Text with Phi-3 Mini-4K-Instruct

Description: This action leverages the Phi-3 Mini-4K-Instruct model to generate text based on a provided prompt. It excels in generating coherent and contextually relevant responses, making it ideal for a variety of use cases, from chatbots to content creation.

  • Category: AI Content Generation

Input

The input for this action requires a JSON object with the following fields:

  • topK (integer, optional): The maximum number of top tokens to consider based on their probability for generating the output. Default is 50.
  • topP (number, optional): A cumulative probability threshold used for generating the output. Default is 0.9.
  • prompt (string, required): The initial text provided to the model to guide the generation process.
  • maxTokens (integer, optional): The maximum number of tokens the model should generate as output. Default is 512.
  • minTokens (integer, optional): The minimum number of tokens the model should generate as output. Default is 0.
  • temperature (number, optional): A value between 0 and 1 used to scale the randomness of the model's output. Default is 0.6.
  • systemPrompt (string, optional): An initial system directive provided to orient the model's behavior. Default is "You are a helpful assistant."
  • stopSequences (string, optional): Sequences of text where the model will cease generation, e.g., 'end,stop'.
  • presencePenalty (number, optional): A penalty applied to discourage the model from using new or rare tokens during generation. Default is 0.
  • frequencyPenalty (number, optional): A penalty applied to nudge the model away from excessive token repetition. Default is 0.

Example Input:

{
  "topK": 1,
  "topP": 1,
  "prompt": "How would you explain Internet to a medieval knight?",
  "maxTokens": 512,
  "minTokens": 0,
  "temperature": 0.1,
  "systemPrompt": "You are a helpful AI assistant.",
  "presencePenalty": 0,
  "frequencyPenalty": 0
}

Output

The action typically returns a list of generated tokens that represent the model's response to the provided prompt. The output may vary based on the input parameters, and it generally captures the essence of the prompt in a coherent manner.

Example Output:

[
  "My",
  " noble",
  " knight",
  ",",
  " imagine",
  " a",
  " vast",
  " kingdom",
  ",",
  " not",
  " of",
  " land",
  " and",
  " stone",
  ",",
  " but",
  " of",
  " ideas",
  " and",
  " knowledge",
  ".",
  " This",
  " kingdom",
  " is",
  " called",
  " the",
  " Internet",
  ".",
  ...
]

Conceptual Usage Example (Python)

Here’s how you might call this action using Python to generate text:

import requests
import json

# Replace with your Cognitive Actions API key and endpoint
COGNITIVE_ACTIONS_API_KEY = "YOUR_COGNITIVE_ACTIONS_API_KEY"
COGNITIVE_ACTIONS_EXECUTE_URL = "https://api.cognitiveactions.com/actions/execute" # Hypothetical endpoint

action_id = "c848c67a-eb67-4ab8-b776-d40ca494a3d6" # Action ID for Generate Text with Phi-3 Mini-4K-Instruct

# Construct the input payload based on the action's requirements
payload = {
    "topK": 1,
    "topP": 1,
    "prompt": "How would you explain Internet to a medieval knight?",
    "maxTokens": 512,
    "minTokens": 0,
    "temperature": 0.1,
    "systemPrompt": "You are a helpful AI assistant.",
    "presencePenalty": 0,
    "frequencyPenalty": 0
}

headers = {
    "Authorization": f"Bearer {COGNITIVE_ACTIONS_API_KEY}",
    "Content-Type": "application/json"
}

try:
    response = requests.post(
        COGNITIVE_ACTIONS_EXECUTE_URL,
        headers=headers,
        json={"action_id": action_id, "inputs": payload} # Hypothetical structure
    )
    response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)

    result = response.json()
    print("Action executed successfully:")
    print(json.dumps(result, indent=2))

except requests.exceptions.RequestException as e:
    print(f"Error executing action {action_id}: {e}")
    if e.response is not None:
        print(f"Response status: {e.response.status_code}")
        try:
            print(f"Response body: {e.response.json()}")
        except json.JSONDecodeError:
            print(f"Response body: {e.response.text}")

In this example, replace YOUR_COGNITIVE_ACTIONS_API_KEY and the endpoint with your actual credentials. The payload object is structured to match the input schema for the action.

Conclusion

The Microsoft Phi-3 Mini-4K-Instruct Cognitive Action offers developers a robust tool for generating high-quality text outputs tailored to specific prompts. By integrating this powerful AI capability into your applications, you can enhance user interaction and create more engaging experiences. Consider exploring additional use cases, such as content creation, educational tools, or chatbots, to fully leverage the potential of this remarkable technology. Happy coding!