Generate Engaging Text with Microsoft Phi-3-Mini-128K-Instruct Actions

23 Apr 2025
Generate Engaging Text with Microsoft Phi-3-Mini-128K-Instruct Actions

In today's rapidly evolving digital landscape, the ability to generate human-like text can significantly enhance applications across various domains. The Microsoft Phi-3-Mini-128K-Instruct API provides a powerful toolset for developers looking to leverage advanced text generation capabilities. With its lightweight architecture and impressive reasoning abilities, the Phi-3-Mini model can handle tasks ranging from creative writing to complex problem-solving. This article will guide you through integrating this model's cognitive action, enabling you to generate text effectively within your applications.

Prerequisites

Before diving into the integration process, ensure you have the following:

  • API Key: You will need an API key to authenticate requests to the Cognitive Actions platform. This key should be included in the headers of your API calls.
  • Environment Setup: Ensure you have a programming environment set up to make HTTP requests (e.g., Python with the requests library).

Conceptually, authentication can be handled by passing your API key in the request headers as follows:

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

Cognitive Actions Overview

Generate Text Using Phi-3-Mini-128K-Instruct

This action allows you to utilize the Phi-3-Mini-128K-Instruct model to generate text based on a given prompt. It's particularly adept at tasks that require strong reasoning skills, such as math, coding, and logic.

  • Category: Text Generation

Input

The input for this action requires several fields, outlined in the schema below:

{
  "prompt": "How would you explain Internet to a medieval knight?",
  "maxLength": 512,
  "topK": 1,
  "topP": 1,
  "temperature": 0.1,
  "systemPrompt": "You are a helpful AI assistant.",
  "repetitionPenalty": 1.1,
  "seed": 42 // Optional
}
  • Required Fields:
    • prompt: The text input that guides the model's response.
  • Optional Fields:
    • seed: An integer to ensure reproducibility of results.
    • topK: Limits the selection to the top K most likely tokens.
    • topP: Samples from the top P percentage of probable tokens.
    • maxLength: Maximum length of the generated text in tokens.
    • temperature: Controls randomness in text generation.
    • systemPrompt: Sets the AI's role or persona.
    • repetitionPenalty: Adjusts penalties for repeated words.

Output

The output will typically be a JSON array containing the generated text tokens. For example:

[
  "",
  "Good ",
  "",
  "morrow, ",
  "noble ",
  "Sir ",
  "Knight! ",
  "I ",
  "shall ",
  "endeavor ",
  "to ",
  "elucidate ",
  "the ",
  "concept ",
  "of ",
  "'Internet' ",
  "in ",
  ...
]

This output can be processed to form coherent text, reflecting the completion of the task based on the provided prompt.

Conceptual Usage Example (Python)

Here’s how you can call the Cognitive Actions execution endpoint to generate text using the Phi-3-Mini-128K-Instruct action:

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 = "93f8eaa3-aaa1-4583-91b5-ab98617f6150"  # Action ID for Generate Text Using Phi-3-Mini-128K-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?",
    "maxLength": 512,
    "temperature": 0.1,
    "systemPrompt": "You are a helpful AI assistant.",
    "repetitionPenalty": 1.1
}

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, you will replace the API key and endpoint URL with your actual values. The payload is structured according to the action's requirements. This conceptual code snippet demonstrates how to invoke the action and handle potential errors.

Conclusion

Integrating the Microsoft Phi-3-Mini-128K-Instruct Cognitive Action into your applications offers a robust solution for generating coherent and contextually relevant text. By leveraging this action, developers can enhance user experiences, automate content creation, or even tackle complex reasoning tasks. Consider experimenting with different prompts and parameters to explore the full potential of this powerful model in your projects. Happy coding!